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