Coverage Report - org.apache.tapestry.form.ValidatableFieldSupportImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidatableFieldSupportImpl
0%
0/35
0%
0/26
2.833
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.form;
 16  
 
 17  
 import java.util.Iterator;
 18  
 
 19  
 import org.apache.hivemind.service.ThreadLocale;
 20  
 import org.apache.tapestry.IMarkupWriter;
 21  
 import org.apache.tapestry.IRequestCycle;
 22  
 import org.apache.tapestry.coerce.ValueConverter;
 23  
 import org.apache.tapestry.form.validator.Validator;
 24  
 import org.apache.tapestry.valid.ValidatorException;
 25  
 
 26  
 /**
 27  
  * Default {@link ValidatableFieldSupport} implementation. This implementation generates calls to a
 28  
  * static javascript function during render if client-side validation is enabled.
 29  
  * 
 30  
  * @author Paul Ferraro
 31  
  * @since 4.0
 32  
  */
 33  0
 public class ValidatableFieldSupportImpl implements ValidatableFieldSupport
 34  
 {
 35  
     private ValueConverter _converter;
 36  
 
 37  
     private ThreadLocale _threadLocale;
 38  
 
 39  
     public void setValueConverter(ValueConverter converter)
 40  
     {
 41  0
         _converter = converter;
 42  0
     }
 43  
 
 44  
     public void setThreadLocale(ThreadLocale threadLocale)
 45  
     {
 46  0
         _threadLocale = threadLocale;
 47  0
     }
 48  
 
 49  
     protected Iterator getValidatorsIterator(ValidatableField component)
 50  
     {
 51  0
         return (Iterator) _converter.coerceValue(component.getValidators(), Iterator.class);
 52  
     }
 53  
 
 54  
     /**
 55  
      * @see org.apache.tapestry.form.ValidatableFieldSupport#renderContributions(ValidatableField, IMarkupWriter, IRequestCycle)
 56  
      */
 57  
     public void renderContributions(ValidatableField component, IMarkupWriter writer,
 58  
             IRequestCycle cycle)
 59  
     {
 60  0
         ValidatableFieldExtension extension = null;
 61  0
         if (ValidatableFieldExtension.class.isInstance(component))
 62  0
             extension = (ValidatableFieldExtension)component;
 63  
         
 64  0
         if (component.getForm().isClientValidationEnabled())
 65  
         {
 66  0
             FormComponentContributorContext context = new FormComponentContributorContextImpl(
 67  
                     _threadLocale.getLocale(), cycle, component);
 68  
 
 69  0
             Iterator validators = getValidatorsIterator(component);
 70  
 
 71  0
             while (validators.hasNext())
 72  
             {
 73  0
                 Validator validator = (Validator) validators.next();
 74  
                 
 75  0
                 if (extension != null && extension.overrideValidator(validator, cycle))
 76  0
                     extension.overrideContributions(validator, context, writer, cycle);
 77  
                 else
 78  0
                     validator.renderContribution(writer, cycle, context, component);
 79  0
             }
 80  
         }
 81  0
     }
 82  
 
 83  
     /**
 84  
      * @see org.apache.tapestry.form.ValidatableFieldSupport#validate(org.apache.tapestry.form.ValidatableField, org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, java.lang.Object)
 85  
      */
 86  
     public void validate(ValidatableField component, IMarkupWriter writer, IRequestCycle cycle, Object object) throws ValidatorException
 87  
     {
 88  0
         boolean isNonNull = (object != null);
 89  
 
 90  0
         Iterator validators = getValidatorsIterator(component);
 91  
 
 92  0
         ValidationMessages messages = new ValidationMessagesImpl(component, _threadLocale.getLocale());
 93  
 
 94  0
         while (validators.hasNext())
 95  
         {
 96  0
             Validator validator = (Validator) validators.next();
 97  
 
 98  0
             if (isNonNull || validator.getAcceptsNull())
 99  0
                 validator.validate(component, messages, object);
 100  0
         }
 101  0
     }
 102  
 
 103  
     public boolean isRequired(ValidatableField field)
 104  
     {
 105  0
         Iterator i = getValidatorsIterator(field);
 106  
 
 107  0
         while (i.hasNext())
 108  
         {
 109  0
             Validator validator = (Validator) i.next();
 110  
 
 111  0
             if (validator.isRequired())
 112  0
                 return true;
 113  0
         }
 114  
 
 115  0
         return false;
 116  
     }
 117  
 }