Coverage Report - javax.faces.component._ComponentUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
_ComponentUtils
0%
0/103
0%
0/56
0
 
 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 javax.faces.component;
 20  
 
 21  
 import java.util.Iterator;
 22  
 
 23  
 import javax.el.ValueExpression;
 24  
 import javax.faces.application.FacesMessage;
 25  
 import javax.faces.context.FacesContext;
 26  
 import javax.faces.el.EvaluationException;
 27  
 import javax.faces.el.MethodBinding;
 28  
 import javax.faces.el.ValueBinding;
 29  
 import javax.faces.validator.Validator;
 30  
 import javax.faces.validator.ValidatorException;
 31  
 
 32  
 /**
 33  
  * A collection of static helper methods for locating UIComponents.
 34  
  * 
 35  
  * @author Manfred Geiler (latest modification by $Author: skitching $)
 36  
  * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
 37  
  */
 38  
 class _ComponentUtils
 39  
 {
 40  0
     private _ComponentUtils() {}
 41  
 
 42  
     static UIComponent findParentNamingContainer(UIComponent component,
 43  
                                                  boolean returnRootIfNotFound)
 44  
     {
 45  0
         UIComponent parent = component.getParent();
 46  0
         if (returnRootIfNotFound && parent == null)
 47  
         {
 48  0
             return component;
 49  
         }
 50  0
         while (parent != null)
 51  
         {
 52  0
             if (parent instanceof NamingContainer) return parent;
 53  0
             if (returnRootIfNotFound)
 54  
             {
 55  0
                 UIComponent nextParent = parent.getParent();
 56  0
                 if (nextParent == null)
 57  
                 {
 58  0
                     return parent;  //Root
 59  
                 }
 60  0
                 parent = nextParent;
 61  0
             }
 62  
             else
 63  
             {
 64  0
                 parent = parent.getParent();
 65  
             }
 66  
         }
 67  0
         return null;
 68  
     }
 69  
 
 70  
     static UIComponent getRootComponent(UIComponent component)
 71  
     {
 72  
         UIComponent parent;
 73  
         for(;;)
 74  
         {
 75  0
             parent = component.getParent();
 76  0
             if (parent == null) return component;
 77  0
             component = parent;
 78  
         }
 79  
     }
 80  
 
 81  
     /**
 82  
      * Find the component with the specified id starting from the specified
 83  
      * component.
 84  
      * <p>
 85  
      * Param id must not contain any NamingContainer.SEPARATOR_CHAR characters
 86  
      * (ie ":"). This method explicitly does <i>not</i> search into any
 87  
      * child naming container components; this is expected to be handled
 88  
      * by the caller of this method.
 89  
      * <p>
 90  
      * For an implementation of findComponent which does descend into
 91  
      * child naming components, see org.apache.myfaces.custom.util.ComponentUtils.
 92  
      * 
 93  
      * @return findBase, a descendant of findBase, or null.
 94  
      */
 95  
     static UIComponent findComponent(UIComponent findBase, String id)
 96  
     {
 97  0
         if (idsAreEqual(id,findBase))
 98  
         {
 99  0
             return findBase;
 100  
         }
 101  
 
 102  0
         for (Iterator it = findBase.getFacetsAndChildren(); it.hasNext(); )
 103  
         {
 104  0
             UIComponent childOrFacet = (UIComponent)it.next();
 105  0
             if (!(childOrFacet instanceof NamingContainer))
 106  
             {
 107  0
                 UIComponent find = findComponent(childOrFacet, id);
 108  0
                 if (find != null) return find;
 109  0
             }
 110  0
             else if (idsAreEqual(id,childOrFacet))
 111  
             {
 112  0
                 return childOrFacet;
 113  
             }
 114  0
         }
 115  
 
 116  0
         return null;
 117  
     }
 118  
 
 119  
     /*
 120  
      * Return true if the specified component matches the provided id.
 121  
      * This needs some quirks to handle components whose id value gets
 122  
      * dynamically "tweaked", eg a UIData component whose id gets
 123  
      * the current row index appended to it.
 124  
      */
 125  
     private static boolean idsAreEqual(String id, UIComponent cmp)
 126  
     {
 127  0
         if(id.equals(cmp.getId()))
 128  0
             return true;
 129  
 
 130  0
         if(cmp instanceof UIData)
 131  
         {
 132  0
             UIData uiData = ((UIData) cmp);
 133  
 
 134  0
             if(uiData.getRowIndex()==-1)
 135  
             {
 136  0
                 return dynamicIdIsEqual(id,cmp.getId());
 137  
             }
 138  0
             return id.equals(cmp.getId()+NamingContainer.SEPARATOR_CHAR+uiData.getRowIndex());
 139  
         }
 140  
 
 141  0
         return false;
 142  
     }
 143  
 
 144  
     private static boolean dynamicIdIsEqual(String dynamicId, String id)
 145  
     {
 146  0
         return dynamicId.matches(id+":[0-9]*");
 147  
     }
 148  
 
 149  
 
 150  
     static void callValidators(FacesContext context, UIInput input, Object convertedValue)
 151  
     {
 152  
         // first invoke the list of validator components
 153  0
         Validator[] validators = input.getValidators();
 154  0
         for (int i = 0; i < validators.length; i++)
 155  
         {
 156  0
             Validator validator = validators[i];
 157  
             try
 158  
             {
 159  0
                 validator.validate(context, input, convertedValue);
 160  
             }
 161  0
             catch (ValidatorException e)
 162  
             {
 163  0
                 input.setValid(false);
 164  
                 
 165  0
                 String validatorMessage = input.getValidatorMessage();
 166  0
                 if(validatorMessage != null) {
 167  0
                     context.addMessage(input.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR,validatorMessage,validatorMessage));
 168  
                 } else {
 169  0
                     FacesMessage facesMessage = e.getFacesMessage();
 170  0
                     if (facesMessage != null)
 171  
                     {
 172  0
                         facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
 173  0
                         context.addMessage(input.getClientId(context), facesMessage);
 174  
                     }
 175  
                 }
 176  0
             }
 177  
         }
 178  
 
 179  
         // now invoke the validator method defined as a method-binding attribute
 180  
         // on the component
 181  0
         MethodBinding validatorBinding = input.getValidator();
 182  0
         if (validatorBinding != null)
 183  
         {
 184  
             try
 185  
             {
 186  0
                 validatorBinding.invoke(context,
 187  
                                         new Object[] {context, input, convertedValue});
 188  
             }
 189  0
             catch (EvaluationException e)
 190  
             {
 191  0
                 input.setValid(false);
 192  0
                 Throwable cause = e.getCause();
 193  0
                 if (cause instanceof ValidatorException)
 194  
                 {
 195  0
                     String validatorMessage = input.getValidatorMessage();
 196  0
                     if(validatorMessage != null) {
 197  0
                         context.addMessage(input.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR,validatorMessage,validatorMessage));
 198  
                     } 
 199  
                     else {
 200  0
                         FacesMessage facesMessage = ((ValidatorException)cause).getFacesMessage();
 201  0
                         if (facesMessage != null)
 202  
                         {
 203  0
                             facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
 204  0
                             context.addMessage(input.getClientId(context), facesMessage);
 205  
                         }
 206  
                     }
 207  0
                 }
 208  
                 else
 209  
                 {
 210  0
                     throw e;
 211  
                 }
 212  0
             }
 213  
         }
 214  0
     }
 215  
     
 216  
     static String getStringValue(FacesContext context, ValueBinding vb)
 217  
     {
 218  0
         Object value = vb.getValue(context);
 219  0
         if(value == null)
 220  
         {
 221  0
             return null;
 222  
         }
 223  0
         return value.toString();
 224  
     }
 225  
 
 226  
 
 227  
     static <T> T getExpressionValue(UIComponent component, String attribute, T overrideValue, T defaultValue)
 228  
     {
 229  0
         if (overrideValue != null)
 230  
         {
 231  0
             return overrideValue;
 232  
         }
 233  0
         ValueExpression ve = component.getValueExpression(attribute);
 234  0
         if (ve != null)
 235  
         {
 236  0
             return (T) ve.getValue(component.getFacesContext().getELContext());
 237  
         }
 238  0
         return defaultValue;
 239  
     }
 240  
 
 241  
     static String getPathToComponent(UIComponent component) {
 242  0
         StringBuffer buf = new StringBuffer();
 243  
 
 244  0
         if(component == null)
 245  
         {
 246  0
             buf.append("{Component-Path : ");
 247  0
             buf.append("[null]}");
 248  0
             return buf.toString();
 249  
         }
 250  
 
 251  0
         getPathToComponent(component,buf);
 252  
 
 253  0
         buf.insert(0,"{Component-Path : ");
 254  0
         buf.append("}");
 255  
 
 256  0
         return buf.toString();
 257  
     }
 258  
 
 259  
     private static void getPathToComponent(UIComponent component, StringBuffer buf)
 260  
     {
 261  0
         if(component == null)
 262  0
             return;
 263  
 
 264  0
         StringBuffer intBuf = new StringBuffer();
 265  
 
 266  0
         intBuf.append("[Class: ");
 267  0
         intBuf.append(component.getClass().getName());
 268  0
         if(component instanceof UIViewRoot)
 269  
         {
 270  0
             intBuf.append(",ViewId: ");
 271  0
             intBuf.append(((UIViewRoot) component).getViewId());
 272  
         }
 273  
         else
 274  
         {
 275  0
             intBuf.append(",Id: ");
 276  0
             intBuf.append(component.getId());
 277  
         }
 278  0
         intBuf.append("]");
 279  
 
 280  0
         buf.insert(0,intBuf.toString());
 281  
 
 282  0
         getPathToComponent(component.getParent(), buf);
 283  0
     }
 284  
 }