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 org.seasar.cubby.action.MessageInfo;
20  import org.seasar.cubby.internal.util.StringUtils;
21  import org.seasar.cubby.validator.ScalarFieldValidator;
22  import org.seasar.cubby.validator.ValidationContext;
23  
24  /**
25   * 文字列の長さの範囲を検証します。
26   * <p>
27   * {@link String#length()} メソッドで文字列の長さを求めます。文字列のバイト数でないこと、半角全角も 1
28   * 文字としてカウントされることに注意してください。
29   * </p>
30   * <p>
31   * <table>
32   * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody>
33   * <tr>
34   * <th scope="row">デフォルトのキー</th>
35   * <td>valid.rangeLength</td>
36   * </tr>
37   * <tr>
38   * <th scope="row">置換文字列</th>
39   * <td>
40   * <ol start="0">
41   * <li>フィールド名</li>
42   * <li>このオブジェクトに設定された文字列の最小長</li>
43   * <li>このオブジェクトに設定された文字列の最大長</li>
44   * </ol></td>
45   * </tr>
46   * </tbody>
47   * </table>
48   * </p>
49   * 
50   * @author agata
51   * @author baba
52   */
53  public class RangeLengthValidator implements ScalarFieldValidator {
54  
55  	/**
56  	 * メッセージキー。
57  	 */
58  	private final String messageKey;
59  
60  	/**
61  	 * 最小文字数
62  	 */
63  	private final int min;
64  
65  	/**
66  	 * 最大文字数
67  	 */
68  	private final int max;
69  
70  	/**
71  	 * コンストラクタ
72  	 * 
73  	 * @param min
74  	 *            最小文字数
75  	 * @param max
76  	 *            最大文字数
77  	 */
78  	public RangeLengthValidator(final int min, final int max) {
79  		this(min, max, "valid.rangeLength");
80  	}
81  
82  	/**
83  	 * エラーメッセージキーを指定するコンストラクタ
84  	 * 
85  	 * @param min
86  	 *            最小文字数
87  	 * @param max
88  	 *            最大文字数
89  	 * @param messageKey
90  	 *            エラーメッセージキー
91  	 */
92  	public RangeLengthValidator(final int min, final int max,
93  			final String messageKey) {
94  		this.min = min;
95  		this.max = max;
96  		this.messageKey = messageKey;
97  	}
98  
99  	/**
100 	 * {@inheritDoc}
101 	 */
102 	public void validate(final ValidationContext context, final Object value) {
103 		if (value instanceof String) {
104 			final String str = (String) value;
105 			if (StringUtils.isEmpty(str)) {
106 				return;
107 			}
108 
109 			final int length = str.length();
110 			if (length >= min && length <= max) {
111 				return;
112 			}
113 		} else if (value == null) {
114 			return;
115 		}
116 		final MessageInfo messageInfo = new MessageInfo();
117 		messageInfo.setKey(this.messageKey);
118 		messageInfo.setArguments(min, max);
119 		context.addMessageInfo(messageInfo);
120 	}
121 
122 }