KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > MessageTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.taglib.core.html;
17
18 import com.blandware.atleap.common.util.StringUtil;
19 import com.blandware.atleap.webapp.util.core.ApplicationResources;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.taglib.TagUtils;
23 import org.apache.struts.util.RequestUtils;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 /**
32  * <p>Provides ability to get message from bundle and show it on page or save it in some scope.
33  * If <b>language</b> is not specified, user language is obtained. If no message
34  * for this language was found, then default language is tried. If still not
35  * found, any language that contains a message for given key will be used.
36  * </p>
37  * <p>
38  * Allowed attributes are:
39  * <ul>
40  * <li>
41  * <b>key</b> - bundle key for which to obtain message
42  * </li>
43  * <li>
44  * <b>language</b> - language for which to obtain message. This must be a
45  * lowercase two-letter ISO-639 code (for example, "en").
46  * If not given, user language is obtained. If no message was found for this
47  * language, then first default
48  * language is tried and, if message was not still found, any language may supply
49  * a message.
50  * </li>
51  * <li>
52  * <b>ignore</b> - whether to ignore a situation when no message was found for
53  * any language. If "true" and no message was found, no exception will be thrown,
54  * message will just be treated as empty one. Default is "false".
55  * </li>
56  * <li>
57  * <b>filter</b> - whether to encode HTML-sensitive characters (like ampersand)
58  * when writing result to page. Default is "false".
59  * </li>
60  * <li>
61  * <b>var</b> - name of scope variable that will accept obtained message. If
62  * not specified, message will be written to page.
63  * </li>
64  * <li>
65  * <b>scope</b> - scope of variable to export message to
66  * </li>
67  * <li>
68  * <b>arg<em>N</em></b> - value of argument number <em>N</em> to put to message
69  * instead of placeholder (<em>N</em> is an integer between 0 and 4).
70  * </li>
71  * </ul>
72  * </p>
73  * <p><a HREF="MessageTag.java.htm"><i>View Source</i></a></p>
74  *
75  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
76  * @version $Revision: 1.3 $ $Date: 2005/10/31 07:19:34 $
77  * @jsp.tag name="message"
78  * body-content="empty"
79  */

80 public class MessageTag extends SimpleTagSupport JavaDoc {
81
82     protected transient final Log log = LogFactory.getLog(MessageTag.class);
83
84     /**
85      * Key in bundle to get message for
86      */

87     protected String JavaDoc key;
88
89     /**
90      * Whether or not to throw an exception if message for specified key could not be found
91      */

92     protected Boolean JavaDoc ignore = Boolean.FALSE;
93
94     /**
95      * Whether or not to replace all HTML-sensitive characters with their entity equivalents
96      */

97     protected Boolean JavaDoc filter = Boolean.FALSE;
98
99     /**
100      * Name of variable to export message to
101      */

102     protected String JavaDoc var;
103
104     /**
105      * Scope to export message to
106      */

107     protected String JavaDoc scope;
108
109     /**
110      * Language to get value for. By default Struts locale is used
111      */

112     protected String JavaDoc language;
113
114     /**
115      * The first optional argument.
116      */

117     protected String JavaDoc arg0 = null;
118
119     /**
120      * The second optional argument.
121      */

122     protected String JavaDoc arg1 = null;
123
124     /**
125      * The third optional argument.
126      */

127     protected String JavaDoc arg2 = null;
128
129     /**
130      * The fourth optional argument.
131      */

132     protected String JavaDoc arg3 = null;
133
134     /**
135      * The fifth optional argument.
136      */

137     protected String JavaDoc arg4 = null;
138
139
140     /**
141      * Returns bundle key for this message
142      *
143      * @return bundle key
144      * @jsp.attribute required="true"
145      * rtexprvalue="true"
146      * type="java.lang.String"
147      * description="Key in bundle to get message for"
148      */

149     public String JavaDoc getKey() {
150         return key;
151     }
152
153     /**
154      * Sets bundle key for this message
155      *
156      * @param key bundle key to set
157      */

158     public void setKey(String JavaDoc key) {
159         this.key = key;
160     }
161
162     /**
163      * Return whether to ignore errors if they occure while getting message
164      *
165      * @return whether to ignore exception if it occure while getting message
166      * @jsp.attribute required="false"
167      * rtexprvalue="true"
168      * type="java.lang.Boolean"
169      * description="Whether or not to throw an exception if message for specified key could not be found"
170      */

171     public Boolean JavaDoc getIgnore() {
172         return ignore;
173     }
174
175     /**
176      * Sets whether to ignore errors if they occure while getting message
177      *
178      * @param ignore whether to ignore exception if it occure while getting message
179      */

180     public void setIgnore(Boolean JavaDoc ignore) {
181         this.ignore = ignore;
182     }
183
184     /**
185      * Returns whether or not to replace all HTML-sensitive characters with their entity equivalents
186      *
187      * @return whether or not to replace all HTML-sensitive characters
188      * @jsp.attribute required="false"
189      * rtexprvalue="true"
190      * type="java.lang.Boolean"
191      * description="Whether or not to replace all HTML-sensitive characters
192      */

193     public Boolean JavaDoc getFilter() {
194         return filter;
195     }
196
197     /**
198      * Sets whether or not to replace all HTML-sensitive characters with their entity equivalents
199      *
200      * @param filter whether or not to replace all HTML-sensitive characters
201      */

202     public void setFilter(Boolean JavaDoc filter) {
203         this.filter = filter;
204     }
205
206     /**
207      * Returns language code for which the value will be obtained
208      *
209      * @return language code
210      * @jsp.attribute required="false"
211      * rtexprvalue="true"
212      * type="java.lang.String"
213      * description="Locale to get value for"
214      */

215     public String JavaDoc getLanguage() {
216         return language;
217     }
218
219     /**
220      * Sets language code for which the value will be obtained
221      *
222      * @param language language code to set
223      */

224     public void setLanguage(String JavaDoc language) {
225         this.language = language;
226     }
227
228     /**
229      * Returns name of variable that will accept the obtained value
230      *
231      * @return name of variable
232      * @jsp.attribute required="false"
233      * rtexprvalue="true"
234      * type="java.lang.String"
235      * description="Name of variable to export message"
236      */

237     public String JavaDoc getVar() {
238         return var;
239     }
240
241     /**
242      * Sets name of variable that will accept the obtained value
243      *
244      * @param var name of variable to set
245      */

246     public void setVar(String JavaDoc var) {
247         this.var = var;
248     }
249
250     /**
251      * Returns variable scope
252      *
253      * @return variable scope
254      * @jsp.attribute required="false"
255      * rtexprvalue="true"
256      * type="java.lang.String"
257      * description="Scope to export variable to"
258      */

259     public String JavaDoc getScope() {
260         return scope;
261     }
262
263     /**
264      * Sets variable scope
265      *
266      * @param scope variable scope to set
267      */

268     public void setScope(String JavaDoc scope) {
269         this.scope = scope;
270     }
271
272     /**
273      * Returns first argument
274      *
275      * @return first argument
276      * @jsp.attribute required="false"
277      * rtexprvalue="true"
278      * type="java.lang.String"
279      * description="The first optional argument."
280      */

281     public String JavaDoc getArg0() {
282         return arg0;
283     }
284
285     /**
286      * Sets first argument
287      *
288      * @param arg0 first argument to set
289      */

290     public void setArg0(String JavaDoc arg0) {
291         this.arg0 = arg0;
292     }
293
294     /**
295      * Returns second argument
296      *
297      * @return second argument
298      * @jsp.attribute required="false"
299      * rtexprvalue="true"
300      * type="java.lang.String"
301      * description="The second optional argument."
302      */

303     public String JavaDoc getArg1() {
304         return arg1;
305     }
306
307     /**
308      * Sets second argument
309      *
310      * @param arg1 second argument to set
311      */

312     public void setArg1(String JavaDoc arg1) {
313         this.arg1 = arg1;
314     }
315
316
317     /**
318      * Returns third argument
319      *
320      * @return third argument
321      * @jsp.attribute required="false"
322      * rtexprvalue="true"
323      * type="java.lang.String"
324      * description="The third optional argument."
325      */

326     public String JavaDoc getArg2() {
327         return arg2;
328     }
329
330     /**
331      * Sets third argument
332      *
333      * @param arg2 third argument to set
334      */

335     public void setArg2(String JavaDoc arg2) {
336         this.arg2 = arg2;
337     }
338
339     /**
340      * Returns fourth argument
341      *
342      * @return fourth argument
343      * @jsp.attribute required="false"
344      * rtexprvalue="true"
345      * type="java.lang.String"
346      * description="The fourth optional argument."
347      */

348     public String JavaDoc getArg3() {
349         return arg3;
350     }
351
352     /**
353      * Sets fourth argument
354      *
355      * @param arg3 fourth argument to set
356      */

357     public void setArg3(String JavaDoc arg3) {
358         this.arg3 = arg3;
359     }
360
361
362     /**
363      * Returns fifth argument
364      *
365      * @return fifth argument
366      * @jsp.attribute required="false"
367      * rtexprvalue="true"
368      * type="java.lang.String"
369      * description="The fifth optional argument."
370      */

371     public String JavaDoc getArg4() {
372         return arg4;
373     }
374
375     /**
376      * Sets fifth argument
377      *
378      * @param arg4 fifth argument to set
379      */

380     public void setArg4(String JavaDoc arg4) {
381         this.arg4 = arg4;
382     }
383
384     /**
385      * Processes the tag
386      *
387      * @throws JspException
388      */

389     public void doTag() throws JspException JavaDoc {
390
391         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
392         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
393
394         if ( ignore == null ) {
395             ignore = Boolean.FALSE;
396         }
397
398         if ( filter == null ) {
399             filter = Boolean.FALSE;
400         }
401
402         Locale JavaDoc locale = null;
403         if ( language != null ) {
404             locale = new Locale JavaDoc(language);
405         } else {
406             locale = RequestUtils.getUserLocale(request, null);
407             if ( locale == null ) {
408                 locale = Locale.getDefault();
409             }
410         }
411
412         Object JavaDoc[] args = null;
413         if ( arg0 != null || arg1 != null || arg2 != null || arg3 != null || arg4 != null ) {
414             args = new Object JavaDoc[]{arg0, arg1, arg2, arg3, arg4};
415         }
416         
417         String JavaDoc message = ApplicationResources.getInstance(pageContext.getServletContext()).getMessage(locale, key, args);
418
419         if ( message == null ) {
420             if ( !ignore.booleanValue() ) {
421                 JspException JavaDoc e = new JspException JavaDoc("No message for key '" + key + "' could be found");
422                 throw e;
423             } else {
424                 if ( log.isDebugEnabled() ) {
425                     log.debug("No message for key '" + key + "' could be found. Ignored");
426                 }
427             }
428         } else if ( message.length() == 0 ) {
429             if ( log.isDebugEnabled() ) {
430                 log.debug("Value for key '" + key + "' is empty string");
431             }
432         }
433
434         TagUtils tagUtils = TagUtils.getInstance();
435         if ( var != null ) {
436             // save message in specified scope
437
int varScope = PageContext.PAGE_SCOPE;
438             if ( scope != null ) {
439                 varScope = tagUtils.getScope(scope);
440             }
441             pageContext.setAttribute(var, message, varScope);
442
443         } else {
444
445             // encode HTML-sensitive characters if requested
446
if ( filter.booleanValue() ) {
447                 message = StringUtil.htmlEncode(message);
448             }
449
450             // write message directly to page
451
tagUtils.write(pageContext, message);
452         }
453
454     }
455 }
456
Popular Tags