View Javadoc

1   /*
2    * $Id: MessageTag.java 376840 2006-02-10 21:00:51Z husted $
3    *
4    * Copyright 1999-2005 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.struts.taglib.bean;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.taglib.TagUtils;
22  import org.apache.struts.util.MessageResources;
23  
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.TagSupport;
26  
27  import java.util.Locale;
28  
29  /***
30   * Custom tag that retrieves an internationalized messages string (with
31   * optional parametric replacement) from the <code>ActionResources</code>
32   * object stored as a context attribute by our associated
33   * <code>ActionServlet</code> implementation.
34   *
35   * @version $Rev: 376840 $ $Date: 2005-09-16 09:38:33 -0400 (Fri, 16 Sep 2005)
36   *          $
37   */
38  public class MessageTag extends TagSupport {
39      /***
40       * The message resources for this package.
41       */
42      protected static MessageResources messages =
43          MessageResources.getMessageResources(
44              "org.apache.struts.taglib.bean.LocalStrings");
45  
46      // ------------------------------------------------------------- Properties
47  
48      /***
49       * The first optional argument.
50       */
51      protected String arg0 = null;
52  
53      /***
54       * The second optional argument.
55       */
56      protected String arg1 = null;
57  
58      /***
59       * The third optional argument.
60       */
61      protected String arg2 = null;
62  
63      /***
64       * The fourth optional argument.
65       */
66      protected String arg3 = null;
67  
68      /***
69       * The fifth optional argument.
70       */
71      protected String arg4 = null;
72  
73      /***
74       * The servlet context attribute key for our resources.
75       */
76      protected String bundle = null;
77  
78      /***
79       * The message key of the message to be retrieved.
80       */
81      protected String key = null;
82  
83      /***
84       * Name of the bean that contains the message key.
85       */
86      protected String name = null;
87  
88      /***
89       * Name of the property to be accessed on the specified bean.
90       */
91      protected String property = null;
92  
93      /***
94       * The scope to be searched to retrieve the specified bean.
95       */
96      protected String scope = null;
97  
98      /***
99       * The session scope key under which our Locale is stored.
100      */
101     protected String localeKey = Globals.LOCALE_KEY;
102 
103     public String getArg0() {
104         return (this.arg0);
105     }
106 
107     public void setArg0(String arg0) {
108         this.arg0 = arg0;
109     }
110 
111     public String getArg1() {
112         return (this.arg1);
113     }
114 
115     public void setArg1(String arg1) {
116         this.arg1 = arg1;
117     }
118 
119     public String getArg2() {
120         return (this.arg2);
121     }
122 
123     public void setArg2(String arg2) {
124         this.arg2 = arg2;
125     }
126 
127     public String getArg3() {
128         return (this.arg3);
129     }
130 
131     public void setArg3(String arg3) {
132         this.arg3 = arg3;
133     }
134 
135     public String getArg4() {
136         return (this.arg4);
137     }
138 
139     public void setArg4(String arg4) {
140         this.arg4 = arg4;
141     }
142 
143     public String getBundle() {
144         return (this.bundle);
145     }
146 
147     public void setBundle(String bundle) {
148         this.bundle = bundle;
149     }
150 
151     public String getKey() {
152         return (this.key);
153     }
154 
155     public void setKey(String key) {
156         this.key = key;
157     }
158 
159     public String getName() {
160         return (this.name);
161     }
162 
163     public void setName(String name) {
164         this.name = name;
165     }
166 
167     public String getProperty() {
168         return (this.property);
169     }
170 
171     public void setProperty(String property) {
172         this.property = property;
173     }
174 
175     public String getScope() {
176         return (this.scope);
177     }
178 
179     public void setScope(String scope) {
180         this.scope = scope;
181     }
182 
183     public String getLocale() {
184         return (this.localeKey);
185     }
186 
187     public void setLocale(String localeKey) {
188         this.localeKey = localeKey;
189     }
190 
191     // --------------------------------------------------------- Public Methods
192 
193     /***
194      * Process the start tag.
195      *
196      * @throws JspException if a JSP exception has occurred
197      */
198     public int doStartTag() throws JspException {
199         String key = this.key;
200 
201         if (key == null) {
202             // Look up the requested property value
203             Object value =
204                 TagUtils.getInstance().lookup(pageContext, name, property, scope);
205 
206             if ((value != null) && !(value instanceof String)) {
207                 JspException e =
208                     new JspException(messages.getMessage("message.property", key));
209 
210                 TagUtils.getInstance().saveException(pageContext, e);
211                 throw e;
212             }
213 
214             key = (String) value;
215         }
216 
217         // Construct the optional arguments array we will be using
218         Object[] args = new Object[] { arg0, arg1, arg2, arg3, arg4 };
219 
220         // Retrieve the message string we are looking for
221         String message =
222             TagUtils.getInstance().message(pageContext, this.bundle,
223                 this.localeKey, key, args);
224 
225         if (message == null) {
226             Locale locale =
227                 TagUtils.getInstance().getUserLocale(pageContext, this.localeKey);
228             String localeVal =
229                 (locale == null) ? "default locale" : locale.toString();
230             JspException e =
231                 new JspException(messages.getMessage("message.message",
232                         "\"" + key + "\"",
233                         "\"" + ((bundle == null) ? "(default bundle)" : bundle)
234                         + "\"", localeVal));
235 
236             TagUtils.getInstance().saveException(pageContext, e);
237             throw e;
238         }
239 
240         TagUtils.getInstance().write(pageContext, message);
241 
242         return (SKIP_BODY);
243     }
244 
245     /***
246      * Release any acquired resources.
247      */
248     public void release() {
249         super.release();
250         arg0 = null;
251         arg1 = null;
252         arg2 = null;
253         arg3 = null;
254         arg4 = null;
255         bundle = Globals.MESSAGES_KEY;
256         key = null;
257         name = null;
258         property = null;
259         scope = null;
260         localeKey = Globals.LOCALE_KEY;
261     }
262 }