1 package org.apache.turbine.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.lang.exception.ExceptionUtils;
24
25 import org.apache.ecs.ConcreteElement;
26 import org.apache.ecs.StringElement;
27
28 import org.apache.velocity.context.Context;
29
30 import org.apache.turbine.Turbine;
31 import org.apache.turbine.TurbineConstants;
32 import org.apache.turbine.services.template.TurbineTemplate;
33 import org.apache.turbine.services.velocity.TurbineVelocity;
34 import org.apache.turbine.util.RunData;
35
36 /***
37 * Base Velocity Screen. The buildTemplate() assumes the template
38 * parameter has been set in the RunData object. This provides the
39 * ability to execute several templates from one Screen.
40 *
41 * <p>
42 *
43 * If you need more specific behavior in your application, extend this
44 * class and override the doBuildTemplate() method.
45 *
46 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
47 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
48 * @version $Id: VelocityScreen.java 534527 2007-05-02 16:10:59Z tv $
49 */
50 public class VelocityScreen
51 extends TemplateScreen
52 {
53 /*** The prefix for lookup up screen pages */
54 private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
55
56 /***
57 * Velocity Screens extending this class should overide this
58 * method to perform any particular business logic and add
59 * information to the context.
60 *
61 * @param data Turbine information.
62 * @param context Context for web pages.
63 * @exception Exception, a generic exception.
64 */
65 protected void doBuildTemplate(RunData data,
66 Context context)
67 throws Exception
68 {
69 }
70
71 /***
72 * Needs to be implemented to make TemplateScreen like us. The
73 * actual method that you should override is the one with the
74 * context in the parameter list.
75 *
76 * @param data Turbine information.
77 * @exception Exception, a generic exception.
78 */
79 protected void doBuildTemplate(RunData data)
80 throws Exception
81 {
82 doBuildTemplate(data, TurbineVelocity.getContext(data));
83 }
84
85 /***
86 * This builds the Velocity template.
87 *
88 * @param data Turbine information.
89 * @return A ConcreteElement.
90 * @exception Exception, a generic exception.
91 */
92 public ConcreteElement buildTemplate(RunData data)
93 throws Exception
94 {
95 String screenData = null;
96
97 Context context = TurbineVelocity.getContext(data);
98
99 String screenTemplate = data.getTemplateInfo().getScreenTemplate();
100 String templateName
101 = TurbineTemplate.getScreenTemplateName(screenTemplate);
102
103
104 if (StringUtils.isEmpty(templateName))
105 {
106 log.error("Screen " + screenTemplate + " not found!");
107 throw new Exception("Could not find screen for " + screenTemplate);
108 }
109
110 try
111 {
112
113
114 if (getLayout(data) == null)
115 {
116 TurbineVelocity.handleRequest(context,
117 prefix + templateName,
118 data.getResponse().getOutputStream());
119 }
120 else
121 {
122 screenData = TurbineVelocity
123 .handleRequest(context, prefix + templateName);
124 }
125 }
126 catch (Exception e)
127 {
128
129
130
131 context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
132 context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
133
134 templateName = Turbine.getConfiguration()
135 .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
136 TurbineConstants.TEMPLATE_ERROR_VM);
137
138 screenData = TurbineVelocity.handleRequest(
139 context, prefix + templateName);
140 }
141
142
143 StringElement output = new StringElement();
144 output.setFilterState(false);
145
146 if (screenData != null)
147 {
148 output.addElement(screenData);
149 }
150 return output;
151 }
152
153 /***
154 * Return the Context needed by Velocity.
155 *
156 * @param data Turbine information.
157 * @return A Context.
158 *
159 * @deprecated Use TurbineVelocity.getContext(data)
160 */
161 public static Context getContext(RunData data)
162 {
163 return TurbineVelocity.getContext(data);
164 }
165 }