View Javadoc

1   /*
2    * $Id: Text.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  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.apache.struts2.views.annotations.StrutsTag;
31  import org.apache.struts2.views.annotations.StrutsTagAttribute;
32  import org.apache.struts2.util.TextProviderHelper;
33  
34  import com.opensymphony.xwork2.util.TextUtils;
35  import com.opensymphony.xwork2.util.ValueStack;
36  import com.opensymphony.xwork2.util.logging.Logger;
37  import com.opensymphony.xwork2.util.logging.LoggerFactory;
38  
39  /***
40   * <!-- START SNIPPET: javadoc -->
41   * Render a I18n text message.
42   *
43   * <p/>
44   *
45   * The message must be in a resource bundle
46   * with the same name as the action that it is associated with. In practice
47   * this means that you should create a properties file in the same package
48   * as your Java class with the same name as your class, but with .properties
49   * extension.
50   *
51   * <p/>
52   *
53   * If the named message is not found, then the body of the tag will be used as default message.
54   * If no body is used, then the name of the message will be used.
55   *
56   * <!-- END SNIPPET: javadoc -->
57   *
58   *
59   *
60   * <!-- START SNIPPET: params -->
61   *
62   * <ul>
63   *      <li>name* (String) - the i18n message key</li>
64   * </ul>
65   *
66   * <!-- END SNIPPET: params -->
67   *
68   * <p/>
69   *
70   * Example:
71   * <pre>
72   * <!-- START SNIPPET: exdescription -->
73   *
74   * Accessing messages from a given bundle (the i18n Shop example bundle in the first example) and using bundle defined through the framework in the second example.</p>
75   *
76   * <!-- END SNIPPET: exdescription -->
77   * </pre>
78   *
79   * <pre>
80   * <!-- START SNIPPET: example -->
81   *
82   * &lt;!-- First Example --&gt;
83   * &lt;s:i18n name="struts.action.test.i18n.Shop"&gt;
84   *     &lt;s:text name="main.title"/&gt;
85   * &lt;/s:i18n&gt;
86   *
87   * &lt;!-- Second Example --&gt;
88   * &lt;s:text name="main.title" /&gt;
89   *
90   * &lt;!-- Third Examlpe --&gt;
91   * &lt;s:text name="i18n.label.greetings"&gt;
92   *    &lt;s:param &gt;Mr Smith&lt;/s:param&gt;
93   * &lt;/s:text&gt;
94   *
95   * <!-- END SNIPPET: example -->
96   * </pre>
97   *
98   *
99   * <pre>
100  * <!-- START SNIPPET: i18nExample -->
101  *
102  * &lt;-- Fourth Example --&gt;
103  * &lt;s:text name="some.key" /&gt;
104  *
105  * &lt;-- Fifth Example --&gt;
106  * &lt;s:text name="some.invalid.key" &gt;
107  *    The Default Message That Will Be Displayed
108  * &lt;/s:text&gt;
109  *
110  * <!-- END SNIPPET: i18nExample -->
111  * </pre>
112  *
113  * @see Param
114  *
115  */
116 @StrutsTag(
117     name="text",
118     tldTagClass="org.apache.struts2.views.jsp.TextTag",
119     description="Render a I18n text message")
120 public class Text extends ContextBean implements Param.UnnamedParametric {
121     private static final Logger LOG = LoggerFactory.getLogger(Text.class);
122 
123     protected List values = Collections.EMPTY_LIST;
124     protected String actualName;
125     protected String name;
126 
127     public Text(ValueStack stack) {
128         super(stack);
129     }
130 
131     @StrutsTagAttribute(description=" Name of resource property to fetch", required=true)
132     public void setName(String name) {
133         this.name = name;
134     }
135 
136 
137     public boolean usesBody() {
138         // overriding this to true such that EVAL_BODY_BUFFERED is return and
139         // bodyContent will be valid hence, text between start & end tag will
140         // be honoured as default message (WW-1268)
141         return true;
142     }
143 
144     public boolean end(Writer writer, String body) {
145         actualName = findString(name, "name", "You must specify the i18n key. Example: welcome.header");
146         String defaultMessage;
147         if (TextUtils.stringSet(body)) {
148             defaultMessage = body;
149         } else {
150             defaultMessage = actualName;
151         }
152 
153         String msg = TextProviderHelper.getText(actualName, defaultMessage, values, getStack());
154 
155         if (msg != null) {
156             try {
157                 if (getVar() == null) {
158                     writer.write(msg);
159                 } else {
160                     putInContext(msg);
161                 }
162             } catch (IOException e) {
163                 LOG.error("Could not write out Text tag", e);
164             }
165         }
166 
167         return super.end(writer, "");
168     }
169 
170     public void addParameter(String key, Object value) {
171         addParameter(value);
172     }
173 
174     public void addParameter(Object value) {
175         if (values.isEmpty()) {
176             values = new ArrayList(4);
177         }
178 
179         values.add(value);
180     }
181 }