1 23 24 package org.infoglue.deliver.util; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 import java.io.Reader ; 30 import java.io.StringReader ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import javax.servlet.RequestDispatcher ; 35 import javax.servlet.ServletException ; 36 37 import org.apache.log4j.Logger; 38 import org.apache.velocity.VelocityContext; 39 import org.apache.velocity.app.Velocity; 40 import org.infoglue.cms.io.FileHelper; 41 import org.infoglue.cms.util.CmsPropertyHandler; 42 import org.infoglue.deliver.applications.databeans.DeliveryContext; 43 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController; 44 45 49 50 public class VelocityTemplateProcessor 51 { 52 private final static Logger logger = Logger.getLogger(VelocityTemplateProcessor.class.getName()); 53 54 58 59 public void renderTemplate(Map params, PrintWriter pw, String templateAsString) throws Exception 60 { 61 renderTemplate(params, pw, templateAsString, false); 62 } 63 64 68 69 public void renderTemplate(Map params, PrintWriter pw, String templateAsString, boolean forceVelocity) throws Exception 70 { 71 try 72 { 73 Timer timer = new Timer(); 74 timer.setActive(false); 75 76 if(templateAsString.indexOf("<%") > -1 || templateAsString.indexOf("http://java.sun.com/products/jsp/dtd/jspcore_1_0.dtd") > -1) 77 { 78 dispatchJSP(params, pw, templateAsString); 79 } 80 else 81 { 82 boolean useFreeMarker = false; 83 String useFreeMarkerString = CmsPropertyHandler.getUseFreeMarker(); 84 if(useFreeMarkerString != null && useFreeMarkerString.equalsIgnoreCase("true")) 85 useFreeMarker = true; 86 87 if((useFreeMarker || templateAsString.indexOf("<#-- IG:FreeMarker -->") > -1) && !forceVelocity) 88 { 89 FreemarkerTemplateProcessor.getProcessor().renderTemplate(params, pw, templateAsString); 90 } 91 else 92 { 93 Velocity.init(); 94 95 VelocityContext context = new VelocityContext(); 96 Iterator i = params.keySet().iterator(); 97 while(i.hasNext()) 98 { 99 String key = (String )i.next(); 100 context.put(key, params.get(key)); 101 } 102 103 Reader reader = new StringReader (templateAsString); 104 boolean finished = Velocity.evaluate(context, pw, "Generator Error", reader); 105 } 106 } 107 108 timer.printElapsedTime("End renderTemplate"); 109 } 110 catch(Exception e) 111 { 112 logger.warn("templateAsString: \n" + (templateAsString.length() > 500 ? templateAsString.substring(0, 500) + "... (template truncated)." : templateAsString)); 113 114 if(CmsPropertyHandler.getOperatingMode().equalsIgnoreCase("0") && (CmsPropertyHandler.getDisableTemplateDebug() == null || !CmsPropertyHandler.getDisableTemplateDebug().equalsIgnoreCase("true"))) 115 pw.println("Error:" + e.getMessage()); 116 else 117 { 118 logger.warn("Warning:" + e.getMessage(), e); 119 throw e; 120 } 121 } 122 } 123 124 133 134 public void dispatchJSP(Map params, PrintWriter pw, String templateAsString) throws ServletException , IOException , Exception 135 { 136 Timer timer = new Timer(); 137 timer.setActive(false); 138 139 int hashCode = templateAsString.hashCode(); 140 141 String contextRootPath = CmsPropertyHandler.getContextRootPath(); 142 String fileName = contextRootPath + "jsp" + File.separator + "Template_" + hashCode + ".jsp"; 143 144 File template = new File (fileName); 145 if(!template.exists()) 146 FileHelper.writeToFile(template, templateAsString, false); 147 148 TemplateController templateController = (TemplateController)params.get("templateLogic"); 149 DeliveryContext deliveryContext = templateController.getDeliveryContext(); 150 RequestDispatcher dispatch = templateController.getHttpServletRequest().getRequestDispatcher("/jsp/Template_" + hashCode + ".jsp"); 151 templateController.getHttpServletRequest().setAttribute("org.infoglue.cms.deliver.templateLogic", templateController); 152 CharResponseWrapper wrapper = new CharResponseWrapper(deliveryContext.getHttpServletResponse()); 153 154 dispatch.include(templateController.getHttpServletRequest(), wrapper); 155 156 String result = wrapper.toString(); 157 158 pw.println(result); 159 } 160 161 } 162 | Popular Tags |