1 /*
2 * Title: PropertyTag
3 * Description:
4 *
5 * This software is published under the terms of the OpenSymphony Software
6 * License version 1.1, of which a copy has been included with this
7 * distribution in the LICENSE.txt file.
8 */
9
10 package com.opensymphony.module.sitemesh.taglib.decorator;
11
12 import com.opensymphony.module.sitemesh.HTMLPage;
13 import com.opensymphony.module.sitemesh.taglib.AbstractTag;
14
15 import java.io.Writer;
16
17 /**
18 * Write property of Page to out.
19 *
20 * @author <a href="joe@truemesh.com">Joe Walnes</a>
21 * @version $Revision: 1.3 $
22 *
23 * @see com.opensymphony.module.sitemesh.Page#getProperty(java.lang.String)
24 */
25 public class PropertyTag extends AbstractTag {
26 private String propertyName, defaultValue;
27 private boolean writeEntireProperty = false;
28
29 /** Key of property to write. */
30 public void setProperty(String propertyName) {
31 this.propertyName = propertyName;
32 }
33
34 /** Value to write if no property matching key is found (optional). */
35 public void setDefault(String defaultValue) {
36 this.defaultValue = defaultValue;
37 }
38
39 /** When begins with y, t or 1, the full attribute (name + value) is written. */
40 public final void setWriteEntireProperty(String writeEntireProperty) {
41 if (writeEntireProperty == null || writeEntireProperty.trim().length() == 0)
42 return;
43
44 switch (writeEntireProperty.charAt(0)) {
45 case '1':
46 case 't':
47 case 'T':
48 case 'y':
49 case 'Y':
50 this.writeEntireProperty = true;
51 break;
52 default:
53 this.writeEntireProperty = false;
54 }
55 }
56
57 public final int doEndTag() {
58 try {
59 HTMLPage htmlPage = (HTMLPage)getPage();
60 String propertyValue = htmlPage.getProperty(propertyName);
61
62 if (propertyValue == null || propertyValue.trim().length() == 0)
63 propertyValue = defaultValue;
64
65 if (propertyValue != null) {
66 Writer out = getOut();
67 if (writeEntireProperty) {
68 out.write(" ");
69 out.write(propertyName.substring(propertyName.lastIndexOf('.') + 1));
70 out.write("=\"");
71 out.write(propertyValue);
72 out.write("\"");
73 }
74 else {
75 out.write(propertyValue);
76 }
77 }
78 }
79 catch (Exception e) {
80 trace(e);
81 }
82 return EVAL_PAGE;
83 }
84
85 }