1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.util;
21
22 import javax.faces.application.FacesMessage;
23 import javax.faces.application.ProjectStage;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30
31 public final class DebugUtils {
32
33 private DebugUtils() {
34
35 }
36
37 public static String toString(final UIComponent component, final int offset) {
38 return toString(component, offset, false, null);
39 }
40
41 public static String toString(
42 final UIComponent component, final int offset, final boolean asFacet, final Integer childIndex) {
43 final StringBuilder result = new StringBuilder();
44 if (component == null) {
45 result.append("null");
46 } else {
47 if (!asFacet) {
48 result.append(spaces(offset));
49 if (childIndex != null) {
50 result.append('[');
51 result.append(childIndex);
52 result.append("] ");
53 }
54 result.append(toString(component));
55 }
56 final Map facets = component.getFacets();
57 if (facets.size() > 0) {
58 for (final Map.Entry<String, UIComponent> entry : (Set<Map.Entry<String, UIComponent>>) facets.entrySet()) {
59 final UIComponent facet = entry.getValue();
60 result.append(spaces(offset + 1));
61 result.append('[');
62 result.append(entry.getKey());
63 result.append("] ");
64 result.append(toString(facet));
65 result.append(toString(facet, offset + 1, true, null));
66 }
67 }
68 final List<UIComponent> children = component.getChildren();
69 for (int i = 0; i < children.size(); i++) {
70 result.append(toString(children.get(i), offset + 1, false, i));
71 }
72 }
73 return result.toString();
74 }
75
76 public static String toString(final UIComponent component) {
77 final StringBuilder buf = new StringBuilder(component.getClass().getName());
78
79
80 buf.append(" ");
81 buf.append(component.getRendererType());
82 buf.append(" ");
83
84
85 buf.append(component.getClientId(FacesContext.getCurrentInstance()));
86 if (component instanceof javax.faces.component.UIViewRoot) {
87 buf.append(" viewId=");
88 buf.append(((javax.faces.component.UIViewRoot) component).getViewId());
89 }
90 buf.append(" rendered=");
91 buf.append(component.isRendered());
92 buf.append('\n');
93 return buf.toString();
94 }
95
96 public static String spaces(final int n) {
97 final StringBuilder buffer = new StringBuilder();
98 for (int i = 0; i < n; i++) {
99 buffer.append(" ");
100 }
101 return buffer.toString();
102 }
103
104 public static void addDevelopmentMessage(final FacesContext facesContext, final String message) {
105 if (facesContext.isProjectStage(ProjectStage.Development)) {
106 facesContext.addMessage(null, new FacesMessage(message));
107 }
108 }
109
110 }