Coverage Report - org.seasar.cubby.validator.validators.DateFormatValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
DateFormatValidator
69%
24/35
57%
8/14
3.2
 
 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.validators;
 17  
 
 18  
 import java.text.DateFormat;
 19  
 import java.text.ParsePosition;
 20  
 import java.text.SimpleDateFormat;
 21  
 import java.util.Date;
 22  
 
 23  
 import org.seasar.cubby.action.FormatPattern;
 24  
 import org.seasar.cubby.controller.CubbyConfiguration;
 25  
 import org.seasar.cubby.controller.ThreadContext;
 26  
 import org.seasar.cubby.validator.MessageHelper;
 27  
 import org.seasar.cubby.validator.ScalarFieldValidator;
 28  
 import org.seasar.cubby.validator.ValidationContext;
 29  
 import org.seasar.framework.exception.SRuntimeException;
 30  
 import org.seasar.framework.util.StringUtil;
 31  
 
 32  
 /**
 33  
  * 日付に対する検証を行います。
 34  
  * <p>
 35  
  * 日付パターンを指定しない場合、「app-cubby.dicon」で指定した日付パターンが使用されます。
 36  
  * </p>
 37  
  * <p>
 38  
  * デフォルトエラーメッセージキー:valid.dateFormat
 39  
  * </p>
 40  
  * 
 41  
  * @author agata
 42  
  * @author baba
 43  
  * @see SimpleDateFormat
 44  
  * @since 1.0.0
 45  
  */
 46  
 public class DateFormatValidator implements ScalarFieldValidator {
 47  
 
 48  
         /**
 49  
          * メッセージヘルパ。
 50  
          */
 51  
         private final MessageHelper messageHelper;
 52  
 
 53  
         /**
 54  
          * 日付パターン
 55  
          */
 56  
         private final String pattern;
 57  
 
 58  
         /**
 59  
          * 日付パターンを指定しないコンストラクタ
 60  
          */
 61  
         public DateFormatValidator() {
 62  0
                 this(null);
 63  0
         }
 64  
 
 65  
         /**
 66  
          * 日付パターンを指定するコンストラクタ
 67  
          * 
 68  
          * @param pattern
 69  
          *            日付パターン(例:"yyyy/MM/dd")
 70  
          */
 71  
         public DateFormatValidator(final String pattern) {
 72  1
                 this(pattern, "valid.dateFormat");
 73  1
         }
 74  
 
 75  
         /**
 76  
          * 日付パターンとエラーメッセージキーを指定したコンストラクタ
 77  
          * 
 78  
          * @param pattern
 79  
          *            日付パターン(例:"yyyy/MM/dd")
 80  
          * @param messageKey
 81  
          *            エラーメッセージキー
 82  
          */
 83  1
         public DateFormatValidator(final String pattern, final String messageKey) {
 84  1
                 this.pattern = pattern;
 85  1
                 this.messageHelper = new MessageHelper(messageKey);
 86  1
         }
 87  
 
 88  
         /**
 89  
          * {@inheritDoc}
 90  
          */
 91  
         public void validate(final ValidationContext context, final Object value) {
 92  4
                 if (value == null) {
 93  0
                         return;
 94  
                 }
 95  4
                 if (value instanceof String) {
 96  4
                         final String stringValue = (String) value;
 97  4
                         if (StringUtil.isEmpty((String) value)) {
 98  0
                                 return;
 99  
                         }
 100  
                         try {
 101  4
                                 final DateFormat dateFormat = createDateFormat(context, value);
 102  4
                                 final ParsePosition parsePosition = new ParsePosition(0);
 103  4
                                 final Date date = dateFormat.parse(stringValue, parsePosition);
 104  4
                                 if (date != null
 105  
                                                 && parsePosition.getIndex() == stringValue.length()) {
 106  1
                                         return;
 107  
                                 }
 108  0
                         } catch (final Exception e) {
 109  3
                         }
 110  
                 }
 111  
 
 112  3
                 context.addMessageInfo(this.messageHelper.createMessageInfo());
 113  3
         }
 114  
 
 115  
         private DateFormat createDateFormat(final ValidationContext context,
 116  
                         final Object value) {
 117  4
                 final SimpleDateFormat dateFormat = new SimpleDateFormat();
 118  
                 final String pattern;
 119  4
                 if (StringUtil.isEmpty(this.pattern)) {
 120  0
                         final CubbyConfiguration configuration = ThreadContext
 121  
                                         .getConfiguration();
 122  0
                         final FormatPattern formatPattern = configuration
 123  
                                         .getFormatPattern();
 124  0
                         if (formatPattern == null) {
 125  0
                                 throw new SRuntimeException("ECUB0301", new Object[] { this,
 126  
                                                 value });
 127  
                         }
 128  0
                         pattern = formatPattern.getDatePattern();
 129  0
                 } else {
 130  4
                         pattern = this.pattern;
 131  
                 }
 132  4
                 dateFormat.applyPattern(pattern);
 133  4
                 dateFormat.setLenient(false);
 134  4
                 return dateFormat;
 135  
         }
 136  
 
 137  
 }