View Javadoc

1   /*
2    * $Id: Property.java 761694 2009-04-03 14:31:59Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  import org.apache.struts2.StrutsConstants;
30  import org.apache.commons.lang.xwork.StringEscapeUtils;
31  
32  import com.opensymphony.xwork2.util.ValueStack;
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  import com.opensymphony.xwork2.inject.Inject;
36  
37  /***
38   * <!-- START SNIPPET: javadoc -->
39   *
40   * Used to get the property of a <i>value</i>, which will default to the top of
41   * the stack if none is specified.
42   *
43   * <!-- END SNIPPET: javadoc -->
44   *
45   * <p/>
46   *
47   *
48   * <!-- START SNIPPET: params -->
49   *
50   * <ul>
51   *      <li>default (String) - The default value to be used if <u>value</u> attribute is null</li>
52   *      <li>escape (Boolean) - Escape HTML. Default to true</li>
53   *      <li>value (Object) - value to be displayed</li>
54   * </ul>
55   *
56   * <!-- END SNIPPET: params -->
57   *
58   *
59   * <pre>
60   * <!-- START SNIPPET: example -->
61   *
62   * <s:push value="myBean">
63   *     <!-- Example 1: -->
64   *     <s:property value="myBeanProperty" />
65   *
66   *     <!-- Example 2: -->TextUtils
67   *     <s:property value="myBeanProperty" default="a default value" />
68   * </s:push>
69   *
70   * <!-- END SNIPPET: example -->
71   * </pre>
72   *
73   * <pre>
74   * <!-- START SNIPPET: exampledescription -->
75   *
76   * Example 1 prints the result of myBean's getMyBeanProperty() method.
77   * Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
78   *
79   * <!-- END SNIPPET: exampledescription -->
80   * </pre>
81   *
82   *
83   * <pre>
84   * <!-- START SNIPPET: i18nExample -->
85   *
86   * &lt;s:property value="getText('some.key')" /&gt;
87   *
88   * <!-- END SNIPPET: i18nExample -->
89   * </pre>
90   *
91   */
92  @StrutsTag(name="property", tldBodyContent="empty", tldTagClass="org.apache.struts2.views.jsp.PropertyTag",
93      description="Print out expression which evaluates against the stack")
94  public class Property extends Component {
95      private static final Logger LOG = LoggerFactory.getLogger(Property.class);
96  
97      public Property(ValueStack stack) {
98          super(stack);
99      }
100 
101     private String defaultValue;
102     private String value;
103     private boolean escape = true;
104     private boolean escapeJavaScript = false;
105 
106     @StrutsTagAttribute(description="The default value to be used if <u>value</u> attribute is null")
107     public void setDefault(String defaultValue) {
108         this.defaultValue = defaultValue;
109     }
110 
111     @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
112     public void setEscape(boolean escape) {
113         this.escape = escape;
114     }
115 
116     @StrutsTagAttribute(description="Whether to escape Javascript", type="Boolean", defaultValue="false")
117     public void setEscapeJavaScript(boolean escapeJavaScript) {
118         this.escapeJavaScript = escapeJavaScript;
119     }
120 
121     @StrutsTagAttribute(description="Value to be displayed", type="Object", defaultValue="&lt;top of stack&gt;")
122     public void setValue(String value) {
123         this.value = value;
124     }
125 
126     public boolean start(Writer writer) {
127         boolean result = super.start(writer);
128 
129         String actualValue = null;
130 
131         if (value == null) {
132             value = "top";
133         }
134         else {
135         	value = stripExpressionIfAltSyntax(value);
136         }
137 
138         // exception: don't call findString(), since we don't want the
139         //            expression parsed in this one case. it really
140         //            doesn't make sense, in fact.
141         actualValue = (String) getStack().findValue(value, String.class, throwExceptionOnELFailure);
142 
143         try {
144             if (actualValue != null) {
145                 writer.write(prepare(actualValue));
146             } else if (defaultValue != null) {
147                 writer.write(prepare(defaultValue));
148             }
149         } catch (IOException e) {
150             LOG.info("Could not print out value '" + value + "'", e);
151         }
152 
153         return result;
154     }
155 
156     private String prepare(String value) {
157     	String result = value;
158         if (escape) {
159         	result = StringEscapeUtils.escapeHtml(result);
160         }
161         if (escapeJavaScript) {
162         	result = StringEscapeUtils.escapeJavaScript(result);
163         }
164         return result;
165     }
166 }