View Javadoc

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  		this(null);
65  	}
66  
67  	/**
68  	 * 日付パターンを指定するコンストラクタ
69  	 * 
70  	 * @param pattern
71  	 *            日付パターン(例:"yyyy/MM/dd")
72  	 */
73  	public DateFormatValidator(final String pattern) {
74  		this(pattern, "valid.dateFormat");
75  	}
76  
77  	/**
78  	 * 日付パターンとエラーメッセージキーを指定したコンストラクタ
79  	 * 
80  	 * @param pattern
81  	 *            日付パターン(例:"yyyy/MM/dd")
82  	 * @param messageKey
83  	 *            エラーメッセージキー
84  	 */
85  	public DateFormatValidator(final String pattern, final String messageKey) {
86  		this.pattern = pattern;
87  		this.messageHelper = new MessageHelper(messageKey);
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 */
93  	public void validate(final ValidationContext context, final Object value) {
94  		if (value == null) {
95  			return;
96  		}
97  		if (value instanceof String) {
98  			final String stringValue = (String) value;
99  			if (StringUtils.isEmpty((String) value)) {
100 				return;
101 			}
102 			try {
103 				final DateFormat dateFormat = createDateFormat(context, value);
104 				final ParsePosition parsePosition = new ParsePosition(0);
105 				final Date date = dateFormat.parse(stringValue, parsePosition);
106 				if (date != null
107 						&& parsePosition.getIndex() == stringValue.length()) {
108 					return;
109 				}
110 			} catch (final Exception e) {
111 			}
112 		}
113 
114 		context.addMessageInfo(this.messageHelper.createMessageInfo());
115 	}
116 
117 	private DateFormat createDateFormat(final ValidationContext context,
118 			final Object value) {
119 		final SimpleDateFormat dateFormat = new SimpleDateFormat();
120 		final String pattern;
121 		if (StringUtils.isEmpty(this.pattern)) {
122 			final Container container = ProviderFactory.get(
123 					ContainerProvider.class).getContainer();
124 			final FormatPattern formatPattern = container
125 					.lookup(FormatPattern.class);
126 			if (formatPattern == null) {
127 				throw new IllegalStateException(format("ECUB0301", this, value));
128 			}
129 			pattern = formatPattern.getDatePattern();
130 		} else {
131 			pattern = this.pattern;
132 		}
133 		dateFormat.applyPattern(pattern);
134 		dateFormat.setLenient(false);
135 		return dateFormat;
136 	}
137 
138 }