001 package 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 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 import org.apache.ecs.ConcreteElement; 027 import org.apache.turbine.TurbineConstants; 028 import org.apache.turbine.modules.Layout; 029 import org.apache.turbine.modules.Screen; 030 import org.apache.turbine.modules.ScreenLoader; 031 import org.apache.turbine.pipeline.PipelineData; 032 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker; 033 import org.apache.turbine.services.velocity.TurbineVelocity; 034 import org.apache.turbine.util.RunData; 035 import org.apache.turbine.util.template.TemplateNavigation; 036 import org.apache.velocity.context.Context; 037 038 /** 039 * This Layout module allows Velocity templates to be used as layouts. 040 * Since dynamic content is supposed to be primarily located in 041 * screens and navigations there should be relatively few reasons to 042 * subclass this Layout. 043 * 044 * To get the same functionality as with VelocityECSLayout, you can use two 045 * supplied VelocityMacros, TurbineHtmlHead and TurbineHtmlBodyAttributes 046 * in your templates. These are used to put HtmlPageAttributes into a page 047 * before rendering. 048 * 049 * Use these macros should be used in the Layout template like this: 050 * 051 * ... set things like style sheets, scripts here. 052 * <html> 053 * #TurbineHtmlHead() 054 * <body #TurbineHtmlBodyAttributes() > 055 * .... your body information 056 * </body> 057 * </html> 058 * 059 * As the layout template is rendered _after_ the screen template, you 060 * can of course, add information to the $page tool in your screen template. 061 * This will be added correctly to the <head>...</head> and 062 * <body> tags. 063 * 064 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 065 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> 066 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 067 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 068 * @version $Id: VelocityOnlyLayout.java 1066558 2011-02-02 18:12:40Z ludwig $ 069 */ 070 public class VelocityOnlyLayout 071 extends Layout 072 { 073 /** Logging */ 074 private static Log log = LogFactory.getLog(VelocityOnlyLayout.class); 075 076 /** The prefix for lookup up layout pages */ 077 private String prefix = Layout.PREFIX + "/"; 078 079 private ScreenLoader screenLoader; 080 081 /** 082 * Default constructor 083 */ 084 public VelocityOnlyLayout() 085 { 086 super(); 087 088 this.screenLoader = (ScreenLoader)TurbineAssemblerBroker.getLoader(Screen.NAME); 089 } 090 091 /** 092 * Build the layout. Also sets the ContentType and Locale headers 093 * of the HttpServletResponse object. 094 * @deprecated Use PipelineData version 095 * @param data Turbine information. 096 * @exception Exception a generic exception. 097 */ 098 @Deprecated 099 @Override 100 public void doBuild(RunData data) 101 throws Exception 102 { 103 // Get the context needed by Velocity. 104 Context context = TurbineVelocity.getContext(data); 105 106 String screenName = data.getScreen(); 107 108 log.debug("Loading Screen " + screenName); 109 110 // First, generate the screen and put it in the context so 111 // we can grab it the layout template. 112 ConcreteElement results = 113 screenLoader.eval(data, screenName); 114 115 String returnValue = (results == null) ? "" : results.toString(); 116 117 // variable for the screen in the layout template 118 context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue); 119 120 // variable to reference the navigation screen in the layout template 121 context.put(TurbineConstants.NAVIGATION_PLACEHOLDER, 122 new TemplateNavigation(data)); 123 124 // Grab the layout template set in the VelocityPage. 125 // If null, then use the default layout template 126 // (done by the TemplateInfo object) 127 String templateName = data.getTemplateInfo().getLayoutTemplate(); 128 129 // Set the locale and content type 130 data.getResponse().setLocale(data.getLocale()); 131 data.getResponse().setContentType(data.getContentType()); 132 133 log.debug("Now trying to render layout " + templateName); 134 135 // Finally, generate the layout template and send it to the browser 136 TurbineVelocity.handleRequest(context, 137 prefix + templateName, data.getResponse().getOutputStream()); 138 } 139 140 /** 141 * Build the layout. Also sets the ContentType and Locale headers 142 * of the HttpServletResponse object. 143 * 144 * 145 * @param data PipelineData 146 * @throws Exception generic exception 147 */ 148 @Override 149 public void doBuild(PipelineData pipelineData) 150 throws Exception 151 { 152 RunData data = getRunData(pipelineData); 153 // Get the context needed by Velocity. 154 Context context = TurbineVelocity.getContext(pipelineData); 155 156 String screenName = data.getScreen(); 157 158 log.debug("Loading Screen " + screenName); 159 160 // First, generate the screen and put it in the context so 161 // we can grab it the layout template. 162 ConcreteElement results = 163 screenLoader.eval(pipelineData, screenName); 164 165 String returnValue = (results == null) ? "" : results.toString(); 166 167 // variable for the screen in the layout template 168 context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue); 169 170 // variable to reference the navigation screen in the layout template 171 context.put(TurbineConstants.NAVIGATION_PLACEHOLDER, 172 new TemplateNavigation(data)); 173 174 // Grab the layout template set in the VelocityPage. 175 // If null, then use the default layout template 176 // (done by the TemplateInfo object) 177 String templateName = data.getTemplateInfo().getLayoutTemplate(); 178 179 // Set the locale and content type 180 data.getResponse().setLocale(data.getLocale()); 181 data.getResponse().setContentType(data.getContentType()); 182 183 log.debug("Now trying to render layout " + templateName); 184 185 // Finally, generate the layout template and send it to the browser 186 TurbineVelocity.handleRequest(context, 187 prefix + templateName, data.getResponse().getOutputStream()); 188 } 189 }