1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components;
22
23 import java.io.IOException;
24 import java.io.Writer;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts2.views.annotations.StrutsTag;
29 import org.apache.struts2.views.annotations.StrutsTagAttribute;
30
31 import com.opensymphony.xwork2.util.ValueStack;
32 import com.opensymphony.xwork2.util.TextUtils;
33
34 /***
35 * <!-- START SNIPPET: javadoc -->
36 *
37 * Used to get the property of a <i>value</i>, which will default to the top of
38 * the stack if none is specified.
39 *
40 * <!-- END SNIPPET: javadoc -->
41 *
42 * <p/>
43 *
44 *
45 * <!-- START SNIPPET: params -->
46 *
47 * <ul>
48 * <li>default (String) - The default value to be used if <u>value</u> attribute is null</li>
49 * <li>escape (Boolean) - Escape HTML. Default to true</li>
50 * <li>value (Object) - value to be displayed</li>
51 * </ul>
52 *
53 * <!-- END SNIPPET: params -->
54 *
55 *
56 * <pre>
57 * <!-- START SNIPPET: example -->
58 *
59 * <s:push value="myBean">
60 * <!-- Example 1: -->
61 * <s:property value="myBeanProperty" />
62 *
63 * <!-- Example 2: -->TextUtils
64 * <s:property value="myBeanProperty" default="a default value" />
65 * </s:push>
66 *
67 * <!-- END SNIPPET: example -->
68 * </pre>
69 *
70 * <pre>
71 * <!-- START SNIPPET: exampledescription -->
72 *
73 * Example 1 prints the result of myBean's getMyBeanProperty() method.
74 * Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
75 *
76 * <!-- END SNIPPET: exampledescription -->
77 * </pre>
78 *
79 *
80 * <pre>
81 * <!-- START SNIPPET: i18nExample -->
82 *
83 * <s:property value="getText('some.key')" />
84 *
85 * <!-- END SNIPPET: i18nExample -->
86 * </pre>
87 *
88 */
89 @StrutsTag(name="property", tldBodyContent="empty", tldTagClass="org.apache.struts2.views.jsp.PropertyTag",
90 description="Print out expression which evaluates against the stack")
91 public class Property extends Component {
92 private static final Log LOG = LogFactory.getLog(Property.class);
93
94 public Property(ValueStack stack) {
95 super(stack);
96 }
97
98 private String defaultValue;
99 private String value;
100 private boolean escape = true;
101
102 @StrutsTagAttribute(description="The default value to be used if <u>value</u> attribute is null")
103 public void setDefault(String defaultValue) {
104 this.defaultValue = defaultValue;
105 }
106
107 @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
108 public void setEscape(boolean escape) {
109 this.escape = escape;
110 }
111
112 @StrutsTagAttribute(description="Value to be displayed", type="Object", defaultValue="<top of stack>")
113 public void setValue(String value) {
114 this.value = value;
115 }
116
117 public boolean start(Writer writer) {
118 boolean result = super.start(writer);
119
120 String actualValue = null;
121
122 if (value == null) {
123 value = "top";
124 }
125 else if (altSyntax()) {
126
127
128 if (value.startsWith("%{") && value.endsWith("}")) {
129 value = value.substring(2, value.length() - 1);
130 }
131 }
132
133
134
135
136 actualValue = (String) getStack().findValue(value, String.class);
137
138 try {
139 if (actualValue != null) {
140 writer.write(prepare(actualValue));
141 } else if (defaultValue != null) {
142 writer.write(prepare(defaultValue));
143 }
144 } catch (IOException e) {
145 LOG.info("Could not print out value '" + value + "'", e);
146 }
147
148 return result;
149 }
150
151 private String prepare(String value) {
152 if (escape) {
153 return TextUtils.htmlEncode(value);
154 } else {
155 return value;
156 }
157 }
158 }