You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Mark Lowe <ma...@boxstuff.it> on 2004/09/30 12:53:43 UTC

[validator] ISBNValidator

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


Re: [validator] ISBNValidator

Posted by Henri Yandell <fl...@gmail.com>.
http://www.generationjava.com/research/ISBN.java for one to compare against.

Hen


On Thu, 30 Sep 2004 08:56:13 -0400, Henri Yandell <fl...@gmail.com> wrote:
> Having a quick peek, I don't see any code in there to handle X being
> 10. Have you tested it for an ISBN with an X of 10 yet. Currently it
> looks as though it strips out X's.
> 
> Submitting a unit-test class as well is always very useful, especially
> with new classes.
> 
> Hen
> 
> 
> 
> 
> On Thu, 30 Sep 2004 12:53:43 +0200, Mark Lowe <ma...@boxstuff.it> wrote:
> > 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
> >
> >
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> >
> >
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [validator] ISBNValidator

Posted by Henri Yandell <fl...@gmail.com>.
Having a quick peek, I don't see any code in there to handle X being
10. Have you tested it for an ISBN with an X of 10 yet. Currently it
looks as though it strips out X's.

Submitting a unit-test class as well is always very useful, especially
with new classes.

Hen


On Thu, 30 Sep 2004 12:53:43 +0200, Mark Lowe <ma...@boxstuff.it> wrote:
> 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
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 
> 
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [validator] ISBNValidator

Posted by Mark Lowe <ma...@boxstuff.it>.
Okay Boss.

Apache license and test class submitted.

Mark

On 30 Sep 2004, at 15:09, David Graham wrote:

> Validator uses the standard Java coding conventions.  Considering I  
> don't
> know anything about ISBN numbers I would need a JUnit test case  
> proving it
> works and some javadoc describing the ISBN rules before I would commit
> anything.  Add the Apache 2.0 license to the top of your .java files
> before attaching them to a new bugzilla enhancement ticket.  See other
> validator source files for an example you can cut and paste.
>
> Thanks!
> David
>
> --- Mark Lowe <ma...@boxstuff.it> wrote:
>
>> 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:mark.lowe@boxstuff.com">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: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
> 		
> __________________________________
> Do you Yahoo!?
> New and Improved Yahoo! Mail - Send 10MB messages!
> http://promotions.yahoo.com/new_mail
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [validator] ISBNValidator

Posted by David Graham <gr...@yahoo.com>.
Validator uses the standard Java coding conventions.  Considering I don't
know anything about ISBN numbers I would need a JUnit test case proving it
works and some javadoc describing the ISBN rules before I would commit
anything.  Add the Apache 2.0 license to the top of your .java files
before attaching them to a new bugzilla enhancement ticket.  See other
validator source files for an example you can cut and paste.

Thanks!
David

--- Mark Lowe <ma...@boxstuff.it> wrote:

> 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:mark.lowe@boxstuff.com">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: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org