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.commons.lang.StringUtils; 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.apache.fulcrum.xslt.XSLTService; 030import org.apache.turbine.TurbineConstants; 031import org.apache.turbine.annotation.TurbineLoader; 032import org.apache.turbine.annotation.TurbineService; 033import org.apache.turbine.modules.Layout; 034import org.apache.turbine.modules.Screen; 035import org.apache.turbine.modules.ScreenLoader; 036import org.apache.turbine.pipeline.PipelineData; 037import org.apache.turbine.services.velocity.VelocityService; 038import org.apache.turbine.util.RunData; 039import org.apache.turbine.util.template.TemplateNavigation; 040import org.apache.velocity.context.Context; 041 042/** 043 * This Layout module allows Velocity XML templates to be used as layouts. 044 * <br><br> 045 * Once the (XML) screen and navigation templates have been inserted into 046 * the layout template the result is transformed with a XSL stylesheet. 047 * The stylesheet (with the same name than the screen template) is loaded 048 * and executed by the XSLT service, so it is important that you correctly 049 * set up your XSLT service. If the named stylsheet does not exist the 050 * default.xsl stylesheet is executed. If default.xsl does not exist 051 * the XML is merely echoed. 052 * <br><br> 053 * Since dynamic content is supposed to be primarily located in 054 * screens and navigations there should be relatively few reasons to 055 * subclass this Layout. 056 * 057 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a> 058 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 059 * @version $Id: VelocityXslLayout.java 1709648 2015-10-20 17:08:10Z tv $ 060 */ 061public class VelocityXslLayout extends Layout 062{ 063 /** Logging */ 064 private static Log log = LogFactory.getLog(VelocityXslLayout.class); 065 066 /** The prefix for lookup up layout pages */ 067 private final String prefix = Layout.PREFIX + "/"; 068 069 /** Injected service instance */ 070 @TurbineService 071 private VelocityService velocityService; 072 073 /** Injected service instance */ 074 @TurbineService 075 private XSLTService xsltService; 076 077 /** Injected loader instance */ 078 @TurbineLoader( Screen.class ) 079 private ScreenLoader screenLoader; 080 081 /** 082 * Build the layout. Also sets the ContentType and Locale headers 083 * of the HttpServletResponse object. 084 * 085 * @param pipelineData Turbine information. 086 * @exception Exception a generic exception. 087 */ 088 @Override 089 public void doBuild(PipelineData pipelineData) 090 throws Exception 091 { 092 RunData data = getRunData(pipelineData); 093 // Get the context needed by Velocity. 094 Context context = velocityService.getContext(pipelineData); 095 096 data.getResponse().setContentType("text/html"); 097 098 String screenName = data.getScreen(); 099 100 log.debug("Loading Screen " + screenName); 101 102 // First, generate the screen and put it in the context so 103 // we can grab it the layout template. 104 String results = screenLoader.eval(pipelineData, screenName); 105 String returnValue = StringUtils.defaultIfEmpty(results, StringUtils.EMPTY); 106 107 // variable for the screen in the layout template 108 context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue); 109 110 // variable to reference the navigation screen in the layout template 111 context.put(TurbineConstants.NAVIGATION_PLACEHOLDER, 112 new TemplateNavigation(data)); 113 114 // Grab the layout template set in the VelocityPage. 115 // If null, then use the default layout template 116 // (done by the TemplateInfo object) 117 String templateName = data.getTemplateInfo().getLayoutTemplate(); 118 119 log.debug("Now trying to render layout " + templateName); 120 121 // Now, generate the layout template. 122 String temp = velocityService.handleRequest(context, 123 prefix + templateName); 124 125 // Finally we do a transformation and send the result 126 // back to the browser 127 xsltService.transform( 128 data.getTemplateInfo().getScreenTemplate(), 129 new StringReader(temp), data.getResponse().getWriter()); 130 } 131}