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
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
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
86
87 ConcreteElement results =
88 ScreenLoader.getInstance().eval(data, screenName);
89
90 String returnValue = (results == null) ? "" : results.toString();
91
92
93 context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
94
95
96 context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
97 new TemplateNavigation(data));
98
99
100
101
102 String templateName = data.getTemplateInfo().getLayoutTemplate();
103
104 log.debug("Now trying to render layout " + templateName);
105
106
107 String temp = TurbineVelocity.handleRequest(context,
108 prefix + templateName);
109
110
111
112 TurbineXSLT.transform(
113 data.getTemplateInfo().getScreenTemplate(),
114 new StringReader(temp), data.getOut());
115 }
116 }