1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.faces.renderer;
23
24
25 import java.beans.Beans;
26 import java.io.IOException;
27 import java.util.Iterator;
28 import java.util.Locale;
29
30 import javax.faces.application.FacesMessage;
31 import javax.faces.component.UIComponent;
32 import javax.faces.context.FacesContext;
33 import javax.faces.context.ResponseWriter;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.struts.Globals;
38 import org.apache.struts.action.ActionMessages;
39 import org.apache.struts.action.ActionMessage;
40 import org.apache.struts.util.MessageResources;
41
42
43
44
45
46
47
48
49
50 public class ErrorsRenderer extends AbstractRenderer {
51
52
53
54
55
56
57
58
59 private static Log log = LogFactory.getLog(ErrorsRenderer.class);
60
61
62
63
64
65 protected static MessageResources dummy =
66 MessageResources.getMessageResources
67 ("org.apache.struts.faces.renderer.Dummy");
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public void encodeEnd(FacesContext context, UIComponent component)
86 throws IOException {
87
88 if ((context == null) || (component == null)) {
89 throw new NullPointerException();
90 }
91
92 if (log.isDebugEnabled()) {
93 log.debug("encodeEnd() started");
94 }
95
96
97 MessageResources resources = resources(context, component);
98 if (Beans.isDesignTime() && (resources == null)) {
99 resources = dummy;
100 }
101 Locale locale = context.getViewRoot().getLocale();
102 boolean headerPresent = resources.isPresent(locale, "errors.header");
103 boolean footerPresent = resources.isPresent(locale, "errors.footer");
104 boolean prefixPresent = resources.isPresent(locale, "errors.prefix");
105 boolean suffixPresent = resources.isPresent(locale, "errors.suffix");
106
107
108 boolean headerDone = false;
109 ResponseWriter writer = context.getResponseWriter();
110 String id = component.getId();
111 String property = (String) component.getAttributes().get("property");
112 if (id != null) {
113 writer.startElement("span", component);
114 if (id != null) {
115 writer.writeAttribute("id", component.getClientId(context),
116 "id");
117 }
118 }
119
120
121 Iterator messages = context.getMessages(property);
122 while (messages.hasNext()) {
123 FacesMessage message = (FacesMessage) messages.next();
124 if (log.isTraceEnabled()) {
125 log.trace("Processing FacesMessage: " + message.getSummary());
126 }
127 if (!headerDone) {
128 if (headerPresent) {
129 writer.write
130 (resources.getMessage(locale, "errors.header"));
131 }
132 headerDone = true;
133 }
134 if (prefixPresent) {
135 writer.write(resources.getMessage(locale, "errors.prefix"));
136 }
137 writer.write(message.getSummary());
138 if (suffixPresent) {
139 writer.write(resources.getMessage(locale, "errors.suffix"));
140 }
141 }
142
143
144 ActionMessages errors = (ActionMessages)
145 context.getExternalContext().getRequestMap().get
146 (Globals.ERROR_KEY);
147 if (errors != null) {
148 if (log.isTraceEnabled()) {
149 log.trace("Processing Struts messages for property '" +
150 property + "'");
151 }
152 Iterator reports = null;
153 if (property == null) {
154 reports = errors.get();
155 } else {
156 reports = errors.get(property);
157 }
158 while (reports.hasNext()) {
159 ActionMessage report = (ActionMessage) reports.next();
160 if (log.isTraceEnabled()) {
161 log.trace("Processing Struts message key='" +
162 report.getKey() + "'");
163 }
164 if (!headerDone) {
165 writer = context.getResponseWriter();
166 if (headerPresent) {
167 writer.write
168 (resources.getMessage(locale, "errors.header"));
169 }
170 headerDone = true;
171 }
172 if (prefixPresent) {
173 writer.write
174 (resources.getMessage(locale, "errors.prefix"));
175 }
176 writer.write(resources.getMessage(locale, report.getKey(),
177 report.getValues()));
178 if (suffixPresent) {
179 writer.write
180 (resources.getMessage(locale, "errors.suffix"));
181 }
182 }
183 }
184
185
186 if (headerDone && footerPresent) {
187 writer.write(resources.getMessage(locale, "errors.footer"));
188 }
189 if (id != null) {
190 writer.endElement("span");
191 }
192
193 if (log.isDebugEnabled()) {
194 log.debug("encodeEnd() finished");
195 }
196
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 protected MessageResources resources(FacesContext context,
213 UIComponent component) {
214
215 String bundle = (String) component.getAttributes().get("bundle");
216 if (bundle == null) {
217 bundle = Globals.MESSAGES_KEY;
218 }
219 return ((MessageResources)
220 context.getExternalContext().getApplicationMap().get(bundle));
221
222 }
223
224
225 }