001: /*
002: * $Id: GenericViewRenderer.java,v 1.1 2003/08/17 08:40:12 ajzeneski 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.view;
026:
027: import java.io.FileInputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.InputStreamReader;
031: import java.io.OutputStream;
032: import java.io.OutputStreamWriter;
033: import java.io.Reader;
034: import java.io.Writer;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.Map;
038:
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpSession;
041:
042: import org.jpublish.JPublishContext;
043: import org.jpublish.Page;
044: import org.jpublish.SiteContext;
045: import org.jpublish.Template;
046: import org.jpublish.page.PageInstance;
047: import org.jpublish.view.ViewRenderException;
048: import org.jpublish.view.ViewRenderer;
049: import org.ofbiz.base.util.Debug;
050: import org.ofbiz.entity.GenericValue;
051: import org.ofbiz.security.Security;
052:
053: import com.anthonyeden.lib.config.Configuration;
054: import com.anthonyeden.lib.config.ConfigurationException;
055: import com.anthonyeden.lib.config.XMLConfiguration;
056:
057: /**
058: * Generic JPublish View Renderer - This is in testing; for use in wrapping other renderers
059: *
060: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
061: * @version $Revision: 1.1 $
062: * @since 2.1
063: */
064: public class GenericViewRenderer implements ViewRenderer {
065:
066: public static final String module = GenericViewRenderer.class
067: .getName();
068: public static final String DEFAULT_RENDERER = "freemarker";
069: public Map renderers = null;
070:
071: protected SiteContext siteContext = null;
072:
073: /**
074: * @see org.jpublish.view.ViewRenderer#setSiteContext(org.jpublish.SiteContext)
075: */
076: public void setSiteContext(SiteContext siteContext) {
077: this .siteContext = siteContext;
078: this .renderers = new HashMap();
079: try {
080: loadCustom();
081: } catch (Exception e) {
082: Debug.logError(e, "Problems loading custom settings",
083: module);
084: throw new RuntimeException(e.toString());
085: }
086: }
087:
088: /* (non-Javadoc)
089: * @see org.jpublish.view.ViewRenderer#init()
090: */
091: public void init() throws Exception {
092: }
093:
094: /**
095: * @see org.jpublish.view.ViewRenderer#render(org.jpublish.JPublishContext, java.io.Reader, java.io.Writer)
096: */
097: public void render(JPublishContext context, String path, Reader in,
098: Writer out) throws IOException, ViewRenderException {
099: HttpServletRequest request = context.getRequest();
100: HttpSession session = context.getSession();
101: Security security = (Security) request.getAttribute("security");
102: GenericValue userLogin = (GenericValue) session
103: .getAttribute("userLogin");
104:
105: Page parent = (Page) context.get("page");
106: Page page = getPage(path);
107:
108: // decorate the content w/ edit images if we have permission
109: if (userLogin != null
110: && security.hasEntityPermission("CONTENTMGR",
111: "_UPDATE", userLogin)) {
112: out.write("<a href='/content/control/editContent?filePath="
113: + path + "'>*</a>");
114: }
115:
116: /* this loops -- not good
117: // if this page has a template, lets render the template
118: if (page != null && parent != null && page.getPath() != parent.getPath()) {
119: Debug.logInfo("Parent: " + parent.getPath(), module);
120: Debug.logInfo("Page: " + page.getPath(), module);
121: Debug.logInfo("Template: " + page.getFullTemplateName(), module);
122: if (!page.getTemplateName().equals("basic")) {
123: renderTemplate(cloneContext(context), page, out);
124: return;
125: }
126: }
127: */
128:
129: // get the view renderer for this page
130: if (Debug.verboseOn())
131: Debug.logVerbose("Getting renderer for: " + path, module);
132: String rendererName = DEFAULT_RENDERER;
133: if (page != null) {
134: rendererName = page.getProperty("page-renderer");
135: if (rendererName == null)
136: rendererName = DEFAULT_RENDERER;
137: }
138:
139: ViewRenderer renderer = (ViewRenderer) renderers
140: .get(rendererName);
141: if (renderer == null)
142: renderer = (ViewRenderer) renderers.get(DEFAULT_RENDERER);
143:
144: // call the renderer to render the rest of the page.
145: Debug.logVerbose("Calling render", module);
146: renderer.render(context, path, in, out);
147: }
148:
149: private void renderTemplate(JPublishContext context, Page page,
150: Writer out) throws IOException, ViewRenderException {
151: context.disableCheckReservedNames(this );
152: context.put("page", page);
153: if (siteContext.isProtectReservedNames()) {
154: context.enableCheckReservedNames(this );
155: }
156: try {
157: Debug.logInfo("Merging template", module);
158: Template template = siteContext.getTemplateManager()
159: .getTemplate(page.getFullTemplateName());
160: template.merge(context, page, out);
161: } catch (Exception e) {
162: throw new ViewRenderException(e);
163: }
164: }
165:
166: private JPublishContext cloneContext(JPublishContext context) {
167: JPublishContext newContext = new JPublishContext(this );
168: context.disableCheckReservedNames(this );
169: Object keys[] = context.getKeys();
170: for (int i = 0; i < keys.length; i++)
171: newContext.put((String) keys[i], context
172: .get((String) keys[i]));
173: if (siteContext.isProtectReservedNames()) {
174: context.enableCheckReservedNames(this );
175: }
176: return newContext;
177: }
178:
179: /**
180: * @see org.jpublish.view.ViewRenderer#render(org.jpublish.JPublishContext, java.io.InputStream, java.io.OutputStream)
181: */
182: public void render(JPublishContext context, String path,
183: InputStream in, OutputStream out) throws IOException,
184: ViewRenderException {
185: render(context, path, new InputStreamReader(in),
186: new OutputStreamWriter(out));
187: }
188:
189: /**
190: * @see org.jpublish.view.ViewRenderer#loadConfiguration(com.anthonyeden.lib.config.Configuration)
191: */
192: public void loadConfiguration(Configuration config)
193: throws ConfigurationException {
194: }
195:
196: private Page getPage(String path) {
197: Page page = null;
198: try {
199: PageInstance pi = siteContext.getPageManager().getPage(
200: path.substring(path.lastIndexOf(":") + 1));
201: if (pi != null)
202: page = new Page(pi);
203: } catch (Exception e) {
204: }
205: return page;
206: }
207:
208: private void loadCustom() throws Exception {
209: ClassLoader cl = Thread.currentThread().getContextClassLoader();
210: InputStream in = new FileInputStream(siteContext
211: .getConfigurationFile());
212: Configuration configuration = new XMLConfiguration(in);
213:
214: Iterator renderElements = configuration.getChildren(
215: "page-renderer").iterator();
216: while (renderElements.hasNext()) {
217: Configuration viewRendererConfiguration = (Configuration) renderElements
218: .next();
219: String renderName = viewRendererConfiguration
220: .getAttribute("name");
221: String className = viewRendererConfiguration
222: .getAttribute("classname");
223: ViewRenderer renderer = (ViewRenderer) cl.loadClass(
224: className).newInstance();
225: renderer.setSiteContext(siteContext);
226: renderer.loadConfiguration(viewRendererConfiguration);
227: renderer.init();
228: Debug.logInfo("Added renderer [" + renderName + "] - ["
229: + className + "]", module);
230: renderers.put(renderName, renderer);
231: }
232: }
233: }
|