01: package org.apache.turbine.modules.layouts;
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.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24:
25: import org.apache.turbine.TurbineConstants;
26: import org.apache.turbine.modules.Layout;
27: import org.apache.turbine.services.velocity.TurbineVelocity;
28: import org.apache.turbine.util.RunData;
29: import org.apache.turbine.util.template.TemplateNavigation;
30: import org.apache.turbine.util.template.TemplateScreen;
31:
32: import org.apache.velocity.context.Context;
33:
34: /**
35: * This Layout module allows Velocity templates
36: * to be used as layouts. It will stream directly the output of
37: * the layout and navigation templates to the output writer without
38: * using a screen. Use this if you have a large page to output
39: * and won't buffer it in the memory.
40: *
41: * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
42: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
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: VelocityDirectLayout.java 534527 2007-05-02 16:10:59Z tv $
46: */
47: public class VelocityDirectLayout extends Layout {
48: /** Logging */
49: private static Log log = LogFactory
50: .getLog(VelocityDirectLayout.class);
51:
52: /** The prefix for lookup up layout pages */
53: private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
54:
55: /**
56: * Method called by LayoutLoader.
57: *
58: * @param data Turbine information.
59: * @exception Exception a generic exception.
60: */
61: public void doBuild(RunData data) throws Exception {
62: // Get the context needed by Velocity.
63: Context context = TurbineVelocity.getContext(data);
64:
65: // variable for the screen in the layout template
66: context.put(TurbineConstants.SCREEN_PLACEHOLDER,
67: new TemplateScreen(data));
68:
69: // variable to reference the navigation screen in the layout template
70: context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
71: new TemplateNavigation(data));
72:
73: // Grab the layout template set in the VelocityPage.
74: // If null, then use the default layout template
75: // (done by the TemplateInfo object)
76: String templateName = data.getTemplateInfo()
77: .getLayoutTemplate();
78:
79: // Set the locale and content type
80: data.getResponse().setLocale(data.getLocale());
81: data.getResponse().setContentType(data.getContentType());
82:
83: log.debug("Now trying to render layout " + templateName);
84:
85: // Finally, generate the layout template and send it to the browser
86: TurbineVelocity.handleRequest(context, prefix + templateName,
87: data.getOut());
88: }
89: }
|