1 package org.apache.turbine.modules.screens;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 // Turbine/ECS Imports
58 import org.apache.ecs.ConcreteElement;
59 import org.apache.ecs.StringElement;
60 import org.apache.turbine.util.RunData;
61 import org.apache.turbine.util.StringUtils;
62 import org.apache.turbine.services.resources.TurbineResources;
63 import org.apache.turbine.services.velocity.TurbineVelocity;
64 import org.apache.turbine.services.template.TurbineTemplate;
65
66 // Velocity Stuff
67 import org.apache.velocity.context.Context;
68
69
70 /***
71 * Base Velocity Screen. The buildTemplate() assumes the template
72 * parameter has been set in the RunData object. This provides the
73 * ability to execute several templates from one Screen.
74 *
75 * <p>
76 *
77 * If you need more specific behavior in your application, extend this
78 * class and override the doBuildTemplate() method.
79 *
80 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
81 * @version $Id: VelocityScreen.java,v 1.1.1.1 2001/08/16 05:08:37 jvanzyl Exp $
82 */
83 public class VelocityScreen extends TemplateScreen
84 {
85 /***
86 * Velocity Screens extending this class should overide this
87 * method to perform any particular business logic and add
88 * information to the context.
89 *
90 * @param data Turbine information.
91 * @param context Context for web pages.
92 * @exception Exception, a generic exception.
93 */
94 protected void doBuildTemplate( RunData data,
95 Context context )
96 throws Exception
97 {
98 }
99
100 /***
101 * Needs to be implemented to make TemplateScreen like us. The
102 * actual method that you should override is the one with the
103 * context in the parameter list.
104 *
105 * @param data Turbine information.
106 * @exception Exception, a generic exception.
107 */
108 protected void doBuildTemplate( RunData data )
109 throws Exception
110 {
111 doBuildTemplate(data, TurbineVelocity.getContext(data));
112 }
113
114 /***
115 * This builds the Velocity template.
116 *
117 * @param data Turbine information.
118 * @return A ConcreteElement.
119 * @exception Exception, a generic exception.
120 */
121 public ConcreteElement buildTemplate( RunData data ) throws Exception
122 {
123 StringElement output = null;
124 String screenData = null;
125 Context context = TurbineVelocity.getContext(data);
126
127 // This will already be properly set and will not be null
128 // because of TemplateSessionValidator.
129 String templateName = TurbineTemplate.getScreenTemplateName(
130 data.getTemplateInfo().getScreenTemplate() );
131
132 // Template service adds the leading slash, but make it sure.
133 if ((templateName.length() > 0) &&
134 (templateName.charAt(0) != '/'))
135 {
136 templateName = '/' + templateName;
137 }
138
139 try
140 {
141 // if a layout has been defined return the results, otherwise
142 // send the results directly to the output stream.
143 if (getLayout(data) == null)
144 {
145 TurbineVelocity.handleRequest(context,
146 "screens" + templateName,
147 data.getResponse().getOutputStream());
148 }
149 else
150 {
151 screenData = TurbineVelocity
152 .handleRequest(context,"screens" + templateName);
153 }
154 }
155 catch (Exception e)
156 {
157 // If there is an error, build a $processingException and
158 // attempt to call the error.vm template in the screens
159 // directory.
160 context.put ( "processingException", e.toString() );
161 context.put ( "stackTrace", StringUtils.stackTrace(e) );
162 templateName = TurbineResources.getString(
163 "template.error", "/error.vm");
164 if ((templateName.length() > 0) &&
165 (templateName.charAt(0) != '/'))
166 {
167 templateName = '/' + templateName;
168 }
169 screenData = TurbineVelocity.handleRequest(
170 context, "screens" + templateName);
171 }
172
173 // package the response in an ECS element
174 if (screenData != null)
175 {
176 output = new StringElement();
177 output.setFilterState(false);
178 output.addElement(screenData);
179 }
180 return output;
181 }
182 /***
183 * Return the Context needed by Velocity.
184 *
185 * @param data Turbine information.
186 * @return A Context.
187 */
188 public static Context getContext(RunData data)
189 {
190 return TurbineVelocity.getContext(data);
191 }
192 }
This page was automatically generated by Maven