KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > modules > screens > VelocityScreen


1 package org.apache.turbine.modules.screens;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 /**
34  * Base Velocity Screen. The buildTemplate() assumes the template
35  * parameter has been set in the RunData object. This provides the
36  * ability to execute several templates from one Screen.
37  *
38  * <p>
39  *
40  * If you need more specific behavior in your application, extend this
41  * class and override the doBuildTemplate() method.
42  *
43  * @author <a HREF="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
44  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
45  * @version $Id: VelocityScreen.java,v 1.9.2.3 2004/08/14 20:11:41 henning Exp $
46  */

47 public class VelocityScreen
48     extends TemplateScreen
49 {
50     /** The prefix for lookup up screen pages */
51     private String JavaDoc prefix = TurbineConstants.SCREEN_PREFIX + "/";
52
53     /**
54      * Velocity Screens extending this class should overide this
55      * method to perform any particular business logic and add
56      * information to the context.
57      *
58      * @param data Turbine information.
59      * @param context Context for web pages.
60      * @exception Exception, a generic exception.
61      */

62     protected void doBuildTemplate(RunData data,
63                                    Context context)
64             throws Exception JavaDoc
65     {
66     }
67
68     /**
69      * Needs to be implemented to make TemplateScreen like us. The
70      * actual method that you should override is the one with the
71      * context in the parameter list.
72      *
73      * @param data Turbine information.
74      * @exception Exception, a generic exception.
75      */

76     protected void doBuildTemplate(RunData data)
77             throws Exception JavaDoc
78     {
79         doBuildTemplate(data, TurbineVelocity.getContext(data));
80     }
81
82     /**
83      * This builds the Velocity template.
84      *
85      * @param data Turbine information.
86      * @return A ConcreteElement.
87      * @exception Exception, a generic exception.
88      */

89     public ConcreteElement buildTemplate(RunData data)
90         throws Exception JavaDoc
91     {
92         String JavaDoc screenData = null;
93         
94         Context context = TurbineVelocity.getContext(data);
95
96         String JavaDoc screenTemplate = data.getTemplateInfo().getScreenTemplate();
97         String JavaDoc templateName
98             = TurbineTemplate.getScreenTemplateName(screenTemplate);
99         
100         // The Template Service could not find the Screen
101
if (StringUtils.isEmpty(templateName))
102         {
103             log.error("Screen " + screenTemplate + " not found!");
104             throw new Exception JavaDoc("Could not find screen for " + screenTemplate);
105         }
106         
107         try
108         {
109             // if a layout has been defined return the results, otherwise
110
// send the results directly to the output stream.
111
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 JavaDoc e)
124         {
125             // If there is an error, build a $processingException and
126
// attempt to call the error.vm template in the screens
127
// directory.
128
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         // package the response in an ECS element
140
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     /**
151      * Return the Context needed by Velocity.
152      *
153      * @param data Turbine information.
154      * @return A Context.
155      *
156      * @deprecated Use TurbineVelocity.getContext(data)
157      */

158     public static Context getContext(RunData data)
159     {
160         return TurbineVelocity.getContext(data);
161     }
162 }
163
Popular Tags