Coverage Report - org.apache.tapestry5.internal.bindings.ValidateBindingFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidateBindingFactory
100%
10/10
100%
2/2
1.667
ValidateBindingFactory$1
100%
2/2
N/A
1.667
 
 1  
 // Copyright 2006, 2008, 2009 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.tapestry5.internal.bindings;
 16  
 
 17  
 import org.apache.tapestry5.Binding;
 18  
 import org.apache.tapestry5.ComponentResources;
 19  
 import org.apache.tapestry5.Field;
 20  
 import org.apache.tapestry5.FieldValidator;
 21  
 import org.apache.tapestry5.internal.services.StringInterner;
 22  
 import org.apache.tapestry5.ioc.Location;
 23  
 import org.apache.tapestry5.ioc.internal.util.TapestryException;
 24  
 import org.apache.tapestry5.services.BindingFactory;
 25  
 import org.apache.tapestry5.services.FieldValidatorSource;
 26  
 
 27  
 /**
 28  
  * Factory for bindings that provide a {@link org.apache.tapestry5.FieldValidator} based on a validator specification.
 29  
  * This binding factory is only useable with components that implement the {@link org.apache.tapestry5.Field}
 30  
  * interface.
 31  
  */
 32  16
 public class ValidateBindingFactory implements BindingFactory
 33  
 {
 34  
     private final FieldValidatorSource fieldValidatorSource;
 35  
 
 36  
     private final StringInterner interner;
 37  
 
 38  
     public ValidateBindingFactory(FieldValidatorSource fieldValidatorSource, StringInterner interner)
 39  6
     {
 40  6
         this.fieldValidatorSource = fieldValidatorSource;
 41  6
         this.interner = interner;
 42  6
     }
 43  
 
 44  
     public Binding newBinding(String description, ComponentResources container,
 45  
                               ComponentResources component, final String expression, Location location)
 46  
     {
 47  18
         Object fieldAsObject = component.getComponent();
 48  
 
 49  18
         if (!Field.class.isInstance(fieldAsObject))
 50  2
             throw new TapestryException(BindingsMessages.validateBindingForFieldsOnly(component),
 51  
                                         location, null);
 52  
 
 53  16
         final Field field = (Field) fieldAsObject;
 54  
 
 55  16
         return new InvariantBinding(location, FieldValidator.class, interner.intern(description + ": " + expression))
 56  
         {
 57  16
             public Object get()
 58  
             {
 59  
                 // The expression is a validator specification, such as "required,minLength=5".
 60  
                 // ValidatorBindingFactory is the odd man out becasuse it needs the binding component (the
 61  
                 // component whose parameter is to be bound) rather than the containing component, the way
 62  
                 // most factories work.
 63  
 
 64  16
                 return fieldValidatorSource.createValidators(field, expression);
 65  
             }
 66  
         };
 67  
     }
 68  
 }