001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import org.apache.oro.text.perl.Perl5Util;
020:
021: /**
022: * A class for validating 10 digit ISBN codes.
023: * Based on this
024: * <a href="http://www.isbn.org/standards/home/isbn/international/html/usm4.htm">
025: * algorithm</a>
026: *
027: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
028: * @since Validator 1.2.0
029: */
030: public class ISBNValidator {
031:
032: private static final String SEP = "(\\-|\\s)";
033: private static final String GROUP = "(\\d{1,5})";
034: private static final String PUBLISHER = "(\\d{1,7})";
035: private static final String TITLE = "(\\d{1,6})";
036: private static final String CHECK = "([0-9X])";
037:
038: /**
039: * ISBN consists of 4 groups of numbers separated by either dashes (-)
040: * or spaces. The first group is 1-5 characters, second 1-7, third 1-6,
041: * and fourth is 1 digit or an X.
042: */
043: private static final String ISBN_PATTERN = "/^" + GROUP + SEP
044: + PUBLISHER + SEP + TITLE + SEP + CHECK + "$/";
045:
046: /**
047: * Default Constructor.
048: */
049: public ISBNValidator() {
050: super ();
051: }
052:
053: /**
054: * If the ISBN is formatted with space or dash separators its format is
055: * validated. Then the digits in the number are weighted, summed, and
056: * divided by 11 according to the ISBN algorithm. If the result is zero,
057: * the ISBN is valid. This method accepts formatted or raw ISBN codes.
058: *
059: * @param isbn Candidate ISBN number to be validated. <code>null</code> is
060: * considered invalid.
061: * @return true if the string is a valid ISBN code.
062: */
063: public boolean isValid(String isbn) {
064: if (isbn == null || isbn.length() < 10 || isbn.length() > 13) {
065: return false;
066: }
067:
068: if (isFormatted(isbn) && !isValidPattern(isbn)) {
069: return false;
070: }
071:
072: isbn = clean(isbn);
073: if (isbn.length() != 10) {
074: return false;
075: }
076:
077: return (sum(isbn) % 11) == 0;
078: }
079:
080: /**
081: * Returns the sum of the weighted ISBN characters.
082: */
083: private int sum(String isbn) {
084: int total = 0;
085: for (int i = 0; i < 9; i++) {
086: int weight = 10 - i;
087: total += (weight * toInt(isbn.charAt(i)));
088: }
089: total += toInt(isbn.charAt(9)); // add check digit
090: return total;
091: }
092:
093: /**
094: * Removes all non-digit characters except for 'X' which is a valid ISBN
095: * character.
096: */
097: private String clean(String isbn) {
098: StringBuffer buf = new StringBuffer(10);
099:
100: for (int i = 0; i < isbn.length(); i++) {
101: char digit = isbn.charAt(i);
102: if (Character.isDigit(digit) || (digit == 'X')) {
103: buf.append(digit);
104: }
105: }
106:
107: return buf.toString();
108: }
109:
110: /**
111: * Returns the numeric value represented by the character. If the
112: * character is not a digit but an 'X', 10 is returned.
113: */
114: private int toInt(char ch) {
115: return (ch == 'X') ? 10 : Character.getNumericValue(ch);
116: }
117:
118: /**
119: * Returns true if the ISBN contains one of the separator characters space
120: * or dash.
121: */
122: private boolean isFormatted(String isbn) {
123: return ((isbn.indexOf('-') != -1) || (isbn.indexOf(' ') != -1));
124: }
125:
126: /**
127: * Returns true if the ISBN is formatted properly.
128: */
129: private boolean isValidPattern(String isbn) {
130: return new Perl5Util().match(ISBN_PATTERN, isbn);
131: }
132:
133: }
|