001 package org.apache.myfaces.tobago.util; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.myfaces.tobago.application.ProjectStage; 021 import org.apache.myfaces.tobago.config.TobagoConfig; 022 023 import javax.faces.application.FacesMessage; 024 import javax.faces.component.UIComponent; 025 import javax.faces.context.FacesContext; 026 import java.util.Map; 027 import java.util.Set; 028 029 030 public class DebugUtils { 031 032 private DebugUtils() { 033 // to prevent instantiation 034 } 035 036 public static String toString(UIComponent component, int offset) { 037 return toString(component, offset, false); 038 } 039 040 public static String toString(UIComponent component, int offset, boolean asFacet) { 041 StringBuilder result = new StringBuilder(); 042 if (component == null) { 043 result.append("null"); 044 } else { 045 result.append('\n'); 046 if (!asFacet) { 047 result.append(spaces(offset)); 048 result.append(toString(component)); 049 } 050 Map facets = component.getFacets(); 051 if (facets.size() > 0) { 052 for (Map.Entry<String, UIComponent> entry : (Set<Map.Entry<String, UIComponent>>) facets.entrySet()) { 053 UIComponent facet = entry.getValue(); 054 result.append('\n'); 055 result.append(spaces(offset + 1)); 056 result.append('\"'); 057 result.append(entry.getKey()); 058 result.append("\" = "); 059 result.append(toString(facet)); 060 result.append(toString(facet, offset + 1, true)); 061 } 062 } 063 for (Object o : component.getChildren()) { 064 result.append(toString((UIComponent) o, offset + 1, false)); 065 } 066 } 067 return result.toString(); 068 } 069 070 public static String toString(UIComponent component) { 071 StringBuilder buf = new StringBuilder(component.getClass().getName()); 072 buf.append('@'); 073 buf.append(Integer.toHexString(component.hashCode())); 074 buf.append(" "); 075 buf.append(component.getRendererType()); 076 buf.append(" "); 077 if (component instanceof javax.faces.component.UIViewRoot) { 078 buf.append(((javax.faces.component.UIViewRoot) component).getViewId()); 079 } else { 080 buf.append(component.getId()); 081 buf.append(" "); 082 buf.append(component.getClientId(FacesContext.getCurrentInstance())); 083 } 084 return buf.toString(); 085 } 086 087 public static String spaces(int n) { 088 StringBuilder buffer = new StringBuilder(); 089 for (int i = 0; i < n; i++) { 090 buffer.append(" "); 091 } 092 return buffer.toString(); 093 } 094 095 public static void addDevelopmentMessage(FacesContext facesContext, String message) { 096 if (TobagoConfig.getInstance(FacesContext.getCurrentInstance()).getProjectStage() == ProjectStage.Development) { 097 facesContext.addMessage(null, new FacesMessage(message)); 098 } 099 } 100 101 }