View Javadoc

1   package org.apache.turbine.modules.screens;
2   
3   /*
4    * Copyright 2001-2005 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  
24  import org.apache.velocity.context.Context;
25  
26  import org.apache.turbine.Turbine;
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.services.template.TurbineTemplate;
29  import org.apache.turbine.services.velocity.TurbineVelocity;
30  import org.apache.turbine.util.RunData;
31  
32  /***
33   * VelocityDirectScreen is a screen class which returns its output
34   * directly to the output stream. It can be used if buffering an
35   * output screen isn't possible or the result doesn't fit in the
36   * memory.
37   * <p>
38   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
39   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40   * @version $Id: VelocityDirectScreen.java 264148 2005-08-29 14:21:04Z henning $
41   */
42  public class VelocityDirectScreen
43      extends VelocityScreen
44  {
45      /*** The prefix for lookup up screen pages */
46      private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
47  
48      /***
49       * This builds the Velocity template.
50       *
51       * @param data Turbine information.
52       * @return A ConcreteElement.
53       * @exception Exception, a generic exception.
54       */
55      public ConcreteElement buildTemplate(RunData data)
56          throws Exception
57      {
58          Context context = TurbineVelocity.getContext(data);
59  
60          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
61          String templateName
62              = TurbineTemplate.getScreenTemplateName(screenTemplate);
63  
64          // The Template Service could not find the Screen
65          if (StringUtils.isEmpty(templateName))
66          {
67              log.error("Screen " + screenTemplate + " not found!");
68              throw new Exception("Could not find screen for " + screenTemplate);
69          }
70  
71          try
72          {
73              TurbineVelocity.handleRequest(context,
74                                            prefix + templateName,
75                                            data.getOut());
76  
77          }
78          catch (Exception e)
79          {
80              // If there is an error, build a $processingException and
81              // attempt to call the error.vm template in the screens
82              // directory.
83              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
84              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
85  
86              templateName = Turbine.getConfiguration()
87                  .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
88                             TurbineConstants.TEMPLATE_ERROR_VM);
89  
90              TurbineVelocity.handleRequest(context,
91                      prefix + templateName,
92                      data.getOut());
93          }
94  
95          return null;
96      }
97  }