01: package org.apache.turbine.modules.screens;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
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:
27: import org.apache.velocity.context.Context;
28:
29: import org.apache.turbine.Turbine;
30: import org.apache.turbine.TurbineConstants;
31: import org.apache.turbine.services.template.TurbineTemplate;
32: import org.apache.turbine.services.velocity.TurbineVelocity;
33: import org.apache.turbine.util.RunData;
34:
35: /**
36: * VelocityDirectScreen is a screen class which returns its output
37: * directly to the output stream. It can be used if buffering an
38: * output screen isn't possible or the result doesn't fit in the
39: * memory.
40: * <p>
41: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
42: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43: * @version $Id: VelocityDirectScreen.java 534527 2007-05-02 16:10:59Z tv $
44: */
45: public class VelocityDirectScreen extends VelocityScreen {
46: /** The prefix for lookup up screen pages */
47: private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
48:
49: /**
50: * This builds the Velocity template.
51: *
52: * @param data Turbine information.
53: * @return A ConcreteElement.
54: * @exception Exception, a generic exception.
55: */
56: public ConcreteElement buildTemplate(RunData data) throws Exception {
57: Context context = TurbineVelocity.getContext(data);
58:
59: String screenTemplate = data.getTemplateInfo()
60: .getScreenTemplate();
61: String templateName = TurbineTemplate
62: .getScreenTemplateName(screenTemplate);
63:
64: // The Template Service could not find the Screen
65: if (StringUtils.isEmpty(templateName)) {
66: log.error("Screen " + screenTemplate + " not found!");
67: throw new Exception("Could not find screen for "
68: + screenTemplate);
69: }
70:
71: try {
72: TurbineVelocity.handleRequest(context, prefix
73: + templateName, data.getOut());
74:
75: } catch (Exception e) {
76: // If there is an error, build a $processingException and
77: // attempt to call the error.vm template in the screens
78: // directory.
79: context.put(
80: TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER,
81: e.toString());
82: context.put(TurbineConstants.STACK_TRACE_PLACEHOLDER,
83: ExceptionUtils.getStackTrace(e));
84:
85: templateName = Turbine.getConfiguration().getString(
86: TurbineConstants.TEMPLATE_ERROR_KEY,
87: TurbineConstants.TEMPLATE_ERROR_VM);
88:
89: TurbineVelocity.handleRequest(context, prefix
90: + templateName, data.getOut());
91: }
92:
93: return null;
94: }
95: }
|