View Javadoc

1   /*
2    * $Id: ValidatorRegistry.java,v 1.6 2004/04/09 18:44:53 eelco12 Exp $
3    * $Revision: 1.6 $
4    * $Date: 2004/04/09 18:44:53 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31   
32  package nl.openedge.baritus;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import nl.openedge.baritus.util.MultiHashMap;
38  import nl.openedge.baritus.validation.FieldValidator;
39  import nl.openedge.baritus.validation.FormValidator;
40  import nl.openedge.baritus.validation.ValidationActivationRule;
41  
42  /***
43   * Registry for populators. Each instance of FormBeanBase has its own instance.
44   * 
45   * @author Eelco Hillenius
46   */
47  public final class ValidatorRegistry
48  {
49  
50  
51  	private MultiHashMap fieldValidators = null;
52  	
53  	private List formValidators = null;
54  	
55  	private List globalValidatorActivationRules = null;
56  
57  	/***
58  	 * register a field validator for the given fieldName. 
59  	 * multiple fieldValidators for one key are allowed.
60  	 * @param fieldName name of field
61  	 * @param validator validator instance
62  	 */
63  	public void addValidator(String fieldName, FieldValidator validator)
64  	{
65  		if(fieldValidators == null)
66  		{
67  			fieldValidators = new MultiHashMap();
68  		}
69  		fieldValidators.put(fieldName, validator);
70  	}
71  	
72  	/***
73  	 * register a form validator.
74  	 * @param validator the form level validator
75  	 */
76  	public void addValidator(FormValidator validator)
77  	{
78  		if(formValidators == null)
79  		{
80  			formValidators = new ArrayList();
81  		}
82  		formValidators.add(validator);
83  	}
84  	
85  	/***
86  	 * de-register the fieldValidators that were registered with the given fieldName
87  	 * @param fieldName name of field
88  	 */
89  	public void removeValidators(String fieldName)
90  	{
91  		if(fieldValidators != null)
92  		{
93  			fieldValidators.remove(fieldName);
94  		}
95  	}
96  	
97  	/***
98  	 * de-register the given validator that was registered with the given fieldName
99  	 * @param fieldName name of field
100 	 * @param validator the validator to remove for the given field
101 	 */
102 	public void removeValidator(String fieldName, FieldValidator validator)
103 	{
104 		if(fieldValidators != null)
105 		{
106 			fieldValidators.remove(fieldName, validator);
107 		}
108 	}
109 	
110 	/***
111 	 * de-register the given form level validator
112 	 * @param validator form validator
113 	 */
114 	public void removeValidator(FormValidator validator)
115 	{
116 		if(formValidators != null)
117 		{
118 			formValidators.remove(validator);
119 		}
120 	}
121 	
122 	/***
123 	 * register the rule for the whole form
124 	 * @param rule validator activation rule
125 	 */
126 	public void addValidationActivationRule(ValidationActivationRule rule)
127 	{
128 		if(globalValidatorActivationRules == null)
129 		{
130 			globalValidatorActivationRules = new ArrayList();
131 		}
132 		globalValidatorActivationRules.add(rule);
133 	}
134 	
135 	/***
136 	 * de-register the given rule for the whole form
137 	 * @param rule validator activation rule
138 	 */
139 	public void removeValidationActivationRule(ValidationActivationRule rule)
140 	{
141 		if(globalValidatorActivationRules != null)
142 		{
143 			globalValidatorActivationRules.remove(rule);
144 			if(globalValidatorActivationRules.isEmpty()) globalValidatorActivationRules = null;
145 		}
146 	}
147 	
148 	/***
149 	 * get the fieldValidators that were registered with the given fieldName
150 	 * @param fieldName name of the field
151 	 * @return FieldValidator the instance of FieldValidator that was registered with the given 
152 	 * 		fieldName or null if none was registered with that name
153 	 */
154 	public MultiHashMap getValidators(String fieldName)
155 	{
156 		return (fieldValidators != null) ? 
157 			(MultiHashMap)fieldValidators.get(fieldName) : null;
158 	}
159 
160 	/***
161 	 * get all global activation rules
162 	 * @return List all global activation rules
163 	 */
164 	public List getGlobalValidatorActivationRules()
165 	{
166 		return globalValidatorActivationRules;
167 	}
168 
169 	/***
170 	 * get all form validators
171 	 * @return List all form validators
172 	 */
173 	public List getFormValidators()
174 	{
175 		return formValidators;
176 	}
177 
178 	/***
179 	 * get all field validators
180 	 * @return get all field validators
181 	 */
182 	public MultiHashMap getFieldValidators()
183 	{
184 		return fieldValidators;
185 	}
186 
187 }