All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commons.validator.routines.ISBNValidator Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
/*
 * 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 commons.validator.routines;

import java.io.Serializable;
import commons.validator.routines.checkdigit.EAN13CheckDigit;
import commons.validator.routines.checkdigit.ISBN10CheckDigit;
import commons.validator.routines.checkdigit.CheckDigitException;

/**
 * ISBN-10 and ISBN-13 Code Validation.
 * 

* This validator validates the code is either a valid ISBN-10 * (using a {@link CodeValidator} with the {@link ISBN10CheckDigit}) * or a valid ISBN-13 code (using a {@link CodeValidator} with the * the {@link EAN13CheckDigit} routine). *

* The validate() methods return the ISBN code with formatting * characters removed if valid or null if invalid. *

* This validator also provides the facility to convert ISBN-10 codes to * ISBN-13 if the convert property is true. *

* From 1st January 2007 the book industry will start to use a new 13 digit * ISBN number (rather than this 10 digit ISBN number). ISBN-13 codes are * EAN * codes, for more information see:

* * * * @version $Revision$ * @since Validator 1.4 */ public class ISBNValidator implements Serializable { private static final long serialVersionUID = 4319515687976420405L; private static final String SEP = "(?:\\-|\\s)"; private static final String GROUP = "(\\d{1,5})"; private static final String PUBLISHER = "(\\d{1,7})"; private static final String TITLE = "(\\d{1,6})"; /** * ISBN-10 consists of 4 groups of numbers separated by either dashes (-) * or spaces. The first group is 1-5 characters, second 1-7, third 1-6, * and fourth is 1 digit or an X. */ static final String ISBN10_REGEX = "^(?:(\\d{9}[0-9X])|(?:" + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + "([0-9X])))$"; /** * ISBN-13 consists of 5 groups of numbers separated by either dashes (-) * or spaces. The first group is 978 or 979, the second group is * 1-5 characters, third 1-7, fourth 1-6, and fifth is 1 digit. */ static final String ISBN13_REGEX = "^(978|979)(?:(\\d{10})|(?:" + SEP + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + "([0-9])))$"; /** ISBN Code Validator (which converts ISBN-10 codes to ISBN-13 */ private static final ISBNValidator ISBN_VALIDATOR = new ISBNValidator(); /** ISBN Code Validator (which converts ISBN-10 codes to ISBN-13 */ private static final ISBNValidator ISBN_VALIDATOR_NO_CONVERT = new ISBNValidator(false); /** ISBN-10 Code Validator */ private final CodeValidator isbn10Validator = new CodeValidator(ISBN10_REGEX, 10, ISBN10CheckDigit.ISBN10_CHECK_DIGIT); /** ISBN-13 Code Validator */ private final CodeValidator isbn13Validator = new CodeValidator(ISBN13_REGEX, 13, EAN13CheckDigit.EAN13_CHECK_DIGIT); private final boolean convert; /** * Return a singleton instance of the ISBN validator which * converts ISBN-10 codes to ISBN-13. * * @return A singleton instance of the ISBN validator. */ public static ISBNValidator getInstance() { return ISBN_VALIDATOR; } /** * Return a singleton instance of the ISBN validator specifying * whether ISBN-10 codes should be converted to ISBN-13. * * @param convert true if valid ISBN-10 codes * should be converted to ISBN-13 codes or false * if valid ISBN-10 codes should be returned unchanged. * @return A singleton instance of the ISBN validator. */ public static ISBNValidator getInstance(boolean convert) { return (convert ? ISBN_VALIDATOR : ISBN_VALIDATOR_NO_CONVERT); } /** * Construct an ISBN validator which converts ISBN-10 codes * to ISBN-13. */ public ISBNValidator() { this(true); } /** * Construct an ISBN validator indicating whether * ISBN-10 codes should be converted to ISBN-13. * * @param convert true if valid ISBN-10 codes * should be converted to ISBN-13 codes or false * if valid ISBN-10 codes should be returned unchanged. */ public ISBNValidator(boolean convert) { this.convert = convert; } /** * Check the code is either a valid ISBN-10 or ISBN-13 code. * * @param code The code to validate. * @return true if a valid ISBN-10 or * ISBN-13 code, otherwise false. */ public boolean isValid(String code) { return (isValidISBN13(code) || isValidISBN10(code)); } /** * Check the code is a valid ISBN-10 code. * * @param code The code to validate. * @return true if a valid ISBN-10 * code, otherwise false. */ public boolean isValidISBN10(String code) { return isbn10Validator.isValid(code); } /** * Check the code is a valid ISBN-13 code. * * @param code The code to validate. * @return true if a valid ISBN-13 * code, otherwise false. */ public boolean isValidISBN13(String code) { return isbn13Validator.isValid(code); } /** * Check the code is either a valid ISBN-10 or ISBN-13 code. *

* If valid, this method returns the ISBN code with * formatting characters removed (i.e. space or hyphen). *

* Converts an ISBN-10 codes to ISBN-13 if * convertToISBN13 is true. * * @param code The code to validate. * @return A valid ISBN code if valid, otherwise null. */ public String validate(String code) { String result = validateISBN13(code); if (result == null) { result = validateISBN10(code); if (result != null && convert) { result = convertToISBN13(result); } } return result; } /** * Check the code is a valid ISBN-10 code. *

* If valid, this method returns the ISBN-10 code with * formatting characters removed (i.e. space or hyphen). * * @param code The code to validate. * @return A valid ISBN-10 code if valid, * otherwise null. */ public String validateISBN10(String code) { Object result = isbn10Validator.validate(code); return (result == null ? null : result.toString()); } /** * Check the code is a valid ISBN-13 code. *

* If valid, this method returns the ISBN-13 code with * formatting characters removed (i.e. space or hyphen). * * @param code The code to validate. * @return A valid ISBN-13 code if valid, * otherwise null. */ public String validateISBN13(String code) { Object result = isbn13Validator.validate(code); return (result == null ? null : result.toString()); } /** * Convert an ISBN-10 code to an ISBN-13 code. *

* This method requires a valid ISBN-10 with NO formatting * characters. * * @param isbn10 The ISBN-10 code to convert * @return A converted ISBN-13 code or null * if the ISBN-10 code is not valid */ public String convertToISBN13(String isbn10) { if (isbn10 == null) { return null; } String input = isbn10.trim(); if (input.length() != 10) { throw new IllegalArgumentException("Invalid length " + input.length() + " for '" + input + "'"); } // Calculate the new ISBN-13 code String isbn13 = "978" + input.substring(0, 9); try { String checkDigit = isbn13Validator.getCheckDigit().calculate(isbn13); isbn13 += checkDigit; return isbn13; } catch (CheckDigitException e) { throw new IllegalArgumentException("Check digit error for '" + input + "' - " + e.getMessage()); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy