001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.pageflow.ServletContainerAdapter;
022: import org.apache.beehive.netui.pageflow.internal.AdapterManager;
023: import org.apache.beehive.netui.pageflow.internal.BindingUpdateError;
024: import org.apache.beehive.netui.pageflow.internal.InternalUtils;
025: import org.apache.beehive.netui.tags.AbstractSimpleTag;
026: import org.apache.beehive.netui.util.Bundle;
027:
028: import javax.servlet.ServletRequest;
029: import javax.servlet.jsp.JspException;
030: import javax.servlet.jsp.PageContext;
031: import java.util.Iterator;
032: import java.util.LinkedHashMap;
033:
034: /**
035: * Renders the set of error messages found during processPopulate when the
036: * values to be updated cannot be resolved. These errors are treated as
037: * warnings. By default, this tag is only on in Iterative Dev mode and
038: * the warning are not displayed in production mode. The tag is intended
039: * for development use.
040: * @jsptagref.tagdescription
041: * Renders the set of error messages found during the process of resolving
042: * data binding expressions (pageFlow.firstname, requestScope.firstname, etc.).
043: * The tag is intended for development use and not for error reporting in deployed applications. When the
044: * NetUI framework is not in development mode, this tag will do nothing.
045: *
046: * @example
047: * <p>
048: * In this first sample, because the <netui:bindingUpdateErrors/> tag is unqualified,
049: * messages will be displayed if <b>any</b> data binding errors occurred when a form was
050: * posted. The messages are displayed on the page and the command window.
051: * </p>
052: * <pre><netui:bindingUpdateErrors /></pre>
053: * <p>
054: * In this next sample, only binding errors for the expression <code>{actionForm.firstName}</code>
055: * will be displayed on the page and the command window.
056: * </p>
057: * <pre><netui:bindingUpdateErrors expression="actionForm.firstName"/></pre>
058: * <p>
059: * Note, the expression used in this tag needs to match the expressions used to POST form data. This
060: * tag's <code>expression</code> attribute must resolve to a valid NetUI expression.
061: * </p>
062: * @netui:tag name="bindingUpdateErrors" body-content="empty"
063: * description="Will display a message for all binding update errors that occurred when a form was posted."
064: */
065: public class BindingUpdateErrors extends AbstractSimpleTag {
066: private String _expression = null;
067: private boolean _alwaysReport = false;
068:
069: /**
070: * Return the name of the Tag.
071: */
072: public String getTagName() {
073: return "BindingUpdateErrors";
074: }
075:
076: /**
077: * Sets the expression to match for binding errors. If an expression
078: * is set, only binding errors for that expression will be displayed.
079: * Otherwise, all errors will be displayed.
080: * @param expression The expression to match against.
081: * @jsptagref.attributedescription String. The data binding expression to match for binding errors.
082: * If a data binding expression is specified, only binding errors for that expression will be displayed.
083: * Otherwise, all errors will be displayed.
084: * @jsptagref.databindable false
085: * @jsptagref.attributesyntaxvalue <i>string_databinding_expression</i>
086: * @netui:attribute required="false" rtexprvalue="true"
087: * description="The data binding expression to match for binding errors. If a data binding expression
088: * is specified, only binding errors for that expression will be displayed.
089: * Otherwise, all errors will be displayed."
090: */
091: public void setExpression(String expression) throws JspException {
092: _expression = setRequiredValueAttribute(expression,
093: "expression");
094: }
095:
096: /**
097: * Set the value which will override the default behavior of not showing
098: * errors in production mode.
099: * @param alwaysReport a boolean that if <code>true</code> will cause
100: * the errors to always be displayed. The default is <code>false</code>
101: * @jsptagref.attributedescription Boolean. If <code>isAlwaysReport</code> is set to true, then the errors will be displayed in Production mode
102: * as well as in Development mode. Otherwise, the errors will be displayed only in Development mode.
103: * @jsptagref.databindable false
104: * @jsptagref.attributesyntaxvalue <i>boolean_alwaysReport</i>
105: * @netui:attribute required="false" rtexprvalue="true" type="boolean"
106: * description="If isAlwaysReport is set to true, then the errors will be displayed in Production mode
107: * as well as in Development mode. Otherwise, the errors will be displayed only in Development mode."
108: */
109: public void setAlwaysReport(boolean alwaysReport) {
110: _alwaysReport = alwaysReport;
111: }
112:
113: /**
114: * Render the specified error messages if there are any.
115: * @throws JspException if a JSP exception has occurred
116: */
117: public void doTag() throws JspException {
118: // report error if there are any
119: if (hasErrors()) {
120: reportErrors();
121: return;
122: }
123:
124: PageContext pageContext = getPageContext();
125: ServletContainerAdapter sa = AdapterManager
126: .getServletContainerAdapter(pageContext
127: .getServletContext());
128: ServletRequest request = pageContext.getRequest();
129: assert (sa != null);
130:
131: // check to see if we are supposed to report the error
132: boolean prodMode = sa.isInProductionMode();
133: if (prodMode && !_alwaysReport)
134: return;
135:
136: LinkedHashMap map = (LinkedHashMap) InternalUtils
137: .getBindingUpdateErrors(request);
138:
139: if (map == null)
140: return;
141:
142: if (_expression != null) {
143: String expr = "{" + _expression + "}";
144: BindingUpdateError err = (BindingUpdateError) map.get(expr);
145: if (err != null) {
146: Throwable cause = err.getCause();
147: String msg = (cause != null) ? cause.getMessage() : err
148: .getMessage();
149: String s = Bundle.getString("Tags_BindingUpdateError",
150: new Object[] { _expression, msg });
151: registerTagError(s, null);
152: reportErrors();
153: }
154: return;
155: }
156:
157: Iterator it = map.values().iterator();
158: while (it.hasNext()) {
159: BindingUpdateError err = (BindingUpdateError) it.next();
160: Throwable cause = err.getCause();
161: String msg = (cause != null) ? cause.getMessage() : err
162: .getMessage();
163: String s = Bundle.getString("Tags_BindingUpdateError",
164: new Object[] { err.getExpression(), msg });
165: registerTagError(s, null);
166: }
167: reportErrors();
168: }
169: }
|