1 | |
package org.seasar.cubby.validator.validators; |
2 | |
|
3 | |
import org.seasar.cubby.validator.ValidationContext; |
4 | |
import org.seasar.cubby.validator.Validator; |
5 | |
import org.seasar.framework.exception.EmptyRuntimeException; |
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
public class ArrayValidator implements Validator { |
12 | |
|
13 | |
private final Validator[] validators; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | 1 | public ArrayValidator(final Validator... validators) { |
20 | 1 | if (validators == null) { |
21 | |
throw new EmptyRuntimeException("validators"); |
22 | |
} |
23 | 1 | this.validators = validators.clone(); |
24 | 1 | } |
25 | |
|
26 | |
public String validate(final ValidationContext ctx) { |
27 | 7 | final Object value = ctx.getValue(); |
28 | 7 | if (value == null || !value.getClass().isArray()) { |
29 | 7 | return validateAll(ctx); |
30 | |
} |
31 | |
Object[] values = (Object[])value; |
32 | |
for (Object currentValue : values) { |
33 | |
ValidationContext currentCtx = new ValidationContext(ctx.getName(), |
34 | |
currentValue, ctx.getParams(), ctx.getFormatPattern()); |
35 | |
String error = validateAll(currentCtx); |
36 | |
if (error != null) { |
37 | |
return error; |
38 | |
} |
39 | |
} |
40 | |
return null; |
41 | |
} |
42 | |
|
43 | |
private String validateAll(final ValidationContext ctx) { |
44 | 11 | for (Validator v : validators) { |
45 | 7 | String error = v.validate(ctx); |
46 | 7 | if (error != null) { |
47 | 3 | return error; |
48 | |
} |
49 | |
} |
50 | 4 | return null; |
51 | |
} |
52 | |
} |