1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 }