Coverage Report - org.seasar.cubby.validator.FieldValidationRule
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldValidationRule
96%
26/27
90%
9/10
0
FieldValidationRule$ArrayFieldValidationInvoker
100%
11/11
100%
2/2
0
FieldValidationRule$ScalarFieldValidationInvoker
100%
12/12
100%
4/4
0
FieldValidationRule$ValidationInvoker
N/A
N/A
0
 
 1  
 /*
 2  
  * Copyright 2004-2009 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  
 import org.seasar.cubby.action.MessageInfo;
 25  
 
 26  
 /**
 27  
  * 入力フォームのフィールドに対する入力検証のルールです。
 28  
  * 
 29  
  * @author baba
 30  
  * @since 1.0.0
 31  
  */
 32  
 public class FieldValidationRule implements ValidationRule {
 33  
 
 34  
         /** 空のオブジェクト配列。 */
 35  3
         private static final Object[] EMPTY_VALUES = new Object[] { "" };
 36  
 
 37  
         /** この入力検証ルールが対応する入力フォームのフィールド名。 */
 38  
         private final String fieldName;
 39  
 
 40  
         /** リソースバンドルからフィールド名を取得するためのキー。 */
 41  
         private final String fieldNameKey;
 42  
 
 43  
         /** 入力検証を実行するクラスのリスト。 */
 44  48
         private final List<ValidationInvoker> invokers = new ArrayList<ValidationInvoker>();
 45  
 
 46  
         /**
 47  
          * 指定されたフィールド名に対する入力検証ルールを生成します。
 48  
          * 
 49  
          * @param fieldName
 50  
          *            フィールド名
 51  
          * @param validators
 52  
          *            入力検証
 53  
          */
 54  
         public FieldValidationRule(final String fieldName,
 55  
                         final Validator... validators) {
 56  9
                 this(fieldName, fieldName, validators);
 57  9
         }
 58  
 
 59  
         /**
 60  
          * 指定されたフィールド名に対する入力検証ルールを生成します。
 61  
          * 
 62  
          * @param fieldName
 63  
          *            フィールド名
 64  
          * @param fieldNameKey
 65  
          *            リソースバンドルからフィールド名を取得するためのキー
 66  
          * @param validators
 67  
          *            入力検証
 68  
          */
 69  
         public FieldValidationRule(final String fieldName,
 70  48
                         final String fieldNameKey, final Validator... validators) {
 71  48
                 this.fieldName = fieldName;
 72  48
                 this.fieldNameKey = fieldNameKey;
 73  138
                 for (final Validator validator : validators) {
 74  90
                         final ValidationInvoker invoker = createInvoker(validator);
 75  90
                         this.invokers.add(invoker);
 76  
                 }
 77  48
         }
 78  
 
 79  
         /**
 80  
          * {@inheritDoc}
 81  
          * <p>
 82  
          * 対応するフィールドに対してこのオブジェクトが保持する入力検証を順次実行します。
 83  
          * </p>
 84  
          */
 85  
         public void apply(final Map<String, Object[]> params, final Object form,
 86  
                         final ActionErrors errors) {
 87  9
                 final Object[] values = getValues(params, this.fieldName);
 88  9
                 for (final ValidationInvoker invoker : this.invokers) {
 89  18
                         invoker.invoke(this, values, errors);
 90  
                 }
 91  9
         }
 92  
 
 93  
         /**
 94  
          * リクエストパラメータの{@link Map}から指定されたフィールド名に対する値を取得します。
 95  
          * 
 96  
          * @param params
 97  
          *            リクエストパラメータの{@link Map}
 98  
          * @param fieldName
 99  
          *            フィールド名
 100  
          * @return フィールド名に対する値
 101  
          */
 102  
         private Object[] getValues(final Map<String, Object[]> params,
 103  
                         final String fieldName) {
 104  9
                 final Object[] values = params.get(fieldName);
 105  9
                 if (values != null) {
 106  6
                         return values;
 107  
                 }
 108  3
                 return EMPTY_VALUES;
 109  
         }
 110  
 
 111  
         /**
 112  
          * この入力検証ルールが対応する入力フォームのフィールド名を取得します。
 113  
          * 
 114  
          * @return この入力検証ルールが対応する入力フォームのフィールド名
 115  
          */
 116  
         public String getFieldName() {
 117  33
                 return fieldName;
 118  
         }
 119  
 
 120  
         /**
 121  
          * リソースバンドルからフィールド名を取得するためのキーを取得します。
 122  
          * 
 123  
          * @return リソースバンドルからフィールド名を取得するためのキー
 124  
          */
 125  
         public String getFieldNameKey() {
 126  18
                 return fieldNameKey;
 127  
         }
 128  
 
 129  
         /**
 130  
          * 入力検証を呼び出すクラスのインスタンスを生成します。
 131  
          * 
 132  
          * @param validator
 133  
          *            入力検証
 134  
          * @return 入力検証を呼び出すクラスのインスタンス
 135  
          */
 136  
         private ValidationInvoker createInvoker(final Validator validator) {
 137  
                 final ValidationInvoker invoker;
 138  90
                 if (validator instanceof ArrayFieldValidator) {
 139  9
                         invoker = new ArrayFieldValidationInvoker(
 140  
                                         (ArrayFieldValidator) validator);
 141  81
                 } else if (validator instanceof ScalarFieldValidator) {
 142  81
                         invoker = new ScalarFieldValidationInvoker(
 143  
                                         (ScalarFieldValidator) validator);
 144  
                 } else {
 145  0
                         throw new UnsupportedOperationException();
 146  
                 }
 147  90
                 return invoker;
 148  
         }
 149  
 
 150  
         /**
 151  
          * 入力検証を呼び出すためのクラスです。
 152  
          * 
 153  
          * @author baba
 154  
          * @since 1.0.0
 155  
          */
 156  
         private interface ValidationInvoker {
 157  
 
 158  
                 /**
 159  
                  * 入力検証を呼び出します。
 160  
                  * 
 161  
                  * @param validationRule
 162  
                  *            入力検証ルール
 163  
                  * @param values
 164  
                  *            入力検証を行う値
 165  
                  * @param errors
 166  
                  *            アクションで発生したエラー
 167  
                  */
 168  
                 void invoke(FieldValidationRule validationRule, Object[] values,
 169  
                                 ActionErrors errors);
 170  
 
 171  
         }
 172  
 
 173  
         /**
 174  
          * {@link ArrayFieldValidator}の入力検証を呼び出すためのクラスです。
 175  
          * 
 176  
          * @author baba
 177  
          * @since 1.0.0
 178  
          */
 179  
         private static class ArrayFieldValidationInvoker implements
 180  
                         ValidationInvoker {
 181  
 
 182  
                 /**
 183  
                  * {@link #invoke(FieldValidationRule, Object[], ActionErrors)}
 184  
                  * で呼び出す入力検証。
 185  
                  */
 186  
                 private final ArrayFieldValidator validator;
 187  
 
 188  
                 /**
 189  
                  * インスタンス化します。
 190  
                  * 
 191  
                  * @param validator
 192  
                  *            入力検証
 193  
                  */
 194  9
                 public ArrayFieldValidationInvoker(final ArrayFieldValidator validator) {
 195  9
                         this.validator = validator;
 196  9
                 }
 197  
 
 198  
                 /**
 199  
                  * {@inheritDoc}
 200  
                  */
 201  
                 public void invoke(final FieldValidationRule validationRule,
 202  
                                 final Object[] values, final ActionErrors errors) {
 203  9
                         final ValidationContext context = new ValidationContext();
 204  9
                         final FieldInfo fieldInfo = new FieldInfo(validationRule
 205  
                                         .getFieldName());
 206  9
                         this.validator.validate(context, values);
 207  9
                         for (final MessageInfo messageInfo : context.getMessageInfos()) {
 208  3
                                 final String message = messageInfo.toMessage(validationRule
 209  
                                                 .getFieldNameKey());
 210  3
                                 errors.add(message, fieldInfo);
 211  3
                         }
 212  9
                 }
 213  
 
 214  
         }
 215  
 
 216  
         /**
 217  
          * {@link ScalarFieldValidator}の入力検証を呼び出すためのクラスです。
 218  
          * 
 219  
          * @author baba
 220  
          * @since 1.0.0
 221  
          */
 222  
         private static class ScalarFieldValidationInvoker implements
 223  
                         ValidationInvoker {
 224  
 
 225  
                 /**
 226  
                  * {@link #invoke(FieldValidationRule, Object[], ActionErrors)}
 227  
                  * で呼び出す入力検証。
 228  
                  */
 229  
                 private final ScalarFieldValidator validator;
 230  
 
 231  
                 /**
 232  
                  * インスタンス化します。
 233  
                  * 
 234  
                  * @param validator
 235  
                  *            入力検証
 236  
                  */
 237  81
                 public ScalarFieldValidationInvoker(final ScalarFieldValidator validator) {
 238  81
                         this.validator = validator;
 239  81
                 }
 240  
 
 241  
                 /**
 242  
                  * {@inheritDoc}
 243  
                  */
 244  
                 public void invoke(final FieldValidationRule validationRule,
 245  
                                 final Object[] values, final ActionErrors errors) {
 246  21
                         for (int i = 0; i < values.length; i++) {
 247  12
                                 final ValidationContext context = new ValidationContext();
 248  12
                                 final FieldInfo fieldInfo = new FieldInfo(validationRule
 249  
                                                 .getFieldName(), i);
 250  12
                                 this.validator.validate(context, values[i]);
 251  12
                                 for (final MessageInfo messageInfo : context.getMessageInfos()) {
 252  3
                                         final String message = messageInfo.toMessage(validationRule
 253  
                                                         .getFieldNameKey());
 254  3
                                         errors.add(message, fieldInfo);
 255  3
                                 }
 256  
                         }
 257  9
                 }
 258  
 
 259  
         }
 260  
 
 261  
 }