KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > MessagesTag


1 /*
2  * $Id: MessagesTag.java 164530 2005-04-25 03:11:07Z niallp $
3  *
4  * Copyright 1999-2005 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.html;
20
21 import java.util.Iterator JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.action.ActionMessage;
28 import org.apache.struts.action.ActionMessages;
29 import org.apache.struts.taglib.TagUtils;
30 import org.apache.struts.util.MessageResources;
31
32 /**
33  * Custom tag that iterates the elements of a message collection.
34  * It defaults to retrieving the messages from <code>Globals.ERROR_KEY</code>,
35  * but if the message attribute is set to true then the messages will be
36  * retrieved from <code>Globals.MESSAGE_KEY</code>. This is an alternative
37  * to the default <code>ErrorsTag</code>.
38  *
39  * @version $Rev: 164530 $ $Date: 2005-04-25 04:11:07 +0100 (Mon, 25 Apr 2005) $
40  * @since Struts 1.1
41  */

42 public class MessagesTag extends BodyTagSupport JavaDoc {
43
44     /**
45      * The message resources for this package.
46      */

47     protected static MessageResources messageResources =
48        MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
49
50     /**
51      * Iterator of the elements of this error collection, while we are actually
52      * running.
53      */

54     protected Iterator JavaDoc iterator = null;
55
56     /**
57      * Whether or not any error messages have been processed.
58      */

59     protected boolean processed = false;
60
61     /**
62      * The name of the scripting variable to be exposed.
63      */

64     protected String JavaDoc id = null;
65
66     /**
67      * The servlet context attribute key for our resources.
68      */

69     protected String JavaDoc bundle = null;
70
71     /**
72      * The session attribute key for our locale.
73      */

74     protected String JavaDoc locale = Globals.LOCALE_KEY;
75
76     /**
77      * The request attribute key for our error messages (if any).
78      */

79     protected String JavaDoc name = Globals.ERROR_KEY;
80
81     /**
82      * The name of the property for which error messages should be returned,
83      * or <code>null</code> to return all errors.
84      */

85     protected String JavaDoc property = null;
86
87     /**
88      * The message resource key for errors header.
89      */

90     protected String JavaDoc header = null;
91
92     /**
93      * The message resource key for errors footer.
94      */

95     protected String JavaDoc footer = null;
96
97     /**
98      * If this is set to 'true', then the <code>Globals.MESSAGE_KEY</code> will
99      * be used to retrieve the messages from scope.
100      */

101     protected String JavaDoc message = null;
102
103     public String JavaDoc getId() {
104         return (this.id);
105     }
106
107     public void setId(String JavaDoc id) {
108         this.id = id;
109     }
110
111     public String JavaDoc getBundle() {
112         return (this.bundle);
113     }
114
115     public void setBundle(String JavaDoc bundle) {
116         this.bundle = bundle;
117     }
118
119     public String JavaDoc getLocale() {
120         return (this.locale);
121     }
122
123     public void setLocale(String JavaDoc locale) {
124         this.locale = locale;
125     }
126
127     public String JavaDoc getName() {
128         return (this.name);
129     }
130
131     public void setName(String JavaDoc name) {
132         this.name = name;
133     }
134
135     public String JavaDoc getProperty() {
136         return (this.property);
137     }
138
139     public void setProperty(String JavaDoc property) {
140         this.property = property;
141     }
142
143     public String JavaDoc getHeader() {
144         return (this.header);
145     }
146
147     public void setHeader(String JavaDoc header) {
148         this.header = header;
149     }
150
151     public String JavaDoc getFooter() {
152         return (this.footer);
153     }
154
155     public void setFooter(String JavaDoc footer) {
156         this.footer = footer;
157     }
158
159     public String JavaDoc getMessage() {
160         return (this.message);
161     }
162
163     public void setMessage(String JavaDoc message) {
164         this.message = message;
165     }
166
167     /**
168      * Construct an iterator for the specified collection, and begin
169      * looping through the body once per element.
170      *
171      * @exception JspException if a JSP exception has occurred
172      */

173     public int doStartTag() throws JspException JavaDoc {
174         // Initialize for a new request.
175
processed = false;
176
177         // Were any messages specified?
178
ActionMessages messages = null;
179
180         // Make a local copy of the name attribute that we can modify.
181
String JavaDoc name = this.name;
182
183         if (message != null && "true".equalsIgnoreCase(message)) {
184             name = Globals.MESSAGE_KEY;
185         }
186
187         try {
188             messages = TagUtils.getInstance().getActionMessages(pageContext, name);
189
190         } catch (JspException JavaDoc e) {
191             TagUtils.getInstance().saveException(pageContext, e);
192             throw e;
193         }
194
195         // Acquire the collection we are going to iterate over
196
this.iterator = (property == null) ? messages.get() : messages.get(property);
197
198         // Store the first value and evaluate, or skip the body if none
199
if (!this.iterator.hasNext()) {
200             return SKIP_BODY;
201         }
202
203         ActionMessage report = (ActionMessage) this.iterator.next();
204         String JavaDoc msg = null;
205         if (report.isResource()) {
206             msg = TagUtils.getInstance().message(
207                      pageContext,
208                      bundle,
209                      locale,
210                      report.getKey(),
211                      report.getValues());
212         } else {
213             msg = report.getKey();
214         }
215
216         if (msg == null) {
217             pageContext.removeAttribute(id);
218         } else {
219             pageContext.setAttribute(id, msg);
220         }
221
222         if (header != null && header.length() > 0) {
223             String JavaDoc headerMessage =
224                 TagUtils.getInstance().message(pageContext, bundle, locale, header);
225
226             if (headerMessage != null) {
227                 TagUtils.getInstance().write(pageContext, headerMessage);
228             }
229         }
230
231         // Set the processed variable to true so the
232
// doEndTag() knows processing took place
233
processed = true;
234
235         return (EVAL_BODY_TAG);
236     }
237
238     /**
239      * Make the next collection element available and loop, or
240      * finish the iterations if there are no more elements.
241      *
242      * @exception JspException if a JSP exception has occurred
243      */

244     public int doAfterBody() throws JspException JavaDoc {
245         // Render the output from this iteration to the output stream
246
if (bodyContent != null) {
247             TagUtils.getInstance().writePrevious(pageContext, bodyContent.getString());
248             bodyContent.clearBody();
249         }
250
251         // Decide whether to iterate or quit
252
if (iterator.hasNext()) {
253             ActionMessage report = (ActionMessage) iterator.next();
254             String JavaDoc msg =
255                 TagUtils.getInstance().message(
256                     pageContext,
257                     bundle,
258                     locale,
259                     report.getKey(),
260                     report.getValues());
261
262            if (msg == null) {
263                pageContext.removeAttribute(id);
264            } else {
265                pageContext.setAttribute(id, msg);
266            }
267
268            return (EVAL_BODY_TAG);
269         } else {
270            return (SKIP_BODY);
271         }
272
273     }
274
275
276     /**
277      * Clean up after processing this enumeration.
278      *
279      * @exception JspException if a JSP exception has occurred
280      */

281     public int doEndTag() throws JspException JavaDoc {
282        if (processed && footer != null && footer.length() > 0) {
283
284         String JavaDoc footerMessage =
285             TagUtils.getInstance().message(pageContext, bundle, locale, footer);
286
287           if (footerMessage != null) {
288              TagUtils.getInstance().write(pageContext, footerMessage);
289           }
290        }
291
292        return EVAL_PAGE;
293     }
294
295
296     /**
297      * Release all allocated resources.
298      */

299     public void release() {
300        super.release();
301        iterator = null;
302        processed = false;
303        id = null;
304        bundle = null;
305        locale = Globals.LOCALE_KEY;
306        name = Globals.ERROR_KEY;
307        property = null;
308        header = null;
309        footer = null;
310        message = null;
311     }
312
313 }
314
Popular Tags