1 package org.apache.turbine.modules.screens; 2 3 18 19 import org.apache.commons.lang.StringUtils; 20 import org.apache.commons.lang.exception.ExceptionUtils; 21 22 import org.apache.ecs.ConcreteElement; 23 import org.apache.ecs.StringElement; 24 25 import org.apache.velocity.context.Context; 26 27 import org.apache.turbine.Turbine; 28 import org.apache.turbine.TurbineConstants; 29 import org.apache.turbine.services.template.TurbineTemplate; 30 import org.apache.turbine.services.velocity.TurbineVelocity; 31 import org.apache.turbine.util.RunData; 32 33 47 public class VelocityScreen 48 extends TemplateScreen 49 { 50 51 private String prefix = TurbineConstants.SCREEN_PREFIX + "/"; 52 53 62 protected void doBuildTemplate(RunData data, 63 Context context) 64 throws Exception 65 { 66 } 67 68 76 protected void doBuildTemplate(RunData data) 77 throws Exception 78 { 79 doBuildTemplate(data, TurbineVelocity.getContext(data)); 80 } 81 82 89 public ConcreteElement buildTemplate(RunData data) 90 throws Exception 91 { 92 String screenData = null; 93 94 Context context = TurbineVelocity.getContext(data); 95 96 String screenTemplate = data.getTemplateInfo().getScreenTemplate(); 97 String templateName 98 = TurbineTemplate.getScreenTemplateName(screenTemplate); 99 100 if (StringUtils.isEmpty(templateName)) 102 { 103 log.error("Screen " + screenTemplate + " not found!"); 104 throw new Exception ("Could not find screen for " + screenTemplate); 105 } 106 107 try 108 { 109 if (getLayout(data) == null) 112 { 113 TurbineVelocity.handleRequest(context, 114 prefix + templateName, 115 data.getResponse().getOutputStream()); 116 } 117 else 118 { 119 screenData = TurbineVelocity 120 .handleRequest(context, prefix + templateName); 121 } 122 } 123 catch (Exception e) 124 { 125 context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString()); 129 context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e)); 130 131 templateName = Turbine.getConfiguration() 132 .getString(TurbineConstants.TEMPLATE_ERROR_KEY, 133 TurbineConstants.TEMPLATE_ERROR_VM); 134 135 screenData = TurbineVelocity.handleRequest( 136 context, prefix + templateName); 137 } 138 139 StringElement output = new StringElement(); 141 output.setFilterState(false); 142 143 if (screenData != null) 144 { 145 output.addElement(screenData); 146 } 147 return output; 148 } 149 150 158 public static Context getContext(RunData data) 159 { 160 return TurbineVelocity.getContext(data); 161 } 162 } 163 | Popular Tags |