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