001: package org.apache.turbine.modules.layouts;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.StringReader;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.apache.ecs.ConcreteElement;
028:
029: import org.apache.turbine.TurbineConstants;
030: import org.apache.turbine.modules.Layout;
031: import org.apache.turbine.modules.ScreenLoader;
032: import org.apache.turbine.services.velocity.TurbineVelocity;
033: import org.apache.turbine.services.xslt.TurbineXSLT;
034: import org.apache.turbine.util.RunData;
035: import org.apache.turbine.util.template.TemplateNavigation;
036:
037: import org.apache.velocity.context.Context;
038:
039: /**
040: * This Layout module allows Velocity XML templates to be used as layouts.
041: * <br><br>
042: * Once the (XML) screen and navigation templates have been inserted into
043: * the layout template the result is transformed with a XSL stylesheet.
044: * The stylesheet (with the same name than the screen template) is loaded
045: * and executed by the XSLT service, so it is important that you correctly
046: * set up your XSLT service. If the named stylsheet does not exist the
047: * default.xsl stylesheet is executed. If default.xsl does not exist
048: * the XML is merely echoed.
049: * <br><br>
050: * Since dynamic content is supposed to be primarily located in
051: * screens and navigations there should be relatively few reasons to
052: * subclass this Layout.
053: *
054: * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
055: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
056: * @version $Id: VelocityXslLayout.java 534527 2007-05-02 16:10:59Z tv $
057: */
058: public class VelocityXslLayout extends Layout {
059: /** Logging */
060: private static Log log = LogFactory.getLog(VelocityXslLayout.class);
061:
062: /** The prefix for lookup up layout pages */
063: private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
064:
065: /**
066: * Build the layout. Also sets the ContentType and Locale headers
067: * of the HttpServletResponse object.
068: *
069: * @param data Turbine information.
070: * @exception Exception a generic exception.
071: */
072: public void doBuild(RunData data) throws Exception {
073: // Get the context needed by Velocity.
074: Context context = TurbineVelocity.getContext(data);
075:
076: data.getResponse().setContentType("text/html");
077:
078: String screenName = data.getScreen();
079:
080: log.debug("Loading Screen " + screenName);
081:
082: // First, generate the screen and put it in the context so
083: // we can grab it the layout template.
084: ConcreteElement results = ScreenLoader.getInstance().eval(data,
085: screenName);
086:
087: String returnValue = (results == null) ? "" : results
088: .toString();
089:
090: // variable for the screen in the layout template
091: context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
092:
093: // variable to reference the navigation screen in the layout template
094: context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
095: new TemplateNavigation(data));
096:
097: // Grab the layout template set in the VelocityPage.
098: // If null, then use the default layout template
099: // (done by the TemplateInfo object)
100: String templateName = data.getTemplateInfo()
101: .getLayoutTemplate();
102:
103: log.debug("Now trying to render layout " + templateName);
104:
105: // Now, generate the layout template.
106: String temp = TurbineVelocity.handleRequest(context, prefix
107: + templateName);
108:
109: // Finally we do a transformation and send the result
110: // back to the browser
111: TurbineXSLT.transform(data.getTemplateInfo()
112: .getScreenTemplate(), new StringReader(temp), data
113: .getOut());
114: }
115: }
|