1 package org.apache.turbine.modules.layouts;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
83
84 ConcreteElement results =
85 ScreenLoader.getInstance().eval(data, screenName);
86
87 String returnValue = (results == null) ? "" : results.toString();
88
89
90 context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
91
92
93 context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
94 new TemplateNavigation(data));
95
96
97
98
99 String templateName = data.getTemplateInfo().getLayoutTemplate();
100
101 log.debug("Now trying to render layout " + templateName);
102
103
104 String temp = TurbineVelocity.handleRequest(context,
105 prefix + templateName);
106
107
108
109 TurbineXSLT.transform(
110 data.getTemplateInfo().getScreenTemplate(),
111 new StringReader(temp), data.getOut());
112 }
113 }