KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > GenericViewRenderer


1 /*
2  * $Id: GenericViewRenderer.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.view;
26
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.OutputStreamWriter JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40
41 import com.anthonyeden.lib.config.Configuration;
42 import com.anthonyeden.lib.config.ConfigurationException;
43 import com.anthonyeden.lib.config.XMLConfiguration;
44
45 import org.ofbiz.base.util.Debug;
46 import org.ofbiz.entity.GenericValue;
47 import org.ofbiz.security.Security;
48
49 import org.jpublish.JPublishContext;
50 import org.jpublish.Page;
51 import org.jpublish.SiteContext;
52 import org.jpublish.Template;
53 import org.jpublish.page.PageInstance;
54 import org.jpublish.view.ViewRenderException;
55 import org.jpublish.view.ViewRenderer;
56
57 /**
58  * Generic JPublish View Renderer - This is in testing; for use in wrapping other renderers
59  *
60  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
61  * @version $Rev: 5462 $
62  * @since 2.1
63  */

64 public class GenericViewRenderer implements ViewRenderer {
65         
66     public static final String JavaDoc module = GenericViewRenderer.class.getName();
67     public static final String JavaDoc DEFAULT_RENDERER = "freemarker";
68     public Map JavaDoc renderers = null;
69         
70     protected SiteContext siteContext = null;
71
72     /**
73      * @see org.jpublish.view.ViewRenderer#setSiteContext(org.jpublish.SiteContext)
74      */

75     public void setSiteContext(SiteContext siteContext) {
76         this.siteContext = siteContext;
77         this.renderers = new HashMap JavaDoc();
78         try {
79             loadCustom();
80         } catch (Exception JavaDoc e) {
81             Debug.logError(e, "Problems loading custom settings", module);
82             throw new RuntimeException JavaDoc(e.toString());
83         }
84     }
85
86     /* (non-Javadoc)
87      * @see org.jpublish.view.ViewRenderer#init()
88      */

89     public void init() throws Exception JavaDoc {
90     }
91
92     /**
93      * @see org.jpublish.view.ViewRenderer#render(org.jpublish.JPublishContext, java.io.Reader, java.io.Writer)
94      */

95     public void render(JPublishContext context, String JavaDoc path, Reader JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc, ViewRenderException {
96         HttpServletRequest JavaDoc request = context.getRequest();
97         HttpSession JavaDoc session = context.getSession();
98         Security security = (Security) request.getAttribute("security");
99         GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
100         
101         Page parent = (Page) context.get("page");
102         Page page = getPage(path);
103             
104         // decorate the content w/ edit images if we have permission
105
if (userLogin != null && security.hasEntityPermission("CONTENTMGR", "_UPDATE", userLogin)) {
106             out.write("<a HREF='/content/control/editContent?filePath=" + path + "'>*</a>");
107         }
108         
109         /* this loops -- not good
110         // if this page has a template, lets render the template
111         if (page != null && parent != null && page.getPath() != parent.getPath()) {
112             Debug.logInfo("Parent: " + parent.getPath(), module);
113             Debug.logInfo("Page: " + page.getPath(), module);
114             Debug.logInfo("Template: " + page.getFullTemplateName(), module);
115             if (!page.getTemplateName().equals("basic")) {
116                 renderTemplate(cloneContext(context), page, out);
117                 return;
118             }
119         }
120         */

121         
122         // get the view renderer for this page
123
if (Debug.verboseOn()) Debug.logVerbose("Getting renderer for: " + path, module);
124         String JavaDoc rendererName = DEFAULT_RENDERER;
125         if (page != null) {
126             rendererName = page.getProperty("page-renderer");
127             if (rendererName == null)
128                 rendererName = DEFAULT_RENDERER;
129         }
130                                                         
131         ViewRenderer renderer = (ViewRenderer) renderers.get(rendererName);
132         if (renderer == null)
133             renderer = (ViewRenderer) renderers.get(DEFAULT_RENDERER);
134                             
135         // call the renderer to render the rest of the page.
136
Debug.logVerbose("Calling render", module);
137         renderer.render(context, path, in, out);
138     }
139     
140     private void renderTemplate(JPublishContext context, Page page, Writer JavaDoc out) throws IOException JavaDoc, ViewRenderException {
141         context.disableCheckReservedNames(this);
142         context.put("page", page);
143         if (siteContext.isProtectReservedNames()) {
144             context.enableCheckReservedNames(this);
145         }
146         try {
147             Debug.logInfo("Merging template", module);
148             Template template = siteContext.getTemplateManager().getTemplate(page.getFullTemplateName());
149             template.merge(context, page, out);
150         } catch (Exception JavaDoc e) {
151             throw new ViewRenderException(e);
152         }
153     }
154     
155     private JPublishContext cloneContext(JPublishContext context) {
156         JPublishContext newContext = new JPublishContext(this);
157         context.disableCheckReservedNames(this);
158         Object JavaDoc keys[] = context.getKeys();
159         for (int i = 0; i < keys.length; i++)
160             newContext.put((String JavaDoc) keys[i], context.get((String JavaDoc) keys[i]));
161         if (siteContext.isProtectReservedNames()) {
162             context.enableCheckReservedNames(this);
163         }
164         return newContext;
165     }
166
167     /**
168      * @see org.jpublish.view.ViewRenderer#render(org.jpublish.JPublishContext, java.io.InputStream, java.io.OutputStream)
169      */

170     public void render(JPublishContext context, String JavaDoc path, InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc, ViewRenderException {
171         render(context, path, new InputStreamReader JavaDoc(in), new OutputStreamWriter JavaDoc(out));
172     }
173
174     /**
175      * @see org.jpublish.view.ViewRenderer#loadConfiguration(com.anthonyeden.lib.config.Configuration)
176      */

177     public void loadConfiguration(Configuration config) throws ConfigurationException {
178     }
179     
180     private Page getPage(String JavaDoc path) {
181         Page page = null;
182         try {
183             PageInstance pi = siteContext.getPageManager().getPage(path.substring(path.lastIndexOf(":")+1));
184             if (pi != null)
185                 page = new Page(pi);
186         } catch (Exception JavaDoc e) {}
187         return page;
188     }
189     
190     private void loadCustom() throws Exception JavaDoc {
191         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
192         InputStream JavaDoc in = new FileInputStream JavaDoc(siteContext.getConfigurationFile());
193         Configuration configuration = new XMLConfiguration(in);
194         
195         Iterator JavaDoc renderElements = configuration.getChildren("page-renderer").iterator();
196         while (renderElements.hasNext()) {
197             Configuration viewRendererConfiguration = (Configuration) renderElements.next();
198             String JavaDoc renderName = viewRendererConfiguration.getAttribute("name");
199             String JavaDoc className = viewRendererConfiguration.getAttribute("classname");
200             ViewRenderer renderer = (ViewRenderer) cl.loadClass(className).newInstance();
201             renderer.setSiteContext(siteContext);
202             renderer.loadConfiguration(viewRendererConfiguration);
203             renderer.init();
204             Debug.logInfo("Added renderer [" + renderName + "] - [" + className + "]", module);
205             renderers.put(renderName, renderer);
206         }
207     }
208 }
Popular Tags