Coverage Report - org.apache.myfaces.taglib.core.DelegateValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
DelegateValidator
0%
0/47
0%
0/12
2.444
 
 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.taglib.core;
 20  
 
 21  
 import javax.el.ELContext;
 22  
 import javax.el.ValueExpression;
 23  
 import javax.faces.application.Application;
 24  
 import javax.faces.application.FacesMessage;
 25  
 import javax.faces.component.StateHolder;
 26  
 import javax.faces.component.UIComponent;
 27  
 import javax.faces.context.FacesContext;
 28  
 import javax.faces.validator.Validator;
 29  
 import javax.faces.validator.ValidatorException;
 30  
 import javax.servlet.jsp.JspException;
 31  
 
 32  
 /**
 33  
  * This class is used in conjunction with ValidatorImplTag. 
 34  
  * 
 35  
  * When a tag like this is in a jsp page:
 36  
  * 
 37  
  * <f:validator binding="#{mybean}"/>
 38  
  *  
 39  
  *  or
 40  
  *  
 41  
  * <f:validator validatorId="#{'anyid'}" binding="#{mybean}"/>
 42  
  * 
 43  
  * The value of mybean could be already on the context, so this
 44  
  * converter avoid creating a new variable and use the previous one.
 45  
  *
 46  
  * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
 47  
  * @version $Revision: 600199 $ $Date: 2007-12-01 16:28:15 -0500 (Sat, 01 Dec 2007) $
 48  
  *
 49  
  */
 50  
 public class DelegateValidator implements Validator, StateHolder
 51  
 {
 52  
 
 53  
     private ValueExpression _validatorId;
 54  
     private ValueExpression _binding;
 55  0
     private String _validatorIdString = null;
 56  
     
 57  0
     public DelegateValidator(){
 58  
         
 59  0
     }
 60  
     
 61  
     public DelegateValidator(ValueExpression id, ValueExpression binding, String converterIdString)
 62  
     {
 63  0
         super();
 64  0
         _validatorId = id;
 65  0
         _binding = binding;
 66  0
         _validatorIdString = converterIdString;
 67  0
     }
 68  
 
 69  
     public boolean isTransient()
 70  
     {
 71  0
         return false;
 72  
     }
 73  
 
 74  
     public void restoreState(FacesContext facesContext, Object state)
 75  
     {
 76  0
         Object[] values = (Object[]) state;
 77  0
         _validatorId = (ValueExpression) values[0];
 78  0
         _binding = (ValueExpression) values[1];
 79  0
         _validatorIdString = (String) values[2];
 80  0
     }
 81  
 
 82  
     public Object saveState(FacesContext facesContext)
 83  
     {
 84  0
         Object[] values = new Object[3];
 85  0
         values[0] = _validatorId;
 86  0
         values[1] = _binding;
 87  0
         values[2] = _validatorIdString;
 88  0
         return values;
 89  
     }
 90  
 
 91  
     public void setTransient(boolean arg0)
 92  
     {
 93  
         // Do nothing        
 94  0
     }
 95  
 
 96  
     private Validator _getDelegate()
 97  
     {
 98  0
         return _createValidator();
 99  
     }
 100  
 
 101  
     private Validator _createValidator()
 102  
     {
 103  0
         FacesContext facesContext = FacesContext.getCurrentInstance();
 104  0
         ELContext elContext = facesContext.getELContext();
 105  0
         if (null != _binding)
 106  
         {
 107  
             Object validator;
 108  
             try
 109  
             {
 110  0
                 validator = _binding.getValue(elContext);
 111  0
             } catch (Exception e)
 112  
             {
 113  0
                 throw new ValidatorException(new FacesMessage("Error while creating the Validator"), e);
 114  0
             }
 115  0
             if (validator instanceof Validator)
 116  
             {
 117  0
                 return (Validator) validator;
 118  
             }
 119  
         }
 120  0
         Application application = facesContext.getApplication();
 121  0
         Validator validator = null;
 122  
         try
 123  
         {
 124  
             // first check if an ValidatorId was set by a method
 125  0
             if (null != _validatorIdString)
 126  
             {
 127  0
                 validator = application.createValidator(_validatorIdString);
 128  0
             } else if (null != _validatorId)
 129  
             {
 130  0
                 String validatorId = (String) _validatorId.getValue(elContext);
 131  0
                 validator = application.createValidator(validatorId);
 132  
             }
 133  0
         } catch (Exception e)
 134  
         {
 135  0
             throw new ValidatorException(new FacesMessage("Error while creating the Validator"), e);
 136  0
         }
 137  
 
 138  0
         if (null != validator)
 139  
         {
 140  0
             if (null != _binding)
 141  
             {
 142  0
                 _binding.setValue(elContext, validator);
 143  
             }
 144  0
             return validator;
 145  
         }
 146  0
         throw new ValidatorException(new FacesMessage("validatorId and/or binding must be specified"));
 147  
     }
 148  
 
 149  
     public void validate(FacesContext facesContext, UIComponent component, Object value)
 150  
             throws ValidatorException
 151  
     {
 152  0
         _getDelegate().validate(facesContext, component, value);
 153  0
     }
 154  
 
 155  
 }