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