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