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.util.internal.InternalStringBuilder;
022:
023: import org.apache.beehive.netui.util.Bundle;
024: import org.apache.beehive.netui.pageflow.internal.InternalUtils;
025: import org.apache.struts.Globals;
026: import org.apache.struts.action.ActionMessage;
027: import org.apache.struts.action.ActionMessages;
028: import org.apache.struts.util.RequestUtils;
029:
030: import javax.servlet.jsp.JspException;
031: import javax.servlet.jsp.PageContext;
032: import java.util.Iterator;
033:
034: /**
035: * Renders the set of error messages found in the ActionMessages
036: * registered in the pageContext at org.apache.struts.action.Action.ERROR_KEY.
037: * Errors ignores its body content.
038: * <p>
039: * The following optional message keys will be utilized if corresponding
040: * messages exist for them in the application resources:
041: * <ul>
042: * <li><b>errors.header</b> - If present, the corresponding message will be
043: * rendered prior to the individual list of error messages.</li>
044: * <li><b>errors.footer</b> - If present, the corresponding message will be
045: * rendered following the individual list of error messages.</li>
046: * <li><b>errors.prefix</b> - If present, the corresponding message will be
047: * rendered before each individual error message.</li>
048: * <li><b>errors.suffix</b> - If present, the corresponding message will be
049: * rendered after each individual error message.</li>
050: * </ul>
051: * @jsptagref.tagdescription Renders the set of error messages found in the ActionMessages
052: * registered in the <code>PageContext</code> at org.apache.struts.action.Action.ERROR_KEY.
053: * <p>
054: * The following optional message keys will be utilized if corresponding
055: * messages exist for them in the application resources:
056: * <blockquote>
057: * <ul>
058: * <li><b>errors.header</b> - If present, the corresponding message will be
059: * rendered prior to the individual list of error messages.</li>
060: * <li><b>errors.footer</b> - If present, the corresponding message will be
061: * rendered following the individual list of error messages.</li>
062: * <li><b>errors.prefix</b> - If present, the corresponding message will be
063: * rendered before each individual error message.</li>
064: * <li><b>errors.suffix</b> - If present, the corresponding message will be
065: * rendered after each individual error message.</li>
066: * </ul>
067: * </blockquote>
068: * @example In this sample, the messages from the <code>errorMessages</code> bundle will be used to
069: * output the errors.
070: * <pre>
071: * <netui:errors bundleName="errorMessages" /></pre>
072: * @netui:tag name="errors" body-content="empty" description="Used to report multiple validation errors."
073: */
074: public class Errors extends ErrorBaseTag {
075: /**
076: * Return the name of the Tag.
077: */
078: public String getTagName() {
079: return "Errors";
080: }
081:
082: /**
083: * Render the specified error messages if there are any.
084: * @throws JspException if a JSP exception has occurred
085: */
086: public void doTag() throws JspException {
087: PageContext pageContext = getPageContext();
088:
089: // Were any error messages specified?
090: ActionMessages errors = null;
091: try {
092: errors = RequestUtils.getActionMessages(pageContext,
093: Globals.ERROR_KEY);
094: } catch (JspException e) {
095: RequestUtils.saveException(pageContext, e);
096: throw e;
097: }
098:
099: if ((errors == null) || errors.isEmpty()) {
100: if (hasErrors())
101: reportErrors();
102: return;
103: }
104:
105: String qualifiedBundle = InternalUtils.getQualifiedBundleName(
106: _bundleName, pageContext.getRequest());
107:
108: boolean headerPresent = false;
109: boolean footerPresent = false;
110: boolean prefixPresent = false;
111: boolean suffixPresent = false;
112:
113: String locale = _locale;
114: if (!isMissingUserDefaultMessages(pageContext)) {
115: try {
116: // Check for presence of header and footer message keys
117: headerPresent = RequestUtils.present(pageContext,
118: qualifiedBundle, locale, "errors.header");
119: footerPresent = RequestUtils.present(pageContext,
120: qualifiedBundle, locale, "errors.footer");
121: prefixPresent = RequestUtils.present(pageContext,
122: qualifiedBundle, locale, "errors.prefix");
123: suffixPresent = RequestUtils.present(pageContext,
124: qualifiedBundle, locale, "errors.suffix");
125: } catch (JspException e) {
126: String s = Bundle.getString("Tags_ErrorsException",
127: new Object[] { e.getMessage() });
128: registerTagError(s, null);
129: reportErrors();
130: return;
131: }
132: }
133:
134: // Render the error messages appropriately
135: InternalStringBuilder results = new InternalStringBuilder(128);
136: boolean headerDone = false;
137: String message = null;
138: Iterator reports = null;
139: reports = errors.get();
140:
141: while (reports.hasNext()) {
142: ActionMessage report = (ActionMessage) reports.next();
143: if (!headerDone) {
144: if (headerPresent) {
145: message = RequestUtils.message(pageContext,
146: qualifiedBundle, locale, "errors.header");
147: results.append(message);
148: results.append("\r\n");
149: }
150: headerDone = true;
151: }
152: if (prefixPresent) {
153: message = RequestUtils.message(pageContext,
154: qualifiedBundle, locale, "errors.prefix");
155: results.append(message);
156: }
157: message = getErrorMessage(report, qualifiedBundle);
158: if (message != null) {
159: results.append(message);
160: results.append("\r\n");
161: }
162: if (suffixPresent) {
163: message = RequestUtils.message(pageContext,
164: qualifiedBundle, locale, "errors.suffix");
165: results.append(message);
166: }
167: }
168: if (headerDone && footerPresent) {
169: message = RequestUtils.message(pageContext,
170: qualifiedBundle, locale, "errors.footer");
171: results.append(message);
172: results.append("\r\n");
173: }
174:
175: if (hasErrors())
176: reportErrors();
177:
178: // Print the results to our output writer
179: write(results.toString());
180: }
181: }
|