View Javadoc
1 package org.apache.turbine.util.template; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import org.apache.ecs.HtmlColor; 58 import org.apache.ecs.html.Link; 59 import org.apache.ecs.html.Meta; 60 import org.apache.ecs.html.Title; 61 import org.apache.turbine.services.pull.ApplicationTool; 62 import org.apache.turbine.util.RunData; 63 64 /*** 65 * Template context tool that will set various attributes of the HTML 66 * page. It is automatically placed in the Template context as 67 * '$page'. Here's an example of some uses: 68 * 69 * <p> 70 * $page.setBgColor("#ffffff"); 71 * $page.setBgColor("white"); 72 * $page.setBackground("/images/standardbg.jpeg"); 73 * $page.setTitle("This is the title!"); 74 * $page.setKeywords("turbine, cool, servlet framework"); 75 * $page.setStyleSheet("/style.css"); 76 * 77 * @author <a href="mailto:sean@somacity.com">Sean Legassick</a> 78 * @version $Id: TemplatePageAttributes.java,v 1.3 2002/07/11 16:53:19 mpoeschl Exp $ 79 */ 80 public class TemplatePageAttributes 81 implements ApplicationTool 82 { 83 /*** The RunData object. */ 84 private RunData data = null; 85 86 /*** The title. */ 87 private String cachedTitle = null; 88 89 90 /*** 91 * Default constructor. The init method must be called before use 92 */ 93 public TemplatePageAttributes() 94 { 95 } 96 97 /*** 98 * Construct a new instance with the given RunData object. 99 * 100 * @param data a RunData instance 101 */ 102 public TemplatePageAttributes(RunData data) 103 { 104 this.data = data; 105 } 106 107 /*** 108 * Initialise this instance with the given RunData object. 109 * (ApplicationTool method) 110 * 111 * @param data Assumed to be a RunData instance 112 */ 113 public void init(Object data) 114 { 115 // we blithely cast to RunData as the runtime error thrown 116 // if data is null or not RunData is appropriate. 117 this.data = (RunData) data; 118 119 // clear cached title 120 this.cachedTitle = null; 121 } 122 123 /*** 124 * Refresh method - does nothing 125 */ 126 public void refresh() 127 { 128 // empty 129 } 130 131 /*** 132 * Set the title in the page. This returns an empty String so 133 * that the template doesn't complain about getting a null return 134 * value. 135 * 136 * @param intitle A String with the title. 137 */ 138 public TemplatePageAttributes setTitle(String intitle) 139 { 140 Title title = data.getPage().getTitle(); 141 if (cachedTitle != null) 142 { 143 cachedTitle += intitle; 144 } 145 else 146 { 147 cachedTitle = intitle; 148 } 149 title.addElement(intitle); 150 return this; 151 } 152 153 /*** 154 * Get the title in the page. This returns an empty String if 155 * empty so that the template doesn't complain about getting a null 156 * return value. 157 * 158 * @return A String with the title. 159 */ 160 public String getTitle() 161 { 162 if (cachedTitle == null) 163 { 164 return ""; 165 } 166 return cachedTitle; 167 } 168 169 /*** 170 * Adds a LINK to a CSS styleshet to the HEAD of the page. 171 * 172 * @param url A String. 173 * @return A TemplatePageAttributes (self). 174 */ 175 public TemplatePageAttributes setStyleSheet(String url) 176 { 177 data.getPage().getHead().addElement(new Link() 178 .setRel("stylesheet").setType("text/css").setHref(url)); 179 return this; 180 } 181 182 /*** 183 * Set a keywords META tag in the HEAD of the page. 184 * 185 * @param keywords A String. 186 * @return A TemplatePageAttributes (self). 187 */ 188 public TemplatePageAttributes setKeywords(String keywords) 189 { 190 data.getPage().getHead().addElement( 191 new Meta().setName("keywords").setContent(keywords)); 192 return this; 193 } 194 195 /*** 196 * Sets a HttpEquiv META tag in the HEAD of the page, usage: 197 * <br><code>setHttpEquiv("refresh", "5; URL=http://localhost/nextpage.html")</code> 198 * <br><code>setHttpEquiv("Expires", "Tue, 20 Aug 1996 14:25:27 GMT")</code> 199 * 200 * @param httpEquiv The value to use for the http-equiv attribute. 201 * @param content The text for the content attribute of the meta tag. 202 * @return A TemplatePageAttributes (self). 203 */ 204 public TemplatePageAttributes setHttpEquiv(String httpEquiv, String content) 205 { 206 data.getPage().getHead().addElement( 207 new Meta().setHttpEquiv(httpEquiv).setContent(content)); 208 return this; 209 } 210 211 /*** 212 * Add a description META tag to the HEAD of the page. 213 * 214 * @param description A String. 215 * @return A TemplatePageAttributes (self). 216 */ 217 public TemplatePageAttributes setDescription(String description) 218 { 219 data.getPage().getHead().addElement( 220 new Meta().setName("description").setContent(description)); 221 return this; 222 } 223 224 /*** 225 * Set the background image for the BODY tag. 226 * 227 * @param url A String. 228 * @return A TemplatePageAttributes (self). 229 */ 230 public TemplatePageAttributes setBackground(String url) 231 { 232 data.getPage().getBody().setBackground(url); 233 return this; 234 } 235 236 /*** 237 * Set the background color for the BODY tag. You can use either 238 * color names or color values (e.g. "white" or "#ffffff" or 239 * "ffffff"). 240 * 241 * @param color A String. 242 * @return A TemplatePageAttributes (self). 243 */ 244 public TemplatePageAttributes setBgColor(String color) 245 { 246 String hexColor = HtmlColor.getColor(color); 247 if (hexColor == null) 248 { 249 hexColor = color; 250 } 251 data.getPage().getBody().setBgColor(hexColor); 252 return this; 253 } 254 255 /*** 256 * Set the text color for the BODY tag. You can use either color 257 * names or color values (e.g. "white" or "#ffffff" or "ffffff"). 258 * 259 * @param color A String. 260 * @return A TemplatePageAttributes (self). 261 */ 262 public TemplatePageAttributes setTextColor(String color) 263 { 264 String hexColor = HtmlColor.getColor(color); 265 if (hexColor == null) 266 { 267 hexColor = color; 268 } 269 data.getPage().getBody().setText(hexColor); 270 return this; 271 } 272 273 /*** 274 * Set the link color for the BODY tag. You can use either color 275 * names or color values (e.g. "white" or "#ffffff" or "ffffff"). 276 * 277 * @param color A String. 278 * @return A TemplatePageAttributes (self). 279 */ 280 public TemplatePageAttributes setLinkColor(String color) 281 { 282 String hexColor = HtmlColor.getColor(color); 283 if (hexColor == null) 284 { 285 hexColor = color; 286 } 287 data.getPage().getBody().setLink(hexColor); 288 return this; 289 } 290 291 /*** 292 * Set the visited link color for the BODY tag. 293 * 294 * @param color A String. 295 * @return A TemplatePageAttributes (self). 296 */ 297 public TemplatePageAttributes setVlinkColor(String color) 298 { 299 String hexColor = HtmlColor.getColor(color); 300 if (hexColor == null) 301 { 302 hexColor = color; 303 } 304 data.getPage().getBody().setVlink(hexColor); 305 return this; 306 } 307 308 /*** 309 * Adds an attribute to the BODY tag. 310 * 311 * @param name A String. 312 * @param value A String. 313 * @return A TemplatePageAttributes (self). 314 */ 315 public TemplatePageAttributes addAttribute(String name, String value) 316 { 317 data.getPage().getBody().addAttribute(name, value); 318 return this; 319 } 320 321 /*** 322 * A dummy toString method that returns an empty string. 323 * 324 * @return An empty String (""). 325 */ 326 public String toString() 327 { 328 return ""; 329 } 330 }

This page was automatically generated by Maven