Coverage Report - org.apache.myfaces.util.DebugUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DebugUtils
0%
0/176
0%
0/90
6.091
 
 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  
 package org.apache.myfaces.util;
 20  
 
 21  
 import java.beans.BeanInfo;
 22  
 import java.beans.IntrospectionException;
 23  
 import java.beans.Introspector;
 24  
 import java.beans.PropertyDescriptor;
 25  
 import java.io.ByteArrayOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.PrintStream;
 28  
 import java.util.HashSet;
 29  
 import java.util.Map;
 30  
 import java.util.logging.Level;
 31  
 import java.util.logging.Logger;
 32  
 
 33  
 import javax.el.MethodExpression;
 34  
 import javax.el.ValueExpression;
 35  
 import javax.faces.FacesException;
 36  
 import javax.faces.component.UICommand;
 37  
 import javax.faces.component.UIComponent;
 38  
 import javax.faces.component.UIInput;
 39  
 import javax.faces.component.UIViewRoot;
 40  
 import javax.faces.component.ValueHolder;
 41  
 import javax.faces.context.FacesContext;
 42  
 import javax.faces.event.FacesListener;
 43  
 import javax.faces.validator.Validator;
 44  
 
 45  
 /**
 46  
  * Utilities for logging.
 47  
  * 
 48  
  * @author Manfred Geiler (latest modification by $Author$)
 49  
  * @version $Revision$ $Date$
 50  
  */
 51  
 public class DebugUtils
 52  
 {
 53  
     //private static final Log log = LogFactory.getLog(DebugUtils.class);
 54  0
     private static final Logger log = Logger.getLogger(DebugUtils.class.getName());
 55  
 
 56  
     // Attributes that should not be printed
 57  
     private static final HashSet<String> IGNORE_ATTRIBUTES;
 58  
     static
 59  
     {
 60  0
         IGNORE_ATTRIBUTES = new HashSet<String>();
 61  0
         IGNORE_ATTRIBUTES.add("attributes");
 62  0
         IGNORE_ATTRIBUTES.add("children");
 63  0
         IGNORE_ATTRIBUTES.add("childCount");
 64  0
         IGNORE_ATTRIBUTES.add("class");
 65  0
         IGNORE_ATTRIBUTES.add("facets");
 66  0
         IGNORE_ATTRIBUTES.add("facetsAndChildren");
 67  0
         IGNORE_ATTRIBUTES.add("parent");
 68  0
         IGNORE_ATTRIBUTES.add("actionListeners");
 69  0
         IGNORE_ATTRIBUTES.add("valueChangeListeners");
 70  0
         IGNORE_ATTRIBUTES.add("validators");
 71  0
         IGNORE_ATTRIBUTES.add("rowData");
 72  0
         IGNORE_ATTRIBUTES.add("javax.faces.webapp.COMPONENT_IDS");
 73  0
         IGNORE_ATTRIBUTES.add("javax.faces.webapp.FACET_NAMES");
 74  0
         IGNORE_ATTRIBUTES.add("javax.faces.webapp.CURRENT_VIEW_ROOT");
 75  0
     }
 76  
 
 77  
     private static final String JSF_COMPONENT_PACKAGE = "javax.faces.component.";
 78  
     private static final String MYFACES_COMPONENT_PACKAGE = "org.apache.myfaces.component.";
 79  
 
 80  
     private DebugUtils()
 81  0
     {
 82  
         // hide from public access
 83  0
     }
 84  
 
 85  
     public static void assertError(boolean condition, Logger logger, String msg) throws FacesException
 86  
     {
 87  0
         if (!condition)
 88  
         {
 89  0
             logger.severe(msg);
 90  0
             throw new FacesException(msg);
 91  
         }
 92  0
     }
 93  
 
 94  
     public static void assertFatal(boolean condition, Logger logger, String msg) throws FacesException
 95  
     {
 96  0
         if (!condition)
 97  
         {
 98  0
             logger.severe(msg);
 99  0
             throw new FacesException(msg);
 100  
         }
 101  0
     }
 102  
 
 103  
     public static void traceView(String additionalMsg)
 104  
     {
 105  0
         if (log.isLoggable(Level.FINEST))
 106  
         {
 107  0
             FacesContext facesContext = FacesContext.getCurrentInstance();
 108  0
             if (facesContext == null)
 109  
             {
 110  0
                 log.severe("Cannot not print view to console (no FacesContext).");
 111  0
                 return;
 112  
             }
 113  0
             UIViewRoot viewRoot = facesContext.getViewRoot();
 114  0
             if (viewRoot == null)
 115  
             {
 116  0
                 log.severe("Cannot not print view to console (no ViewRoot in FacesContext).");
 117  0
                 return;
 118  
             }
 119  
 
 120  0
             traceView(additionalMsg, viewRoot);
 121  
         }
 122  0
     }
 123  
 
 124  
     /**
 125  
      * Be careful, when using this version of traceView: Some component properties (e.g. getRenderer) assume, that there
 126  
      * is a valid viewRoot set in the FacesContext!
 127  
      * 
 128  
      * @param additionalMsg
 129  
      * @param viewRoot
 130  
      */
 131  
     private static void traceView(String additionalMsg, UIViewRoot viewRoot)
 132  
     {
 133  0
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 134  0
         PrintStream ps = new PrintStream(baos);
 135  0
         if (additionalMsg != null)
 136  
         {
 137  0
             ps.println(additionalMsg);
 138  
         }
 139  0
         ps.println("========================================");
 140  0
         printView(viewRoot, ps);
 141  0
         ps.println("========================================");
 142  0
         ps.close();
 143  0
         log.finest(baos.toString());
 144  0
     }
 145  
 
 146  
     public static void printView(UIViewRoot uiViewRoot, PrintStream stream)
 147  
     {
 148  0
         printComponent(uiViewRoot, stream, 0, true, null);
 149  0
     }
 150  
 
 151  
     public static void printComponent(UIComponent comp, PrintStream stream)
 152  
     {
 153  0
         printComponent(comp, stream, 0, false, null);
 154  0
     }
 155  
 
 156  
     private static void printComponent(UIComponent comp, PrintStream stream, int indent, boolean withChildrenAndFacets,
 157  
                                        String facetName)
 158  
     {
 159  0
         printIndent(stream, indent);
 160  0
         stream.print('<');
 161  
 
 162  0
         String compType = comp.getClass().getName();
 163  0
         if (compType.startsWith(JSF_COMPONENT_PACKAGE))
 164  
         {
 165  0
             compType = compType.substring(JSF_COMPONENT_PACKAGE.length());
 166  
         }
 167  0
         else if (compType.startsWith(MYFACES_COMPONENT_PACKAGE))
 168  
         {
 169  0
             compType = compType.substring(MYFACES_COMPONENT_PACKAGE.length());
 170  
         }
 171  0
         stream.print(compType);
 172  
         
 173  0
         printAttribute(stream, "id", comp.getId());
 174  
 
 175  0
         if (facetName != null)
 176  
         {
 177  0
             printAttribute(stream, "facetName", facetName);
 178  
         }
 179  
         
 180  0
         for (Map.Entry<String, Object> entry : comp.getAttributes().entrySet())
 181  
         {
 182  0
             if (!"id".equals(entry.getKey()))
 183  
             {
 184  0
                 printAttribute(stream, entry.getKey(), entry.getValue());
 185  
             }
 186  0
         }
 187  
         
 188  
         // HACK: comp.getAttributes() only returns attributes, that are NOT backed
 189  
         // by a corresponding component property. So, we must explicitly get the
 190  
         // available properties by Introspection:
 191  
         BeanInfo beanInfo;
 192  
         try
 193  
         {
 194  0
             beanInfo = Introspector.getBeanInfo(comp.getClass());
 195  
         }
 196  0
         catch (IntrospectionException e)
 197  
         {
 198  0
             throw new RuntimeException(e);
 199  0
         }
 200  
 
 201  0
         if (!compType.startsWith("org.apache.myfaces.view.facelets.compiler"))
 202  
         {
 203  0
             PropertyDescriptor propDescriptors[] = beanInfo.getPropertyDescriptors();
 204  0
             for (int i = 0; i < propDescriptors.length; i++)
 205  
             {
 206  0
                 if (propDescriptors[i].getReadMethod() != null)
 207  
                 {
 208  0
                     String name = propDescriptors[i].getName();
 209  0
                     if (!"id".equals(name))
 210  
                     {
 211  0
                         ValueExpression ve = comp.getValueExpression(name);
 212  0
                         if (ve != null)
 213  
                         {
 214  0
                             printAttribute(stream, name, ve.getExpressionString());
 215  
                         }
 216  
                         else
 217  
                         {
 218  0
                             if (name.equals("value") && comp instanceof ValueHolder)
 219  
                             {
 220  
                                 // -> localValue
 221  
                             }
 222  0
                             else if (!IGNORE_ATTRIBUTES.contains(name))
 223  
                             {
 224  
                                 try
 225  
                                 {
 226  0
                                     Object value = comp.getAttributes().get(name);
 227  0
                                     printAttribute(stream, name, value);
 228  
                                 }
 229  0
                                 catch (Exception e)
 230  
                                 {
 231  0
                                     log.log(Level.SEVERE, e.getMessage() , e);
 232  0
                                     printAttribute(stream, name, null);
 233  0
                                 }
 234  
                             }
 235  
                         }
 236  
                     }
 237  
                 }
 238  
             }
 239  
         }
 240  
 
 241  0
         boolean mustClose = true;
 242  0
         boolean nestedObjects = false;
 243  
 
 244  0
         if (comp instanceof UICommand)
 245  
         {
 246  0
             FacesListener[] listeners = ((UICommand)comp).getActionListeners();
 247  0
             if (listeners != null && listeners.length > 0)
 248  
             {
 249  0
                 nestedObjects = true;
 250  0
                 stream.println('>');
 251  0
                 mustClose = false;
 252  0
                 for (int i = 0; i < listeners.length; i++)
 253  
                 {
 254  0
                     FacesListener listener = listeners[i];
 255  0
                     printIndent(stream, indent + 1);
 256  0
                     stream.print('<');
 257  0
                     stream.print(listener.getClass().getName());
 258  0
                     stream.println("/>");
 259  
                 }
 260  
             }
 261  
         }
 262  
 
 263  0
         if (comp instanceof UIInput)
 264  
         {
 265  0
             FacesListener[] listeners = ((UIInput)comp).getValueChangeListeners();
 266  0
             if (listeners != null && listeners.length > 0)
 267  
             {
 268  0
                 nestedObjects = true;
 269  0
                 stream.println('>');
 270  0
                 mustClose = false;
 271  0
                 for (int i = 0; i < listeners.length; i++)
 272  
                 {
 273  0
                     FacesListener listener = listeners[i];
 274  0
                     printIndent(stream, indent + 1);
 275  0
                     stream.print('<');
 276  0
                     stream.print(listener.getClass().getName());
 277  0
                     stream.println("/>");
 278  
                 }
 279  
             }
 280  
 
 281  0
             Validator[] validators = ((UIInput)comp).getValidators();
 282  0
             if (validators != null && validators.length > 0)
 283  
             {
 284  0
                 nestedObjects = true;
 285  0
                 stream.println('>');
 286  0
                 mustClose = false;
 287  0
                 for (int i = 0; i < validators.length; i++)
 288  
                 {
 289  0
                     Validator validator = validators[i];
 290  0
                     printIndent(stream, indent + 1);
 291  0
                     stream.print('<');
 292  0
                     stream.print(validator.getClass().getName());
 293  0
                     stream.println("/>");
 294  
                 }
 295  
             }
 296  
         }
 297  
 
 298  0
         if (withChildrenAndFacets)
 299  
         {
 300  0
             int childCount = comp.getChildCount();
 301  0
             if (childCount > 0 || comp.getFacetCount() > 0)
 302  
             {
 303  0
                 Map<String, UIComponent> facetsMap = comp.getFacets();
 304  0
                 nestedObjects = true;
 305  0
                 if (mustClose)
 306  
                 {
 307  0
                     stream.println('>');
 308  0
                     mustClose = false;
 309  
                 }
 310  
 
 311  0
                 if (childCount > 0)
 312  
                 {
 313  0
                     for (int i = 0; i < childCount; i++)
 314  
                     {
 315  0
                         UIComponent child = comp.getChildren().get(i);
 316  0
                         printComponent(child, stream, indent + 1, true, null);
 317  
                     }
 318  
                 }
 319  
 
 320  0
                 for (Map.Entry<String, UIComponent> entry : facetsMap.entrySet())
 321  
                 {
 322  0
                     printComponent(entry.getValue(), stream, indent + 1, true, entry.getKey());
 323  0
                 }
 324  
             }
 325  
         }
 326  
 
 327  0
         if (nestedObjects)
 328  
         {
 329  0
             if (mustClose)
 330  
             {
 331  0
                 stream.println("/>");
 332  
             }
 333  
             else
 334  
             {
 335  0
                 printIndent(stream, indent);
 336  0
                 stream.print("</");
 337  0
                 stream.print(compType);
 338  0
                 stream.println('>');
 339  
             }
 340  
         }
 341  
         else
 342  
         {
 343  0
             stream.println("/>");
 344  
         }
 345  0
     }
 346  
 
 347  
     private static void printAttribute(PrintStream stream, String name, Object value)
 348  
     {
 349  0
         if (IGNORE_ATTRIBUTES.contains(name))
 350  
         {
 351  0
             return;
 352  
         }
 353  0
         if (name.startsWith("javax.faces.webapp.UIComponentTag."))
 354  
         {
 355  0
             name = name.substring("javax.faces.webapp.UIComponentTag.".length());
 356  
         }
 357  0
         stream.print(' ');
 358  0
         stream.print(name);
 359  0
         stream.print("=\"");
 360  0
         if (value != null)
 361  
         {
 362  0
             if (value instanceof UIComponent)
 363  
             {
 364  0
                 stream.print("[id:");
 365  0
                 stream.print(((UIComponent)value).getId());
 366  0
                 stream.print(']');
 367  
             }
 368  0
             else if (value instanceof MethodExpression)
 369  
             {
 370  0
                 stream.print(((MethodExpression)value).getExpressionString());
 371  
             }
 372  
             else
 373  
             {
 374  0
                 stream.print(value.toString());
 375  
             }
 376  
         }
 377  
         else
 378  
         {
 379  0
             stream.print("NULL");
 380  
         }
 381  0
         stream.print('"');
 382  0
     }
 383  
 
 384  
     private static void printIndent(PrintStream stream, int depth)
 385  
     {
 386  0
         for (int i = 0; i < depth; i++)
 387  
         {
 388  0
             stream.print("  ");
 389  
         }
 390  0
     }
 391  
 
 392  
     public static String componentAsString(UIComponent comp)
 393  
     {
 394  
         try
 395  
         {
 396  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 397  0
             printComponent(comp, new PrintStream(baos));
 398  0
             baos.close();
 399  0
             return baos.toString();
 400  
         }
 401  0
         catch (IOException e)
 402  
         {
 403  0
             throw new RuntimeException(e);
 404  
         }
 405  
     }
 406  
 }