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