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  
  */
 45  
 public class DateFormatValidator implements ScalarFieldValidator {
 46  
 
 47  
         /**
 48  
          * メッセージヘルパ。
 49  
          */
 50  
         private final MessageHelper messageHelper;
 51  
 
 52  
         /**
 53  
          * 日付パターン
 54  
          */
 55  
         private final String pattern;
 56  
 
 57  
         /**
 58  
          * 日付パターンを指定しないコンストラクタ
 59  
          */
 60  
         public DateFormatValidator() {
 61  0
                 this(null);
 62  0
         }
 63  
 
 64  
         /**
 65  
          * 日付パターンを指定するコンストラクタ
 66  
          * 
 67  
          * @param pattern
 68  
          *            日付パターン(例:"yyyy/MM/dd")
 69  
          */
 70  
         public DateFormatValidator(final String pattern) {
 71  1
                 this(pattern, "valid.dateFormat");
 72  1
         }
 73  
 
 74  
         /**
 75  
          * 日付パターンとエラーメッセージキーを指定したコンストラクタ
 76  
          * 
 77  
          * @param pattern
 78  
          *            日付パターン(例:"yyyy/MM/dd")
 79  
          * @param messageKey
 80  
          *            エラーメッセージキー
 81  
          */
 82  1
         public DateFormatValidator(final String pattern, final String messageKey) {
 83  1
                 this.pattern = pattern;
 84  1
                 this.messageHelper = new MessageHelper(messageKey);
 85  1
         }
 86  
 
 87  
         public void validate(final ValidationContext context, final Object value) {
 88  4
                 if (value == null) {
 89  0
                         return;
 90  
                 }
 91  4
                 if (value instanceof String) {
 92  4
                         final String stringValue = (String) value;
 93  4
                         if (StringUtil.isEmpty((String) value)) {
 94  0
                                 return;
 95  
                         }
 96  
                         try {
 97  4
                                 final DateFormat dateFormat = createDateFormat(context, value);
 98  4
                                 final ParsePosition parsePosition = new ParsePosition(0);
 99  4
                                 final Date date = dateFormat.parse(stringValue, parsePosition);
 100  4
                                 if (date != null
 101  
                                                 && parsePosition.getIndex() == stringValue.length()) {
 102  1
                                         return;
 103  
                                 }
 104  0
                         } catch (final Exception e) {
 105  3
                         }
 106  
                 }
 107  
 
 108  3
                 context.addMessageInfo(this.messageHelper.createMessageInfo());
 109  3
         }
 110  
 
 111  
         private DateFormat createDateFormat(final ValidationContext context,
 112  
                         final Object value) {
 113  4
                 final SimpleDateFormat dateFormat = new SimpleDateFormat();
 114  
                 final String pattern;
 115  4
                 if (StringUtil.isEmpty(this.pattern)) {
 116  0
                         final CubbyConfiguration configuration = ThreadContext
 117  
                                         .getConfiguration();
 118  0
                         final FormatPattern formatPattern = configuration
 119  
                                         .getFormatPattern();
 120  0
                         if (formatPattern == null) {
 121  0
                                 throw new SRuntimeException("ECUB0301", new Object[] { this,
 122  
                                                 value });
 123  
                         }
 124  0
                         pattern = formatPattern.getDatePattern();
 125  0
                 } else {
 126  4
                         pattern = this.pattern;
 127  
                 }
 128  4
                 dateFormat.applyPattern(pattern);
 129  4
                 dateFormat.setLenient(false);
 130  4
                 return dateFormat;
 131  
         }
 132  
 
 133  
 }