001: package org.apache.turbine.util.template;
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.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.ArrayList;
026:
027: import org.apache.commons.configuration.Configuration;
028:
029: import org.apache.commons.lang.StringUtils;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import org.apache.turbine.Turbine;
035: import org.apache.turbine.TurbineConstants;
036: import org.apache.turbine.services.pull.ApplicationTool;
037: import org.apache.turbine.util.RunData;
038:
039: /**
040: * Template context tool that can be used to set various attributes of a
041: * HTML page. This tool does not automatically make the changes in the HTML
042: * page for you. You must use this tool in your layout template to retrieve
043: * the attributes.
044: * <p>
045: * The set/add methods are can be used from a screen template, action, screen
046: * class, layour template, or anywhere else. The get methods should be used in
047: * your layout template(s) to construct the appropriate HTML tags.
048: *<p>
049: * Example usage of this tool to build the HEAD and BODY tags in your layout
050: * templates:
051: * <p>
052: * <code>
053: * ## Set defaults for all pages using this layout. Anything set here can<br>
054: * ## be overridden in the screen template.<br>
055: * $page.setTitle("My default page title");<br>
056: * $page.setHttpEquiv("Content-Style-Type","text/css")<br>
057: * $page.addStyleSheet($content.getURI("myStyleSheet.css"))<br>
058: * $page.addScript($content.getURI("globalJavascriptCode.js"))<br>
059: * <br>
060: * ## build the HTML, HEAD, and BODY tags dynamically<br>
061: * <html><br>
062: * <head><br>
063: * #if( $page.Title != "" )<br>
064: * <title>$page.Title</title><br>
065: * #end<br>
066: * #foreach($metaTag in $page.MetaTags.keySet())<br>
067: * <meta name="$metaTag" content="$page.MetaTags.get($metaTag)"><br>
068: * #end<br>
069: * #foreach($httpEquiv in $page.HttpEquivs.keySet())<br>
070: * <meta http-equiv="$httpEquiv" content="$page.HttpEquivs.get($httpEquiv)"><br>
071: * #end<br>
072: * #foreach( $linkTag in $page.Links )<br>
073: * <link rel="$linkTag.Relation" href="$linkTag.Url"<br>
074: * #if($linkTag.Type != "" ) type="$linkTag.Type" #end<br>
075: * #if($linkTag.Media != "") media="$linkTag.Media" #end<br>
076: * #if($linkTag.Title != "") title="$linkTag.Title" #end<br>
077: * /><br>
078: * #end<br>
079: * #foreach( $script in $page.Scripts )<br>
080: * <script type="text/javascript" src="$script" language="JavaScript"></script><br>
081: * #end<br>
082: * </head><br>
083: *<br>
084: * ## Construct the body tag. Iterate through the body attributes to build the opening tag<br>
085: * <body<br>
086: * #foreach( $attributeName in $page.BodyAttributes.keySet() )<br>
087: * $attributeName = "$page.BodyAttributes.get($attributeName)"<br>
088: * #end<br>
089: * >
090: * </code>
091: * <p>
092: * Example usages of this tool in your screen templates:<br>
093: * <code>$page.addScript($content.getURI("myJavascript.js")<br>
094: * $page.setTitle("My page title")<br>
095: * $page.setHttpEquiv("refresh","5; URL=http://localhost/nextpage.html")</code>
096: *
097: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
098: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
099: * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
100: * @version $Id: HtmlPageAttributes.java 534527 2007-05-02 16:10:59Z tv $
101: */
102: public class HtmlPageAttributes implements ApplicationTool {
103: /** Logging */
104: private static Log log = LogFactory
105: .getLog(HtmlPageAttributes.class);
106:
107: /** The title */
108: private String title;
109:
110: /** Body Attributes */
111: private Map bodyAttributes = new HashMap();
112:
113: /** Script references */
114: private List scripts = new ArrayList();
115:
116: /** External references */
117: private List linkTags = new ArrayList();
118:
119: /** Inline styles */
120: private List styles = new ArrayList();
121:
122: /** Meta tags for the HEAD */
123: private Map metaTags = new HashMap();
124:
125: /** http-equiv tags */
126: private Map httpEquivs = new HashMap();
127:
128: /** Doctype */
129: private String doctype = null;
130:
131: /**
132: * Default constructor. The init method must be called before use
133: */
134: public HtmlPageAttributes() {
135: }
136:
137: /**
138: * Construct a new instance with the given RunData object.
139: *
140: * @param data a RunData instance
141: */
142: public HtmlPageAttributes(RunData data) {
143: init(data);
144: }
145:
146: /**
147: * Initialise this instance with the given RunData object.
148: * (ApplicationTool method)
149: *
150: * @param data Assumed to be a RunData instance
151: */
152: public void init(Object data) {
153: this .title = null;
154: this .bodyAttributes.clear();
155: this .scripts.clear();
156: this .linkTags.clear();
157: this .styles.clear();
158: this .metaTags.clear();
159: this .httpEquivs.clear();
160: }
161:
162: /**
163: * Refresh method - does nothing
164: */
165: public void refresh() {
166: // empty
167: }
168:
169: /**
170: * Set the title in the page. This returns an empty String so
171: * that the template doesn't complain about getting a null return
172: * value. Subsequent calls to this method will replace the current
173: * title.
174: *
175: * @param title A String with the title.
176: * @return a <code>HtmlPageAttributes</code> (self).
177: */
178: public HtmlPageAttributes setTitle(String title) {
179: this .title = title;
180: return this ;
181: }
182:
183: /**
184: * Get the title in the page. This returns an empty String if
185: * empty so that the template doesn't complain about getting a null
186: * return value.
187: *
188: * @return A String with the title.
189: */
190: public String getTitle() {
191: if (StringUtils.isEmpty(this .title)) {
192: return "";
193: }
194: return title;
195: }
196:
197: /**
198: * Adds an attribute to the BODY tag.
199: *
200: * @param name A String.
201: * @param value A String.
202: * @return a <code>HtmlPageAttributes</code> (self).
203: * @deprecated Use addBodyAttribute instead.
204: */
205: public HtmlPageAttributes addAttribute(String name, String value) {
206: log
207: .info("Use of the addAttribute(name,value) method is deprecated. Please use "
208: + "addBodyAttribute(name,value) instead.");
209: return addBodyAttribute(name, value);
210: }
211:
212: /**
213: * Adds an attribute to the BODY tag.
214: *
215: * @param name A String.
216: * @param value A String.
217: * @return a <code>HtmlPageAttributes</code> (self).
218: */
219: public HtmlPageAttributes addBodyAttribute(String name, String value) {
220: this .bodyAttributes.put(name, value);
221: return this ;
222: }
223:
224: /**
225: * Returns the map of body attributes
226: *
227: * @return the map
228: */
229: public Map getBodyAttributes() {
230: return this .bodyAttributes;
231: }
232:
233: /**
234: * Adds a script reference
235: *
236: * @param scriptURL
237: * @return a <code>HtmlPageAttributes</code> (self).
238: */
239: public HtmlPageAttributes addScript(String scriptURL) {
240: this .scripts.add(scriptURL);
241: return this ;
242: }
243:
244: /**
245: * Adds a script reference
246: *
247: * @param scriptURL
248: * @return a <code>HtmlPageAttributes</code> (self).
249: * @deprecated Use addScript instead
250: */
251: public HtmlPageAttributes setScript(String scriptURL) {
252: log
253: .info("Use of the setScript(scriptURL) method is deprecated. Please use "
254: + "addScript(scriptURL) instead.");
255: return addScript(scriptURL);
256: }
257:
258: /**
259: * Returns a collection of script URLs
260: *
261: * @return list of String objects constainings URLs of javascript files
262: * to include
263: */
264: public List getScripts() {
265: return this .scripts;
266: }
267:
268: /**
269: * Adds a style sheet reference
270: *
271: * @param styleSheetURL URL of the style sheet
272: * @return a <code>HtmlPageAttributes</code> (self).
273: */
274: public HtmlPageAttributes addStyleSheet(String styleSheetURL) {
275: addStyleSheet(styleSheetURL, "screen", null, "text/css");
276: return this ;
277: }
278:
279: /**
280: * Adds a style sheet reference
281: *
282: * @param styleSheetURL URL of the style sheet
283: * @param media name of the media
284: * @param title title of the stylesheet
285: * @param type content type
286: * @return a <code>HtmlPageAttributes</code> (self).
287: */
288: public HtmlPageAttributes addStyleSheet(String styleSheetURL,
289: String media, String title, String type) {
290: LinkTag ss = new LinkTag("stylesheet", styleSheetURL);
291: ss.setMedia(media);
292: ss.setTitle(title);
293: ss.setType(type);
294: this .linkTags.add(ss);
295: return this ;
296: }
297:
298: /**
299: * Adds a style sheet reference
300: *
301: * @param styleSheetURL
302: * @return a <code>HtmlPageAttributes</code> (self).
303: * @deprecated use addStyleSheet instead
304: */
305: public HtmlPageAttributes setStyleSheet(String styleSheetURL) {
306: log
307: .info("Use of the setStyleSheet(styleSheetURL) method is deprecated. Please use "
308: + "addStyleSheet(styleSheetURL) instead.");
309: return addStyleSheet(styleSheetURL);
310: }
311:
312: /**
313: * Adds a style sheet reference
314: *
315: * @param styleSheetURL
316: * @param media name of the media
317: * @return a <code>HtmlPageAttributes</code> (self).
318: * @deprecated use addStyleSheet instead
319: */
320: public HtmlPageAttributes setStyleSheet(String styleSheetURL,
321: String media) {
322: log
323: .info("Use of the setStyleSheet(styleSheetURL,media) method is deprecated. "
324: + "Please use addStyleSheet(styleSheetURL,media) instead.");
325: return addStyleSheet(styleSheetURL, media, null, "text/css");
326: }
327:
328: /**
329: * Adds a generic external reference
330: *
331: * @param relation type of the reference (prev, next, first, last, top, etc.)
332: * @param linkURL URL of the reference
333: * @return a <code>HtmlPageAttributes</code> (self).
334: */
335: public HtmlPageAttributes addLink(String relation, String linkURL) {
336: return addLink(relation, linkURL, null, null);
337: }
338:
339: /**
340: * Adds a generic external reference
341: *
342: * @param relation type of the reference (prev, next, first, last, top, etc.)
343: * @param linkURL URL of the reference
344: * @param title title of the reference
345: * @return a <code>HtmlPageAttributes</code> (self).
346: */
347: public HtmlPageAttributes addLink(String relation, String linkURL,
348: String title) {
349: return addLink(relation, linkURL, title, null);
350: }
351:
352: /**
353: * Adds a generic external reference
354: *
355: * @param relation type of the reference (prev, next, first, last, top, etc.)
356: * @param linkURL URL of the reference
357: * @param title title of the reference
358: * @param type content type
359: * @return a <code>HtmlPageAttributes</code> (self).
360: */
361: public HtmlPageAttributes addLink(String relation, String linkURL,
362: String title, String type) {
363: LinkTag ss = new LinkTag(relation, linkURL);
364: ss.setTitle(title);
365: ss.setType(type);
366: this .linkTags.add(ss);
367: return this ;
368: }
369:
370: /**
371: * Returns a collection of script URLs
372: *
373: * @return list LinkTag objects (inner class)
374: * @deprecated use getLinks() instead
375: */
376: public List getStyleSheets() {
377: return this .linkTags;
378: }
379:
380: /**
381: * Returns a collection of link URLs
382: *
383: * @return list LinkTag objects (inner class)
384: */
385: public List getLinks() {
386: return this .linkTags;
387: }
388:
389: /**
390: * Adds a STYLE element to the HEAD of the page with the provided content.
391: *
392: * @param styleText The contents of the <code>style</code> tag.
393: * @return a <code>HtmlPageAttributes</code> (self).
394: * @deprecated use addStyle instead
395: */
396: public HtmlPageAttributes setStyle(String styleText) {
397: log
398: .info("Use of the setStyle(styleText) method is deprecated. Please use "
399: + "addStyle(styleText) instead.");
400: return addStyle(styleText);
401: }
402:
403: /**
404: * Adds a STYLE element to the HEAD of the page with the provided content.
405: *
406: * @param styleText The contents of the <code>style</code> tag.
407: * @return a <code>HtmlPageAttributes</code> (self).
408: */
409: public HtmlPageAttributes addStyle(String styleText) {
410: this .styles.add(styleText);
411: return this ;
412: }
413:
414: /**
415: * Returns a collection of styles
416: *
417: * @return list of String objects containing the contents of style tags
418: */
419: public List getStyles() {
420: return this .styles;
421: }
422:
423: /**
424: * Set a keywords META tag in the HEAD of the page.
425: *
426: * @param keywords A String.
427: * @return a <code>HtmlPageAttributes</code> (self).
428: */
429: public HtmlPageAttributes setKeywords(String keywords) {
430: this .metaTags.put("keywords", keywords);
431: return this ;
432: }
433:
434: /**
435: * Sets a HttpEquiv META tag in the HEAD of the page, usage:
436: * <br><code>setHttpEquiv("refresh", "5; URL=http://localhost/nextpage.html")</code>
437: * <br><code>setHttpEquiv("Expires", "Tue, 20 Aug 1996 14:25:27 GMT")</code>
438: *
439: * @param httpEquiv The value to use for the http-equiv attribute.
440: * @param content The text for the content attribute of the meta tag.
441: * @return a <code>HtmlPageAttributes</code> (self).
442: */
443: public HtmlPageAttributes setHttpEquiv(String httpEquiv,
444: String content) {
445: this .httpEquivs.put(httpEquiv, content);
446: return this ;
447: }
448:
449: /**
450: * Add a description META tag to the HEAD of the page.
451: *
452: * @param description A String.
453: * @return a <code>HtmlPageAttributes</code> (self).
454: */
455: public HtmlPageAttributes setDescription(String description) {
456: this .metaTags.put("description", description);
457: return this ;
458: }
459:
460: /**
461: * Set the background image for the BODY tag.
462: *
463: * @param url A String.
464: * @return a <code>HtmlPageAttributes</code> (self).
465: */
466: public HtmlPageAttributes setBackground(String url) {
467: this .bodyAttributes.put("background", url);
468: return this ;
469: }
470:
471: /**
472: * Set the background color for the BODY tag. You can use either
473: * color names or color values (e.g. "white" or "#ffffff" or
474: * "ffffff").
475: *
476: * @param color A String.
477: * @return a <code>HtmlPageAttributes</code> (self).
478: */
479: public HtmlPageAttributes setBgColor(String color) {
480: this .bodyAttributes.put("BGCOLOR", color);
481: return this ;
482: }
483:
484: /**
485: * Set the text color for the BODY tag. You can use either color
486: * names or color values (e.g. "white" or "#ffffff" or "ffffff").
487: *
488: * @param color A String.
489: * @return a <code>HtmlPageAttributes</code> (self).
490: */
491: public HtmlPageAttributes setTextColor(String color) {
492: this .bodyAttributes.put("TEXT", color);
493: return this ;
494: }
495:
496: /**
497: * Set the link color for the BODY tag. You can use either color
498: * names or color values (e.g. "white" or "#ffffff" or "ffffff").
499: *
500: * @param color A String.
501: * @return a <code>HtmlPageAttributes</code> (self).
502: */
503: public HtmlPageAttributes setLinkColor(String color) {
504: this .bodyAttributes.put("LINK", color);
505: return this ;
506: }
507:
508: /**
509: * Set the visited link color for the BODY tag.
510: *
511: * @param color A String.
512: * @return a <code>HtmlPageAttributes</code> (self).
513: */
514: public HtmlPageAttributes setVlinkColor(String color) {
515: this .bodyAttributes.put("VLINK", color);
516: return this ;
517: }
518:
519: /**
520: * Set the active link color for the BODY tag.
521: *
522: * @param color A String.
523: * @return a <code>HtmlPageAttributes</code> (self).
524: */
525: public HtmlPageAttributes setAlinkColor(String color) {
526: this .bodyAttributes.put("ALINK", color);
527: return this ;
528: }
529:
530: /**
531: * Gets the map of http equiv tags
532: *
533: * @return Map of http equiv names to the contents
534: */
535: public Map getHttpEquivs() {
536: return this .httpEquivs;
537: }
538:
539: /**
540: * Gets the map of meta tags
541: *
542: * @return Map of http equiv names to the contents
543: */
544: public Map getMetaTags() {
545: return this .metaTags;
546: }
547:
548: /**
549: * A dummy toString method that returns an empty string.
550: *
551: * @return An empty String ("").
552: */
553: public String toString() {
554: return "";
555: }
556:
557: /**
558: * Helper class to hold data about a <link ... /> html header tag
559: */
560: public class LinkTag {
561: private String relation;
562: private String url;
563: private String title;
564: private String media;
565: private String type;
566:
567: /**
568: * Constructor requiring the URL and relation to be set
569: *
570: * @param relation Relation type the external link such as prev, next,
571: * stylesheet, shortcut icon
572: * @param url URL of the external link
573: */
574: public LinkTag(String relation, String url) {
575: setRelation(relation);
576: setUrl(url);
577: }
578:
579: /**
580: * Gets the content type of the style sheet
581: *
582: * @return content type
583: */
584: public String getType() {
585: return (StringUtils.isEmpty(type) ? "" : type);
586: }
587:
588: /**
589: * Sets the content type of the style sheet
590: *
591: * @param type content type
592: */
593: public void setType(String type) {
594: this .type = type;
595: }
596:
597: /**
598: * @return String representation of the URL
599: */
600: public String getUrl() {
601: return url;
602: }
603:
604: /**
605: * Sets the URL of the external style sheet
606: *
607: * @param url The URL of the stylesheet
608: */
609: private void setUrl(String url) {
610: this .url = url;
611: }
612:
613: /**
614: * Gets the title of the style sheet
615: *
616: * @return title
617: */
618: public String getTitle() {
619: return (StringUtils.isEmpty(title) ? "" : title);
620: }
621:
622: /**
623: * Sets the title of the stylesheet
624: *
625: * @param title
626: */
627: public void setTitle(String title) {
628: this .title = title;
629: }
630:
631: /**
632: * Gets the media for which the stylesheet should be applied.
633: *
634: * @return name of the media
635: */
636: public String getMedia() {
637: return (StringUtils.isEmpty(media) ? "" : media);
638: }
639:
640: /**
641: * Sets the media for which the stylesheet should be applied.
642: *
643: * @param media name of the media
644: */
645: public void setMedia(String media) {
646: this .media = media;
647: }
648:
649: /**
650: * Gets the relation type of the tag.
651: *
652: * @return name of the relation
653: */
654: public String getRelation() {
655: return (StringUtils.isEmpty(relation) ? "" : relation);
656: }
657:
658: /**
659: * Sets the relation type of the tag.
660: *
661: * @param relation name of the relation
662: */
663: public void setRelation(String relation) {
664: this .relation = relation;
665: }
666: }
667:
668: /**
669: * Retrieve the default Doctype as configured by the
670: * TurbineResources.peoperties
671: * default.doctype.root.element, default.doctype.identifier and
672: * default.doctype.url properties (defaults are "HTML",
673: * "-//W3C//DTD HTML 4.01 Transitional//EN" and
674: * "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd" respectively).
675: *
676: * @return the DOCTYPE tag constructed from the properties in
677: * TurbineResources.properties.
678: */
679: public String getDefaultDoctype() {
680: Configuration conf = Turbine.getConfiguration();
681: if (doctype == null) {
682: String tag = conf
683: .getString(
684: TurbineConstants.DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_KEY,
685: TurbineConstants.DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_DEFAULT);
686:
687: if (StringUtils.isEmpty(tag)) {
688: doctype = "";
689: } else {
690: String identifier = conf
691: .getString(
692: TurbineConstants.DEFAULT_HTML_DOCTYPE_IDENTIFIER_KEY,
693: TurbineConstants.DEFAULT_HTML_DOCTYPE_IDENTIFIER_DEFAULT);
694:
695: String uri = conf
696: .getString(
697: TurbineConstants.DEFAULT_HTML_DOCTYPE_URI_KEY,
698: TurbineConstants.DEFAULT_HTML_DOCTYPE_URI_DEFAULT);
699:
700: doctype = buildDoctype(tag, identifier, uri);
701: }
702: }
703: return doctype;
704: }
705:
706: /**
707: * Build the doctype element.
708: *
709: * @param tag the tag whose DTD is being declared.
710: * @param identifier the identifier for the doctype declaration.
711: * @param uri the uri for the doctype declaration.
712: * @return the doctype.
713: */
714: private String buildDoctype(String tag, String identifier,
715: String uri) {
716: StringBuffer doctypeBuf = new StringBuffer("<!DOCTYPE ");
717: doctypeBuf.append(tag);
718:
719: if (StringUtils.isNotEmpty(identifier)) {
720: doctypeBuf.append(" PUBLIC \"");
721: doctypeBuf.append(identifier);
722: doctypeBuf.append("\" \"");
723: } else {
724: doctypeBuf.append(" SYSTEM \"");
725: }
726:
727: doctypeBuf.append(uri);
728: doctypeBuf.append("\">");
729:
730: return doctypeBuf.toString();
731: }
732:
733: }
|