01: /*
02: * Title: PropertyTag
03: * Description:
04: *
05: * This software is published under the terms of the OpenSymphony Software
06: * License version 1.1, of which a copy has been included with this
07: * distribution in the LICENSE.txt file.
08: */
09:
10: package com.opensymphony.module.sitemesh.taglib.decorator;
11:
12: import com.opensymphony.module.sitemesh.Page;
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.4 $
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: protected String getProperty() {
35: return propertyName;
36: }
37:
38: /** Value to write if no property matching key is found (optional). */
39: public void setDefault(String defaultValue) {
40: this .defaultValue = defaultValue;
41: }
42:
43: /** When begins with y, t or 1, the full attribute (name + value) is written. */
44: public final void setWriteEntireProperty(String writeEntireProperty) {
45: if (writeEntireProperty == null
46: || writeEntireProperty.trim().length() == 0)
47: return;
48:
49: switch (writeEntireProperty.charAt(0)) {
50: case '1':
51: case 't':
52: case 'T':
53: case 'y':
54: case 'Y':
55: this .writeEntireProperty = true;
56: break;
57: default:
58: this .writeEntireProperty = false;
59: }
60: }
61:
62: public int doEndTag() {
63: try {
64: Page page = getPage();
65: String propertyValue = page.getProperty(propertyName);
66:
67: if (propertyValue == null
68: || propertyValue.trim().length() == 0)
69: propertyValue = defaultValue;
70:
71: if (propertyValue != null) {
72: Writer out = getOut();
73: if (writeEntireProperty) {
74: out.write(" ");
75: out.write(propertyName.substring(propertyName
76: .lastIndexOf('.') + 1));
77: out.write("=\"");
78: out.write(propertyValue);
79: out.write("\"");
80: } else {
81: out.write(propertyValue);
82: }
83: }
84: } catch (Exception e) {
85: trace(e);
86: }
87: return EVAL_PAGE;
88: }
89:
90: }
|