View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      // to prevent instantiation
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  //    buf.append('@');
79  //    buf.append(Integer.toHexString(component.hashCode()));
80      buf.append(" ");
81      buf.append(component.getRendererType());
82      buf.append(" ");
83  //      buf.append(component.getId());
84  //      buf.append(" ");
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 }