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
37
38
39
40
41
42
43
44
45
46
47
48 public class FrenchSirenFieldValidator extends NuitonFieldValidatorSupport {
49
50 protected static final String SIREN_REGEXP = "[0-9]{9}";
51
52 protected static final Pattern p = Pattern.compile(SIREN_REGEXP);
53
54 @Override
55 public void validateWhenNotSkip(Object object) throws ValidationException {
56
57 String fieldName = getFieldName();
58 Object value = getFieldValue(fieldName, object);
59
60 if (value == null) {
61
62 return;
63 }
64 String siren;
65
66 if (value.getClass().isArray()) {
67
68 siren = "";
69 for (int i = 0; i < Array.getLength(value); i++) {
70 siren += String.valueOf(Array.get(value, i));
71 }
72 } else if (value instanceof Collection<?>) {
73
74
75 siren = "";
76 for (Object o : (Collection<?>) value) {
77 siren += String.valueOf(o);
78 }
79 } else {
80
81
82 siren = String.valueOf(value);
83 }
84
85 if (StringUtils.isEmpty(siren)) {
86
87 return;
88 }
89
90
91 siren = siren.replaceAll(" ", "");
92
93 Matcher m = p.matcher(siren);
94 if (!m.matches() || !FieldValidatorUtil.luhnChecksum(siren)) {
95 addFieldError(fieldName, object);
96 }
97 }
98
99 @Override
100 public String getValidatorType() {
101 return "frenchSiret";
102 }
103
104 }