View Javadoc

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