KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > Errors


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.util.Bundle;
23 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
24 import org.apache.struts.Globals;
25 import org.apache.struts.action.ActionMessage;
26 import org.apache.struts.action.ActionMessages;
27 import org.apache.struts.util.RequestUtils;
28
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * Renders the set of error messages found in the ActionMessages
35  * registered in the pageContext at org.apache.struts.action.Action.ERROR_KEY.
36  * Errors ignores its body content.
37  * <p>
38  * The following optional message keys will be utilized if corresponding
39  * messages exist for them in the application resources:
40  * <ul>
41  * <li><b>errors.header</b> - If present, the corresponding message will be
42  * rendered prior to the individual list of error messages.</li>
43  * <li><b>errors.footer</b> - If present, the corresponding message will be
44  * rendered following the individual list of error messages.</li>
45  * <li><b>errors.prefix</b> - If present, the corresponding message will be
46  * rendered before each individual error message.</li>
47  * <li><b>errors.suffix</b> - If present, the corresponding message will be
48  * rendered after each individual error message.</li>
49  * </ul>
50  * @jsptagref.tagdescription Renders the set of error messages found in the ActionMessages
51  * registered in the <code>PageContext</code> at org.apache.struts.action.Action.ERROR_KEY.
52  * <p>
53  * The following optional message keys will be utilized if corresponding
54  * messages exist for them in the application resources:
55  * <blockquote>
56  * <ul>
57  * <li><b>errors.header</b> - If present, the corresponding message will be
58  * rendered prior to the individual list of error messages.</li>
59  * <li><b>errors.footer</b> - If present, the corresponding message will be
60  * rendered following the individual list of error messages.</li>
61  * <li><b>errors.prefix</b> - If present, the corresponding message will be
62  * rendered before each individual error message.</li>
63  * <li><b>errors.suffix</b> - If present, the corresponding message will be
64  * rendered after each individual error message.</li>
65  * </ul>
66  * </blockquote>
67  * @example In this sample, the messages from the <code>errorMessages</code> bundle will be used to
68  * output the errors.
69  * <pre>
70  * &lt;netui:errors bundleName="errorMessages" /></pre>
71  * @netui:tag name="errors" body-content="empty" description="Used to report multiple validation errors."
72  */

73 public class Errors extends ErrorBaseTag
74 {
75     /**
76      * Return the name of the Tag.
77      */

78     public String JavaDoc getTagName()
79     {
80         return "Errors";
81     }
82
83     /**
84      * Render the specified error messages if there are any.
85      * @throws JspException if a JSP exception has occurred
86      */

87     public void doTag()
88             throws JspException JavaDoc
89     {
90         PageContext JavaDoc pageContext = getPageContext();
91
92         // Were any error messages specified?
93
ActionMessages errors = null;
94         try {
95             errors = RequestUtils.getActionMessages(pageContext, Globals.ERROR_KEY);
96         }
97         catch (JspException JavaDoc e) {
98             RequestUtils.saveException(pageContext, e);
99             throw e;
100         }
101
102         if ((errors == null) || errors.isEmpty()) {
103             if (hasErrors())
104                 reportErrors();
105             return;
106         }
107
108         String JavaDoc qualifiedBundle = InternalUtils.getQualifiedBundleName(_bundleName, pageContext.getRequest());
109
110         boolean headerPresent = false;
111         boolean footerPresent = false;
112         boolean prefixPresent = false;
113         boolean suffixPresent = false;
114
115         String JavaDoc locale = _locale;
116         if (!isMissingUserDefaultMessages(pageContext)) {
117             try {
118                 // Check for presence of header and footer message keys
119
headerPresent =
120                         RequestUtils.present(pageContext, qualifiedBundle, locale, "errors.header");
121                 footerPresent =
122                         RequestUtils.present(pageContext, qualifiedBundle, locale, "errors.footer");
123                 prefixPresent =
124                         RequestUtils.present(pageContext, qualifiedBundle, locale, "errors.prefix");
125                 suffixPresent =
126                         RequestUtils.present(pageContext, qualifiedBundle, locale, "errors.suffix");
127             }
128             catch (JspException JavaDoc e) {
129                 String JavaDoc s = Bundle.getString("Tags_ErrorsException",
130                         new Object JavaDoc[]{e.getMessage()});
131                 registerTagError(s, null);
132                 reportErrors();
133                 return;
134             }
135         }
136
137         // Render the error messages appropriately
138
InternalStringBuilder results = new InternalStringBuilder(128);
139         boolean headerDone = false;
140         String JavaDoc message = null;
141         Iterator JavaDoc reports = null;
142         reports = errors.get();
143
144         while (reports.hasNext()) {
145             ActionMessage report = (ActionMessage) reports.next();
146             if (!headerDone) {
147                 if (headerPresent) {
148                     message = RequestUtils.message(pageContext, qualifiedBundle, locale, "errors.header");
149                     results.append(message);
150                     results.append("\r\n");
151                 }
152                 headerDone = true;
153             }
154             if (prefixPresent) {
155                 message = RequestUtils.message(pageContext, qualifiedBundle, locale, "errors.prefix");
156                 results.append(message);
157             }
158             message = getErrorMessage(report, qualifiedBundle);
159             if (message != null) {
160                 results.append(message);
161                 results.append("\r\n");
162             }
163             if (suffixPresent) {
164                 message = RequestUtils.message(pageContext, qualifiedBundle, locale, "errors.suffix");
165                 results.append(message);
166             }
167         }
168         if (headerDone && footerPresent) {
169             message = RequestUtils.message(pageContext, qualifiedBundle, locale, "errors.footer");
170             results.append(message);
171             results.append("\r\n");
172         }
173
174         if (hasErrors())
175             reportErrors();
176
177         // Print the results to our output writer
178
write(results.toString());
179     }
180 }
181
Popular Tags