1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.validator;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.springframework.validation.Errors;
24 import org.springframework.validation.Validator;
25
26 /**
27 * A validator that is compound of aother validators. This validator deligates the validation
28 * task to all its contained validators.
29 *
30 * @author Uri Boness
31 */
32 public class CompoundValidator implements Validator {
33
34
35 private List validators;
36
37 /**
38 * Constructs a new CompoundValidator with no internal validators.
39 */
40 public CompoundValidator() {
41 this(new Validator[0]);
42 }
43
44 /**
45 * Constructs a new CompoundValidator with the given internal validators.
46 *
47 * @param validators The internal validators this validator is compound of.
48 */
49 public CompoundValidator(Validator[] validators) {
50 setValidators(validators);
51 }
52
53 /**
54 * Returns whether this validator supports the given class. In practice this validator
55 * supports the given class only if any of its internal validators support it.
56 *
57 * @param clazz The class to be validated.
58 * @return Whether this validator supports the given class.
59 * @see Validator#supports(Class)
60 */
61 public boolean supports(Class clazz) {
62 for (Iterator i = validators.iterator(); i.hasNext();) {
63 Validator validator = (Validator) i.next();
64 if (validator.supports(clazz)) {
65 return true;
66 }
67 }
68 return false;
69 }
70
71 /**
72 * Validates the given object. All internal validators that support the given object class
73 * will perform the actual validation.
74 *
75 * @param obj The validated object.
76 * @param errors A registry where validation errors are registered.
77 * @see Validator#validate(Object, org.springframework.validation.Errors)
78 */
79 public void validate(Object obj, Errors errors) {
80 for (Iterator i = validators.iterator(); i.hasNext();) {
81 Validator validator = (Validator) i.next();
82 if (validator.supports(obj.getClass())) {
83 validator.validate(obj, errors);
84 }
85 }
86 }
87
88 /**
89 * Adds the given validator to this compound validator.
90 *
91 * @param validator The validator to be added to this compound validator.
92 */
93 public void addValidator(Validator validator) {
94 validators.add(validator);
95 }
96
97 /**
98 * Adds the given validators to this compound validator.
99 *
100 * @param validators The validators to be added to this compound validator.
101 */
102 public void addValidators(Validator[] validators) {
103 for (int i = 0; i < validators.length; i++) {
104 addValidator(validators[i]);
105 }
106 }
107
108 /**
109 * Sets the internal validators of this compound validator.
110 *
111 * @param validators The internal validators of this compound validator.
112 */
113 public void setValidators(Validator[] validators) {
114 List validatorList = new ArrayList(validators.length);
115 for (int i = 0; i < validators.length; i++) {
116 validatorList.add(validators[i]);
117 }
118 this.validators = validatorList;
119 }
120
121
122 }