View Javadoc

1   package org.apache.turbine.modules.layouts;
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.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import org.apache.turbine.TurbineConstants;
23  import org.apache.turbine.modules.Layout;
24  import org.apache.turbine.services.velocity.TurbineVelocity;
25  import org.apache.turbine.util.RunData;
26  import org.apache.turbine.util.template.TemplateNavigation;
27  import org.apache.turbine.util.template.TemplateScreen;
28  
29  import org.apache.velocity.context.Context;
30  
31  /***
32   * This Layout module allows Velocity templates
33   * to be used as layouts. It will stream directly the output of
34   * the layout and navigation templates to the output writer without
35   * using a screen. Use this if you have a large page to output
36   * and won't buffer it in the memory.
37   *
38   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
39   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
40   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
41   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42   * @version $Id: VelocityDirectLayout.java 278958 2005-09-06 09:35:39Z henning $
43   */
44  public class VelocityDirectLayout
45      extends Layout
46  {
47      /*** Logging */
48      private static Log log = LogFactory.getLog(VelocityDirectLayout.class);
49  
50      /*** The prefix for lookup up layout pages */
51      private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
52  
53      /***
54       * Method called by LayoutLoader.
55       *
56       * @param data Turbine information.
57       * @exception Exception a generic exception.
58       */
59      public void doBuild(RunData data)
60          throws Exception
61      {
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().getLayoutTemplate();
77  
78          // Set the locale and content type
79          data.getResponse().setLocale(data.getLocale());
80          data.getResponse().setContentType(data.getContentType());
81  
82          log.debug("Now trying to render layout " + templateName);
83  
84          // Finally, generate the layout template and send it to the browser
85          TurbineVelocity.handleRequest(context,
86                  prefix + templateName, data.getOut());
87      }
88  }