KickJava   Java API By Example, From Geeks To Geeks.

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


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.pageflow.ServletContainerAdapter;
21 import org.apache.beehive.netui.pageflow.internal.AdapterManager;
22 import org.apache.beehive.netui.pageflow.internal.BindingUpdateError;
23 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
24 import org.apache.beehive.netui.tags.AbstractSimpleTag;
25 import org.apache.beehive.netui.util.Bundle;
26
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedHashMap JavaDoc;
32
33 /**
34  * Renders the set of error messages found during processPopulate when the
35  * values to be updated cannot be resolved. These errors are treated as
36  * warnings. By default, this tag is only on in Iterative Dev mode and
37  * the warning are not displayed in production mode. The tag is intended
38  * for development use.
39  * @jsptagref.tagdescription Renders the set of error messages found during the process of resolving
40  * data binding expressions (${pageFlow.firstname}, ${requestScope.firstname}, etc.).
41  * The tag is intended
42  * for development use, not for error reporting in deployed applications.
43  * @example In this first sample, because the <netui:bindingUpdateErrors/> tag is unqualified,
44  * messages will be displayed if <b>any</b> data binding errors occurred when a form was
45  * posted. The messages are displayed on the page and the command window.
46  * <pre>&lt;netui:bindingUpdateErrors /></pre>
47  * <p>In this next sample, only binding errors for the expression <code>{actionForm.firstName}</code>
48  * will be displayed on the page and the command window. </p>
49  * <pre>&lt;netui:bindingUpdateErrors expression="${actionForm.firstName}"/></pre>
50  * @netui:tag name="bindingUpdateErrors" body-content="empty" description="Will display a message for all binding update errors that occurred when a form was posted."
51  */

52 public class BindingUpdateErrors extends AbstractSimpleTag
53 {
54     private String JavaDoc _expression = null;
55     private boolean _alwaysReport = false;
56
57     /**
58      * Return the name of the Tag.
59      */

60     public String JavaDoc getTagName()
61     {
62         return "BindingUpdateErrors";
63     }
64
65     /**
66      * Sets the expression to match for binding errors. If an expression
67      * is set, only binding errors for that expression will be displayed.
68      * Otherwise, all errors will be displayed.
69      * @param expression The expression to match against.
70      * @jsptagref.attributedescription String. The data binding expression to match for binding errors. If an data binding expression
71      * is specified, only binding errors for that expression will be displayed.
72      * Otherwise, all errors will be displayed.
73      * @jsptagref.databindable false
74      * @jsptagref.attributesyntaxvalue <i>string_databinding_expression</i>
75      * @netui:attribute required="false" rtexprvalue="true"
76      * description="The data binding expression to match for binding errors. If an data binding expression
77      * is specified, only binding errors for that expression will be displayed.
78      * Otherwise, all errors will be displayed."
79      */

80     public void setExpression(String JavaDoc expression)
81             throws JspException JavaDoc
82     {
83         _expression = setRequiredValueAttribute(expression, "expression");
84     }
85
86     /**
87      * Set the value which will override the default behavior of not showing
88      * errors in production mode.
89      * @param alwaysReport a boolean that if <code>true</code> will cause
90      * the errors to always be displayed. The default is <code>false</code>
91      * @jsptagref.attributedescription Boolean. If <code>isAlwaysReport</code> is set to true, then the errors will be displayed in Production mode
92      * as well as in Development mode. Otherwise, the errors will be displayed only in Development mode.
93      * @jsptagref.databindable false
94      * @jsptagref.attributesyntaxvalue <i>boolean_alwaysReport</i>
95      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
96      * description="If isAlwaysReport is set to true, then the errors will be displayed in Production mode
97      * as well as in Development mode. Otherwise, the errors will be displayed only in Development mode."
98      */

99     public void setAlwaysReport(boolean alwaysReport)
100     {
101         _alwaysReport = alwaysReport;
102     }
103
104     /**
105      * Render the specified error messages if there are any.
106      * @throws JspException if a JSP exception has occurred
107      */

108     public void doTag()
109             throws JspException JavaDoc
110     {
111         // report error if there are any
112
if (hasErrors()) {
113             reportErrors();
114             return;
115         }
116
117         PageContext JavaDoc pageContext = getPageContext();
118         ServletContainerAdapter sa = AdapterManager.getServletContainerAdapter(pageContext.getServletContext());
119         ServletRequest JavaDoc request = pageContext.getRequest();
120         assert(sa != null);
121
122         // check to see if we are supposed to report the error
123
boolean prodMode = sa.isInProductionMode();
124         if (prodMode && !_alwaysReport)
125             return;
126
127         LinkedHashMap JavaDoc map = (LinkedHashMap JavaDoc)
128                 InternalUtils.getBindingUpdateErrors(request);
129
130         if (map == null)
131             return;
132
133         if (_expression != null) {
134             String JavaDoc expr = "{" + _expression + "}";
135             BindingUpdateError err = (BindingUpdateError) map.get(expr);
136             if (err != null) {
137                 Throwable JavaDoc cause = err.getCause();
138                 String JavaDoc msg = (cause != null) ? cause.getMessage() : err.getMessage();
139                 String JavaDoc s = Bundle.getString("Tags_BindingUpdateError", new Object JavaDoc[]{_expression, msg});
140                 registerTagError(s, null);
141                 reportErrors();
142             }
143             return;
144         }
145
146         Iterator JavaDoc it = map.values().iterator();
147         while (it.hasNext()) {
148             BindingUpdateError err = (BindingUpdateError) it.next();
149             Throwable JavaDoc cause = err.getCause();
150             String JavaDoc msg = (cause != null) ? cause.getMessage() : err.getMessage();
151             String JavaDoc s = Bundle.getString("Tags_BindingUpdateError", new Object JavaDoc[]{err.getExpression(), msg});
152             registerTagError(s, null);
153         }
154         reportErrors();
155     }
156 }
157
Popular Tags