001: /*
002: * $Id: FreeMarkerViewRenderer.java,v 1.6 2004/01/07 19:30:11 byersa Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.ftl;
026:
027: import java.io.IOException;
028: import java.io.Reader;
029: import java.io.Writer;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035:
036: import org.jpublish.JPublishContext;
037: import org.jpublish.Page;
038: import org.jpublish.SiteContext;
039: import org.jpublish.page.PageInstance;
040: import org.jpublish.view.ViewRenderException;
041: import org.ofbiz.base.util.Debug;
042:
043: import freemarker.ext.beans.BeansWrapper;
044: import freemarker.template.SimpleHash;
045: import freemarker.template.Template;
046: import freemarker.template.WrappingTemplateModel;
047:
048: /**
049: * JPublish View Renderer For Freemarker Template Engine
050: *
051: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
052: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
053: * @version $Revision: 1.6 $
054: * @since 2.1
055: */
056: public class FreeMarkerViewRenderer extends
057: org.jpublish.view.freemarker.FreeMarkerViewRenderer {
058:
059: public static final String module = FreeMarkerViewRenderer.class
060: .getName();
061:
062: public void init() throws Exception {
063: super .init();
064: //TODO: find some way of getting the site identifier... hmmm...
065: String id = "unknown";
066: fmConfig.setCacheStorage(new OfbizCacheStorage(id));
067: fmConfig.setSetting("datetime_format",
068: "yyyy-MM-dd HH:mm:ss.SSS");
069: }
070:
071: protected Object createViewContext(JPublishContext context,
072: String path) throws ViewRenderException {
073: HttpServletRequest request = context.getRequest();
074: HttpServletResponse response = context.getResponse();
075:
076: BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
077: WrappingTemplateModel.setDefaultObjectWrapper(wrapper);
078: Map contextMap = new HashMap();
079: SimpleHash root = new SimpleHash(wrapper);
080: try {
081: Object[] keys = context.getKeys();
082: for (int i = 0; i < keys.length; i++) {
083: String key = (String) keys[i];
084: Object value = context.get(key);
085: if (value != null) {
086: contextMap.put(key, value);
087: //no longer wrapping; let FM do it if needed, more efficient
088: //root.put(key, wrapper.wrap(value));
089: root.put(key, value);
090: }
091: //Debug.logVerbose("Key: " + key + " Value: " + value + ":" + (value == null ? "null" : value.getClass().getName()), module);
092: }
093: root.put("context", wrapper.wrap(contextMap));
094: //root.put("jpublishContext", wrapper.wrap(context));
095: FreeMarkerViewHandler
096: .prepOfbizRoot(root, request, response);
097: } catch (Exception e) {
098: throw new ViewRenderException(e);
099: }
100: return root;
101: }
102:
103: public void render(JPublishContext context, String path, Reader in,
104: Writer out) throws IOException, ViewRenderException {
105: if (Debug.verboseOn())
106: Debug.logVerbose("render(" + path + ")", module);
107: try {
108: Page page = (Page) context
109: .get(JPublishContext.JPUBLISH_PAGE);
110: Object viewContext = createViewContext(context, path);
111:
112: Template template = fmConfig.getTemplate(path, page
113: .getLocale());
114: template
115: .setObjectWrapper(BeansWrapper.getDefaultInstance());
116: template.process(viewContext, out);
117: } catch (IOException e) {
118: throw e;
119: } catch (Exception e) {
120: Debug.logError(e, "Exception from FreeMarker", module);
121: throw new ViewRenderException(e);
122: }
123: }
124:
125: private Page getPage(String path, JPublishContext context) {
126: Page page = null;
127: try {
128: SiteContext siteContext = (SiteContext) context.get("site");
129: PageInstance pi = siteContext.getPageManager().getPage(
130: path.substring(path.lastIndexOf(":") + 1));
131: if (pi != null)
132: page = new Page(pi);
133: } catch (Exception e) {
134: }
135: return page;
136: }
137:
138: }
|