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 java.io.StringReader;
    25
    26  import org.apache.commons.lang.StringUtils;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.fulcrum.xslt.XSLTService;
    30  import org.apache.turbine.TurbineConstants;
    31  import org.apache.turbine.annotation.TurbineLoader;
    32  import org.apache.turbine.annotation.TurbineService;
    33  import org.apache.turbine.modules.Layout;
    34  import org.apache.turbine.modules.Screen;
    35  import org.apache.turbine.modules.ScreenLoader;
    36  import org.apache.turbine.pipeline.PipelineData;
    37  import org.apache.turbine.services.velocity.VelocityService;
    38  import org.apache.turbine.util.RunData;
    39  import org.apache.turbine.util.template.TemplateNavigation;
    40  import org.apache.velocity.context.Context;
    41
    42  /**
    43   * This Layout module allows Velocity XML templates to be used as layouts.
    44   * <br><br>
    45   * Once the (XML) screen and navigation templates have been inserted into
    46   * the layout template the result is transformed with a XSL stylesheet.
    47   * The stylesheet (with the same name than the screen template) is loaded
    48   * and executed by the XSLT service, so it is important that you correctly
    49   * set up your XSLT service.  If the named stylsheet does not exist the
    50   * default.xsl stylesheet is executed.  If default.xsl does not exist
    51   * the XML is merely echoed.
    52   * <br><br>
    53   * Since dynamic content is supposed to be primarily located in
    54   * screens and navigations there should be relatively few reasons to
    55   * subclass this Layout.
    56   *
    57   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
    58   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    59   * @version $Id: VelocityXslLayout.java 1709648 2015-10-20 17:08:10Z tv $
    60   */
    61  public class VelocityXslLayout extends Layout
    62  {
    63      /** Logging */
    64      private static Log log = LogFactory.getLog(VelocityXslLayout.class);
    65
    66      /** The prefix for lookup up layout pages */
    67      private final String prefix = Layout.PREFIX + "/";
    68
    69      /** Injected service instance */
    70      @TurbineService
    71      private VelocityService velocityService;
    72
    73      /** Injected service instance */
    74      @TurbineService
    75      private XSLTService xsltService;
    76
    77      /** Injected loader instance */
    78      @TurbineLoader( Screen.class )
    79      private ScreenLoader screenLoader;
    80
    81      /**
    82       * Build the layout.  Also sets the ContentType and Locale headers
    83       * of the HttpServletResponse object.
    84       *
    85       * @param pipelineData Turbine information.
    86       * @exception Exception a generic exception.
    87       */
    88      @Override
    89      public void doBuild(PipelineData pipelineData)
    90          throws Exception
    91      {
    92          RunData data = getRunData(pipelineData);
    93          // Get the context needed by Velocity.
    94          Context context = velocityService.getContext(pipelineData);
    95
    96          data.getResponse().setContentType("text/html");
    97
    98          String screenName = data.getScreen();
    99
    100         log.debug("Loading Screen " + screenName);
    101
    102         // First, generate the screen and put it in the context so
    103         // we can grab it the layout template.
    104         String results = screenLoader.eval(pipelineData, screenName);
    105         String returnValue = StringUtils.defaultIfEmpty(results, StringUtils.EMPTY);
    106
    107         // variable for the screen in the layout template
    108         context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
    109
    110         // variable to reference the navigation screen in the layout template
    111         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
    112                     new TemplateNavigation(data));
    113
    114         // Grab the layout template set in the VelocityPage.
    115         // If null, then use the default layout template
    116         // (done by the TemplateInfo object)
    117         String templateName = data.getTemplateInfo().getLayoutTemplate();
    118
    119         log.debug("Now trying to render layout " + templateName);
    120
    121         // Now, generate the layout template.
    122         String temp = velocityService.handleRequest(context,
    123                 prefix + templateName);
    124
    125         // Finally we do a transformation and send the result
    126         // back to the browser
    127         xsltService.transform(
    128             data.getTemplateInfo().getScreenTemplate(),
    129                 new StringReader(temp), data.getResponse().getWriter());
    130     }
    131 }