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.validator.ScalarFieldValidator;
20  import org.seasar.cubby.validator.ValidationContext;
21  
22  /**
23   * 指定された文字列と等しいかどうかを検証します。
24   * <p>
25   * <table>
26   * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody>
27   * <tr>
28   * <th scope="row">デフォルトのキー</th>
29   * <td>valid.equals</td>
30   * </tr>
31   * <tr>
32   * <th scope="row">置換文字列</th>
33   * <td>
34   * <ol start="0">
35   * <li>フィールド名</li>
36   * <li>このオブジェクトに設定された比較対象の文字列</li>
37   * </ol></td>
38   * </tr>
39   * </tbody>
40   * </table>
41   * </p>
42   * 
43   * @see String#equals(Object)
44   * @author agata
45   * @author baba
46   */
47  public class EqualsValidator implements ScalarFieldValidator {
48  
49  	/**
50  	 * メッセージキー。
51  	 */
52  	private final String messageKey;
53  
54  	/**
55  	 * 対象文字列
56  	 */
57  	private final String value;
58  
59  	/**
60  	 * コンストラクタ
61  	 * 
62  	 * @param value
63  	 *            比較対象の文字列
64  	 */
65  	public EqualsValidator(final String value) {
66  		this(value, "valid.equals");
67  	}
68  
69  	/**
70  	 * エラーメッセージキーを指定するコンストラクタ
71  	 * 
72  	 * @param value
73  	 * @param messageKey
74  	 */
75  	public EqualsValidator(final String value, final String messageKey) {
76  		this.value = value;
77  		this.messageKey = messageKey;
78  	}
79  
80  	/**
81  	 * {@inheritDoc}
82  	 */
83  	public void validate(final ValidationContext context, final Object value) {
84  		if (this.value.equals(value)) {
85  			return;
86  		}
87  
88  		final MessageInfo messageInfo = new MessageInfo();
89  		messageInfo.setKey(this.messageKey);
90  		messageInfo.setArguments(this.value);
91  		context.addMessageInfo(messageInfo);
92  	}
93  
94  }