View Javadoc

1   /*
2    * $Id: Property.java 726715 2008-12-15 15:39:15Z 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  
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   * &lt;s:property value="getText('some.key')" /&gt;
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     private boolean escapeJavaScript = false;
103 
104     @StrutsTagAttribute(description="The default value to be used if <u>value</u> attribute is null")
105     public void setDefault(String defaultValue) {
106         this.defaultValue = defaultValue;
107     }
108 
109     @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
110     public void setEscape(boolean escape) {
111         this.escape = escape;
112     }
113 
114     @StrutsTagAttribute(description="Whether to escape Javascript", type="Boolean", defaultValue="false")
115     public void setEscapeJavaScript(boolean escapeJavaScript) {
116         this.escapeJavaScript = escapeJavaScript;
117     }
118 
119     @StrutsTagAttribute(description="Value to be displayed", type="Object", defaultValue="&lt;top of stack&gt;")
120     public void setValue(String value) {
121         this.value = value;
122     }
123 
124     public boolean start(Writer writer) {
125         boolean result = super.start(writer);
126 
127         String actualValue = null;
128 
129         if (value == null) {
130             value = "top";
131         }
132         else {
133         	value = stripExpressionIfAltSyntax(value);
134         }
135 
136         // exception: don't call findString(), since we don't want the
137         //            expression parsed in this one case. it really
138         //            doesn't make sense, in fact.
139         actualValue = (String) getStack().findValue(value, String.class);
140 
141         try {
142             if (actualValue != null) {
143                 writer.write(prepare(actualValue));
144             } else if (defaultValue != null) {
145                 writer.write(prepare(defaultValue));
146             }
147         } catch (IOException e) {
148             LOG.info("Could not print out value '" + value + "'", e);
149         }
150 
151         return result;
152     }
153 
154     private String prepare(String value) {
155     	String result = value;
156         if (escape) {
157         	result = TextUtils.htmlEncode(result);
158         }
159         if (escapeJavaScript) {
160         	result = TextUtils.escapeJavaScript(result);
161         }
162         return result;
163     }
164 }