Author: sebb
Date: Mon Nov 9 12:32:58 2015
New Revision: 1713421
URL: http://svn.apache.org/viewvc?rev=1713421&view=rev
Log:
VALIDATOR-321 ISSN validator and converter to EAN-13
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISSNValidator.java
(with props)
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigitTest.java
(with props)
Modified:
commons/proper/validator/trunk/src/changes/changes.xml
Modified: commons/proper/validator/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/changes/changes.xml?rev=1713421&r1=1713420&r2=1713421&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/changes/changes.xml (original)
+++ commons/proper/validator/trunk/src/changes/changes.xml Mon Nov 9 12:32:58
2015
@@ -42,6 +42,9 @@ The <action> type attribute can be add,u
<body>
<release version="1.5.0" date="tba" description="tba">
+ <action issue="VALIDATOR-321" type="add" dev="sebb">
+ ISSN validator and converter to EAN-13
+ </action>
<action issue="VALIDATOR-325" type="add" dev="sebb">
Improve IBAN validation with format checks
</action>
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISSNValidator.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISSNValidator.java?rev=1713421&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISSNValidator.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISSNValidator.java
Mon Nov 9 12:32:58 2015
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines;
+
+import java.io.Serializable;
+
+import org.apache.commons.validator.routines.checkdigit.CheckDigitException;
+import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
+import org.apache.commons.validator.routines.checkdigit.ISSNCheckDigit;
+
+/**
+ * International Standard Serial Number (ISSN)
+ * is an eight-digit serial number used to
+ * uniquely identify a serial publication.
+ * <p>
+ * <pre>
+ * The format is:
+ *
+ * ISSN dddd-dddC
+ * where:
+ * d = decimal digit (0-9)
+ * C = checksum (0-9 or X)
+ *
+ * The checksum is formed by adding the first 7 digits multiplied by
+ * the position in the entire number (counting from the right).
+ *
+ * For example, abcd-efg would be 8a + 7b + 6c + 5d + 4e +3f +2g.
+ * The check digit is modulus 11, where the value 10 is represented by 'X'
+ * For example:
+ * ISSN 0317-8471
+ * ISSN 1050-124X
+ *
+ * This class strips off the 'ISSN ' prefix if it is present before passing
+ * the remainder to the checksum routine.
+ *
+ * </pre>
+ * <p>
+ * Note: the {@link #isValid(String)} and {@link } methods strip off any
leading
+ * or trailing spaces before doing the validation.
+ * To ensure that only a valid code (without 'ISSN ' prefix) is passed to a
method,
+ * use the following code:
+ * <pre>
+ * Object valid = validator.validate(input);
+ * if (valid != null) {
+ * some_method(valid.toString());
+ * }
+ * </pre>
+ * @since 1.5.0
+ */
+public class ISSNValidator implements Serializable {
+
+ private static final long serialVersionUID = 4319515687976420405L;
+
+ 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
+
+ private static final CodeValidator issnValidator = new
CodeValidator(ISSN_REGEX, 8, ISSNCheckDigit.ISSN_CHECK_DIGIT);
+
+ /** ISSN Code Validator */
+ private static final ISSNValidator ISSN_VALIDATOR = new ISSNValidator();
+
+ /**
+ * Return a singleton instance of the ISSN validator
+ *
+ * @return A singleton instance of the ISSN validator.
+ */
+ public static ISSNValidator getInstance() {
+ return ISSN_VALIDATOR;
+ }
+
+ /**
+ * Check the code is a valid ISSN code after any transformation
+ * by the validate routine.
+ * @param code The code to validate.
+ * @return <code>true</code> if a valid ISSN
+ * code, otherwise <code>false</code>.
+ */
+ public boolean isValid(String code) {
+ return issnValidator.isValid(code);
+ }
+
+ /**
+ * Check the code is valid ISSN code.
+ * <p>
+ * If valid, this method returns the ISSN code with
+ * the 'ISSN ' prefix removed (if it was present)
+ *
+ * @param code The code to validate.
+ * @return A valid ISSN code if valid, otherwise <code>null</code>.
+ */
+ public Object validate(String code) {
+ return issnValidator.validate(code);
+ }
+
+ /**
+ * Convert an ISSN code to an EAN-13 code.
+ * <p>
+ * This method requires a valid ISSN code.
+ * It may contain a leading 'ISSN ' prefix,
+ * as the input is passed through the {@link #validate(String)}
+ * method.
+ *
+ * @param issn The ISSN code to convert
+ * @param suffix the two digit suffix, e.g. "00"
+ * @return A converted EAN-13 code or <code>null</code>
+ * if the input ISSN code is not valid
+ */
+ public String convertToEAN13(String issn, String suffix) {
+
+ if (suffix == null || !suffix.matches("\\d\\d")) {
+ throw new IllegalArgumentException("Suffix must be two digits: '"
+ suffix + "'");
+ }
+
+ Object result = validate(issn);
+ if (result == null) {
+ return null;
+ }
+
+ // Calculate the new EAN-13 code
+ final String input = result.toString();
+ String ean13 = "977" + input.substring(0, input.length() -1) + suffix;
+ try {
+ String checkDigit =
EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(ean13);
+ ean13 += checkDigit;
+ return ean13;
+ } catch (CheckDigitException e) { // Should not happen
+ throw new IllegalArgumentException("Check digit error for '" +
ean13 + "' - " + e.getMessage());
+ }
+
+ }
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISSNValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java?rev=1713421&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java
Mon Nov 9 12:32:58 2015
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines.checkdigit;
+
+/**
+ * International Standard Serial Number (ISSN)
+ * is an eight-digit serial number used to
+ * uniquely identify a serial publication.
+ * <pre>
+ * The format is:
+ *
+ * ISSN dddd-dddC
+ * where:
+ * d = decimal digit (0-9)
+ * C = checksum (0-9 or X)
+ *
+ * The checksum is formed by adding the first 7 digits multiplied by
+ * the position in the entire number (counting from the right).
+ * For example, abcd-efg would be 8a + 7b + 6c + 5d + 4e +3f +2g.
+ * The check digit is modulus 11, where the value 10 is represented by 'X'
+ * For example:
+ * ISSN 0317-8471
+ * ISSN 1050-124X
+ * </pre>
+ * <p>
+ * <b>Note:</b> This class expects the input to be numeric only,
+ * with all formatting removed.
+ * For example:
+ * <pre>
+ * 03178471
+ * 1050124X
+ * </pre>
+ * @since 1.5.0
+ */
+public final class ISSNCheckDigit extends ModulusCheckDigit {
+
+
+ private static final long serialVersionUID = 1L;
+
+ /** Singleton ISSN Check Digit instance */
+ public static final CheckDigit ISSN_CHECK_DIGIT = new ISSNCheckDigit();
+
+ public ISSNCheckDigit() {
+ super(11);
+ }
+
+ @Override
+ protected int weightedValue(int charValue, int leftPos, int rightPos)
throws CheckDigitException {
+ return charValue * (9 - leftPos);
+ }
+
+ @Override
+ protected String toCheckDigit(int charValue) throws CheckDigitException {
+ if (charValue == 10) {
+ return "X";
+ }
+ return super.toCheckDigit(charValue);
+ }
+
+ @Override
+ protected int toInt(char character, int leftPos, int rightPos)
+ throws CheckDigitException {
+ if (rightPos == 1 && character == 'X') {
+ return 10;
+ }
+ return super.toInt(character, leftPos, rightPos);
+ }
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java?rev=1713421&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java
Mon Nov 9 12:32:58 2015
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines;
+
+import java.util.Random;
+
+import org.apache.commons.validator.routines.checkdigit.CheckDigit;
+import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
+
+import junit.framework.TestCase;
+
+/**
+ * ISSNValidator Test Case.
+ *
+ * @version $Revision: 1649191 $
+ */
+public class ISSNValidatorTest extends TestCase {
+
+ private static final ISSNValidator VALIDATOR = ISSNValidator.getInstance();
+
+ private final String[] validFormat = new String[] {
+ "ISSN 0317-8471",
+ "1050-124X",
+ "ISSN 1562-6865",
+ "1063-7710",
+ "1748-7188",
+ "ISSN 0264-2875",
+ "1750-0095",
+ "1188-1534",
+ "1911-1479",
+ "ISSN 1911-1460",
+ "0001-6772",
+ "1365-201X",
+ };
+
+ private final String[] invalidFormat = new String[] {
+ "", // empty
+ " ", // empty
+ "ISBN 0317-8471", // wrong prefix
+ "'1050-124X", // leading garbage
+ "ISSN1562-6865", // missing separator
+ "10637710", // missing separator
+ "1748-7188'", // trailing garbage
+ "ISSN 0264-2875", // extra space
+ "1750 0095", // invalid separator
+ "1188_1534", // invalid separator
+ "1911-1478", // invalid checkdigit
+ };
+
+ /**
+ * Create a test case with the specified name.
+ * @param name The name of the test
+ */
+ public ISSNValidatorTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Test isValid() ISSN codes
+ */
+ public void testIsValidISSN() {
+ for(String f : validFormat) {
+ assertTrue(f, VALIDATOR.isValid(f));
+ }
+ }
+
+ /**
+ * Test null values
+ */
+ public void testNull() {
+ assertFalse("isValid", VALIDATOR.isValid(null));
+ }
+
+ /**
+ * Test Invalid ISSN codes
+ */
+ public void testInvalid() {
+ for(String f : invalidFormat) {
+ assertFalse(f, VALIDATOR.isValid(f));
+ }
+ }
+
+ public void testIsValidISSNConvertNull() {
+ assertNull(VALIDATOR.convertToEAN13(null, "00"));
+ }
+
+ public void testIsValidISSNConvertSuffix() {
+ try {
+ assertNull(VALIDATOR.convertToEAN13(null, null));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+
+ }
+ try {
+ assertNull(VALIDATOR.convertToEAN13(null, ""));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+
+ }
+ try {
+ assertNull(VALIDATOR.convertToEAN13(null, "0"));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+
+ }
+ try {
+ assertNull(VALIDATOR.convertToEAN13(null, "A"));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+
+ }
+ try {
+ assertNull(VALIDATOR.convertToEAN13(null, "AA"));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+
+ }
+ try {
+ assertNull(VALIDATOR.convertToEAN13(null, "999"));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+
+ }
+ }
+
+ /**
+ * Test isValid() ISSN codes and convert them
+ */
+ public void testIsValidISSNConvert() {
+ CheckDigit ean13cd = EAN13CheckDigit.EAN13_CHECK_DIGIT;
+ Random r = new Random();
+ for(String f : validFormat) {
+ String suffix = String.format("%02d", r.nextInt(100));
+ String ean13 = VALIDATOR.convertToEAN13(f, suffix);
+ assertTrue(ean13, ean13cd.isValid(ean13));
+ }
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigitTest.java?rev=1713421&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigitTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigitTest.java
Mon Nov 9 12:32:58 2015
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines.checkdigit;
+
+
+/**
+ * ISSN Check Digit Test.
+ *
+ * @since Validator 1.5.0
+ */
+public class ISSNCheckDigitTest extends AbstractCheckDigitTest {
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public ISSNCheckDigitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Set up routine & valid codes.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ routine = ISSNCheckDigit.ISSN_CHECK_DIGIT;
+ valid = new String[] {
+ "03178471",
+ "1050124X",
+ "15626865",
+ "10637710",
+ "17487188",
+ "02642875",
+ "17500095",
+ "11881534",
+ "19111479",
+ "19111460",
+ "00016772",
+ "1365201X",
+ };
+ invalid = new String[] {
+ "03178472", // wrong check
+ "1050-124X", // format char
+ " 1365201X",
+ "1365201X ",
+ " 1365201X ",
+ };
+ missingMessage = "Code is missing";
+ zeroSum = "00000000";
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISSNCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native