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