KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > ftl > FreeMarkerViewRenderer


1 /*
2  * $Id: FreeMarkerViewRenderer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.ftl;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import freemarker.ext.beans.BeansWrapper;
36 import freemarker.template.SimpleHash;
37 import freemarker.template.Template;
38 import freemarker.template.WrappingTemplateModel;
39
40 import org.ofbiz.base.util.Debug;
41 import org.ofbiz.base.util.UtilHttp;
42
43 import org.jpublish.JPublishContext;
44 import org.jpublish.Page;
45 import org.jpublish.SiteContext;
46 import org.jpublish.page.PageInstance;
47 import org.jpublish.view.ViewRenderException;
48
49 /**
50  * JPublish View Renderer For Freemarker Template Engine
51  *
52  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
53  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
54  * @version $Rev: 5462 $
55  * @since 2.1
56  */

57 public class FreeMarkerViewRenderer extends org.jpublish.view.freemarker.FreeMarkerViewRenderer {
58
59     public static final String JavaDoc module = FreeMarkerViewRenderer.class.getName();
60
61     public void init() throws Exception JavaDoc {
62         super.init();
63         //TODO: find some way of getting the site identifier... hmmm...
64
String JavaDoc id = "unknown";
65         fmConfig.setCacheStorage(new OfbizCacheStorage(id));
66         fmConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
67     }
68
69     protected Object JavaDoc createViewContext(JPublishContext context, String JavaDoc path) throws ViewRenderException {
70         HttpServletRequest JavaDoc request = context.getRequest();
71         HttpServletResponse JavaDoc response = context.getResponse();
72
73         BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
74         WrappingTemplateModel.setDefaultObjectWrapper(wrapper);
75         Map JavaDoc contextMap = new HashMap JavaDoc();
76         SimpleHash root = new SimpleHash(wrapper);
77         try {
78             Object JavaDoc[] keys = context.getKeys();
79             for (int i = 0; i < keys.length; i++) {
80                 String JavaDoc key = (String JavaDoc) keys[i];
81                 Object JavaDoc value = context.get(key);
82                 if (value != null) {
83                     contextMap.put(key, value);
84                     //no longer wrapping; let FM do it if needed, more efficient
85
//root.put(key, wrapper.wrap(value));
86
root.put(key, value);
87                 }
88             }
89             root.put("context", wrapper.wrap(contextMap));
90             root.put("cachedInclude", new JpCacheIncludeTransform()); // only adding this in for JP!
91
//root.put("jpublishContext", wrapper.wrap(context));
92
FreeMarkerViewHandler.prepOfbizRoot(root, request, response);
93         } catch (Exception JavaDoc e) {
94             throw new ViewRenderException(e);
95         }
96         return root;
97     }
98
99     public void render(JPublishContext context, String JavaDoc path, Reader JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc, ViewRenderException {
100         try {
101             Page page = (Page) context.get(JPublishContext.JPUBLISH_PAGE);
102             Object JavaDoc viewContext = createViewContext(context, path);
103
104             Template template = fmConfig.getTemplate(path, UtilHttp.getLocale(context.getRequest()));
105             template.setObjectWrapper(BeansWrapper.getDefaultInstance());
106
107             /* NEVER add content to the beginning of templates; this effects XML processing which requires the
108                first line remain intact.
109             boolean showTemplateId = UtilProperties.propertyValueEquals("content", "freemarker.showTemplateId", "Y");
110             String templateIdPrefix = UtilProperties.getPropertyValue("content", "freemarker.templateIdPrefix", "[system]");
111             if (showTemplateId) {
112                 out.write("\n<!-- " + templateIdPrefix + " begin: " + template.getName() + " -->\n");
113             }
114             */

115
116             template.process(viewContext, out);
117
118             /*
119             if (showTemplateId) {
120                 out.write("\n<!-- " + templateIdPrefix + " end: " + template.getName() + " -->\n");
121             }
122             */

123         } catch (IOException JavaDoc e) {
124             throw e;
125         } catch (Exception JavaDoc e) {
126             Debug.logError(e, "Exception from FreeMarker", module);
127             throw new ViewRenderException(e);
128         }
129     }
130
131     private Page getPage(String JavaDoc path, JPublishContext context) {
132         Page page = null;
133         try {
134             SiteContext siteContext = (SiteContext) context.get("site");
135             PageInstance pi = siteContext.getPageManager().getPage(path.substring(path.lastIndexOf(":") + 1));
136             if (pi != null)
137                 page = new Page(pi);
138         } catch (Exception JavaDoc e) {
139         }
140         return page;
141     }
142
143 }
144
Popular Tags