View Javadoc

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