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: ValidationRuleRegexp.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.checkRegexp;
15:
16: public class ValidationRuleRegexp extends PropertyValidationRule {
17: private String mRegexp = null;
18:
19: public ValidationRuleRegexp(String propertyName, String regexp) {
20: super (propertyName);
21:
22: mRegexp = regexp;
23: }
24:
25: public boolean validate() {
26: Object value;
27: try {
28: value = BeanUtils.getPropertyValue(getBean(),
29: getPropertyName());
30: } catch (BeanUtilsException e) {
31: // an error occurred when obtaining the value of the property
32: // just consider it valid to skip over it
33: return true;
34: }
35:
36: if (null == value) {
37: return true;
38: }
39:
40: ConstrainedProperty constrained_property = ConstrainedUtils
41: .getConstrainedProperty(getBean(), getPropertyName());
42: if (value.getClass().isArray()) {
43: int length = Array.getLength(value);
44: for (int i = 0; i < length; i++) {
45: if (!checkRegexp(BeanUtils.formatPropertyValue(Array
46: .get(value, i), constrained_property), mRegexp)) {
47: return false;
48: }
49: }
50:
51: return true;
52: } else {
53: return checkRegexp(BeanUtils.formatPropertyValue(value,
54: constrained_property), mRegexp);
55: }
56: }
57:
58: public ValidationError getError() {
59: return new ValidationError.INVALID(getSubject());
60: }
61: }
|