KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > freemarker > SpringTemplateLoader


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.ui.freemarker;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 import freemarker.cache.TemplateLoader;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.core.io.Resource;
28 import org.springframework.core.io.ResourceLoader;
29
30 /**
31  * FreeMarker TemplateLoader adapter that loads via a Spring ResourceLoader.
32  * Used by FreeMarkerConfigurationFactory for any resource loader path that
33  * cannot be resolved to a java.io.File.
34  *
35  * <p>Note that this loader does not allow for modification detection:
36  * Use FreeMarker's default TemplateLoader for java.io.File resources.
37  *
38  * @author Juergen Hoeller
39  * @since 14.03.2004
40  * @see FreeMarkerConfigurationFactory#setTemplateLoaderPath
41  * @see freemarker.template.Configuration#setDirectoryForTemplateLoading
42  */

43 public class SpringTemplateLoader implements TemplateLoader {
44
45     protected final Log logger = LogFactory.getLog(getClass());
46
47     private final ResourceLoader resourceLoader;
48
49     private final String JavaDoc templateLoaderPath;
50
51
52     /**
53      * Create a new SpringTemplateLoader.
54      * @param resourceLoader the Spring ResourceLoader to use
55      * @param templateLoaderPath the template loader path to use
56      */

57     public SpringTemplateLoader(ResourceLoader resourceLoader, String JavaDoc templateLoaderPath) {
58         this.resourceLoader = resourceLoader;
59         if (!templateLoaderPath.endsWith("/")) {
60             templateLoaderPath += "/";
61         }
62         this.templateLoaderPath = templateLoaderPath;
63         if (logger.isInfoEnabled()) {
64             logger.info("SpringTemplateLoader for FreeMarker: using resource loader [" + this.resourceLoader +
65                     "] and template loader path [" + this.templateLoaderPath + "]");
66         }
67     }
68
69     public Object JavaDoc findTemplateSource(String JavaDoc name) throws IOException JavaDoc {
70         if (logger.isDebugEnabled()) {
71             logger.debug("Looking for FreeMarker template with name [" + name + "]");
72         }
73         Resource resource = this.resourceLoader.getResource(this.templateLoaderPath + name);
74         return (resource.exists() ? resource : null);
75     }
76
77     public Reader JavaDoc getReader(Object JavaDoc templateSource, String JavaDoc encoding) throws IOException JavaDoc {
78         Resource resource = (Resource) templateSource;
79         try {
80             return new InputStreamReader JavaDoc(resource.getInputStream(), encoding);
81         }
82         catch (IOException JavaDoc ex) {
83             if (logger.isDebugEnabled()) {
84                 logger.debug("Could not find FreeMarker template: " + resource);
85             }
86             throw ex;
87         }
88     }
89
90
91     public long getLastModified(Object JavaDoc templateSource) {
92         return -1;
93     }
94
95     public void closeTemplateSource(Object JavaDoc templateSource) throws IOException JavaDoc {
96     }
97
98 }
99
Popular Tags