01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ValidationRuleLimitedLength.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.site;
09:
10: import com.uwyn.rife.tools.BeanUtils;
11: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
12: import java.lang.reflect.Array;
13:
14: import static com.uwyn.rife.site.ValidityChecks.checkLength;
15:
16: public class ValidationRuleLimitedLength extends PropertyValidationRule {
17: private int mMin = -1;
18: private int mMax = -1;
19:
20: public ValidationRuleLimitedLength(String propertyName, int min,
21: int max) {
22: super (propertyName);
23:
24: mMin = min;
25: mMax = max;
26: }
27:
28: public boolean validate() {
29: Object value;
30: try {
31: value = BeanUtils.getPropertyValue(getBean(),
32: getPropertyName());
33: } catch (BeanUtilsException e) {
34: // an error occurred when obtaining the value of the property
35: // just consider it valid to skip over it
36: return true;
37: }
38:
39: if (null == value) {
40: return true;
41: }
42:
43: ConstrainedProperty constrained_property = ConstrainedUtils
44: .getConstrainedProperty(getBean(), getPropertyName());
45: if (value.getClass().isArray()) {
46: int length = Array.getLength(value);
47: for (int i = 0; i < length; i++) {
48: if (!checkLength(BeanUtils.formatPropertyValue(Array
49: .get(value, i), constrained_property), mMin,
50: mMax)) {
51: return false;
52: }
53: }
54:
55: return true;
56: } else {
57: return checkLength(BeanUtils.formatPropertyValue(value,
58: constrained_property), mMin, mMax);
59: }
60: }
61:
62: public ValidationError getError() {
63: return new ValidationError.WRONGLENGTH(getSubject());
64: }
65: }
|