View Javadoc

1   /*
2    * Copyright 2004-2008 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;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * 入力検証を保持するクラス
23   * @author agata
24   */
25  public class DefaultValidationRules implements ValidationRules {
26  
27  	/**
28  	 * 入力検証ルールのリスト
29  	 */
30  	public final List<ValidationRule> rules = new ArrayList<ValidationRule>();
31  
32  	/**
33  	 * メッセージキーのプリフィックス
34  	 */
35  	private final String resourceKeyPrefix;
36  
37  	/**
38  	 * メッセージキーのプリフィックスなしのコンストラクタ
39  	 */
40  	public DefaultValidationRules() {
41  		this(null);
42  	}
43  
44  	/**
45  	 * メッセージキーのプリフィックス付きのコンストラクタ
46  	 * @param resourceKeyPrefix メッセージキーのプリフィックス
47  	 */
48  	public DefaultValidationRules(final String resourceKeyPrefix) {
49  		this.resourceKeyPrefix = resourceKeyPrefix;
50  		initialize();
51  	}
52  	
53  	/**
54  	 * 初期化メソッド。
55  	 * このメソッドをサブクラスでオーバーライドして各項目の入力検証ルールを追加します。
56  	 */
57  	public void initialize() {
58  	}
59  	
60  	/**
61  	 * 入力検証ルールを追加します。
62  	 * @param rule 入力検証ルール
63  	 */
64  	protected void add(final ValidationRule rule) {
65  		rules.add(rule);
66  	}
67  
68  	/**
69  	 * 入力検証ルールを追加します。
70  	 * 項目名のメッセージキーとしてパラメータ名が使用されます。
71  	 * @param paramName パラメータ名
72  	 * @param validators 入力検証ルールリスト
73  	 */
74  	public void add(final String paramName, final Validator... validators) {
75  		add(paramName, paramName, validators);
76  	}
77  	/**
78  	 * 項目名のメッセージキーを指定して入力検証ルールを追加します。
79  	 * @param paramName パラメータ名
80  	 * @param paramNameMessageKey 項目名のメッセージキー
81  	 * @param validators 入力検証ルールリスト
82  	 */
83  	public void add(final String paramName, final String paramNameMessageKey, final Validator... validators) {
84  		add(new FieldValidationRule(paramName, makePropertyNameKey(paramNameMessageKey), validators));
85  	}
86  
87  	/**
88  	 * メッセージキーを作成します。
89  	 * キーのプリフィックスが指定されていた場合、メッセージキーに付加します。
90  	 * @param messageKey メッセージキー
91  	 * @return 作成後のメッセージキー
92  	 */
93  	private String makePropertyNameKey(final String messageKey) {
94  		if (resourceKeyPrefix == null) {
95  			return messageKey;
96  		} else {
97  			return resourceKeyPrefix + messageKey;
98  		}
99  	}
100 	
101 	/**
102 	 * 入力検証ルールを取得します。
103 	 * @see ValidationRules#getRules()
104 	 */
105 	public List<ValidationRule> getRules() {
106 		return rules;
107 	}
108 
109 }