001: /*
002: * $Id: ErrorsRenderer.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.renderer;
023:
024: import java.beans.Beans;
025: import java.io.IOException;
026: import java.util.Iterator;
027: import java.util.Locale;
028:
029: import javax.faces.application.FacesMessage;
030: import javax.faces.component.UIComponent;
031: import javax.faces.context.FacesContext;
032: import javax.faces.context.ResponseWriter;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.struts.Globals;
037: import org.apache.struts.action.ActionMessages;
038: import org.apache.struts.action.ActionMessage;
039: import org.apache.struts.util.MessageResources;
040:
041: /**
042: * <p><code>Renderer</code> implementation for the <code>errors</code> tag
043: * from the <em>Struts-Faces Integration Library</em>.</p>
044: *
045: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
046: */
047:
048: public class ErrorsRenderer extends AbstractRenderer {
049:
050: // -------------------------------------------------------- Static Variables
051:
052: /**
053: * <p>The <code>Log</code> instance for this class.</p>
054: */
055: private static Log log = LogFactory.getLog(ErrorsRenderer.class);
056:
057: /**
058: * The dummy message resources for this package.
059: */
060: protected static MessageResources dummy = MessageResources
061: .getMessageResources("org.apache.struts.faces.renderer.Dummy");
062:
063: // ---------------------------------------------------------- Public Methods
064:
065: /**
066: * <p>Render a combination of error messages from JavaServer Faces
067: * <code>Validator</code>s, and Struts messages from form bean
068: * <code>validate()</code> methods and corresponding business logic
069: * error checks.</p>
070: *
071: * @param context FacesContext for the request we are processing
072: * @param component UIComponent to be rendered
073: *
074: * @exception IOException if an input/output error occurs while rendering
075: * @exception NullPointerException if <code>context</code>
076: * or <code>component</code> is null
077: */
078: public void encodeEnd(FacesContext context, UIComponent component)
079: throws IOException {
080:
081: if ((context == null) || (component == null)) {
082: throw new NullPointerException();
083: }
084:
085: if (log.isDebugEnabled()) {
086: log.debug("encodeEnd() started");
087: }
088:
089: // Look up availability of our predefined resource keys
090: MessageResources resources = resources(context, component);
091: if (Beans.isDesignTime() && (resources == null)) {
092: resources = dummy;
093: }
094: Locale locale = context.getViewRoot().getLocale();
095: boolean headerPresent = resources.isPresent(locale,
096: "errors.header");
097: boolean footerPresent = resources.isPresent(locale,
098: "errors.footer");
099: boolean prefixPresent = resources.isPresent(locale,
100: "errors.prefix");
101: boolean suffixPresent = resources.isPresent(locale,
102: "errors.suffix");
103:
104: // Set up to render the error messages appropriately
105: boolean headerDone = false;
106: ResponseWriter writer = context.getResponseWriter();
107: String id = component.getId();
108: String property = (String) component.getAttributes().get(
109: "property");
110: if (id != null) {
111: writer.startElement("span", component);
112: if (id != null) {
113: writer.writeAttribute("id", component
114: .getClientId(context), "id");
115: }
116: }
117:
118: // Render any JavaServer Faces messages
119: Iterator messages = context.getMessages(property);
120: while (messages.hasNext()) {
121: FacesMessage message = (FacesMessage) messages.next();
122: if (log.isTraceEnabled()) {
123: log.trace("Processing FacesMessage: "
124: + message.getSummary());
125: }
126: if (!headerDone) {
127: if (headerPresent) {
128: writer.write(resources.getMessage(locale,
129: "errors.header"));
130: }
131: headerDone = true;
132: }
133: if (prefixPresent) {
134: writer.write(resources.getMessage(locale,
135: "errors.prefix"));
136: }
137: writer.write(message.getSummary());
138: if (suffixPresent) {
139: writer.write(resources.getMessage(locale,
140: "errors.suffix"));
141: }
142: }
143:
144: // Render any Struts messages
145: ActionMessages errors = (ActionMessages) context
146: .getExternalContext().getRequestMap().get(
147: Globals.ERROR_KEY);
148: if (errors != null) {
149: if (log.isTraceEnabled()) {
150: log.trace("Processing Struts messages for property '"
151: + property + "'");
152: }
153: Iterator reports = null;
154: if (property == null) {
155: reports = errors.get();
156: } else {
157: reports = errors.get(property);
158: }
159: while (reports.hasNext()) {
160: ActionMessage report = (ActionMessage) reports.next();
161: if (log.isTraceEnabled()) {
162: log.trace("Processing Struts message key='"
163: + report.getKey() + "'");
164: }
165: if (!headerDone) {
166: writer = context.getResponseWriter();
167: if (headerPresent) {
168: writer.write(resources.getMessage(locale,
169: "errors.header"));
170: }
171: headerDone = true;
172: }
173: if (prefixPresent) {
174: writer.write(resources.getMessage(locale,
175: "errors.prefix"));
176: }
177: writer.write(resources.getMessage(locale, report
178: .getKey(), report.getValues()));
179: if (suffixPresent) {
180: writer.write(resources.getMessage(locale,
181: "errors.suffix"));
182: }
183: }
184: }
185:
186: // Append the list footer if needed
187: if (headerDone && footerPresent) {
188: writer.write(resources.getMessage(locale, "errors.footer"));
189: }
190: if (id != null) {
191: writer.endElement("span");
192: }
193:
194: if (log.isDebugEnabled()) {
195: log.debug("encodeEnd() finished");
196: }
197:
198: }
199:
200: // ------------------------------------------------------ Protected Methods
201:
202: /**
203: * <p>Return the <code>MessageResources</code> bundle from which
204: * we should return any Struts based error messages. If no such
205: * bundle can be located, return <code>null</code>.</p>
206: *
207: * @param context FacesContext for the request we are processing
208: * @param component UIComponent to be rendered
209: */
210: protected MessageResources resources(FacesContext context,
211: UIComponent component) {
212:
213: String bundle = (String) component.getAttributes()
214: .get("bundle");
215: if (bundle == null) {
216: bundle = Globals.MESSAGES_KEY;
217: }
218: return ((MessageResources) context.getExternalContext()
219: .getApplicationMap().get(bundle));
220:
221: }
222:
223: }
|