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