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