dgraham 2004/10/03 20:54:46
Modified: validator/src/test/org/apache/commons/validator
ValidatorTestSuite.java
validator/xdocs changes.xml
validator project.xml
Added: validator/src/share/org/apache/commons/validator
ISBNValidator.java
validator/src/test/org/apache/commons/validator
ISBNValidatorTest.java
Log:
Added ISBNValidator. PR: 31489
Revision Changes Path
1.1
jakarta-commons/validator/src/share/org/apache/commons/validator/ISBNValidator.java
Index: ISBNValidator.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ISBNValidator.java,v
1.1 2004/10/04 03:54:46 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2004/10/04 03:54:46 $
*
* ====================================================================
* Copyright 2004 The Apache Software Foundation
*
* Licensed 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;
import org.apache.oro.text.perl.Perl5Util;
/**
* A class for validating 10 digit ISBN codes.
* Based on this
* <a href="http://www.isbn.org/standards/home/isbn/international/html/usm4.htm">
* algorithm</a>
*
* @since Validator 1.2.0
*/
public class ISBNValidator {
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})";
private static final String CHECK = "([0-9X])";
/**
* ISBN 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.
*/
private static final String ISBN_PATTERN =
"/^" + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + CHECK + "$/";
public ISBNValidator() {
super();
}
/**
* If the ISBN is formatted with space or dash separators its format is
* validated. Then the digits in the number are weighted, summed, and
* divided by 11 according to the ISBN algorithm. If the result is zero,
* the ISBN is valid. This method accepts formatted or raw ISBN codes.
*
* @param isbn Candidate ISBN number to be validated. <code>null</code> is
* considered invalid.
* @return true if the string is a valid ISBN code.
*/
public boolean isValid(String isbn) {
if (isbn == null || isbn.length() < 10 || isbn.length() > 13) {
return false;
}
if (isFormatted(isbn) && !isValidPattern(isbn)) {
return false;
}
isbn = clean(isbn);
if (isbn.length() != 10) {
return false;
}
return (sum(isbn) % 11) == 0;
}
/**
* Returns the sum of the weighted ISBN characters.
*/
private int sum(String isbn) {
int total = 0;
for (int i = 0; i < 9; i++) {
int weight = 10 - i;
total += (weight * toInt(isbn.charAt(i)));
}
total += toInt(isbn.charAt(9)); // add check digit
return total;
}
/**
* Removes all non-digit characters except for 'X' which is a valid ISBN
* character.
*/
private String clean(String isbn) {
StringBuffer buf = new StringBuffer(10);
for (int i = 0; i < isbn.length(); i++) {
char digit = isbn.charAt(i);
if (Character.isDigit(digit) || (digit == 'X')) {
buf.append(digit);
}
}
return buf.toString();
}
/**
* Returns the numeric value represented by the character. If the
* character is not a digit but an 'X', 10 is returned.
*/
private int toInt(char ch) {
return (ch == 'X') ? 10 : Character.getNumericValue(ch);
}
/**
* Returns true if the ISBN contains one of the separator characters space
* or dash.
*/
private boolean isFormatted(String isbn) {
return ((isbn.indexOf('-') != -1) || (isbn.indexOf(' ') != -1));
}
/**
* Returns true if the ISBN is formatted properly.
*/
private boolean isValidPattern(String isbn) {
return new Perl5Util().match(ISBN_PATTERN, isbn);
}
}
1.20 +4 -3
jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java
Index: ValidatorTestSuite.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTestSuite.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ValidatorTestSuite.java 8 Jun 2004 16:19:20 -0000 1.19
+++ ValidatorTestSuite.java 4 Oct 2004 03:54:46 -0000 1.20
@@ -54,6 +54,7 @@
suite.addTest(ExtensionTest.suite());
suite.addTest(EmailTest.suite());
suite.addTestSuite(CreditCardValidatorTest.class);
+ suite.addTestSuite(ISBNValidatorTest.class);
suite.addTest(ValidatorTest.suite());
suite.addTest(LocaleTest.suite());
suite.addTestSuite(FieldTest.class);
1.1
jakarta-commons/validator/src/test/org/apache/commons/validator/ISBNValidatorTest.java
Index: ISBNValidatorTest.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ISBNValidatorTest.java,v
1.1 2004/10/04 03:54:46 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2004/10/04 03:54:46 $
*
* ====================================================================
* Copyright 2004 The Apache Software Foundation
*
* Licensed 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;
import junit.framework.TestCase;
public class ISBNValidatorTest extends TestCase {
private static final String VALID_ISBN_RAW = "1930110995";
private static final String VALID_ISBN_DASHES = "1-930110-99-5";
private static final String VALID_ISBN_SPACES = "1 930110 99 5";
private static final String VALID_ISBN_X = "0-201-63385-X";
private static final String INVALID_ISBN = "068-556-98-45";
public ISBNValidatorTest(String name) {
super(name);
}
public void testIsValid() throws Exception {
ISBNValidator validator = new ISBNValidator();
assertFalse(validator.isValid(null));
assertFalse(validator.isValid(""));
assertFalse(validator.isValid("1"));
assertFalse(validator.isValid("12345678901234"));
assertFalse(validator.isValid("dsasdsadsads"));
assertFalse(validator.isValid("535365"));
assertFalse(validator.isValid("I love sparrows!"));
assertFalse(validator.isValid("--1 930110 99 5"));
assertFalse(validator.isValid("1 930110 99 5--"));
assertFalse(validator.isValid("1 930110-99 5-"));
assertTrue(validator.isValid(VALID_ISBN_RAW));
assertTrue(validator.isValid(VALID_ISBN_DASHES));
assertTrue(validator.isValid(VALID_ISBN_SPACES));
assertTrue(validator.isValid(VALID_ISBN_X));
assertFalse(validator.isValid(INVALID_ISBN));
}
}
1.6 +4 -0 jakarta-commons/validator/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/xdocs/changes.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- changes.xml 25 Aug 2004 04:27:03 -0000 1.5
+++ changes.xml 4 Oct 2004 03:54:46 -0000 1.6
@@ -39,6 +39,10 @@
<body>
<release version="1.2.0 (alpha)" date="in CVS">
+ <action dev="dgraham" type="add">
+ Added ISBNValidator for validating book numbers.
+ PR# 31489
+ </action>
<action dev="husted" type="add">
Add support for min or max numeric values.
PR# 29015
1.52 +6 -0 jakarta-commons/validator/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/project.xml,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- project.xml 27 Aug 2004 16:17:28 -0000 1.51
+++ project.xml 4 Oct 2004 03:54:46 -0000 1.52
@@ -251,6 +251,12 @@
<email></email>
<organization></organization>
</contributor>
+ <contributor>
+ <name>Mark Lowe</name>
+ <id></id>
+ <email>[EMAIL PROTECTED]</email>
+ <organization></organization>
+ </contributor>
</contributors>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]