Skip to content

Commit aba1bc6

Browse files
committed
SireneValidator wird für VATidFRCheckDigit benötigt
1 parent 754c8a0 commit aba1bc6

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.validator.routines;
18+
19+
import org.apache.commons.validator.GenericValidator;
20+
import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;
21+
22+
/**
23+
* SIRENE (French System Information et Repertoire des Entreprise et des Etablissements) Validator.
24+
*
25+
* <p>
26+
* International Code Designator, ICD : 0002 for SIRENE
27+
* </p>
28+
* <p>
29+
* Issuing Organization :
30+
* Institut National de la Statistique et des Etudes Economiques, (I.N.S.E.E.),
31+
* Departement des Repertoires, 18, Bd Adolphe Pinard, 75675 PARIS Cedex 14
32+
* </p>
33+
* <p>
34+
* Structure of Code :
35+
* </p>
36+
* <p>
37+
* 1) Number of characters: 9 characters ("SIREN") 14 " 9+5 ("SIRET"),
38+
* </p>
39+
* <p>
40+
* The 9 character number designates an organization,
41+
* the 14 character number designates a specific establishment of the organization designated by the first 9 characters.
42+
* </p>
43+
* <p>
44+
* 2) Check digits: 9th and 14th character respectively
45+
* </p>
46+
* <p>
47+
* See <a href="https://en.wikipedia.org/wiki/SIRET_code">Wikipedia - SIRET</a> for more details.
48+
* </p>
49+
* @since 1.10.0
50+
*/
51+
public class SireneValidator {
52+
53+
final Validator formatValidator;
54+
55+
private static final Validator DEFAULT_FORMAT =
56+
new Validator(new String[]
57+
{ "^(\\d{9})$" // SIREN
58+
, "^(\\d{14})$" // SIRET
59+
});
60+
61+
private static final int SIREN_CODE_LEN = 9;
62+
private static final int SIRET_CODE_LEN = 14;
63+
64+
/**
65+
* The format validation class contains regex for SIREN and SIRET.
66+
*/
67+
public static class Validator {
68+
final RegexValidator validator;
69+
70+
/**
71+
* Creates the format validator
72+
*
73+
* @param formats the regex to use to check the format
74+
*/
75+
public Validator(final String[] formats) {
76+
this.validator = new RegexValidator(formats);
77+
}
78+
}
79+
80+
/** The singleton instance which uses the default formats */
81+
private static final SireneValidator DEFAULT_SIRENE_VALIDATOR = new SireneValidator();
82+
83+
/**
84+
* Gets the singleton instance of the SIRENE validator using the default formats
85+
*
86+
* @return A singleton instance of the validator
87+
*/
88+
public static SireneValidator getInstance() {
89+
return DEFAULT_SIRENE_VALIDATOR;
90+
}
91+
92+
/**
93+
* Create a default format validator.
94+
*/
95+
public SireneValidator() {
96+
this.formatValidator = DEFAULT_FORMAT;
97+
}
98+
99+
/**
100+
* Validate a SIRENE-ID (SIREN or SIRET)
101+
*
102+
* @param code The value validation is being performed on
103+
* @return <code>true</code> if the value is valid
104+
*/
105+
public boolean isValid(final String code) {
106+
if (GenericValidator.isBlankOrNull(code)) {
107+
return false;
108+
}
109+
final String id = code.trim();
110+
if (id.length() != SIREN_CODE_LEN && id.length() != SIRET_CODE_LEN) {
111+
return false;
112+
}
113+
// format check:
114+
if (!formatValidator.validator.isValid(id)) {
115+
return false;
116+
}
117+
if (id.length() == SIREN_CODE_LEN) {
118+
return LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(id);
119+
}
120+
if (!LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(id.substring(0, SIREN_CODE_LEN))) {
121+
return false;
122+
}
123+
// check SIRET:
124+
return LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(id);
125+
}
126+
127+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.validator.routines;
18+
19+
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
public class SireneValidatorTest {
28+
29+
private final List<String> validFormat = Arrays.asList(new String[] {
30+
"123456782", // SIREN
31+
"572141885", // SIREN
32+
"502090897", // SIREN flexitec.fr
33+
"910167758", "893560474", // SIREN overseas department Reunion
34+
"82425921200028 ", "82882805300029", // SIRET overseas department Reunion
35+
"57214188502180", // SIRET
36+
"50810215900334", // SIRET
37+
"40483304800022", // valid but not longer active in SIRENE catalogue
38+
});
39+
40+
private final List<String> invalidFormat = Arrays.asList(new String[] {
41+
"12345678", // invalid length, to short
42+
"1234567820", // invalid length, to long for SIREN, to short for SIRET
43+
"572141885021810", // invalid length, to long
44+
"123456780", // invalid check digit "0" should be "2"
45+
"57214188502181", // invalid check digit "1" should be "0"
46+
"50810215000333", // SIRET with invalid SIREN check digit "0" at pos 9
47+
});
48+
49+
private static final SireneValidator VALIDATOR = SireneValidator.getInstance();
50+
51+
@Test
52+
public void testValid() {
53+
validFormat.forEach(e -> {
54+
assertTrue(VALIDATOR.isValid(e), e);
55+
});
56+
}
57+
58+
@Test
59+
public void testInValid() {
60+
invalidFormat.forEach(e -> {
61+
assertFalse(VALIDATOR.isValid(e), e);
62+
});
63+
}
64+
65+
}

0 commit comments

Comments
 (0)