Hello
I've just written an ISBN validator I don't think i can diff a file that doesn't exist. But i'd like to see if its of use to anyone other than myself.
I've attached it and i'll just check the commons stuff out of cvs and see if there's a jalopy template that could iron out anything that would be displeasing to anyone in terms of coding.
What do i have to do to get this committed?
I still need to add an option to switch checking the regular expression off in case folk want to use it to check numbers with no hyphens or spaces. At the moment the input can be either.
Mark
package com.boxstuff.bookshop.util;
import org.apache.oro.text.perl.Perl5Util; /** *<p>A class for validating ISBN numbers. * Based on document <a href="http://www.isbn.org/standards/home/isbn/international/html/usm4.htm"> * http://www.isbn.org/standards/home/isbn/international/html/usm4.htm</a></p> * @author <a href="mailto:[EMAIL PROTECTED]">Mark Lowe</a> */ public class ISBNValidator { /** * @return isbn regular expression */ private final static String ISBN_NUMBER_PATTERN = "([0-9 ]|-){11}(-| )[0-9X]"; /** * @return isbn regular expression including ISBN prefix */ private final static String ISBN_PATTERN = "(ISBN[= ])?" + ISBN_NUMBER_PATTERN; /** * <p>Empty contructor.</p> */ public ISBNValidator() { } /** * <p>First checked against reqular expression to see if hyphens or spaces are correct. * Second validates actual number. ISBN is weighted from length to penultimate number which is used as a control number. * The sum of each digit and respective weight is calcuated into a total along with the conrtol number. * If the total returns 0 for modulus 11 it returns valid.</p> * * @param isbn Candidate ISBN number to be validated. * @return whether the number is valid or not. */ public boolean isValid(String isbn) { boolean result = false; if (!isValidPattern(isbn)) { return false; } isbn = clean(isbn); int j = isbn.length(); int total = 0; int sum = 0; int num = 0; int control = Character.getNumericValue(isbn.charAt(isbn.length() - 1)); for (int i = 0; (i < isbn.length()) && (j >= 2); i++) { num = Character.getNumericValue(isbn.charAt(i)); sum = j * num; total = total + sum; j--; } total = total + control; result = (total % 11) == 0; return result; } /** * @param isbn The string to be cleaned before full validation. * @return clean isbn string without any non digits. */ private String clean(String isbn) { String result = ""; int j = 0; for (int i = 0; i < isbn.length(); i++) { char ch = isbn.charAt(i); if (Character.isDigit(ch)) { result += ch; } } return new String(result); } /** * @param isbn Full isbn code * @return whether the isbn code fits matches regular expression * @see org.apache.oro.text.perl.Perl5Util#match */ private boolean isValidPattern(String isbn) { Perl5Util regexpValidator = new Perl5Util(); return regexpValidator.match("/" + ISBN_NUMBER_PATTERN + "/", isbn); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]