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.Map;
29  import java.util.Set;
30  
31  
32  public final class DebugUtils {
33  
34    private DebugUtils() {
35      // to prevent instantiation
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  //      buf.append(component.getId());
80  //      buf.append(" ");
81        buf.append(component.getClientId(FacesContext.getCurrentInstance()));
82      if (component instanceof javax.faces.component.UIViewRoot) {
83        buf.append(" viewId=");
84        buf.append(((javax.faces.component.UIViewRoot) component).getViewId());
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 }