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