KickJava   Java API By Example, From Geeks To Geeks.

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


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 an error message with a given error key if that key can be found in the
35  * ActionMessages registered in the <code>pageContext</code> at
36  * <code>org.apache.struts.action.Action.ERROR_KEY</code>. Error 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>error.prefix</b> - If present, the corresponding message will be
42  * rendered before each individual error message.</li>
43  * <li><b>error.suffix</b> - If present, the corresponding message will be
44  * rendered after each individual error message.</li>
45  * </ul>
46  * @jsptagref.tagdescription Renders an error message with a given error key if that key can be found in the
47  * ActionMessages registered in the <code>PageContext</code> at
48  * <code>org.apache.struts.action.Action.ERROR_KEY</code>.
49  *
50  * <p>The following optional message keys will be utilized if corresponding
51  * messages exist for them in the application resources:
52  * <blockquote>
53  * <ul>
54  * <li><b>error.prefix</b> - If present, the corresponding message will be
55  * rendered before each individual error message.</li>
56  * <li><b>error.suffix</b> - If present, the corresponding message will be
57  * rendered after each individual error message.</li>
58  * </ul>
59  * </blockquote>
60  * @example In this sample, the "InvalidName" message from the errorMessages bundle will be used to output the error.
61  * <pre>
62  * &lt;netui:error bundleName="errorMessages" key="InvalidName"/></pre>
63  * @netui:tag name="error" body-content="empty" description="Renders an error message with a given error key."
64  */

65 public class Error extends ErrorBaseTag
66 {
67     private String JavaDoc _key = null; // error key name, may be an expression
68

69     /**
70      * Return the name of the Tag.
71      */

72     public String JavaDoc getTagName()
73     {
74         return "Error";
75     }
76
77     /**
78      * Set the key under which the error was stored (often the name of the form bean property associated with the error).
79      * @param key the key under which the error was stored
80      * @jsptagref.attributedescription The key under which the error was stored (often the name of the form bean property associated with the error)
81      * @jsptagref.databindable false
82      * @jsptagref.attributesyntaxvalue <i>string_or_expression_key</i>
83      * @netui:attribute required="true" rtexprvalue="true"
84      * description="The key under which the error was stored (often the name of the form bean property associated with the error)"
85      */

86     public void setKey(String JavaDoc key)
87             throws JspException JavaDoc
88     {
89         _key = setRequiredValueAttribute(key, "key");
90     }
91
92     /**
93      * Render the specified error message if it can be found.
94      * @throws JspException if a JSP exception has occurred
95      */

96     public void doTag()
97             throws JspException JavaDoc
98     {
99         // Error will try an always work even if there are expression
100
// errors. The error will be reported at the end.
101

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