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