KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > view > freemarker > JPublishTemplateLoader


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.view.freemarker;
50
51 import java.io.IOException JavaDoc;
52 import java.io.Reader JavaDoc;
53
54 import freemarker.cache.TemplateLoader;
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57 import org.jpublish.SiteContext;
58 import org.jpublish.view.ContentSource;
59
60 /**
61  * Implementation of the FreeMarker TemplateLoader interface.
62  *
63  * @author Anthony Eden
64  * @since 2.0
65  */

66
67 public class JPublishTemplateLoader implements TemplateLoader {
68
69     private Log log = LogFactory.getLog(JPublishTemplateLoader.class);
70
71     private SiteContext siteContext;
72
73     /**
74      * Set the SiteContext.
75      *
76      * @param siteContext The SiteContext
77      */

78
79     public void setSiteContext(SiteContext siteContext) {
80         this.siteContext = siteContext;
81     }
82
83     /**
84      * Find the template source.
85      *
86      * @param name The template name
87      * @return The source object
88      */

89
90     public Object JavaDoc findTemplateSource(String JavaDoc name) {
91         if (log.isDebugEnabled()) {
92             log.debug("findTemplateSource(" + name + ")");
93         }
94
95         Object JavaDoc content = siteContext.getContentSource(name);
96         if (log.isDebugEnabled()) {
97             log.debug("findTemplateSource() content: " + content);
98         }
99         return content;
100     }
101
102     /**
103      * Get the last modified time of the template source.
104      *
105      * @param templateSource The source
106      * @return The last modified time
107      */

108
109     public long getLastModified(Object JavaDoc templateSource) {
110         try {
111             log.debug("getLastModified() invoked");
112             long lastModified =
113                     ((ContentSource) templateSource).getLastModified();
114             if (log.isDebugEnabled()) {
115                 log.debug("Last modified time: " + lastModified);
116             }
117             return lastModified;
118         } catch (IOException JavaDoc e) {
119             log.error("An IO exception occured while retrieving the last " +
120                     "modified time");
121             return -1;
122         }
123     }
124
125     /**
126      * Get the template source reader.
127      *
128      * @param templateSource The source
129      * @param encoding The character encoding
130      * @return The Reader
131      * @throws IOException
132      */

133
134     public Reader JavaDoc getReader(Object JavaDoc templateSource, String JavaDoc encoding)
135             throws IOException JavaDoc {
136         log.debug("getReader() invoked");
137         return ((ContentSource) templateSource).getReader(encoding);
138     }
139
140     /**
141      * Close the specified template source.
142      *
143      * @param templateSource The template source
144      */

145
146     public void closeTemplateSource(Object JavaDoc templateSource) {
147         // currently no-op. Cleanup needed?
148
}
149
150 }
151
Popular Tags