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