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 org.apache.myfaces.tobago.application.ProjectStage;
23 import org.apache.myfaces.tobago.config.TobagoConfig;
24
25 import javax.faces.application.FacesMessage;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import java.util.Map;
29 import java.util.Set;
30
31
32 public class DebugUtils {
33
34 private DebugUtils() {
35
36 }
37
38 public static String toString(UIComponent component, int offset) {
39 return toString(component, offset, false);
40 }
41
42 public static String toString(UIComponent component, int offset, boolean asFacet) {
43 StringBuilder result = new StringBuilder();
44 if (component == null) {
45 result.append("null");
46 } else {
47 result.append('\n');
48 if (!asFacet) {
49 result.append(spaces(offset));
50 result.append(toString(component));
51 }
52 Map facets = component.getFacets();
53 if (facets.size() > 0) {
54 for (Map.Entry<String, UIComponent> entry : (Set<Map.Entry<String, UIComponent>>) facets.entrySet()) {
55 UIComponent facet = entry.getValue();
56 result.append('\n');
57 result.append(spaces(offset + 1));
58 result.append('\"');
59 result.append(entry.getKey());
60 result.append("\" = ");
61 result.append(toString(facet));
62 result.append(toString(facet, offset + 1, true));
63 }
64 }
65 for (UIComponent child : component.getChildren()) {
66 result.append(toString(child, offset + 1, false));
67 }
68 }
69 return result.toString();
70 }
71
72 public static String toString(UIComponent component) {
73 StringBuilder buf = new StringBuilder(component.getClass().getName());
74 buf.append('@');
75 buf.append(Integer.toHexString(component.hashCode()));
76 buf.append(" ");
77 buf.append(component.getRendererType());
78 buf.append(" ");
79 if (component instanceof javax.faces.component.UIViewRoot) {
80 buf.append(((javax.faces.component.UIViewRoot) component).getViewId());
81 } else {
82 buf.append(component.getId());
83 buf.append(" ");
84 buf.append(component.getClientId(FacesContext.getCurrentInstance()));
85 }
86 return buf.toString();
87 }
88
89 public static String spaces(int n) {
90 StringBuilder buffer = new StringBuilder();
91 for (int i = 0; i < n; i++) {
92 buffer.append(" ");
93 }
94 return buffer.toString();
95 }
96
97 public static void addDevelopmentMessage(FacesContext facesContext, String message) {
98 if (TobagoConfig.getInstance(FacesContext.getCurrentInstance()).getProjectStage() == ProjectStage.Development) {
99 facesContext.addMessage(null, new FacesMessage(message));
100 }
101 }
102
103 }