KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
52
53 import com.anthonyeden.lib.config.Configuration;
54 import com.anthonyeden.lib.config.ConfigurationException;
55 import freemarker.template.Template;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.jpublish.RequestContext;
59 import org.jpublish.page.Page;
60 import org.jpublish.view.AbstractViewRenderer;
61 import org.jpublish.view.ViewRenderException;
62
63 /**
64  * ViewRenderer which uses the FreeMarker template engine.
65  *
66  * @author Anthony Eden
67  */

68
69 public class FreeMarkerViewRenderer extends AbstractViewRenderer {
70
71     private Log log = LogFactory.getLog(FreeMarkerViewRenderer.class);
72     private JPublishTemplateLoader templateLoader = null;
73     private freemarker.template.Configuration fmConfig = null;
74
75     /**
76      * Initialize the ViewRenderer.
77      */

78
79     public void init() {
80         fmConfig = new freemarker.template.Configuration();
81
82         templateLoader = new JPublishTemplateLoader();
83         templateLoader.setSiteContext(siteContext);
84         fmConfig.setTemplateLoader(templateLoader);
85         fmConfig.setLocalizedLookup(false);
86     }
87
88     /**
89      * Render the view.
90      *
91      * @param context The RequestContext
92      * @param path The path to the template
93      * @param out The Writer to write the rendered view
94      * @throws IOException
95      * @throws ViewRenderException
96      */

97
98     public void render(RequestContext context, String JavaDoc path, Writer out)
99             throws IOException, ViewRenderException {
100         if (log.isDebugEnabled()) {
101             log.debug("render(" + path + ")");
102         }
103
104         try {
105             Page page = context.getPage();
106             Object JavaDoc viewContext = createViewContext(context, path);
107             Template template = fmConfig.getTemplate(path, page.getLocale(),
108                     page.getEncoding());
109             template.process(viewContext, out);
110         } catch (IOException e) {
111             throw e;
112         } catch (Exception JavaDoc e) {
113             throw new ViewRenderException(e);
114         }
115     }
116
117     /**
118      * Render the view.
119      *
120      * @param context The RequestContext
121      * @param path The path to the template
122      * @param out The OutputStream to write the rendered view
123      * @throws IOException
124      * @throws ViewRenderException
125      */

126
127     public void render(RequestContext context, String JavaDoc path, OutputStream out)
128             throws IOException, ViewRenderException {
129         render(context, path, new OutputStreamWriter(out));
130     }
131
132     /**
133      * Render the view.
134      *
135      * <p>Note that the implementation of this method results in no template caching which can affect performance. This
136      * method should only be used in cases where the template is generated dynamically (i.e. through pipelining.)</p>
137      *
138      * @param context The RequestContext
139      * @param path The path to the content
140      * @param in The Reader providing the raw content
141      * @param out The Writer to write the rendered view
142      * @throws IOException
143      * @throws ViewRenderException
144      */

145
146     public void render(RequestContext context, String JavaDoc path, Reader in,
147             Writer out) throws IOException, ViewRenderException {
148         if (log.isDebugEnabled()) {
149             log.debug("render(" + path + ")");
150         }
151
152         try {
153             Page page = context.getPage();
154             Object JavaDoc viewContext = createViewContext(context, path);
155             Template template = new Template(path, in);
156             template.process(viewContext, out);
157         } catch (IOException e) {
158             throw e;
159         } catch (Exception JavaDoc e) {
160             throw new ViewRenderException(e);
161         }
162     }
163
164     /**
165      * Render the view.
166      *
167      * <p>Note that the implementation of this method results in no template caching which can affect performance. This
168      * method should only be used in cases where the template is generated dynamically (i.e. through pipelining.)</p>
169      *
170      * @param context The RequestContext
171      * @param path The path to the content
172      * @param in The InputStream providing the raw content
173      * @param out The OutputStream to write the rendered view
174      * @throws IOException
175      * @throws ViewRenderException
176      */

177
178     public void render(RequestContext context, String JavaDoc path, InputStream in,
179             OutputStream out) throws IOException, ViewRenderException {
180         render(context, path, new InputStreamReader(in),
181                 new OutputStreamWriter(out));
182     }
183
184     /**
185      * Load the configuration for the view.
186      *
187      * @param configuration The configuration object
188      */

189
190     public void loadConfiguration(Configuration configuration)
191             throws ConfigurationException {
192
193     }
194
195     /**
196      * Create the 'root' context for the template engine. This method can be overridden in subclasses in case the
197      * viewContext needs to be populated with additional values. The default implementation wraps the existing
198      * JPublishContext in a class which is useable by FreeMarker.
199      *
200      * @param context The RequestContext
201      * @param path The path to the template
202      * @return Object The 'root' template context
203      * @throws ViewRenderException
204      */

205
206     protected Object JavaDoc createViewContext(RequestContext context, String JavaDoc path)
207             throws ViewRenderException {
208         FreeMarkerViewContext viewContext =
209                 new FreeMarkerViewContext(context);
210         return viewContext;
211     }
212
213 }
214
Popular Tags