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