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 java.io.StringReader;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.ecs.ConcreteElement;
25  
26  import org.apache.turbine.TurbineConstants;
27  import org.apache.turbine.modules.Layout;
28  import org.apache.turbine.modules.ScreenLoader;
29  import org.apache.turbine.services.velocity.TurbineVelocity;
30  import org.apache.turbine.services.xslt.TurbineXSLT;
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 XML templates to be used as layouts.
38   * <br><br>
39   * Once the (XML) screen and navigation templates have been inserted into
40   * the layout template the result is transformed with a XSL stylesheet.
41   * The stylesheet (with the same name than the screen template) is loaded
42   * and executed by the XSLT service, so it is important that you correctly
43   * set up your XSLT service.  If the named stylsheet does not exist the
44   * default.xsl stylesheet is executed.  If default.xsl does not exist
45   * the XML is merely echoed.
46   * <br><br>
47   * Since dynamic content is supposed to be primarily located in
48   * screens and navigations there should be relatively few reasons to
49   * subclass this Layout.
50   *
51   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
52   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
53   * @version $Id: VelocityXslLayout.java 264148 2005-08-29 14:21:04Z henning $
54   */
55  public class VelocityXslLayout extends Layout
56  {
57      /*** Logging */
58      private static Log log = LogFactory.getLog(VelocityXslLayout.class);
59  
60      /*** The prefix for lookup up layout pages */
61      private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
62  
63      /***
64       * Build the layout.  Also sets the ContentType and Locale headers
65       * of the HttpServletResponse object.
66       *
67       * @param data Turbine information.
68       * @exception Exception a generic exception.
69       */
70      public void doBuild(RunData data)
71          throws Exception
72      {
73          // Get the context needed by Velocity.
74          Context context = TurbineVelocity.getContext(data);
75  
76          data.getResponse().setContentType("text/html");
77  
78          String screenName = data.getScreen();
79  
80          log.debug("Loading Screen " + screenName);
81  
82          // First, generate the screen and put it in the context so
83          // we can grab it the layout template.
84          ConcreteElement results =
85              ScreenLoader.getInstance().eval(data, screenName);
86  
87          String returnValue = (results == null) ? "" : results.toString();
88  
89          // variable for the screen in the layout template
90          context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
91  
92          // variable to reference the navigation screen in the layout template
93          context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
94                      new TemplateNavigation(data));
95  
96          // Grab the layout template set in the VelocityPage.
97          // If null, then use the default layout template
98          // (done by the TemplateInfo object)
99          String templateName = data.getTemplateInfo().getLayoutTemplate();
100 
101         log.debug("Now trying to render layout " + templateName);
102 
103         // Now, generate the layout template.
104         String temp = TurbineVelocity.handleRequest(context,
105                 prefix + templateName);
106 
107         // Finally we do a transformation and send the result
108         // back to the browser
109         TurbineXSLT.transform(
110             data.getTemplateInfo().getScreenTemplate(),
111                 new StringReader(temp), data.getOut());
112     }
113 }