View Javadoc

1   package org.seasar.cubby.validator;
2   
3   import java.text.MessageFormat;
4   import java.util.MissingResourceException;
5   
6   import org.seasar.cubby.util.Messages;
7   
8   /**
9    * 入力検証の基底クラスです。
10   * エラーメッセージのリソースからの取得・作成をサポートします。
11   * @author agata
12   */
13  abstract public class BaseValidator implements Validator {
14  	
15  	/**
16  	 * エラーメッセージのキー
17  	 */
18  	private String messageKey;
19  
20  	/**
21  	 * エラーメッセージのキーをセットします。
22  	 * @param messageKey エラーメッセージのキー
23  	 */
24  	protected void setMessageKey(final String messageKey) {
25  		this.messageKey = messageKey;
26  	}
27  
28  	/**
29  	 * エラーメッセージのキーを元に、メッセージを作成して取得します。
30  	 * @param args 置換文字列
31  	 * @return 置換後のエラーメッセージ
32  	 */
33  	protected String getMessage(final Object... args) {
34  		String message = Messages.getText(messageKey);
35  		return MessageFormat.format(message, args);
36  	}
37  
38  	/**
39  	 * 項目名をメッセージリソースから取得します。
40  	 * @param key 項目名のメッセージキー
41  	 * @return 項目名の文字列
42  	 */
43  	protected String getPropertyMessage(final String key) {
44  		try {
45  			return Messages.getText(key);
46  		} catch (MissingResourceException ex) {
47  			return key;
48  		}
49  	}
50  
51  }