1 /*
2 * #%L
3 * Nuiton Validator
4 * %%
5 * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20 * #L%
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 * Validator for French SIRET numbers
34 *
35 * Siret can be in:
36 * <ul>
37 * <li>String format: "44211670300038"</li>
38 * <li>long, int: 44211670300038</li>
39 * <li>Array or Collection of something: [4,4,2,1,1,6,7,0,,3,0,0,0,3,8] or ["442","116","703", "0003", "8"]</li>
40 * </ul>
41 *
42 * @author Jean Couteau - couteau@codelutin.com
43 * @since 2.3
44 * Validation do the Luhn checksum too
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 // no value defined
60 return;
61 }
62 String siret;
63
64 if (value.getClass().isArray()) {
65 // le siret est stocker dans un tableau, par exemple un byte[]
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 // le siret est stocker dans une collection,
72 // ca doit pas arriver souvent :D, mais autant le gerer
73 siret = "";
74 for (Object o : (Collection<?>) value) {
75 siret += String.valueOf(o);
76 }
77 } else {
78 // sinon dans tous les autres cas (String, int, long, BigInteger ...)
79 // on prend le toString
80 siret = String.valueOf(value);
81 }
82
83 if (StringUtils.isEmpty(siret)) {
84 // no value defined
85 return;
86 }
87
88 // Remove any space
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 }