Coverage Report - org.seasar.cubby.validator.ConversionValidationRule
 
Classes in this File Line Coverage Branch Coverage Complexity
ConversionValidationRule
0%
0/18
0%
0/8
2.333
 
 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 static org.seasar.cubby.CubbyConstants.ATTR_CONVERSION_FAILURES;
 19  
 
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 
 25  
 import org.seasar.cubby.action.ActionErrors;
 26  
 import org.seasar.cubby.action.FieldInfo;
 27  
 import org.seasar.cubby.action.MessageInfo;
 28  
 import org.seasar.cubby.internal.controller.ConversionFailure;
 29  
 import org.seasar.cubby.internal.controller.ThreadContext;
 30  
 import org.seasar.cubby.internal.util.RequestUtils;
 31  
 
 32  
 /**
 33  
  * 要求パラメータをフォームオブジェクトへのバインドする時の型変換エラーを検証する入力検証ルールです。
 34  
  * 
 35  
  * @author baba
 36  
  */
 37  
 public class ConversionValidationRule implements ValidationRule {
 38  
 
 39  
         /** リソースのキープレフィックス。 */
 40  
         private final String resourceKeyPrefix;
 41  
 
 42  
         /**
 43  
          * キーのプレフィックスなしでインスタンス化します。
 44  
          */
 45  
         public ConversionValidationRule() {
 46  0
                 this(null);
 47  0
         }
 48  
 
 49  
         /**
 50  
          * 指定されたプレフィックスをフィールド名のキーのプレフィックスとしてインスタンス化します。
 51  
          * 
 52  
          * @param resourceKeyPrefix
 53  
          *            リソースのキープレフィックス
 54  
          */
 55  0
         public ConversionValidationRule(final String resourceKeyPrefix) {
 56  0
                 this.resourceKeyPrefix = resourceKeyPrefix;
 57  0
         }
 58  
 
 59  
         /**
 60  
          * {@inheritDoc}
 61  
          */
 62  
         public void apply(final Map<String, Object[]> params, final Object form,
 63  
                         final ActionErrors errors) throws ValidationException {
 64  0
                 final HttpServletRequest request = ThreadContext.getRequest();
 65  0
                 final List<ConversionFailure> conversionFailures = RequestUtils
 66  
                                 .getAttribute(request, ATTR_CONVERSION_FAILURES);
 67  0
                 if (conversionFailures != null && !conversionFailures.isEmpty()) {
 68  0
                         for (final ConversionFailure conversionFailure : conversionFailures) {
 69  0
                                 final MessageInfo messageInfo = conversionFailure
 70  
                                                 .getMessageInfo();
 71  
                                 final String fieldNameKey;
 72  0
                                 if (resourceKeyPrefix == null) {
 73  0
                                         fieldNameKey = conversionFailure.getFieldName();
 74  
                                 } else {
 75  0
                                         fieldNameKey = resourceKeyPrefix
 76  
                                                         + conversionFailure.getFieldName();
 77  
                                 }
 78  0
                                 final String message = messageInfo.toMessage(fieldNameKey);
 79  0
                                 final FieldInfo[] fieldInfos = conversionFailure
 80  
                                                 .getFieldInfos();
 81  0
                                 errors.add(message, fieldInfos);
 82  0
                         }
 83  
                 }
 84  0
         }
 85  
 
 86  
 }