KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > MessageTag


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.tags;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24
25 import org.springframework.context.MessageSource;
26 import org.springframework.context.MessageSourceResolvable;
27 import org.springframework.context.NoSuchMessageException;
28 import org.springframework.util.ObjectUtils;
29 import org.springframework.util.StringUtils;
30 import org.springframework.web.util.ExpressionEvaluationUtils;
31 import org.springframework.web.util.HtmlUtils;
32 import org.springframework.web.util.JavaScriptUtils;
33 import org.springframework.web.util.TagUtils;
34
35 /**
36  * Custom JSP tag to look up a message in the scope of this page.
37  * Messages are looked up using the ApplicationContext, and thus should
38  * support internationalization.
39  *
40  * <p>Regards a HTML escaping setting, either on this tag instance,
41  * the page level, or the web.xml level. Can also apply JavaScript escaping.
42  *
43  * <p>If "code" isn't set or cannot be resolved, "text" will be used as default
44  * message. Thus, this tag can also be used for HTML escaping of any texts.
45  *
46  * @author Rod Johnson
47  * @author Juergen Hoeller
48  * @see #setCode
49  * @see #setText
50  * @see #setHtmlEscape
51  * @see #setJavaScriptEscape
52  * @see HtmlEscapeTag#setDefaultHtmlEscape
53  * @see org.springframework.web.util.WebUtils#HTML_ESCAPE_CONTEXT_PARAM
54  */

55 public class MessageTag extends HtmlEscapingAwareTag {
56
57     /**
58      * Default separator for splitting an arguments String: a comma (",")
59      */

60     public static final String JavaDoc DEFAULT_ARGUMENT_SEPARATOR = ",";
61
62
63     private Object JavaDoc message;
64
65     private String JavaDoc code;
66
67     private Object JavaDoc arguments;
68
69     private String JavaDoc argumentSeparator = DEFAULT_ARGUMENT_SEPARATOR;
70
71     private String JavaDoc text;
72     
73     private String JavaDoc var;
74     
75     private String JavaDoc scope = TagUtils.SCOPE_PAGE;
76
77     private boolean javaScriptEscape = false;
78
79
80     /**
81      * Set the MessageSourceResolvable for this tag.
82      * Accepts a direct MessageSourceResolvable instance as well as a JSP
83      * expression language String that points to a MessageSourceResolvable.
84      * <p>If a MessageSourceResolvable is specified, it effectively overrides
85      * any code, arguments or text specified on this tag.
86      */

87     public void setMessage(Object JavaDoc message) {
88         this.message = message;
89     }
90
91     /**
92      * Set the message code for this tag.
93      */

94     public void setCode(String JavaDoc code) {
95         this.code = code;
96     }
97
98     /**
99      * Set optional message arguments for this tag, as a comma-delimited
100      * String (each String argument can contain JSP EL), an Object array
101      * (used as argument array), or a single Object (used as single argument).
102      */

103     public void setArguments(Object JavaDoc arguments) {
104         this.arguments = arguments;
105     }
106
107     /**
108      * Set the separator to use for splitting an arguments String.
109      * Default is a comma (",");
110      * @see #setArguments
111      */

112     public void setArgumentSeparator(String JavaDoc argumentSeparator) {
113         this.argumentSeparator = argumentSeparator;
114     }
115
116     /**
117      * Set the message text for this tag.
118      */

119     public void setText(String JavaDoc text) {
120         this.text = text;
121     }
122     
123     /**
124      * Set PageContext attribute name under which to expose
125      * a variable that contains the resolved message.
126      * @see #setScope
127      * @see javax.servlet.jsp.PageContext#setAttribute
128      */

129     public void setVar(String JavaDoc var) {
130         this.var = var;
131     }
132     
133     /**
134      * Set the scope to export the variable to.
135      * Default is SCOPE_PAGE ("page").
136      * @see #setVar
137      * @see org.springframework.web.util.TagUtils#SCOPE_PAGE
138      * @see javax.servlet.jsp.PageContext#setAttribute
139      */

140     public void setScope(String JavaDoc scope) {
141         this.scope = scope;
142     }
143
144     /**
145      * Set JavaScript escaping for this tag, as boolean value.
146      * Default is "false".
147      */

148     public void setJavaScriptEscape(String JavaDoc javaScriptEscape) throws JspException JavaDoc {
149         this.javaScriptEscape =
150                 ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, pageContext);
151     }
152
153
154     /**
155      * Resolves the message, escapes it if demanded,
156      * and writes it to the page (or exposes it as variable).
157      * @see #resolveMessage()
158      * @see org.springframework.web.util.HtmlUtils#htmlEscape(String)
159      * @see org.springframework.web.util.JavaScriptUtils#javaScriptEscape(String)
160      * @see #writeMessage(String)
161      */

162     protected final int doStartTagInternal() throws JspException JavaDoc, IOException JavaDoc {
163         try {
164             // Resolve the unescaped message.
165
String JavaDoc msg = resolveMessage();
166
167             // HTML and/or JavaScript escape, if demanded.
168
msg = isHtmlEscape() ? HtmlUtils.htmlEscape(msg) : msg;
169             msg = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(msg) : msg;
170
171             // Expose as variable, if demanded, else write to the page.
172
String JavaDoc resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext);
173             if (resolvedVar != null) {
174                 String JavaDoc resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext);
175                 pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(resolvedScope));
176             }
177             else {
178                 writeMessage(msg);
179             }
180
181             return EVAL_BODY_INCLUDE;
182         }
183         catch (NoSuchMessageException ex) {
184             throw new JspTagException JavaDoc(getNoSuchMessageExceptionDescription(ex));
185         }
186     }
187
188     /**
189      * Resolve the specified message into a concrete message String.
190      * The returned message String should be unescaped.
191      */

192     protected String JavaDoc resolveMessage() throws JspException JavaDoc, NoSuchMessageException {
193         MessageSource messageSource = getMessageSource();
194         if (messageSource == null) {
195             throw new JspTagException JavaDoc("No corresponding MessageSource found");
196         }
197
198         // Evaluate the specified MessageSourceResolvable, if any.
199
MessageSourceResolvable resolvedMessage = null;
200         if (this.message instanceof MessageSourceResolvable) {
201             resolvedMessage = (MessageSourceResolvable) this.message;
202         }
203         else if (this.message != null) {
204             String JavaDoc expr = this.message.toString();
205             resolvedMessage = (MessageSourceResolvable)
206                     ExpressionEvaluationUtils.evaluate("message", expr, MessageSourceResolvable.class, pageContext);
207         }
208
209         if (resolvedMessage != null) {
210             // We have a given MessageSourceResolvable.
211
return messageSource.getMessage(resolvedMessage, getRequestContext().getLocale());
212         }
213
214         String JavaDoc resolvedCode = ExpressionEvaluationUtils.evaluateString("code", this.code, pageContext);
215         String JavaDoc resolvedText = ExpressionEvaluationUtils.evaluateString("text", this.text, pageContext);
216
217         if (resolvedCode != null) {
218             // We have a code that we need to resolve.
219
Object JavaDoc[] argumentsArray = resolveArguments(this.arguments);
220                 if (resolvedText != null) {
221                 // We have a fallback text to consider.
222
return messageSource.getMessage(
223                         resolvedCode, argumentsArray, resolvedText, getRequestContext().getLocale());
224             }
225             else {
226                 // We have no fallback text to consider.
227
return messageSource.getMessage(
228                         resolvedCode, argumentsArray, getRequestContext().getLocale());
229             }
230         }
231
232         // All we have is a specified literal text.
233
return resolvedText;
234     }
235
236     /**
237      * Resolve the given arguments Object into an arguments array.
238      * @param arguments the specified arguments Object
239      * @return the resolved arguments as array
240      * @throws JspException if argument conversion failed
241      * @see #setArguments
242      */

243     protected Object JavaDoc[] resolveArguments(Object JavaDoc arguments) throws JspException JavaDoc {
244         if (arguments instanceof String JavaDoc) {
245             String JavaDoc[] stringArray =
246                     StringUtils.delimitedListToStringArray((String JavaDoc) arguments, this.argumentSeparator);
247             if (stringArray.length == 1) {
248                 Object JavaDoc argument = ExpressionEvaluationUtils.evaluate("argument", stringArray[0], pageContext);
249                 if (argument != null && argument.getClass().isArray()) {
250                     return ObjectUtils.toObjectArray(argument);
251                 }
252                 else {
253                     return new Object JavaDoc[] {argument};
254                 }
255             }
256             else {
257                 Object JavaDoc[] argumentsArray = new Object JavaDoc[stringArray.length];
258                 for (int i = 0; i < stringArray.length; i++) {
259                     argumentsArray[i] =
260                             ExpressionEvaluationUtils.evaluate("argument[" + i + "]", stringArray[i], pageContext);
261                 }
262                 return argumentsArray;
263             }
264         }
265         else if (arguments instanceof Object JavaDoc[]) {
266             return (Object JavaDoc[]) arguments;
267         }
268         else if (arguments instanceof Collection JavaDoc) {
269             return ((Collection JavaDoc) arguments).toArray();
270         }
271         else if (arguments != null) {
272             // Assume a single argument object.
273
return new Object JavaDoc[] {arguments};
274         }
275         else {
276             return null;
277         }
278     }
279
280     /**
281      * Write the message to the page.
282      * <p>Can be overridden in subclasses, e.g. for testing purposes.
283      * @param msg the message to write
284      * @throws IOException if writing failed
285      */

286     protected void writeMessage(String JavaDoc msg) throws IOException JavaDoc {
287         pageContext.getOut().write(String.valueOf(msg));
288     }
289
290     /**
291      * Use the application context itself for default message resolution.
292      */

293     protected MessageSource getMessageSource() {
294         return getRequestContext().getWebApplicationContext();
295     }
296
297     /**
298      * Return default exception message.
299      */

300     protected String JavaDoc getNoSuchMessageExceptionDescription(NoSuchMessageException ex) {
301         return ex.getMessage();
302     }
303
304 }
305
Popular Tags