KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > template > Template


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

44 package org.jpublish.template;
45
46 import java.io.InputStream JavaDoc;
47 import java.io.Reader JavaDoc;
48 import java.io.Writer JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import com.anthonyeden.lib.config.Configuration;
54 import com.anthonyeden.lib.config.ConfigurationException;
55 import com.anthonyeden.lib.config.ConfigurationFactory;
56 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory;
57 import org.apache.commons.logging.Log;
58 import org.apache.commons.logging.LogFactory;
59 import org.jpublish.RequestContext;
60 import org.jpublish.SiteContext;
61 import org.jpublish.action.Action;
62 import org.jpublish.action.ActionWrapper;
63 import org.jpublish.page.Page;
64 import org.jpublish.util.PathUtilities;
65 import org.jpublish.view.ViewRenderer;
66
67 /**
68  * The Template class represents a reusable design template which is applied to one or more pages.
69  *
70  * @author Anthony Eden
71  */

72 public class Template {
73
74     private Log log = LogFactory.getLog(Template.class);
75
76     private SiteContext siteContext = null;
77     private String JavaDoc path = null;
78     private String JavaDoc templateText = null;
79     private String JavaDoc viewRendererName = null;
80     private long lastModified = -1;
81     private List JavaDoc templateActions = new ArrayList JavaDoc();
82     private String JavaDoc templateManagerName = null;
83
84     /**
85      * Construct a new Template.
86      *
87      * @param siteContext The current SiteContext
88      * @param path The template path
89      * @param templateManagerName The template manager name
90      */

91     public Template(SiteContext siteContext, String JavaDoc path,
92             String JavaDoc templateManagerName) {
93         this.siteContext = siteContext;
94         this.path = path;
95         
96         // note to self: this should probably be reimplemented so
97
// a template holds a reference to a TemplateInstance object
98
// in the same way that Content objects hold references to
99
// ContentInstance objects.
100
this.templateManagerName = templateManagerName;
101     }
102
103     /**
104      * Get the path to the template.
105      *
106      * @return The path
107      */

108     public String JavaDoc getPath() {
109         return path;
110     }
111
112     /**
113      * Get the template text.
114      *
115      * @return The template text
116      */

117
118     public String JavaDoc getText() {
119         log.debug("getText()");
120         return templateText;
121     }
122
123     /**
124      * Set the template text.
125      *
126      * @param templateText
127      */

128
129     public void setText(String JavaDoc templateText) {
130         this.templateText = templateText;
131     }
132
133     /**
134      * Get the last modified time of this template.
135      *
136      * @return The last modified time
137      */

138
139     public long getLastModified() {
140         return lastModified;
141     }
142
143     /**
144      * Set the last modified time of this template.
145      *
146      * @param lastModified The new last modified time
147      */

148
149     public void setLastModified(long lastModified) {
150         this.lastModified = lastModified;
151     }
152
153     /**
154      * Get the view renderer name. This method may return null if the default view renderer should be used.
155      *
156      * @return The view renderer name
157      */

158
159     public String JavaDoc getViewRendererName() {
160         return viewRendererName;
161     }
162
163     /**
164      * Set the view renderer name. Set this value to null to indicate that the default view renderer for the site should
165      * be used.
166      *
167      * @param viewRendererName The view renderer name
168      */

169
170     public void setViewRendererName(String JavaDoc viewRendererName) {
171         this.viewRendererName = viewRendererName;
172     }
173
174     /**
175      * Get the template manager name.
176      *
177      * @return The template manager name
178      */

179     public String JavaDoc getTemplateManagerName() {
180         return templateManagerName;
181     }
182
183     /**
184      * Get a List of actions associated with this template.
185      *
186      * @return A list of actions
187      */

188
189     public List JavaDoc getTemplateActions() {
190         return templateActions;
191     }
192
193     /**
194      * Execute any template actions.
195      *
196      * @param context The context
197      */

198
199     public void executeActions(RequestContext context) {
200         Iterator JavaDoc templateActions = getTemplateActions().iterator();
201         while (templateActions.hasNext()) {
202             ((ActionWrapper) templateActions.next()).execute(context);
203         }
204     }
205
206     /**
207      * Merge the given page with the template.
208      *
209      * @param context The current context
210      * @param page The page
211      * @param out The OutputStream
212      * @throws TemplateMergeException Exception while merging the template and the page.
213      */

214
215     public void merge(RequestContext context, Page page, Writer JavaDoc out)
216             throws TemplateMergeException {
217         try {
218             log.debug("Executing template actions");
219             executeActions(context);
220
221             /*
222             CharacterEncodingMap characterEncodingMap =
223                 siteContext.getCcharacterEncodingMap();
224             String templateEncoding =
225                 characterEncodingMap.getTemplateEncoding();
226             */

227             String JavaDoc path = PathUtilities.makeTemplateURI(getPath(),
228                     templateManagerName);
229             ViewRenderer renderer = siteContext.getViewRenderer();
230             String JavaDoc viewRendererName = getViewRendererName();
231             if (viewRendererName != null) {
232                 renderer = siteContext.getViewRenderer(viewRendererName);
233             }
234             renderer.render(context, path, out);
235
236             log.debug("Merge complete.");
237         } catch (Exception JavaDoc e) {
238             throw new TemplateMergeException(e.getMessage(), e);
239         }
240     }
241
242     /**
243      * Load the template's additional configuration information from the specified InputStream.
244      *
245      * @param in The InputStream
246      * @throws ConfigurationException Any configuration exception
247      */

248
249     public void loadConfiguration(InputStream JavaDoc in) throws ConfigurationException {
250         ConfigurationFactory configFactory =
251                 SAXConfigurationFactory.getInstance();
252         loadConfiguration(configFactory.getConfiguration(path, in));
253     }
254
255     /**
256      * Load the template's additional configuration information from the specified Reader.
257      *
258      * @param in The Reader
259      * @throws ConfigurationException Any configuration exception
260      */

261
262     public void loadConfiguration(Reader JavaDoc in) throws ConfigurationException {
263         ConfigurationFactory configFactory =
264                 SAXConfigurationFactory.getInstance();
265         loadConfiguration(configFactory.getConfiguration(path, in));
266     }
267
268     /**
269      * Load the template's configuration information.
270      *
271      * @param configuration The Configuration object
272      * @throws ConfigurationException Any configuration exception
273      * @since 2.0
274      */

275
276     public void loadConfiguration(Configuration configuration)
277             throws ConfigurationException {
278         templateActions.clear();
279
280         // set the template view renderer
281
setViewRendererName(configuration.getChildValue("view-renderer"));
282
283         // load template actions
284
log.debug("Looping through template-action elements.");
285         Iterator JavaDoc actionElements =
286                 configuration.getChildren("template-action").iterator();
287         while (actionElements.hasNext()) {
288             Configuration actionElement =
289                     (Configuration) actionElements.next();
290             String JavaDoc name = actionElement.getAttribute("name");
291             if (name == null) {
292                 name = actionElement.getValue();
293             }
294
295             if (name == null) {
296                 throw new ConfigurationException("Error configuring page-action.");
297             }
298
299             Action action = siteContext.getActionManager().findAction(name);
300             if (action == null) {
301                 throw new ConfigurationException("Action " + name + " not defined");
302             }
303
304             templateActions.add(new ActionWrapper(action, actionElement));
305         }
306     }
307
308 }
309
310
Popular Tags