Coverage Report - org.seasar.cubby.validator.FieldValidationRule
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldValidationRule
96%
27/28
90%
9/10
0
FieldValidationRule$ArrayFieldValidationInvoker
100%
9/9
100%
2/2
0
FieldValidationRule$ScalarFieldValidationInvoker
100%
12/12
100%
4/4
0
FieldValidationRule$ValidationInvoker
N/A
N/A
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.validator;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.seasar.cubby.action.ActionErrors;
 23  
 import org.seasar.cubby.action.FieldInfo;
 24  
 
 25  18
 public class FieldValidationRule implements ValidationRule {
 26  
 
 27  1
         private static final Object[] EMPTY_VALUES = new Object[] { "" };
 28  
 
 29  
         private final String fieldName;
 30  
 
 31  
         private final String fieldNameKey;
 32  
 
 33  18
         private final List<ValidationInvoker> invokers = new ArrayList<ValidationInvoker>();
 34  
 
 35  
         public FieldValidationRule(final String fieldName,
 36  
                         final Validator... validators) {
 37  6
                 this(fieldName, fieldName, validators);
 38  6
         }
 39  
 
 40  
         public FieldValidationRule(final String fieldName,
 41  18
                         final String fieldNameKey, final Validator... validators) {
 42  18
                 this.fieldName = fieldName;
 43  18
                 this.fieldNameKey = fieldNameKey;
 44  46
                 for (final Validator validator : validators) {
 45  28
                         final ValidationInvoker invoker = createInvoker(validator);
 46  28
                         this.invokers.add(invoker);
 47  
                 }
 48  18
         }
 49  
 
 50  
         public void apply(final Map<String, Object[]> params, final Object form,
 51  
                         final ActionErrors errors) {
 52  9
                 final Object[] values = getValues(params, this.fieldName);
 53  9
                 for (final ValidationInvoker invoker : this.invokers) {
 54  12
                         invoker.invoke(values, errors);
 55  
                 }
 56  9
         }
 57  
 
 58  
         private Object[] getValues(final Map<String, Object[]> params,
 59  
                         final String fieldName) {
 60  9
                 final Object[] values = params.get(fieldName);
 61  9
                 if (values != null) {
 62  5
                         return values;
 63  
                 }
 64  4
                 return EMPTY_VALUES;
 65  
         }
 66  
 
 67  
         public String getFieldName() {
 68  4
                 return fieldName;
 69  
         }
 70  
 
 71  
         public String getFieldNameKey() {
 72  4
                 return fieldNameKey;
 73  
         }
 74  
 
 75  
         private ValidationInvoker createInvoker(final Validator validator) {
 76  
                 final ValidationInvoker invoker;
 77  28
                 if (validator instanceof ArrayFieldValidator) {
 78  3
                         invoker = new ArrayFieldValidationInvoker(
 79  
                                         (ArrayFieldValidator) validator);
 80  25
                 } else if (validator instanceof ScalarFieldValidator) {
 81  25
                         invoker = new ScalarFieldValidationInvoker(
 82  
                                         (ScalarFieldValidator) validator);
 83  
                 } else {
 84  0
                         throw new UnsupportedOperationException();
 85  
                 }
 86  28
                 return invoker;
 87  
         }
 88  
 
 89  
         interface ValidationInvoker {
 90  
 
 91  
                 void invoke(Object[] values, ActionErrors errors);
 92  
 
 93  
         }
 94  
 
 95  
         class ArrayFieldValidationInvoker implements ValidationInvoker {
 96  
 
 97  
                 private final ArrayFieldValidator validator;
 98  
 
 99  3
                 public ArrayFieldValidationInvoker(final ArrayFieldValidator validator) {
 100  3
                         this.validator = validator;
 101  3
                 }
 102  
 
 103  
                 public void invoke(final Object[] values, final ActionErrors errors) {
 104  3
                         final ValidationContext context = new ValidationContext();
 105  3
                         final FieldInfo fieldInfo = new FieldInfo(fieldName);
 106  3
                         this.validator.validate(context, values);
 107  3
                         for (MessageInfo message : context.getMessageInfos()) {
 108  1
                                 errors.add(message.builder().fieldNameKey(fieldNameKey)
 109  
                                                 .toString(), fieldInfo);
 110  
                         }
 111  3
                 }
 112  
 
 113  
         }
 114  
 
 115  
         class ScalarFieldValidationInvoker implements ValidationInvoker {
 116  
 
 117  
                 private final ScalarFieldValidator validator;
 118  
 
 119  25
                 public ScalarFieldValidationInvoker(final ScalarFieldValidator validator) {
 120  25
                         this.validator = validator;
 121  25
                 }
 122  
 
 123  
                 public void invoke(final Object[] values, final ActionErrors errors) {
 124  19
                         for (int i = 0; i < values.length; i++) {
 125  10
                                 final ValidationContext context = new ValidationContext();
 126  10
                                 final FieldInfo fieldInfo = new FieldInfo(fieldName, i);
 127  10
                                 this.validator.validate(context, values[i]);
 128  10
                                 for (final MessageInfo messageInfo : context.getMessageInfos()) {
 129  4
                                         final String message = messageInfo.builder().fieldNameKey(
 130  
                                                         fieldNameKey).toString();
 131  4
                                         errors.add(message, fieldInfo);
 132  4
                                 }
 133  
                         }
 134  9
                 }
 135  
 
 136  
         }
 137  
 
 138  
 }