ISSNValidator.java

  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.  *      https://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. import java.io.Serializable;

  19. import org.apache.commons.validator.routines.checkdigit.CheckDigitException;
  20. import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
  21. import org.apache.commons.validator.routines.checkdigit.ISSNCheckDigit;

  22. /**
  23.  * International Standard Serial Number (ISSN)
  24.  * is an eight-digit serial number used to
  25.  * uniquely identify a serial publication.
  26.  * <pre>
  27.  * The format is:
  28.  *
  29.  * ISSN dddd-dddC
  30.  * where:
  31.  * d = decimal digit (0-9)
  32.  * C = checksum (0-9 or X)
  33.  *
  34.  * The checksum is formed by adding the first 7 digits multiplied by
  35.  * the position in the entire number (counting from the right).
  36.  *
  37.  * For example, abcd-efg would be 8a + 7b + 6c + 5d + 4e +3f +2g.
  38.  * The check digit is modulus 11, where the value 10 is represented by 'X'
  39.  * For example:
  40.  * ISSN 0317-8471
  41.  * ISSN 1050-124X
  42.  *
  43.  * This class strips off the 'ISSN ' prefix if it is present before passing
  44.  * the remainder to the checksum routine.
  45.  *
  46.  * </pre>
  47.  * <p>
  48.  * Note: the {@link #isValid(String)} and {@link #validate(String)} methods strip off any leading
  49.  * or trailing spaces before doing the validation.
  50.  * To ensure that only a valid code (without 'ISSN ' prefix) is passed to a method,
  51.  * use the following code:
  52.  * </p>
  53.  * <pre>
  54.  * Object valid = validator.validate(input);
  55.  * if (valid != null) {
  56.  *    some_method(valid.toString());
  57.  * }
  58.  * </pre>
  59.  * @since 1.5.0
  60.  */
  61. public class ISSNValidator implements Serializable {

  62.     private static final long serialVersionUID = 4319515687976420405L;

  63.     private static final String ISSN_REGEX = "(?:ISSN )?(\\d{4})-(\\d{3}[0-9X])$"; // We don't include the '-' in the code, so it is 8 chars

  64.     private static final int ISSN_LEN = 8;

  65.     private static final String ISSN_PREFIX = "977";

  66.     private static final String EAN_ISSN_REGEX = "^(977)(?:(\\d{10}))$";

  67.     private static final int EAN_ISSN_LEN = 13;

  68.     private static final CodeValidator VALIDATOR = new CodeValidator(ISSN_REGEX, ISSN_LEN, ISSNCheckDigit.ISSN_CHECK_DIGIT);

  69.     private static final CodeValidator EAN_VALIDATOR = new CodeValidator(EAN_ISSN_REGEX, EAN_ISSN_LEN, EAN13CheckDigit.EAN13_CHECK_DIGIT);

  70.     /** ISSN Code Validator. */
  71.     private static final ISSNValidator ISSN_VALIDATOR = new ISSNValidator();

  72.     /**
  73.      * Gets the singleton instance of the ISSN validator.
  74.      *
  75.      * @return A singleton instance of the ISSN validator.
  76.      */
  77.     public static ISSNValidator getInstance() {
  78.         return ISSN_VALIDATOR;
  79.     }

  80.     /**
  81.      * Constructs a new instance.
  82.      */
  83.     public ISSNValidator() {
  84.         // empty
  85.     }

  86.     /**
  87.      * Converts an ISSN code to an EAN-13 code.
  88.      * <p>
  89.      * This method requires a valid ISSN code.
  90.      * It may contain a leading 'ISSN ' prefix,
  91.      * as the input is passed through the {@link #validate(String)}
  92.      * method.
  93.      * </p>
  94.      *
  95.      * @param issn The ISSN code to convert
  96.      * @param suffix the two digit suffix, for example, "00"
  97.      * @return A converted EAN-13 code or {@code null}
  98.      * if the input ISSN code is not valid
  99.      */
  100.     public String convertToEAN13(final String issn, final String suffix) {
  101.         if (suffix == null || !suffix.matches("\\d\\d")) {
  102.             throw new IllegalArgumentException("Suffix must be two digits: '" + suffix + "'");
  103.         }
  104.         final Object result = validate(issn);
  105.         if (result == null) {
  106.             return null;
  107.         }
  108.         // Calculate the new EAN-13 code
  109.         final String input = result.toString();
  110.         String ean13 = ISSN_PREFIX + input.substring(0, input.length() - 1) + suffix;
  111.         try {
  112.             final String checkDigit = EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(ean13);
  113.             ean13 += checkDigit;
  114.             return ean13;
  115.         } catch (final CheckDigitException e) { // Should not happen
  116.             throw new IllegalArgumentException("Check digit error for '" + ean13 + "' - " + e.getMessage());
  117.         }
  118.     }

  119.     /**
  120.      * Extracts an ISSN code from an ISSN-EAN-13 code.
  121.      * <p>
  122.      * This method requires a valid ISSN-EAN-13 code with NO formatting
  123.      * characters.
  124.      * That is a 13 digit EAN-13 code with the '977' prefix.
  125.      * </p>
  126.      *
  127.      * @param ean13 The ISSN code to convert
  128.      * @return A valid ISSN code or {@code null}
  129.      * if the input ISSN EAN-13 code is not valid
  130.      * @since 1.7
  131.      */
  132.     public String extractFromEAN13(final String ean13) {
  133.         String input = ean13.trim();
  134.         if (input.length() != EAN_ISSN_LEN) {
  135.             throw new IllegalArgumentException("Invalid length " + input.length() + " for '" + input + "'");
  136.         }
  137.         if (!input.startsWith(ISSN_PREFIX)) {
  138.             throw new IllegalArgumentException("Prefix must be " + ISSN_PREFIX + " to contain an ISSN: '" + ean13 + "'");
  139.         }
  140.         final Object result = validateEan(input);
  141.         if (result == null) {
  142.             return null;
  143.         }
  144.         // Calculate the ISSN code
  145.         input = result.toString();
  146.         try {
  147.             //CHECKSTYLE:OFF: MagicNumber
  148.             final String issnBase = input.substring(3, 10); // TODO: how to derive these
  149.             //CHECKSTYLE:ON: MagicNumber
  150.             final String checkDigit = ISSNCheckDigit.ISSN_CHECK_DIGIT.calculate(issnBase);
  151.             return issnBase + checkDigit;
  152.         } catch (final CheckDigitException e) { // Should not happen
  153.             throw new IllegalArgumentException("Check digit error for '" + ean13 + "' - " + e.getMessage());
  154.         }
  155.     }

  156.     /**
  157.      * Tests whether the code is a valid ISSN code after any transformation
  158.      * by the validate routine.
  159.      *
  160.      * @param code The code to validate.
  161.      * @return {@code true} if a valid ISSN
  162.      * code, otherwise {@code false}.
  163.      */
  164.     public boolean isValid(final String code) {
  165.         return VALIDATOR.isValid(code);
  166.     }

  167.     /**
  168.      * Checks the code is valid ISSN code.
  169.      * <p>
  170.      * If valid, this method returns the ISSN code with
  171.      * the 'ISSN ' prefix removed (if it was present)
  172.      * </p>
  173.      *
  174.      * @param code The code to validate.
  175.      * @return A valid ISSN code if valid, otherwise {@code null}.
  176.      */
  177.     public Object validate(final String code) {
  178.         return VALIDATOR.validate(code);
  179.     }

  180.     /**
  181.      * Checks the code is a valid EAN code.
  182.      * <p>
  183.      * If valid, this method returns the EAN code
  184.      * </p>
  185.      *
  186.      * @param code The code to validate.
  187.      * @return A valid EAN code if valid, otherwise {@code null}.
  188.      * @since 1.7
  189.      */
  190.     public Object validateEan(final String code) {
  191.         return EAN_VALIDATOR.validate(code);
  192.     }
  193. }