View Javadoc

1   package org.apache.turbine.modules.layouts;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.turbine.TurbineConstants;
27  import org.apache.turbine.modules.Layout;
28  import org.apache.turbine.pipeline.PipelineData;
29  import org.apache.turbine.services.velocity.TurbineVelocity;
30  import org.apache.turbine.util.RunData;
31  import org.apache.turbine.util.template.TemplateNavigation;
32  import org.apache.turbine.util.template.TemplateScreen;
33  import org.apache.velocity.context.Context;
34  
35  /**
36   * This Layout module allows Velocity templates
37   * to be used as layouts. It will stream directly the output of
38   * the layout and navigation templates to the output writer without
39   * using a screen. Use this if you have a large page to output
40   * and won't buffer it in the memory.
41   *
42   * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
43   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
44   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
45   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
47   * @version $Id: VelocityDirectLayout.java 1071044 2011-02-15 20:47:31Z tv $
48   */
49  public class VelocityDirectLayout
50      extends Layout
51  {
52      /** Logging */
53      private static Log log = LogFactory.getLog(VelocityDirectLayout.class);
54  
55      /** The prefix for lookup up layout pages */
56      private String prefix = Layout.PREFIX + "/";
57  
58      /**
59       * Method called by LayoutLoader.
60       *
61       * @deprecated Use the PipelineData version instead
62       * @param data Turbine information.
63       * @exception Exception a generic exception.
64       */
65      @Deprecated
66      @Override
67      public void doBuild(RunData data)
68          throws Exception
69      {
70          // Get the context needed by Velocity.
71          Context context = TurbineVelocity.getContext(data);
72  
73          // variable for the screen in the layout template
74          context.put(TurbineConstants.SCREEN_PLACEHOLDER,
75                      new TemplateScreen(data));
76  
77          // variable to reference the navigation screen in the layout template
78          context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
79                      new TemplateNavigation(data));
80  
81          // Grab the layout template set in the VelocityPage.
82          // If null, then use the default layout template
83          // (done by the TemplateInfo object)
84          String templateName = data.getTemplateInfo().getLayoutTemplate();
85  
86          // Set the locale and content type
87          data.getResponse().setLocale(data.getLocale());
88          data.getResponse().setContentType(data.getContentType());
89  
90          log.debug("Now trying to render layout " + templateName);
91  
92          // Finally, generate the layout template and send it to the browser
93          TurbineVelocity.handleRequest(context,
94                  prefix + templateName, data.getResponse().getOutputStream());
95      }
96  
97      /**
98       * Method called by LayoutLoader.
99       *
100      *
101      * @param data PipelineData
102      * @throws Exception generic exception
103      */
104     @Override
105     public void doBuild(PipelineData pipelineData)
106         throws Exception
107     {
108         RunData data = getRunData(pipelineData);
109         // Get the context needed by Velocity.
110         Context context = TurbineVelocity.getContext(pipelineData);
111 
112         // variable for the screen in the layout template
113         context.put(TurbineConstants.SCREEN_PLACEHOLDER,
114                     new TemplateScreen(data));
115 
116         // variable to reference the navigation screen in the layout template
117         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
118                     new TemplateNavigation(data));
119 
120         // Grab the layout template set in the VelocityPage.
121         // If null, then use the default layout template
122         // (done by the TemplateInfo object)
123         String templateName = data.getTemplateInfo().getLayoutTemplate();
124 
125         // Set the locale and content type
126         data.getResponse().setLocale(data.getLocale());
127         data.getResponse().setContentType(data.getContentType());
128 
129         log.debug("Now trying to render layout " + templateName);
130 
131         // Finally, generate the layout template and send it to the browser
132         TurbineVelocity.handleRequest(context,
133                 prefix + templateName, data.getResponse().getOutputStream());
134     }
135 
136 
137 }