View Javadoc

1   /*
2    * $Id: Property.java 651946 2008-04-27 13:41:38Z apetrelli $
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 
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="&lt;top of stack&gt;")
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             // the same logic as with findValue(String)
128             // if value start with %{ and end with }, just cut it off!
129             if (value.startsWith("%{") && value.endsWith("}")) {
130                 value = value.substring(2, value.length() - 1);
131             }
132         }
133 
134         // exception: don't call findString(), since we don't want the
135         //            expression parsed in this one case. it really
136         //            doesn't make sense, in fact.
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 }