1 package org.nuiton.validator.xwork2.field;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.opensymphony.xwork2.validator.ValidationException;
26 import org.apache.commons.lang3.StringUtils;
27
28 import java.lang.reflect.Array;
29 import java.util.Collection;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33
34
35
36 public class FrenchFinessFieldValidator extends NuitonFieldValidatorSupport {
37
38 protected static final String FINESS_REGEXP = "(0[1-9]|[1-9][0-9]|2A|2B)0([0-9]{6})";
39
40 protected static final Pattern p = Pattern.compile(FINESS_REGEXP);
41
42 @Override
43 protected void validateWhenNotSkip(Object object) throws ValidationException {
44 String fieldName = getFieldName();
45 Object value = getFieldValue(fieldName, object);
46
47 if (value == null) {
48
49 return;
50 }
51 String finess;
52
53 if (value.getClass().isArray()) {
54
55 finess = "";
56 for (int i = 0; i < Array.getLength(value); i++) {
57 finess += String.valueOf(Array.get(value, i));
58 }
59 } else if (value instanceof Collection<?>) {
60
61
62 finess = "";
63 for (Object o : (Collection<?>) value) {
64 finess += String.valueOf(o);
65 }
66 } else {
67
68
69 finess = String.valueOf(value);
70 }
71
72 if (StringUtils.isEmpty(finess)) {
73
74 return;
75 }
76
77
78 finess = finess.replaceAll(" ", "");
79
80 Matcher m = p.matcher(finess);
81 if (!m.matches() || !FieldValidatorUtil.luhnChecksum(finess)) {
82 addFieldError(fieldName, object);
83 }
84 }
85
86 @Override
87 public String getValidatorType() {
88 return "frenchFiness";
89 }
90 }