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