1 25 package org.ofbiz.webapp.view; 26 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.io.OutputStreamWriter ; 33 import java.io.Reader ; 34 import java.io.Writer ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpSession ; 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 64 public class GenericViewRenderer implements ViewRenderer { 65 66 public static final String module = GenericViewRenderer.class.getName(); 67 public static final String DEFAULT_RENDERER = "freemarker"; 68 public Map renderers = null; 69 70 protected SiteContext siteContext = null; 71 72 75 public void setSiteContext(SiteContext siteContext) { 76 this.siteContext = siteContext; 77 this.renderers = new HashMap (); 78 try { 79 loadCustom(); 80 } catch (Exception e) { 81 Debug.logError(e, "Problems loading custom settings", module); 82 throw new RuntimeException (e.toString()); 83 } 84 } 85 86 89 public void init() throws Exception { 90 } 91 92 95 public void render(JPublishContext context, String path, Reader in, Writer out) throws IOException , ViewRenderException { 96 HttpServletRequest request = context.getRequest(); 97 HttpSession 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 if (userLogin != null && security.hasEntityPermission("CONTENTMGR", "_UPDATE", userLogin)) { 106 out.write("<a HREF='/content/control/editContent?filePath=" + path + "'>*</a>"); 107 } 108 109 121 122 if (Debug.verboseOn()) Debug.logVerbose("Getting renderer for: " + path, module); 124 String 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 Debug.logVerbose("Calling render", module); 137 renderer.render(context, path, in, out); 138 } 139 140 private void renderTemplate(JPublishContext context, Page page, Writer out) throws IOException , 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 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 keys[] = context.getKeys(); 159 for (int i = 0; i < keys.length; i++) 160 newContext.put((String ) keys[i], context.get((String ) keys[i])); 161 if (siteContext.isProtectReservedNames()) { 162 context.enableCheckReservedNames(this); 163 } 164 return newContext; 165 } 166 167 170 public void render(JPublishContext context, String path, InputStream in, OutputStream out) throws IOException , ViewRenderException { 171 render(context, path, new InputStreamReader (in), new OutputStreamWriter (out)); 172 } 173 174 177 public void loadConfiguration(Configuration config) throws ConfigurationException { 178 } 179 180 private Page getPage(String 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 e) {} 187 return page; 188 } 189 190 private void loadCustom() throws Exception { 191 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 192 InputStream in = new FileInputStream (siteContext.getConfigurationFile()); 193 Configuration configuration = new XMLConfiguration(in); 194 195 Iterator renderElements = configuration.getChildren("page-renderer").iterator(); 196 while (renderElements.hasNext()) { 197 Configuration viewRendererConfiguration = (Configuration) renderElements.next(); 198 String renderName = viewRendererConfiguration.getAttribute("name"); 199 String 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 |