View Javadoc

1   package org.apache.turbine.modules.layouts;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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
48      extends Layout
49  {
50      /*** Logging */
51      private static Log log = LogFactory.getLog(VelocityDirectLayout.class);
52  
53      /*** The prefix for lookup up layout pages */
54      private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
55  
56      /***
57       * Method called by LayoutLoader.
58       *
59       * @param data Turbine information.
60       * @exception Exception a generic exception.
61       */
62      public void doBuild(RunData data)
63          throws Exception
64      {
65          // Get the context needed by Velocity.
66          Context context = TurbineVelocity.getContext(data);
67  
68          // variable for the screen in the layout template
69          context.put(TurbineConstants.SCREEN_PLACEHOLDER,
70                      new TemplateScreen(data));
71  
72          // variable to reference the navigation screen in the layout template
73          context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
74                      new TemplateNavigation(data));
75  
76          // Grab the layout template set in the VelocityPage.
77          // If null, then use the default layout template
78          // (done by the TemplateInfo object)
79          String templateName = data.getTemplateInfo().getLayoutTemplate();
80  
81          // Set the locale and content type
82          data.getResponse().setLocale(data.getLocale());
83          data.getResponse().setContentType(data.getContentType());
84  
85          log.debug("Now trying to render layout " + templateName);
86  
87          // Finally, generate the layout template and send it to the browser
88          TurbineVelocity.handleRequest(context,
89                  prefix + templateName, data.getOut());
90      }
91  }