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.ecs.ConcreteElement;
    27  import org.apache.turbine.TurbineConstants;
    28  import org.apache.turbine.modules.Layout;
    29  import org.apache.turbine.modules.Screen;
    30  import org.apache.turbine.modules.ScreenLoader;
    31  import org.apache.turbine.pipeline.PipelineData;
    32  import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
    33  import org.apache.turbine.services.velocity.TurbineVelocity;
    34  import org.apache.turbine.util.RunData;
    35  import org.apache.turbine.util.template.TemplateNavigation;
    36  import org.apache.velocity.context.Context;
    37
    38  /**
    39   * This Layout module allows Velocity templates to be used as layouts.
    40   * Since dynamic content is supposed to be primarily located in
    41   * screens and navigations there should be relatively few reasons to
    42   * subclass this Layout.
    43   *
    44   * To get the same functionality as with VelocityECSLayout, you can use two
    45   * supplied VelocityMacros, TurbineHtmlHead and TurbineHtmlBodyAttributes
    46   * in your templates. These are used to put HtmlPageAttributes into a page
    47   * before rendering.
    48   *
    49   * Use these macros should be used in the Layout template like this:
    50   *
    51   * ... set things like style sheets, scripts here.
    52   * &lt;html&gt;
    53   * #TurbineHtmlHead()
    54   * &lt;body #TurbineHtmlBodyAttributes() &gt;
    55   *  .... your body information
    56   * &lt;/body&gt;
    57   * &lt;/html&gt;
    58   *
    59   * As the layout template is rendered _after_ the screen template, you
    60   * can of course, add information to the $page tool in your screen template.
    61   * This will be added correctly to the &lt;head&gt;...&lt;/head&gt; and
    62   * &lt;body&gt; tags.
    63   *
    64   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
    65   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    66   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    67   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    68   * @version $Id: VelocityOnlyLayout.java 1066558 2011-02-02 18:12:40Z ludwig $
    69   */
    70  public class VelocityOnlyLayout
    71      extends Layout
    72  {
    73      /** Logging */
    74      private static Log log = LogFactory.getLog(VelocityOnlyLayout.class);
    75
    76      /** The prefix for lookup up layout pages */
    77      private String prefix = Layout.PREFIX + "/";
    78
    79      private ScreenLoader screenLoader;
    80
    81      /**
    82       * Default constructor
    83       */
    84      public VelocityOnlyLayout()
    85      {
    86          super();
    87
    88          this.screenLoader = (ScreenLoader)TurbineAssemblerBroker.getLoader(Screen.NAME);
    89      }
    90
    91      /**
    92       * Build the layout.  Also sets the ContentType and Locale headers
    93       * of the HttpServletResponse object.
    94       * @deprecated Use PipelineData version
    95       * @param data Turbine information.
    96       * @exception Exception a generic exception.
    97       */
    98      @Deprecated
    99      @Override
    100     public void doBuild(RunData data)
    101         throws Exception
    102     {
    103         // Get the context needed by Velocity.
    104         Context context = TurbineVelocity.getContext(data);
    105
    106         String screenName = data.getScreen();
    107
    108         log.debug("Loading Screen " + screenName);
    109
    110         // First, generate the screen and put it in the context so
    111         // we can grab it the layout template.
    112         ConcreteElement results =
    113             screenLoader.eval(data, screenName);
    114
    115         String returnValue = (results == null) ? "" : results.toString();
    116
    117         // variable for the screen in the layout template
    118         context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
    119
    120         // variable to reference the navigation screen in the layout template
    121         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
    122                     new TemplateNavigation(data));
    123
    124         // Grab the layout template set in the VelocityPage.
    125         // If null, then use the default layout template
    126         // (done by the TemplateInfo object)
    127         String templateName = data.getTemplateInfo().getLayoutTemplate();
    128
    129         // Set the locale and content type
    130         data.getResponse().setLocale(data.getLocale());
    131         data.getResponse().setContentType(data.getContentType());
    132
    133         log.debug("Now trying to render layout " + templateName);
    134
    135         // Finally, generate the layout template and send it to the browser
    136         TurbineVelocity.handleRequest(context,
    137                 prefix + templateName, data.getResponse().getOutputStream());
    138     }
    139
    140     /**
    141      * Build the layout.  Also sets the ContentType and Locale headers
    142      * of the HttpServletResponse object.
    143      *
    144      *
    145      * @param data PipelineData
    146      * @throws Exception generic exception
    147      */
    148     @Override
    149     public void doBuild(PipelineData pipelineData)
    150         throws Exception
    151     {
    152         RunData data = getRunData(pipelineData);
    153         // Get the context needed by Velocity.
    154         Context context = TurbineVelocity.getContext(pipelineData);
    155
    156         String screenName = data.getScreen();
    157
    158         log.debug("Loading Screen " + screenName);
    159
    160         // First, generate the screen and put it in the context so
    161         // we can grab it the layout template.
    162         ConcreteElement results =
    163             screenLoader.eval(pipelineData, screenName);
    164
    165         String returnValue = (results == null) ? "" : results.toString();
    166
    167         // variable for the screen in the layout template
    168         context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
    169
    170         // variable to reference the navigation screen in the layout template
    171         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
    172                     new TemplateNavigation(data));
    173
    174         // Grab the layout template set in the VelocityPage.
    175         // If null, then use the default layout template
    176         // (done by the TemplateInfo object)
    177         String templateName = data.getTemplateInfo().getLayoutTemplate();
    178
    179         // Set the locale and content type
    180         data.getResponse().setLocale(data.getLocale());
    181         data.getResponse().setContentType(data.getContentType());
    182
    183         log.debug("Now trying to render layout " + templateName);
    184
    185         // Finally, generate the layout template and send it to the browser
    186         TurbineVelocity.handleRequest(context,
    187                 prefix + templateName, data.getResponse().getOutputStream());
    188     }
    189 }