1 16 package org.apache.myfaces.util; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import javax.faces.FacesException; 22 import javax.faces.component.*; 23 import javax.faces.context.FacesContext; 24 import javax.faces.el.MethodBinding; 25 import javax.faces.el.ValueBinding; 26 import javax.faces.event.FacesListener; 27 import javax.faces.validator.Validator; 28 import java.beans.BeanInfo ; 29 import java.beans.IntrospectionException ; 30 import java.beans.Introspector ; 31 import java.beans.PropertyDescriptor ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.IOException ; 34 import java.io.PrintStream ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 39 40 62 public class DebugUtils 63 { 64 private static final Log log = LogFactory.getLog(DebugUtils.class); 65 66 private static final HashSet IGNORE_ATTRIBUTES; 68 static 69 { 70 IGNORE_ATTRIBUTES = new HashSet (); 71 IGNORE_ATTRIBUTES.add("attributes"); 72 IGNORE_ATTRIBUTES.add("children"); 73 IGNORE_ATTRIBUTES.add("childCount"); 74 IGNORE_ATTRIBUTES.add("class"); 75 IGNORE_ATTRIBUTES.add("facets"); 76 IGNORE_ATTRIBUTES.add("facetsAndChildren"); 77 IGNORE_ATTRIBUTES.add("parent"); 78 IGNORE_ATTRIBUTES.add("actionListeners"); 79 IGNORE_ATTRIBUTES.add("valueChangeListeners"); 80 IGNORE_ATTRIBUTES.add("validators"); 81 IGNORE_ATTRIBUTES.add("rowData"); 82 IGNORE_ATTRIBUTES.add("javax.faces.webapp.COMPONENT_IDS"); 83 IGNORE_ATTRIBUTES.add("javax.faces.webapp.FACET_NAMES"); 84 IGNORE_ATTRIBUTES.add("javax.faces.webapp.CURRENT_VIEW_ROOT"); 85 } 86 87 private static final String JSF_COMPONENT_PACKAGE = "javax.faces.component."; 88 private static final String MYFACES_COMPONENT_PACKAGE = "org.apache.myfaces.component."; 89 90 91 private DebugUtils() 92 { 93 } 95 96 public static void assertError(boolean condition, Log log_, String msg) 97 throws FacesException 98 { 99 if (!condition) 100 { 101 log_.error(msg); 102 throw new FacesException(msg); 103 } 104 } 105 106 public static void assertFatal(boolean condition, Log log_, String msg) 107 throws FacesException 108 { 109 if (!condition) 110 { 111 log_.fatal(msg); 112 throw new FacesException(msg); 113 } 114 } 115 116 117 118 public static void traceView(String additionalMsg) 119 { 120 if (log.isTraceEnabled()) 121 { 122 FacesContext facesContext = FacesContext.getCurrentInstance(); 123 if (facesContext == null) 124 { 125 log.error("Cannot not print view to console (no FacesContext)."); 126 return; 127 } 128 UIViewRoot viewRoot = facesContext.getViewRoot(); 129 if (viewRoot == null) 130 { 131 log.error("Cannot not print view to console (no ViewRoot in FacesContext)."); 132 return; 133 } 134 135 traceView(additionalMsg, viewRoot); 136 } 137 } 138 139 146 private static void traceView(String additionalMsg, UIViewRoot viewRoot) 147 { 148 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 149 PrintStream ps = new PrintStream (baos); 150 if (additionalMsg != null) 151 { 152 ps.println(additionalMsg); 153 } 154 ps.println("========================================"); 155 printView(viewRoot, ps); 156 ps.println("========================================"); 157 ps.close(); 158 log.trace(baos.toString()); 159 } 160 161 public static void printView(UIViewRoot uiViewRoot, PrintStream stream) 162 { 163 printComponent(uiViewRoot, stream, 0, true, null); 164 } 165 166 public static void printComponent(UIComponent comp, PrintStream stream) 167 { 168 printComponent(comp, stream, 0, false, null); 169 } 170 171 private static void printComponent(UIComponent comp, 172 PrintStream stream, 173 int indent, 174 boolean withChildrenAndFacets, 175 String facetName) 176 { 177 printIndent(stream, indent); 178 stream.print('<'); 179 180 String compType = comp.getClass().getName(); 181 if (compType.startsWith(JSF_COMPONENT_PACKAGE)) 182 { 183 compType = compType.substring(JSF_COMPONENT_PACKAGE.length()); 184 } 185 else if (compType.startsWith(MYFACES_COMPONENT_PACKAGE)) 186 { 187 compType = compType.substring(MYFACES_COMPONENT_PACKAGE.length()); 188 } 189 stream.print(compType); 190 191 printAttribute(stream, "id", comp.getId()); 192 193 if (facetName != null) 194 { 195 printAttribute(stream, "facetName", facetName); 196 } 197 198 for (Iterator it = comp.getAttributes().entrySet().iterator(); it.hasNext(); ) 199 { 200 Map.Entry entry = (Map.Entry )it.next(); 201 if (!"id".equals(entry.getKey())) 202 { 203 printAttribute(stream, (String )entry.getKey(), entry.getValue()); 204 } 205 } 206 207 BeanInfo beanInfo; 211 try 212 { 213 beanInfo = Introspector.getBeanInfo(comp.getClass()); 214 } 215 catch (IntrospectionException e) 216 { 217 throw new RuntimeException (e); 218 } 219 220 PropertyDescriptor propDescriptors[] = beanInfo.getPropertyDescriptors(); 221 for (int i = 0; i < propDescriptors.length; i++) 222 { 223 if (propDescriptors[i].getReadMethod() != null) 224 { 225 String name = propDescriptors[i].getName(); 226 if (!"id".equals(name)) 227 { 228 ValueBinding vb = comp.getValueBinding(name); 229 if (vb != null) 230 { 231 printAttribute(stream, name, vb.getExpressionString()); 232 } 233 else 234 { 235 if (name.equals("value") && comp instanceof ValueHolder) 236 { 237 } 239 else if (!IGNORE_ATTRIBUTES.contains(name)) 240 { 241 try 242 { 243 Object value = comp.getAttributes().get(name); 244 printAttribute(stream, name, value); 245 } 246 catch (Exception e) 247 { 248 log.error(e); 249 printAttribute(stream, name, null); 250 } 251 } 252 } 253 } 254 } 255 } 256 257 boolean mustClose = true; 258 boolean nestedObjects = false; 259 260 if (comp instanceof UICommand) 261 { 262 FacesListener[] listeners = ((UICommand)comp).getActionListeners(); 263 if (listeners != null && listeners.length > 0) 264 { 265 nestedObjects = true; 266 stream.println('>'); mustClose = false; 267 for (int i = 0; i < listeners.length; i++) 268 { 269 FacesListener listener = listeners[i]; 270 printIndent(stream, indent + 1); 271 stream.print('<'); 272 stream.print(listener.getClass().getName()); 273 stream.println("/>"); 274 } 275 } 276 } 277 278 if (comp instanceof UIInput) 279 { 280 FacesListener[] listeners = ((UIInput)comp).getValueChangeListeners(); 281 if (listeners != null && listeners.length > 0) 282 { 283 nestedObjects = true; 284 stream.println('>'); mustClose = false; 285 for (int i = 0; i < listeners.length; i++) 286 { 287 FacesListener listener = listeners[i]; 288 printIndent(stream, indent + 1); 289 stream.print('<'); 290 stream.print(listener.getClass().getName()); 291 stream.println("/>"); 292 } 293 } 294 295 Validator[] validators = ((UIInput)comp).getValidators(); 296 if (validators != null && validators.length > 0) 297 { 298 nestedObjects = true; 299 stream.println('>'); mustClose = false; 300 for (int i = 0; i < validators.length; i++) 301 { 302 Validator validator = validators[i]; 303 printIndent(stream, indent + 1); 304 stream.print('<'); 305 stream.print(validator.getClass().getName()); 306 stream.println("/>"); 307 } 308 } 309 } 310 311 if (withChildrenAndFacets) 312 { 313 int childCount = comp.getChildCount(); 314 Map facetsMap = comp.getFacets(); 315 if (childCount > 0 || !facetsMap.isEmpty()) 316 { 317 nestedObjects = true; 318 if (mustClose) 319 { 320 stream.println('>'); 321 mustClose = false; 322 } 323 324 if (childCount > 0) 325 { 326 for (Iterator it = comp.getChildren().iterator(); it.hasNext(); ) 327 { 328 UIComponent child = (UIComponent)it.next(); 329 printComponent(child, stream, indent + 1, true, null); 330 } 331 } 332 333 for (Iterator it = facetsMap.entrySet().iterator(); it.hasNext(); ) 334 { 335 Map.Entry entry = (Map.Entry )it.next(); 336 printComponent((UIComponent)entry.getValue(), 337 stream, indent + 1, true, 338 (String )entry.getKey()); 339 } 340 } 341 } 342 343 if (nestedObjects) 344 { 345 if (mustClose) 346 { 347 stream.println("/>"); 348 } 349 else 350 { 351 printIndent(stream, indent); 352 stream.print("</"); 353 stream.print(compType); 354 stream.println('>'); 355 } 356 } 357 else 358 { 359 stream.println("/>"); 360 } 361 } 362 363 private static void printAttribute(PrintStream stream, 364 String name, 365 Object value) 366 { 367 if (IGNORE_ATTRIBUTES.contains(name)) return; 368 if (name.startsWith("javax.faces.webapp.UIComponentTag.")) 369 { 370 name = name.substring("javax.faces.webapp.UIComponentTag.".length()); 371 } 372 stream.print(' '); 373 stream.print(name.toString()); 374 stream.print("=\""); 375 if (value != null) 376 { 377 if (value instanceof UIComponent) 378 { 379 stream.print("[id:"); 380 stream.print(((UIComponent)value).getId()); 381 stream.print(']'); 382 } 383 else if (value instanceof MethodBinding) 384 { 385 ((MethodBinding)value).getExpressionString(); 386 } 387 else 388 { 389 stream.print(value.toString()); 390 } 391 } 392 else 393 { 394 stream.print("NULL"); 395 } 396 stream.print('"'); 397 } 398 399 400 private static void printIndent(PrintStream stream, int depth) 401 { 402 for (int i = 0; i < depth; i++) 403 { 404 stream.print(" "); 405 } 406 } 407 408 public static String componentAsString(UIComponent comp) 409 { 410 try 411 { 412 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 413 printComponent(comp, new PrintStream (baos)); 414 baos.close(); 415 return baos.toString(); 416 } 417 catch (IOException e) 418 { 419 throw new RuntimeException (e); 420 } 421 } 422 } 423 | Popular Tags |