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