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.ecs.ConcreteElement;
26  
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.modules.Layout;
29  import org.apache.turbine.modules.ScreenLoader;
30  import org.apache.turbine.services.velocity.TurbineVelocity;
31  import org.apache.turbine.util.RunData;
32  import org.apache.turbine.util.template.TemplateNavigation;
33  
34  import org.apache.velocity.context.Context;
35  
36  /***
37   * This Layout module allows Velocity templates to be used as layouts.
38   * Since dynamic content is supposed to be primarily located in
39   * screens and navigations there should be relatively few reasons to
40   * subclass this Layout.
41   *
42   * To get the same functionality as with VelocityECSLayout, you can use two
43   * supplied VelocityMacros, TurbineHtmlHead and TurbineHtmlBodyAttributes
44   * in your templates. These are used to put HtmlPageAttributes into a page
45   * before rendering.
46   *
47   * Use these macros should be used in the Layout template like this:
48   *
49   * ... set things like style sheets, scripts here.
50   * <html>
51   * #TurbineHtmlHead()
52   * <body #TurbineHtmlBodyAttributes() >
53   *  .... your body information
54   * </body>
55   * </html>
56   *
57   * As the layout template is rendered _after_ the screen template, you
58   * can of course, add information to the $page tool in your screen template.
59   * This will be added correctly to the <head>...</head> and
60   * <body> tags.
61   *
62   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
63   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
64   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
65   * @version $Id: VelocityOnlyLayout.java 534527 2007-05-02 16:10:59Z tv $
66   */
67  public class VelocityOnlyLayout
68      extends Layout
69  {
70      /*** Logging */
71      private static Log log = LogFactory.getLog(VelocityOnlyLayout.class);
72  
73      /*** The prefix for lookup up layout pages */
74      private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
75  
76      /***
77       * Build the layout.  Also sets the ContentType and Locale headers
78       * of the HttpServletResponse object.
79       *
80       * @param data Turbine information.
81       * @exception Exception a generic exception.
82       */
83      public void doBuild(RunData data)
84          throws Exception
85      {
86          // Get the context needed by Velocity.
87          Context context = TurbineVelocity.getContext(data);
88  
89          String screenName = data.getScreen();
90  
91          log.debug("Loading Screen " + screenName);
92  
93          // First, generate the screen and put it in the context so
94          // we can grab it the layout template.
95          ConcreteElement results =
96              ScreenLoader.getInstance().eval(data, screenName);
97  
98          String returnValue = (results == null) ? "" : results.toString();
99  
100         // variable for the screen in the layout template
101         context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
102 
103         // variable to reference the navigation screen in the layout template
104         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
105                     new TemplateNavigation(data));
106 
107         // Grab the layout template set in the VelocityPage.
108         // If null, then use the default layout template
109         // (done by the TemplateInfo object)
110         String templateName = data.getTemplateInfo().getLayoutTemplate();
111 
112         // Set the locale and content type
113         data.getResponse().setLocale(data.getLocale());
114         data.getResponse().setContentType(data.getContentType());
115 
116         log.debug("Now trying to render layout " + templateName);
117 
118         // Finally, generate the layout template and send it to the browser
119         data.getOut().print(TurbineVelocity
120                 .handleRequest(context, prefix + templateName));
121     }
122 }