1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.util; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
|
24 | |
import javax.faces.FacesException; |
25 | |
import javax.faces.component.*; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.el.MethodBinding; |
28 | |
import javax.faces.el.ValueBinding; |
29 | |
import javax.faces.event.FacesListener; |
30 | |
import javax.faces.validator.Validator; |
31 | |
import java.beans.BeanInfo; |
32 | |
import java.beans.IntrospectionException; |
33 | |
import java.beans.Introspector; |
34 | |
import java.beans.PropertyDescriptor; |
35 | |
import java.io.ByteArrayOutputStream; |
36 | |
import java.io.IOException; |
37 | |
import java.io.PrintStream; |
38 | |
import java.util.HashSet; |
39 | |
import java.util.Iterator; |
40 | |
import java.util.Map; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class DebugUtils |
50 | |
{ |
51 | 0 | private static final Log log = LogFactory.getLog(DebugUtils.class); |
52 | |
|
53 | |
|
54 | |
private static final HashSet<String> IGNORE_ATTRIBUTES; |
55 | |
static |
56 | |
{ |
57 | 0 | IGNORE_ATTRIBUTES = new HashSet<String>(); |
58 | 0 | IGNORE_ATTRIBUTES.add("attributes"); |
59 | 0 | IGNORE_ATTRIBUTES.add("children"); |
60 | 0 | IGNORE_ATTRIBUTES.add("childCount"); |
61 | 0 | IGNORE_ATTRIBUTES.add("class"); |
62 | 0 | IGNORE_ATTRIBUTES.add("facets"); |
63 | 0 | IGNORE_ATTRIBUTES.add("facetsAndChildren"); |
64 | 0 | IGNORE_ATTRIBUTES.add("parent"); |
65 | 0 | IGNORE_ATTRIBUTES.add("actionListeners"); |
66 | 0 | IGNORE_ATTRIBUTES.add("valueChangeListeners"); |
67 | 0 | IGNORE_ATTRIBUTES.add("validators"); |
68 | 0 | IGNORE_ATTRIBUTES.add("rowData"); |
69 | 0 | IGNORE_ATTRIBUTES.add("javax.faces.webapp.COMPONENT_IDS"); |
70 | 0 | IGNORE_ATTRIBUTES.add("javax.faces.webapp.FACET_NAMES"); |
71 | 0 | IGNORE_ATTRIBUTES.add("javax.faces.webapp.CURRENT_VIEW_ROOT"); |
72 | 0 | } |
73 | |
|
74 | |
private static final String JSF_COMPONENT_PACKAGE = "javax.faces.component."; |
75 | |
private static final String MYFACES_COMPONENT_PACKAGE = "org.apache.myfaces.component."; |
76 | |
|
77 | |
|
78 | |
private DebugUtils() |
79 | 0 | { |
80 | |
|
81 | 0 | } |
82 | |
|
83 | |
public static void assertError(boolean condition, Log log_, String msg) |
84 | |
throws FacesException |
85 | |
{ |
86 | 0 | if (!condition) |
87 | |
{ |
88 | 0 | log_.error(msg); |
89 | 0 | throw new FacesException(msg); |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | |
public static void assertFatal(boolean condition, Log log_, String msg) |
94 | |
throws FacesException |
95 | |
{ |
96 | 0 | if (!condition) |
97 | |
{ |
98 | 0 | log_.fatal(msg); |
99 | 0 | throw new FacesException(msg); |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
public static void traceView(String additionalMsg) |
106 | |
{ |
107 | 0 | if (log.isTraceEnabled()) |
108 | |
{ |
109 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
110 | 0 | if (facesContext == null) |
111 | |
{ |
112 | 0 | log.error("Cannot not print view to console (no FacesContext)."); |
113 | 0 | return; |
114 | |
} |
115 | 0 | UIViewRoot viewRoot = facesContext.getViewRoot(); |
116 | 0 | if (viewRoot == null) |
117 | |
{ |
118 | 0 | log.error("Cannot not print view to console (no ViewRoot in FacesContext)."); |
119 | 0 | return; |
120 | |
} |
121 | |
|
122 | 0 | traceView(additionalMsg, viewRoot); |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private static void traceView(String additionalMsg, UIViewRoot viewRoot) |
134 | |
{ |
135 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
136 | 0 | PrintStream ps = new PrintStream(baos); |
137 | 0 | if (additionalMsg != null) |
138 | |
{ |
139 | 0 | ps.println(additionalMsg); |
140 | |
} |
141 | 0 | ps.println("========================================"); |
142 | 0 | printView(viewRoot, ps); |
143 | 0 | ps.println("========================================"); |
144 | 0 | ps.close(); |
145 | 0 | log.trace(baos.toString()); |
146 | 0 | } |
147 | |
|
148 | |
public static void printView(UIViewRoot uiViewRoot, PrintStream stream) |
149 | |
{ |
150 | 0 | printComponent(uiViewRoot, stream, 0, true, null); |
151 | 0 | } |
152 | |
|
153 | |
public static void printComponent(UIComponent comp, PrintStream stream) |
154 | |
{ |
155 | 0 | printComponent(comp, stream, 0, false, null); |
156 | 0 | } |
157 | |
|
158 | |
private static void printComponent(UIComponent comp, |
159 | |
PrintStream stream, |
160 | |
int indent, |
161 | |
boolean withChildrenAndFacets, |
162 | |
String facetName) |
163 | |
{ |
164 | 0 | printIndent(stream, indent); |
165 | 0 | stream.print('<'); |
166 | |
|
167 | 0 | String compType = comp.getClass().getName(); |
168 | 0 | if (compType.startsWith(JSF_COMPONENT_PACKAGE)) |
169 | |
{ |
170 | 0 | compType = compType.substring(JSF_COMPONENT_PACKAGE.length()); |
171 | |
} |
172 | 0 | else if (compType.startsWith(MYFACES_COMPONENT_PACKAGE)) |
173 | |
{ |
174 | 0 | compType = compType.substring(MYFACES_COMPONENT_PACKAGE.length()); |
175 | |
} |
176 | 0 | stream.print(compType); |
177 | |
|
178 | 0 | printAttribute(stream, "id", comp.getId()); |
179 | |
|
180 | 0 | if (facetName != null) |
181 | |
{ |
182 | 0 | printAttribute(stream, "facetName", facetName); |
183 | |
} |
184 | |
|
185 | 0 | for (Iterator it = comp.getAttributes().entrySet().iterator(); it.hasNext(); ) |
186 | |
{ |
187 | 0 | Map.Entry entry = (Map.Entry)it.next(); |
188 | 0 | if (!"id".equals(entry.getKey())) |
189 | |
{ |
190 | 0 | printAttribute(stream, (String)entry.getKey(), entry.getValue()); |
191 | |
} |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
BeanInfo beanInfo; |
198 | |
try |
199 | |
{ |
200 | 0 | beanInfo = Introspector.getBeanInfo(comp.getClass()); |
201 | |
} |
202 | 0 | catch (IntrospectionException e) |
203 | |
{ |
204 | 0 | throw new RuntimeException(e); |
205 | 0 | } |
206 | |
|
207 | 0 | PropertyDescriptor propDescriptors[] = beanInfo.getPropertyDescriptors(); |
208 | 0 | for (int i = 0; i < propDescriptors.length; i++) |
209 | |
{ |
210 | 0 | if (propDescriptors[i].getReadMethod() != null) |
211 | |
{ |
212 | 0 | String name = propDescriptors[i].getName(); |
213 | 0 | if (!"id".equals(name)) |
214 | |
{ |
215 | 0 | ValueBinding vb = comp.getValueBinding(name); |
216 | 0 | if (vb != null) |
217 | |
{ |
218 | 0 | printAttribute(stream, name, vb.getExpressionString()); |
219 | |
} |
220 | |
else |
221 | |
{ |
222 | 0 | if (name.equals("value") && comp instanceof ValueHolder) |
223 | |
{ |
224 | |
|
225 | |
} |
226 | 0 | else if (!IGNORE_ATTRIBUTES.contains(name)) |
227 | |
{ |
228 | |
try |
229 | |
{ |
230 | 0 | Object value = comp.getAttributes().get(name); |
231 | 0 | printAttribute(stream, name, value); |
232 | |
} |
233 | 0 | catch (Exception e) |
234 | |
{ |
235 | 0 | log.error(e); |
236 | 0 | printAttribute(stream, name, null); |
237 | 0 | } |
238 | |
} |
239 | |
} |
240 | |
} |
241 | |
} |
242 | |
} |
243 | |
|
244 | 0 | boolean mustClose = true; |
245 | 0 | boolean nestedObjects = false; |
246 | |
|
247 | 0 | if (comp instanceof UICommand) |
248 | |
{ |
249 | 0 | FacesListener[] listeners = ((UICommand)comp).getActionListeners(); |
250 | 0 | if (listeners != null && listeners.length > 0) |
251 | |
{ |
252 | 0 | nestedObjects = true; |
253 | 0 | stream.println('>'); mustClose = false; |
254 | 0 | for (int i = 0; i < listeners.length; i++) |
255 | |
{ |
256 | 0 | FacesListener listener = listeners[i]; |
257 | 0 | printIndent(stream, indent + 1); |
258 | 0 | stream.print('<'); |
259 | 0 | stream.print(listener.getClass().getName()); |
260 | 0 | stream.println("/>"); |
261 | |
} |
262 | |
} |
263 | |
} |
264 | |
|
265 | 0 | if (comp instanceof UIInput) |
266 | |
{ |
267 | 0 | FacesListener[] listeners = ((UIInput)comp).getValueChangeListeners(); |
268 | 0 | if (listeners != null && listeners.length > 0) |
269 | |
{ |
270 | 0 | nestedObjects = true; |
271 | 0 | stream.println('>'); mustClose = false; |
272 | 0 | for (int i = 0; i < listeners.length; i++) |
273 | |
{ |
274 | 0 | FacesListener listener = listeners[i]; |
275 | 0 | printIndent(stream, indent + 1); |
276 | 0 | stream.print('<'); |
277 | 0 | stream.print(listener.getClass().getName()); |
278 | 0 | stream.println("/>"); |
279 | |
} |
280 | |
} |
281 | |
|
282 | 0 | Validator[] validators = ((UIInput)comp).getValidators(); |
283 | 0 | if (validators != null && validators.length > 0) |
284 | |
{ |
285 | 0 | nestedObjects = true; |
286 | 0 | stream.println('>'); mustClose = false; |
287 | 0 | for (int i = 0; i < validators.length; i++) |
288 | |
{ |
289 | 0 | Validator validator = validators[i]; |
290 | 0 | printIndent(stream, indent + 1); |
291 | 0 | stream.print('<'); |
292 | 0 | stream.print(validator.getClass().getName()); |
293 | 0 | stream.println("/>"); |
294 | |
} |
295 | |
} |
296 | |
} |
297 | |
|
298 | 0 | if (withChildrenAndFacets) |
299 | |
{ |
300 | 0 | int childCount = comp.getChildCount(); |
301 | 0 | Map<String, UIComponent> facetsMap = comp.getFacets(); |
302 | 0 | if (childCount > 0 || !facetsMap.isEmpty()) |
303 | |
{ |
304 | 0 | nestedObjects = true; |
305 | 0 | if (mustClose) |
306 | |
{ |
307 | 0 | stream.println('>'); |
308 | 0 | mustClose = false; |
309 | |
} |
310 | |
|
311 | 0 | if (childCount > 0) |
312 | |
{ |
313 | 0 | for (Iterator it = comp.getChildren().iterator(); it.hasNext(); ) |
314 | |
{ |
315 | 0 | UIComponent child = (UIComponent)it.next(); |
316 | 0 | printComponent(child, stream, indent + 1, true, null); |
317 | 0 | } |
318 | |
} |
319 | |
|
320 | 0 | for (Iterator it = facetsMap.entrySet().iterator(); it.hasNext(); ) |
321 | |
{ |
322 | 0 | Map.Entry entry = (Map.Entry)it.next(); |
323 | 0 | printComponent((UIComponent)entry.getValue(), |
324 | |
stream, indent + 1, true, |
325 | |
(String)entry.getKey()); |
326 | 0 | } |
327 | |
} |
328 | |
} |
329 | |
|
330 | 0 | if (nestedObjects) |
331 | |
{ |
332 | 0 | if (mustClose) |
333 | |
{ |
334 | 0 | stream.println("/>"); |
335 | |
} |
336 | |
else |
337 | |
{ |
338 | 0 | printIndent(stream, indent); |
339 | 0 | stream.print("</"); |
340 | 0 | stream.print(compType); |
341 | 0 | stream.println('>'); |
342 | |
} |
343 | |
} |
344 | |
else |
345 | |
{ |
346 | 0 | stream.println("/>"); |
347 | |
} |
348 | 0 | } |
349 | |
|
350 | |
private static void printAttribute(PrintStream stream, |
351 | |
String name, |
352 | |
Object value) |
353 | |
{ |
354 | 0 | if (IGNORE_ATTRIBUTES.contains(name)) return; |
355 | 0 | if (name.startsWith("javax.faces.webapp.UIComponentTag.")) |
356 | |
{ |
357 | 0 | name = name.substring("javax.faces.webapp.UIComponentTag.".length()); |
358 | |
} |
359 | 0 | stream.print(' '); |
360 | 0 | stream.print(name.toString()); |
361 | 0 | stream.print("=\""); |
362 | 0 | if (value != null) |
363 | |
{ |
364 | 0 | if (value instanceof UIComponent) |
365 | |
{ |
366 | 0 | stream.print("[id:"); |
367 | 0 | stream.print(((UIComponent)value).getId()); |
368 | 0 | stream.print(']'); |
369 | |
} |
370 | 0 | else if (value instanceof MethodBinding) |
371 | |
{ |
372 | 0 | stream.print(((MethodBinding)value).getExpressionString()); |
373 | |
} |
374 | |
else |
375 | |
{ |
376 | 0 | stream.print(value.toString()); |
377 | |
} |
378 | |
} |
379 | |
else |
380 | |
{ |
381 | 0 | stream.print("NULL"); |
382 | |
} |
383 | 0 | stream.print('"'); |
384 | 0 | } |
385 | |
|
386 | |
|
387 | |
private static void printIndent(PrintStream stream, int depth) |
388 | |
{ |
389 | 0 | for (int i = 0; i < depth; i++) |
390 | |
{ |
391 | 0 | stream.print(" "); |
392 | |
} |
393 | 0 | } |
394 | |
|
395 | |
public static String componentAsString(UIComponent comp) |
396 | |
{ |
397 | |
try |
398 | |
{ |
399 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
400 | 0 | printComponent(comp, new PrintStream(baos)); |
401 | 0 | baos.close(); |
402 | 0 | return baos.toString(); |
403 | |
} |
404 | 0 | catch (IOException e) |
405 | |
{ |
406 | 0 | throw new RuntimeException(e); |
407 | |
} |
408 | |
} |
409 | |
} |