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: ValidationRuleEmail.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.site;
09:
10: import static com.uwyn.rife.site.ValidityChecks.checkEmail;
11:
12: import java.lang.reflect.Array;
13:
14: import com.uwyn.rife.tools.BeanUtils;
15: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
16:
17: public class ValidationRuleEmail extends PropertyValidationRule {
18: public ValidationRuleEmail(String propertyName) {
19: super (propertyName);
20: }
21:
22: public boolean validate() {
23: Object value;
24: try {
25: value = BeanUtils.getPropertyValue(getBean(),
26: getPropertyName());
27: } catch (BeanUtilsException e) {
28: // an error occurred when obtaining the value of the property
29: // just consider it valid to skip over it
30: return true;
31: }
32:
33: if (null == value) {
34: return true;
35: }
36:
37: ConstrainedProperty constrained_property = ConstrainedUtils
38: .getConstrainedProperty(getBean(), getPropertyName());
39: if (value.getClass().isArray()) {
40: int length = Array.getLength(value);
41: for (int i = 0; i < length; i++) {
42: if (!checkEmail(BeanUtils.formatPropertyValue(Array
43: .get(value, i), constrained_property))) {
44: return false;
45: }
46: }
47:
48: return true;
49: } else {
50: return checkEmail(BeanUtils.formatPropertyValue(value,
51: constrained_property));
52: }
53: }
54:
55: public ValidationError getError() {
56: return new ValidationError.INVALID(getSubject());
57: }
58: }
|