Coverage Report - org.apache.tapestry.form.validator.ValidatorFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidatorFactoryImpl
98% 
100% 
8
 
 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.validator;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.HiveMind;
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.hivemind.util.PropertyUtils;
 21  
 import org.apache.tapestry.IComponent;
 22  
 import org.apache.tapestry.util.RegexpMatch;
 23  
 import org.apache.tapestry.util.RegexpMatcher;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collections;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 
 30  
 /**
 31  
  * Implementation of the tapestry.form.validator.ValidatorFactory service, which builds and caches
 32  
  * validators and lists of validators from a "magic" string specification.
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  16
 public class ValidatorFactoryImpl implements ValidatorFactory
 38  
 {
 39  
     private static final String PATTERN = "^\\s*(\\$?\\w+)\\s*(=\\s*(((?!,|\\[).)*))?";
 40  
 
 41  
     /**
 42  
      * Injected map of validator names to ValidatorContribution.
 43  
      */
 44  
 
 45  
     private Map _validators;
 46  
 
 47  
     public List constructValidatorList(IComponent component, String specification)
 48  
     {
 49  16
         Defense.notNull(component, "component");
 50  
 
 51  16
         if (HiveMind.isBlank(specification))
 52  1
             return Collections.EMPTY_LIST;
 53  
 
 54  15
         List result = new ArrayList();
 55  15
         String chopped = specification;
 56  
 
 57  15
         RegexpMatcher matcher = new RegexpMatcher();
 58  
 
 59  
         while (true)
 60  
         {
 61  25
             if (chopped.length() == 0)
 62  5
                 break;
 63  
 
 64  20
             if (!result.isEmpty())
 65  
             {
 66  5
                 if (chopped.charAt(0) != ',')
 67  0
                     throw new ApplicationRuntimeException(ValidatorMessages
 68  
                             .badSpecification(specification));
 69  
 
 70  5
                 chopped = chopped.substring(1);
 71  
             }
 72  
 
 73  20
             RegexpMatch[] matches = matcher.getMatches(PATTERN, chopped);
 74  
 
 75  20
             if (matches.length != 1)
 76  1
                 throw new ApplicationRuntimeException(ValidatorMessages.badSpecification(specification));
 77  
 
 78  19
             RegexpMatch match = matches[0];
 79  
 
 80  19
             String name = match.getGroup(1);
 81  19
             String value = match.getGroup(3);
 82  19
             String message = null;
 83  
 
 84  19
             int length = match.getMatchLength();
 85  
 
 86  19
             if (chopped.length() > length)
 87  
             {
 88  11
                 char lastChar = chopped.charAt(length);
 89  11
                 if (lastChar == ',')
 90  3
                     length--;
 91  8
                 else if (lastChar == '[')
 92  
                 {
 93  8
                     int messageClose = chopped.indexOf(']', length);
 94  8
                     message = chopped.substring(length + 1, messageClose);
 95  8
                     length = messageClose;
 96  
                 }
 97  
             }
 98  
 
 99  19
             Validator validator = buildValidator(component, name, value, message);
 100  
 
 101  13
             result.add(validator);
 102  
 
 103  13
             if (length >= chopped.length())
 104  3
                 break;
 105  
 
 106  10
             chopped = chopped.substring(length + 1);
 107  
 
 108  10
         }
 109  
 
 110  8
         return Collections.unmodifiableList(result);
 111  
     }
 112  
 
 113  
     private Validator buildValidator(IComponent component, String name, String value, String message)
 114  
     {
 115  19
         if (name.startsWith("$"))
 116  4
             return extractValidatorBean(component, name, value, message);
 117  
 
 118  15
         ValidatorContribution vc = (ValidatorContribution) _validators.get(name);
 119  
 
 120  15
         if (vc == null)
 121  1
             throw new ApplicationRuntimeException(ValidatorMessages.unknownValidator(name));
 122  
 
 123  14
         if (value == null && vc.isConfigurable())
 124  1
             throw new ApplicationRuntimeException(ValidatorMessages.needsConfiguration("name"));
 125  
 
 126  13
         if (value != null && !vc.isConfigurable())
 127  1
             throw new ApplicationRuntimeException(ValidatorMessages.notConfigurable(name, value));
 128  
 
 129  
         try
 130  
         {
 131  12
             Object result = vc.getValidatorClass().newInstance();
 132  
 
 133  12
             if (vc.isConfigurable())
 134  6
                 PropertyUtils.smartWrite(result, name, value);
 135  
 
 136  12
             if (message != null)
 137  7
                 PropertyUtils.write(result, "message", message);
 138  
 
 139  12
             return (Validator) result;
 140  
         }
 141  1
         catch (Exception ex)
 142  
         {
 143  1
             throw new ApplicationRuntimeException(ValidatorMessages.errorInitializingValidator(
 144  
                     name,
 145  
                     vc.getValidatorClass(),
 146  
                     ex), ex);
 147  
         }
 148  
     }
 149  
 
 150  
     private Validator extractValidatorBean(IComponent component, String validatorName,
 151  
             String value, String message)
 152  
     {
 153  4
         String beanName = validatorName.substring(1);
 154  
 
 155  4
         if (HiveMind.isNonBlank(value) || HiveMind.isNonBlank(message))
 156  2
             throw new ApplicationRuntimeException(ValidatorMessages
 157  
                     .noValueOrMessageForBean(beanName));
 158  
 
 159  2
         return new BeanValidatorWrapper(component, beanName);
 160  
     }
 161  
 
 162  
     public void setValidators(Map validators)
 163  
     {
 164  15
         _validators = validators;
 165  15
     }
 166  
 }