001: /*
002: * $Id: MessageTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.bean;
022:
023: import org.apache.struts.Globals;
024: import org.apache.struts.taglib.TagUtils;
025: import org.apache.struts.util.MessageResources;
026:
027: import javax.servlet.jsp.JspException;
028: import javax.servlet.jsp.tagext.TagSupport;
029:
030: import java.util.Locale;
031:
032: /**
033: * Custom tag that retrieves an internationalized messages string (with
034: * optional parametric replacement) from the <code>ActionResources</code>
035: * object stored as a context attribute by our associated
036: * <code>ActionServlet</code> implementation.
037: *
038: * @version $Rev: 471754 $ $Date: 2005-09-16 09:38:33 -0400 (Fri, 16 Sep 2005)
039: * $
040: */
041: public class MessageTag extends TagSupport {
042: /**
043: * The message resources for this package.
044: */
045: protected static MessageResources messages = MessageResources
046: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
047:
048: // ------------------------------------------------------------- Properties
049:
050: /**
051: * The first optional argument.
052: */
053: protected String arg0 = null;
054:
055: /**
056: * The second optional argument.
057: */
058: protected String arg1 = null;
059:
060: /**
061: * The third optional argument.
062: */
063: protected String arg2 = null;
064:
065: /**
066: * The fourth optional argument.
067: */
068: protected String arg3 = null;
069:
070: /**
071: * The fifth optional argument.
072: */
073: protected String arg4 = null;
074:
075: /**
076: * The servlet context attribute key for our resources.
077: */
078: protected String bundle = null;
079:
080: /**
081: * The message key of the message to be retrieved.
082: */
083: protected String key = null;
084:
085: /**
086: * Name of the bean that contains the message key.
087: */
088: protected String name = null;
089:
090: /**
091: * Name of the property to be accessed on the specified bean.
092: */
093: protected String property = null;
094:
095: /**
096: * The scope to be searched to retrieve the specified bean.
097: */
098: protected String scope = null;
099:
100: /**
101: * The session scope key under which our Locale is stored.
102: */
103: protected String localeKey = Globals.LOCALE_KEY;
104:
105: public String getArg0() {
106: return (this .arg0);
107: }
108:
109: public void setArg0(String arg0) {
110: this .arg0 = arg0;
111: }
112:
113: public String getArg1() {
114: return (this .arg1);
115: }
116:
117: public void setArg1(String arg1) {
118: this .arg1 = arg1;
119: }
120:
121: public String getArg2() {
122: return (this .arg2);
123: }
124:
125: public void setArg2(String arg2) {
126: this .arg2 = arg2;
127: }
128:
129: public String getArg3() {
130: return (this .arg3);
131: }
132:
133: public void setArg3(String arg3) {
134: this .arg3 = arg3;
135: }
136:
137: public String getArg4() {
138: return (this .arg4);
139: }
140:
141: public void setArg4(String arg4) {
142: this .arg4 = arg4;
143: }
144:
145: public String getBundle() {
146: return (this .bundle);
147: }
148:
149: public void setBundle(String bundle) {
150: this .bundle = bundle;
151: }
152:
153: public String getKey() {
154: return (this .key);
155: }
156:
157: public void setKey(String key) {
158: this .key = key;
159: }
160:
161: public String getName() {
162: return (this .name);
163: }
164:
165: public void setName(String name) {
166: this .name = name;
167: }
168:
169: public String getProperty() {
170: return (this .property);
171: }
172:
173: public void setProperty(String property) {
174: this .property = property;
175: }
176:
177: public String getScope() {
178: return (this .scope);
179: }
180:
181: public void setScope(String scope) {
182: this .scope = scope;
183: }
184:
185: public String getLocale() {
186: return (this .localeKey);
187: }
188:
189: public void setLocale(String localeKey) {
190: this .localeKey = localeKey;
191: }
192:
193: // --------------------------------------------------------- Public Methods
194:
195: /**
196: * Process the start tag.
197: *
198: * @throws JspException if a JSP exception has occurred
199: */
200: public int doStartTag() throws JspException {
201: String key = this .key;
202:
203: if (key == null) {
204: // Look up the requested property value
205: Object value = TagUtils.getInstance().lookup(pageContext,
206: name, property, scope);
207:
208: if ((value != null) && !(value instanceof String)) {
209: JspException e = new JspException(messages.getMessage(
210: "message.property", key));
211:
212: TagUtils.getInstance().saveException(pageContext, e);
213: throw e;
214: }
215:
216: key = (String) value;
217: }
218:
219: // Construct the optional arguments array we will be using
220: Object[] args = new Object[] { arg0, arg1, arg2, arg3, arg4 };
221:
222: // Retrieve the message string we are looking for
223: String message = TagUtils.getInstance().message(pageContext,
224: this .bundle, this .localeKey, key, args);
225:
226: if (message == null) {
227: Locale locale = TagUtils.getInstance().getUserLocale(
228: pageContext, this .localeKey);
229: String localeVal = (locale == null) ? "default locale"
230: : locale.toString();
231: JspException e = new JspException(messages.getMessage(
232: "message.message", "\"" + key + "\"", "\""
233: + ((bundle == null) ? "(default bundle)"
234: : bundle) + "\"", 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: }
|