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