KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ErrorsTag.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 import java.util.Locale JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionMessage;
29 import org.apache.struts.action.ActionMessages;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.util.MessageResources;
32
33 /**
34  * Custom tag that renders error messages if an appropriate request attribute
35  * has been created. The tag looks for a request attribute with a reserved
36  * key, and assumes that it is either a String, a String array, containing
37  * message keys to be looked up in the module's MessageResources, or
38  * an object of type <code>org.apache.struts.action.ActionErrors</code>.
39  * <p>
40  * The following optional message keys will be utilized if corresponding
41  * messages exist for them in the application resources:
42  * <ul>
43  * <li><b>errors.header</b> - If present, the corresponding message will be
44  * rendered prior to the individual list of error messages.</li>
45  * <li><b>errors.footer</b> - If present, the corresponding message will be
46  * rendered following the individual list of error messages.</li>
47  * <li><b>errors.prefix</b> - If present, the corresponding message will be
48  * rendered before each individual error message.</li>
49  * <li><b>errors.suffix</b> - If present, the corresponding message will be
50  * rendered after each individual error message.</li>
51  * </ul>
52  *
53  * @version $Rev: 164530 $ $Date: 2005-04-25 04:11:07 +0100 (Mon, 25 Apr 2005) $
54  */

55 public class ErrorsTag extends TagSupport JavaDoc {
56
57     // ----------------------------------------------------------- Properties
58

59     /**
60      * The servlet context attribute key for our resources.
61      */

62     protected String JavaDoc bundle = null;
63
64     public String JavaDoc getBundle() {
65         return (this.bundle);
66     }
67
68     public void setBundle(String JavaDoc bundle) {
69         this.bundle = bundle;
70     }
71
72     /**
73      * The default locale on our server.
74      * @deprecated Use Locale.getDefault() directly.
75      */

76     protected static Locale JavaDoc defaultLocale = Locale.getDefault();
77
78     /**
79      * The line ending string.
80      * @deprecated No longer used.
81      */

82     protected static String JavaDoc lineEnd = System.getProperty("line.separator");
83
84     /**
85      * The session attribute key for our locale.
86      */

87     protected String JavaDoc locale = Globals.LOCALE_KEY;
88
89     public String JavaDoc getLocale() {
90         return (this.locale);
91     }
92
93     public void setLocale(String JavaDoc locale) {
94         this.locale = locale;
95     }
96
97     /**
98      * The message resources for this package.
99      */

100     protected static MessageResources messages =
101         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
102
103     /**
104      * The request attribute key for our error messages (if any).
105      */

106     protected String JavaDoc name = Globals.ERROR_KEY;
107
108     public String JavaDoc getName() {
109         return (this.name);
110     }
111
112     public void setName(String JavaDoc name) {
113         this.name = name;
114     }
115
116     /**
117      * The name of the property for which error messages should be returned,
118      * or <code>null</code> to return all errors.
119      */

120     protected String JavaDoc property = null;
121
122     public String JavaDoc getProperty() {
123         return (this.property);
124     }
125
126     public void setProperty(String JavaDoc property) {
127         this.property = property;
128     }
129
130     /**
131      * The message resource key for errors header.
132      */

133     protected String JavaDoc header = null;
134
135     public String JavaDoc getHeader() {
136         return header == null ? "errors.header" : header;
137     }
138
139     public void setHeader(String JavaDoc header) {
140         this.header = header;
141     }
142
143     /**
144      * The message resource key for errors footer.
145      */

146     protected String JavaDoc footer = null;
147
148     public String JavaDoc getFooter() {
149         return footer == null ? "errors.footer" : footer;
150     }
151
152     public void setFooter(String JavaDoc footer) {
153         this.footer = footer;
154     }
155
156     /**
157      * The message resource key for errors prefix.
158      */

159     protected String JavaDoc prefix= null;
160
161     public String JavaDoc getPrefix() {
162         return prefix == null ? "errors.prefix" : prefix;
163     }
164
165     public void setPrefix(String JavaDoc prefix) {
166         this.prefix= prefix;
167     }
168
169     /**
170      * The message resource key for errors suffix.
171      */

172     protected String JavaDoc suffix= null;
173
174     public String JavaDoc getSuffix() {
175         return suffix == null ? "errors.suffix" : suffix;
176     }
177
178     public void setSuffix(String JavaDoc suffix) {
179         this.suffix= suffix;
180     }
181
182     // ------------------------------------------------------- Public Methods
183

184     /**
185      * Render the specified error messages if there are any.
186      *
187      * @exception JspException if a JSP exception has occurred
188      */

189     public int doStartTag() throws JspException JavaDoc {
190
191         // Were any error messages specified?
192
ActionMessages errors = null;
193         try {
194             errors = TagUtils.getInstance().getActionMessages(pageContext, name);
195         } catch (JspException JavaDoc e) {
196             TagUtils.getInstance().saveException(pageContext, e);
197             throw e;
198         }
199
200         if ((errors == null) || errors.isEmpty()) {
201             return (EVAL_BODY_INCLUDE);
202         }
203
204         boolean headerPresent =
205             TagUtils.getInstance().present(pageContext, bundle, locale, getHeader());
206
207         boolean footerPresent =
208             TagUtils.getInstance().present(pageContext, bundle, locale, getFooter());
209
210         boolean prefixPresent =
211             TagUtils.getInstance().present(pageContext, bundle, locale, getPrefix());
212
213         boolean suffixPresent =
214             TagUtils.getInstance().present(pageContext, bundle, locale, getSuffix());
215
216         // Render the error messages appropriately
217
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
218         boolean headerDone = false;
219         String JavaDoc message = null;
220         Iterator JavaDoc reports = (property == null) ? errors.get() : errors.get(property);
221
222         while (reports.hasNext()) {
223             ActionMessage report = (ActionMessage) reports.next();
224             if (!headerDone) {
225                 if (headerPresent) {
226                     message =
227                         TagUtils.getInstance().message(
228                             pageContext,
229                             bundle,
230                             locale,
231                             getHeader());
232                             
233                     results.append(message);
234                 }
235                 headerDone = true;
236             }
237             
238             if (prefixPresent) {
239                 message =
240                     TagUtils.getInstance().message(
241                         pageContext,
242                         bundle,
243                         locale,
244                         getPrefix());
245                 results.append(message);
246             }
247             
248             if (report.isResource()) {
249                 message =
250                     TagUtils.getInstance().message(
251                         pageContext,
252                         bundle,
253                         locale,
254                         report.getKey(),
255                         report.getValues());
256             } else {
257                 message = report.getKey();
258             }
259                     
260             if (message != null) {
261                 results.append(message);
262             }
263             
264             if (suffixPresent) {
265                 message =
266                     TagUtils.getInstance().message(
267                         pageContext,
268                         bundle,
269                         locale,
270                         getSuffix());
271                 results.append(message);
272             }
273         }
274         
275         if (headerDone && footerPresent) {
276             message =
277                 TagUtils.getInstance().message(pageContext, bundle, locale, getFooter());
278             results.append(message);
279         }
280
281         TagUtils.getInstance().write(pageContext, results.toString());
282
283         return (EVAL_BODY_INCLUDE);
284
285     }
286
287     /**
288      * Release any acquired resources.
289      */

290     public void release() {
291         super.release();
292         bundle = Globals.MESSAGES_KEY;
293         locale = Globals.LOCALE_KEY;
294         name = Globals.ERROR_KEY;
295         property = null;
296         header = null;
297         footer = null;
298         prefix = null;
299         suffix = null;
300     }
301
302 }
303
Popular Tags