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