View Javadoc

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  		this(null);
79  	}
80  
81  	/**
82  	 * 日付パターンを指定するコンストラクタ
83  	 * 
84  	 * @param pattern
85  	 *            日付パターン(例:"yyyy/MM/dd")
86  	 */
87  	public DateFormatValidator(final String pattern) {
88  		this(pattern, "valid.dateFormat");
89  	}
90  
91  	/**
92  	 * 日付パターンとエラーメッセージキーを指定したコンストラクタ
93  	 * 
94  	 * @param pattern
95  	 *            日付パターン(例:"yyyy/MM/dd")
96  	 * @param messageKey
97  	 *            エラーメッセージキー
98  	 */
99  	public DateFormatValidator(final String pattern, final String messageKey) {
100 		this.pattern = pattern;
101 		this.messageKey = messageKey;
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 */
107 	public void validate(final ValidationContext context, final Object value) {
108 		if (value == null) {
109 			return;
110 		}
111 		if (value instanceof String) {
112 			final String stringValue = (String) value;
113 			if (StringUtils.isEmpty((String) value)) {
114 				return;
115 			}
116 			try {
117 				final DateFormat dateFormat = createDateFormat(context, value);
118 				final ParsePosition parsePosition = new ParsePosition(0);
119 				final Date date = dateFormat.parse(stringValue, parsePosition);
120 				if (date != null
121 						&& parsePosition.getIndex() == stringValue.length()) {
122 					return;
123 				}
124 			} catch (final Exception e) {
125 			}
126 		}
127 
128 		final MessageInfo messageInfo = new MessageInfo();
129 		messageInfo.setKey(this.messageKey);
130 		context.addMessageInfo(messageInfo);
131 	}
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 		final SimpleDateFormat dateFormat = new SimpleDateFormat();
145 		final String pattern;
146 		if (StringUtils.isEmpty(this.pattern)) {
147 			final Container container = ProviderFactory.get(
148 					ContainerProvider.class).getContainer();
149 			final FormatPattern formatPattern = container
150 					.lookup(FormatPattern.class);
151 			if (formatPattern == null) {
152 				throw new IllegalStateException(format("ECUB0301", this, value));
153 			}
154 			pattern = formatPattern.getDatePattern();
155 		} else {
156 			pattern = this.pattern;
157 		}
158 		dateFormat.applyPattern(pattern);
159 		dateFormat.setLenient(false);
160 		return dateFormat;
161 	}
162 
163 }