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.fulcrum.xslt.XSLTService;
27 import org.apache.turbine.TurbineConstants;
28 import org.apache.turbine.annotation.TurbineLoader;
29 import org.apache.turbine.annotation.TurbineService;
30 import org.apache.turbine.modules.Screen;
31 import org.apache.turbine.modules.ScreenLoader;
32 import org.apache.turbine.pipeline.PipelineData;
33 import org.apache.turbine.util.RunData;
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$
54 */
55 public class VelocityXslLayout extends VelocityOnlyLayout
56 {
57 /** Injected service instance */
58 @TurbineService
59 private XSLTService xsltService;
60
61 /** Injected loader instance */
62 @TurbineLoader( Screen.class )
63 private ScreenLoader screenLoader;
64
65 /**
66 * Build the layout. Also sets the ContentType and Locale headers
67 * of the HttpServletResponse object.
68 *
69 * @param pipelineData Turbine information.
70 * @throws Exception a generic exception.
71 */
72 @Override
73 public void doBuild(PipelineData pipelineData)
74 throws Exception
75 {
76 RunData data = pipelineData.getRunData();
77 // Get the context needed by Velocity.
78 Context context = velocityService.getContext(pipelineData);
79
80 data.getResponse().setContentType(TurbineConstants.DEFAULT_HTML_CONTENT_TYPE);
81
82 // Provide objects to Velocity context
83 populateContext(pipelineData, context);
84
85 // Grab the layout template set in the VelocityPage.
86 // If null, then use the default layout template
87 // (done by the TemplateInfo object)
88 String templateName = data.getTemplateInfo().getLayoutTemplate();
89
90 log.debug("Now trying to render layout {}", templateName);
91
92 // Now, generate the layout template.
93 String temp = velocityService.handleRequest(context,
94 prefix + templateName);
95
96 // Finally we do a transformation and send the result
97 // back to the browser
98 xsltService.transform(
99 data.getTemplateInfo().getScreenTemplate(),
100 new StringReader(temp), data.getResponse().getWriter());
101 }
102 }