|
| 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 | +} |
0 commit comments