KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > fmt > MessageSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.taglibs.standard.tag.common.fmt;
18
19 import java.io.IOException JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.MissingResourceException JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspTagException JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
31 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
32 import javax.servlet.jsp.tagext.Tag JavaDoc;
33
34 import org.apache.taglibs.standard.tag.common.core.Util;
35
36 /**
37  * Support for tag handlers for <message>, the message formatting tag
38  * in JSTL 1.0.
39  *
40  * @author Jan Luehe
41  */

42
43 public abstract class MessageSupport extends BodyTagSupport JavaDoc {
44
45     //*********************************************************************
46
// Public constants
47

48     public static final String JavaDoc UNDEFINED_KEY = "???";
49
50
51     //*********************************************************************
52
// Protected state
53

54     protected String JavaDoc keyAttrValue; // 'key' attribute value
55
protected boolean keySpecified; // 'key' attribute specified
56
protected LocalizationContext bundleAttrValue; // 'bundle' attribute value
57
protected boolean bundleSpecified; // 'bundle' attribute specified?
58

59
60     //*********************************************************************
61
// Private state
62

63     private String JavaDoc var; // 'var' attribute
64
private int scope; // 'scope' attribute
65
private List JavaDoc params;
66
67
68     //*********************************************************************
69
// Constructor and initialization
70

71     public MessageSupport() {
72     super();
73     params = new ArrayList JavaDoc();
74     init();
75     }
76
77     private void init() {
78     var = null;
79     scope = PageContext.PAGE_SCOPE;
80     keyAttrValue = null;
81     keySpecified = false;
82     bundleAttrValue = null;
83     bundleSpecified = false;
84     }
85
86
87     //*********************************************************************
88
// Tag attributes known at translation time
89

90     public void setVar(String JavaDoc var) {
91         this.var = var;
92     }
93
94     public void setScope(String JavaDoc scope) {
95     this.scope = Util.getScope(scope);
96     }
97
98
99     //*********************************************************************
100
// Collaboration with subtags
101

102     /**
103      * Adds an argument (for parametric replacement) to this tag's message.
104      *
105      * @see ParamSupport
106      */

107     public void addParam(Object JavaDoc arg) {
108     params.add(arg);
109     }
110
111
112     //*********************************************************************
113
// Tag logic
114

115     public int doStartTag() throws JspException JavaDoc {
116     params.clear();
117     return EVAL_BODY_BUFFERED;
118     }
119
120     public int doEndTag() throws JspException JavaDoc {
121
122         String JavaDoc key = null;
123     LocalizationContext locCtxt = null;
124
125         // determine the message key by...
126
if (keySpecified) {
127         // ... reading 'key' attribute
128
key = keyAttrValue;
129     } else {
130         // ... retrieving and trimming our body
131
if (bodyContent != null && bodyContent.getString() != null)
132             key = bodyContent.getString().trim();
133     }
134
135     if ((key == null) || key.equals("")) {
136         try {
137         pageContext.getOut().print("??????");
138         } catch (IOException JavaDoc ioe) {
139         throw new JspTagException JavaDoc(ioe.toString(), ioe);
140         }
141         return EVAL_PAGE;
142     }
143
144     String JavaDoc prefix = null;
145     if (!bundleSpecified) {
146         Tag JavaDoc t = findAncestorWithClass(this, BundleSupport.class);
147         if (t != null) {
148         // use resource bundle from parent <bundle> tag
149
BundleSupport parent = (BundleSupport) t;
150         locCtxt = parent.getLocalizationContext();
151         prefix = parent.getPrefix();
152         } else {
153         locCtxt = BundleSupport.getLocalizationContext(pageContext);
154         }
155     } else {
156         // localization context taken from 'bundle' attribute
157
locCtxt = bundleAttrValue;
158         if (locCtxt.getLocale() != null) {
159         SetLocaleSupport.setResponseLocale(pageContext,
160                            locCtxt.getLocale());
161         }
162     }
163         
164     String JavaDoc message = UNDEFINED_KEY + key + UNDEFINED_KEY;
165     if (locCtxt != null) {
166         ResourceBundle JavaDoc bundle = locCtxt.getResourceBundle();
167         if (bundle != null) {
168         try {
169             // prepend 'prefix' attribute from parent bundle
170
if (prefix != null)
171             key = prefix + key;
172             message = bundle.getString(key);
173             // Perform parametric replacement if required
174
if (!params.isEmpty()) {
175             Object JavaDoc[] messageArgs = params.toArray();
176             MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(""); // empty pattern, default Locale
177
if (locCtxt.getLocale() != null) {
178                 formatter.setLocale(locCtxt.getLocale());
179             } else {
180                             // For consistency with the <fmt:formatXXX> actions,
181
// we try to get a locale that matches the user's preferences
182
// as well as the locales supported by 'date' and 'number'.
183
//System.out.println("LOCALE-LESS LOCCTXT: GETTING FORMATTING LOCALE");
184
Locale JavaDoc locale = SetLocaleSupport.getFormattingLocale(pageContext);
185                             //System.out.println("LOCALE: " + locale);
186
if (locale != null) {
187                                 formatter.setLocale(locale);
188                             }
189                         }
190             formatter.applyPattern(message);
191             message = formatter.format(messageArgs);
192             }
193         } catch (MissingResourceException JavaDoc mre) {
194             message = UNDEFINED_KEY + key + UNDEFINED_KEY;
195         }
196         }
197     }
198
199     if (var != null) {
200         pageContext.setAttribute(var, message, scope);
201     } else {
202         try {
203         pageContext.getOut().print(message);
204         } catch (IOException JavaDoc ioe) {
205         throw new JspTagException JavaDoc(ioe.toString(), ioe);
206         }
207     }
208
209     return EVAL_PAGE;
210     }
211
212     // Releases any resources we may have (or inherit)
213
public void release() {
214     init();
215     }
216 }
217
Popular Tags