View Javadoc

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