Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ClassPathTemplateLoader |
|
| 3.3333333333333335;3.333 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | package org.webmacro.resource; | |
23 | ||
24 | import org.slf4j.Logger; | |
25 | import org.slf4j.LoggerFactory; | |
26 | import org.webmacro.Broker; | |
27 | import org.webmacro.InitException; | |
28 | import org.webmacro.ResourceException; | |
29 | import org.webmacro.Template; | |
30 | import org.webmacro.util.Settings; | |
31 | ||
32 | import java.net.URL; | |
33 | ||
34 | /** | |
35 | * Implementation of TemplateLoader that loads template from the classpath. | |
36 | * The "path" setting is used as a prefix for all request, so it should | |
37 | * end with a slash.<br> | |
38 | * <br> | |
39 | * Example: If you have <pre>classpath:templates/</pre> as a | |
40 | * TemplateLoaderPath and request the template "foo/bar.wm", it will search for | |
41 | * "templates/foo/bar.wm" in your classpath. | |
42 | * @author Sebastian Kanthak (sebastian.kanthak@muehlheim.de) | |
43 | */ | |
44 | 0 | public class ClassPathTemplateLoader extends AbstractTemplateLoader |
45 | { | |
46 | ||
47 | 0 | static Logger _log = LoggerFactory.getLogger(ClassPathTemplateLoader.class); |
48 | private ClassLoader loader; | |
49 | private String path; | |
50 | ||
51 | public void init (Broker broker, Settings config) throws InitException | |
52 | { | |
53 | 0 | super.init(broker, config); |
54 | 0 | loader = broker.getClassLoader(); |
55 | 0 | } |
56 | ||
57 | public void setConfig (String config) | |
58 | { | |
59 | // as we'll later use this as a prefix, it should end with a slash | |
60 | 0 | if (config.length() > 0 && !config.endsWith("/")) |
61 | { | |
62 | 0 | _log.info("ClassPathTemplateLoader: appending \"/\" to path " + config); |
63 | 0 | config = config.concat("/"); |
64 | } | |
65 | ||
66 | // It isn't clear from the javadocs, whether ClassLoader.getResource() | |
67 | // needs a starting slash, so won't add one at the moment. Even worse, | |
68 | // most class-loaders require you to _not have_ a slash, so we'll | |
69 | // remove it, if it exists | |
70 | 0 | if (config.startsWith("/")) |
71 | { | |
72 | 0 | config = config.substring(1); |
73 | } | |
74 | 0 | this.path = config; |
75 | 0 | } |
76 | ||
77 | public Template load (String query, CacheElement ce) throws ResourceException | |
78 | { | |
79 | 0 | if (query.startsWith("/")) |
80 | { | |
81 | 0 | query = query.substring(1); |
82 | } | |
83 | 0 | URL url = loader.getResource(path.concat(query)); |
84 | 0 | if (url != null && _log.isDebugEnabled()) |
85 | { | |
86 | 0 | _log.debug("ClassPathTemplateProvider: Found Template " + url.toString()); |
87 | } | |
88 | 0 | return (url != null) ? helper.load(url, ce) : null; |
89 | } | |
90 | } |