commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator CreditCardValidator.java GenericValidator.java
Date Tue, 29 Apr 2003 01:45:43 GMT
dgraham     2003/04/28 18:45:43

  Modified:    validator/src/share/org/apache/commons/validator
                        GenericValidator.java
  Added:       validator/src/share/org/apache/commons/validator
                        CreditCardValidator.java
  Log:
  Refactored credit card related methods into a new CreditCardValidator
  class.
  
  Revision  Changes    Path
  1.14      +45 -115   jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java
  
  Index: GenericValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- GenericValidator.java	15 Mar 2003 23:18:12 -0000	1.13
  +++ GenericValidator.java	29 Apr 2003 01:45:43 -0000	1.14
  @@ -74,6 +74,8 @@
    *
    * @author David Winterfeldt
    * @author James Turner
  + * @author <a href="mailto:husted@apache.org">Ted Husted</a>
  + * @author David Graham
    * @version $Revision$ $Date$
    */
   public class GenericValidator implements Serializable {
  @@ -81,7 +83,7 @@
       /**
        * Delimiter to put around a regular expression 
        * following Perl 5 syntax.
  -    */
  +     */
       public final static String REGEXP_DELIM = "/";
   
       /**
  @@ -89,7 +91,7 @@
        * including whitespace.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isBlankOrNull(String value) {
           return ((value == null) || (value.trim().length() == 0));
       }
  @@ -99,7 +101,7 @@
        *
        * @param 	value 		The value validation is being performed on.
        * @param 	regexp		The regular expression.
  -    */
  +     */
       public static boolean matchRegexp(String value, String regexp) {
           boolean match = false;
   
  @@ -115,7 +117,7 @@
        * <p>Checks if the value can safely be converted to a byte primitive.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isByte(String value) {
           return (GenericTypeValidator.formatByte(value) != null);
       }
  @@ -124,7 +126,7 @@
        * <p>Checks if the value can safely be converted to a short primitive.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isShort(String value) {
           return (GenericTypeValidator.formatShort(value) != null);
       }
  @@ -133,7 +135,7 @@
        * <p>Checks if the value can safely be converted to a int primitive.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isInt(String value) {
           return (GenericTypeValidator.formatInt(value) != null);
       }
  @@ -142,7 +144,7 @@
        * <p>Checks if the value can safely be converted to a long primitive.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isLong(String value) {
           return (GenericTypeValidator.formatLong(value) != null);
       }
  @@ -151,7 +153,7 @@
        * <p>Checks if the value can safely be converted to a float primitive.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isFloat(String value) {
           return (GenericTypeValidator.formatFloat(value) != null);
       }
  @@ -160,7 +162,7 @@
        * <p>Checks if the value can safely be converted to a double primitive.</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isDouble(String value) {
           return (GenericTypeValidator.formatDouble(value) != null);
       }
  @@ -172,7 +174,7 @@
        *
        * @param 	value 		The value validation is being performed on.
        * @param 	Locale	        The locale to use for the date format, defaults to the default
system default if null.
  -    */
  +     */
       public static boolean isDate(String value, Locale locale) {
           boolean bValid = true;
   
  @@ -211,7 +213,7 @@
        * @param 	value 		The value validation is being performed on.
        * @param 	datePattern	The pattern passed to <code>SimpleDateFormat</code>.
        * @param 	strict	        Whether or not to have an exact match of the datePattern.
  -    */
  +     */
       public static boolean isDate(String value, String datePattern, boolean strict) {
   
           boolean bValid = true;
  @@ -246,7 +248,7 @@
        * @param 	value 		The value validation is being performed on.
        * @param 	min		The minimum value of the range.
        * @param 	max		The maximum value of the range.
  -    */
  +     */
       public static boolean isInRange(int value, int min, int max) {
           return ((value >= min) && (value <= max));
       }
  @@ -258,7 +260,7 @@
        * @param 	value 		The value validation is being performed on.
        * @param 	min		The minimum value of the range.
        * @param 	max		The maximum value of the range.
  -    */
  +     */
       public static boolean isInRange(float value, float min, float max) {
           return ((value >= min) && (value <= max));
       }
  @@ -270,7 +272,7 @@
        * @param 	value 		The value validation is being performed on.
        * @param 	min		The minimum value of the range.
        * @param 	max		The maximum value of the range.
  -    */
  +     */
       public static boolean isInRange(short value, short min, short max) {
           return ((value >= min) && (value <= max));
       }
  @@ -282,112 +284,40 @@
        * @param 	value 		The value validation is being performed on.
        * @param 	min		The minimum value of the range.
        * @param 	max		The maximum value of the range.
  -    */
  +     */
       public static boolean isInRange(double value, double min, double max) {
           return ((value >= min) && (value <= max));
       }
   
       /**
  -     * <p>Checks if the field is a valid credit card number.</p>
  -     * <p>Translated to Java by Ted Husted (<a href="mailto:husted@apache.org">husted@apache.org</a>).<br>
  -     * &nbsp;&nbsp;&nbsp; Reference Sean M. Burke's script at http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl</p>
  +     * Checks if the field is a valid credit card number.
        *
  -     * @param 	value 		The value validation is being performed on.
  -    */
  +     * @param value The value validation is being performed on.
  +     */
       public static boolean isCreditCard(String value) {
  -        return (
  -            validateCreditCardLuhnCheck(value)
  -                && validateCreditCardPrefixCheck(value));
  +		return CreditCardValidator.getInstance().isCreditCard(value);
       }
  -
  +    
       /**
  -     * <p>Checks for a valid credit card number.</p>
  -     * <p>Translated to Java by Ted Husted (<a href="mailto:husted@apache.org">husted@apache.org</a>).<br>
  -     * &nbsp;&nbsp;&nbsp; Reference Sean M. Burke's script at http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl</p>
  -     *
  -     * @param 	cardNumber 		Credit Card Number
  -    */
  -    protected static boolean validateCreditCardLuhnCheck(String cardNumber) {
  -        // number must be validated as 0..9 numeric first!!
  -        int digits = cardNumber.length();
  -        int oddoeven = digits & 1;
  -        long sum = 0;
  -        for (int count = 0; count < digits; count++) {
  -            int digit = 0;
  -            try {
  -                digit = Integer.parseInt(String.valueOf(cardNumber.charAt(count)));
  -            } catch (NumberFormatException e) {
  -                return false;
  -            }
  -            if (((count & 1) ^ oddoeven) == 0) { // not
  -                digit *= 2;
  -                if (digit > 9) {
  -                    digit -= 9;
  -                }
  -            }
  -            sum += digit;
  -        }
  -        if (sum == 0) {
  -            return false;
  -        }
  -
  -        if (sum % 10 == 0) {
  -            return true;
  -        }
  -
  -        return false;
  -    }
  -
  -    /**
  -     * <p>Checks for a valid credit card number.</p>
  -     * <p>Translated to Java by Ted Husted (<a href="mailto:husted@apache.org">husted@apache.org</a>).<br>
  -     * &nbsp;&nbsp;&nbsp; Reference Sean M. Burke's script at http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl</p>
  +     * Checks for a valid credit card number.
        *
  -     * @param 	cardNumber 		Credit Card Number
  -    */
  -    protected static boolean validateCreditCardPrefixCheck(String cardNumber) {
  -        final String AX_PREFIX = "34,37,";
  -        final String VS_PREFIX = "4";
  -        final String MC_PREFIX = "51,52,53,54,55,";
  -        final String DS_PREFIX = "6011";
  -
  -        int length = cardNumber.length();
  -        if (length < 13) {
  -            return false;
  -        }
  -
  -        boolean valid = false;
  -        int cardType = 0;
  -
  -        String prefix2 = cardNumber.substring(0, 2) + ",";
  -
  -        if (AX_PREFIX.indexOf(prefix2) != -1) {
  -            cardType = 3;
  -        }
  -        if (cardNumber.substring(0, 1).equals(VS_PREFIX)) {
  -            cardType = 4;
  -        }
  -        if (MC_PREFIX.indexOf(prefix2) != -1) {
  -            cardType = 5;
  -        }
  -        if (cardNumber.substring(0, 4).equals(DS_PREFIX)) {
  -            cardType = 6;
  -        }
  -
  -        if ((cardType == 3) && (length == 15)) {
  -            valid = true;
  -        }
  -        if ((cardType == 4) && ((length == 13) || (length == 16))) {
  -            valid = true;
  -        }
  -        if ((cardType == 5) && (length == 16)) {
  -            valid = true;
  -        }
  -        if ((cardType == 6) && (length == 16)) {
  -            valid = true;
  -        }
  -
  -        return valid;
  +     * @param cardNumber Credit Card Number.
  +     * @deprecated Use CreditCardValidator.validateCreditCardLuhnCheck() instead.
  +     */
  +	protected static boolean validateCreditCardLuhnCheck(String cardNumber) {
  +		return CreditCardValidator.getInstance().validateCreditCardLuhnCheck(
  +			cardNumber);
  +	}
  +    
  +    /**
  +     * Checks for a valid credit card number.
  +     *
  +     * @param cardNumber Credit Card Number.
  +     * @deprecated Use CreditCardValidator.validateCreditCardPrefixCheck() instead.
  +     */
  +    protected boolean validateCreditCardPrefixCheck(String cardNumber) {
  +        return CreditCardValidator.getInstance().validateCreditCardPrefixCheck(
  +                    cardNumber);
       }
   
       /**
  @@ -396,7 +326,7 @@
        * http://javascript.internet.com</p>
        *
        * @param 	value 		The value validation is being performed on.
  -    */
  +     */
       public static boolean isEmail(String value) {
           boolean bValid = true;
   
  
  
  
  1.1                  jakarta-commons/validator/src/share/org/apache/commons/validator/CreditCardValidator.java
  
  Index: CreditCardValidator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/CreditCardValidator.java,v
1.1 2003/04/29 01:45:43 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/29 01:45:43 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.validator;
  
  /**
   * <p>Perform credit card validations.</p>
   * <p>
   * This class is a Singleton; you can retrieve the instance via the getInstance() method.
   * </p>
   * Reference Sean M. Burke's script at 
   * http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
   * 
   * @author David Winterfeldt
   * @author James Turner
   * @author <a href="mailto:husted@apache.org">Ted Husted</a>
   * @author David Graham
   * @version $Revision: 1.1 $ $Date: 2003/04/29 01:45:43 $
   */
  public class CreditCardValidator {
  
  	private static final String AX_PREFIX = "34,37,";
  	private static final String VS_PREFIX = "4";
  	private static final String MC_PREFIX = "51,52,53,54,55,";
  	private static final String DS_PREFIX = "6011";
  
  	/**
  	 * Singleton instance of this class.
  	 */
  	private static final CreditCardValidator instance =
  		new CreditCardValidator();
  
  	/**
  	 * Returns the Singleton instance of this validator.
  	 */
  	public static CreditCardValidator getInstance() {
  		return instance;
  	}
  
  	/**
  	 * Protected constructor for subclasses to use.
  	 */
  	protected CreditCardValidator() {
  		super();
  	}
  
  	/**
  	 * Checks if the field is a valid credit card number.
  	 *
  	 * @param value The value validation is being performed on.
  	 */
  	public boolean isCreditCard(String value) {
  		return (
  			this.validateCreditCardLuhnCheck(value)
  				&& this.validateCreditCardPrefixCheck(value));
  	}
  
  	/**
  	 * Checks for a valid credit card number.
  	 *
  	 * @param cardNumber Credit Card Number.
  	 */
  	protected boolean validateCreditCardLuhnCheck(String cardNumber) {
  		// number must be validated as 0..9 numeric first!!
  		int digits = cardNumber.length();
  		int oddoeven = digits & 1;
  		long sum = 0;
  		for (int count = 0; count < digits; count++) {
  			int digit = 0;
  			try {
  				digit =
  					Integer.parseInt(String.valueOf(cardNumber.charAt(count)));
  			} catch (NumberFormatException e) {
  				return false;
  			}
  			if (((count & 1) ^ oddoeven) == 0) { // not
  				digit *= 2;
  				if (digit > 9) {
  					digit -= 9;
  				}
  			}
  			sum += digit;
  		}
  		if (sum == 0) {
  			return false;
  		}
  
  		if (sum % 10 == 0) {
  			return true;
  		}
  
  		return false;
  	}
  
  	/**
  	 * Checks for a valid credit card number.
  	 *
  	 * @param cardNumber Credit Card Number.
  	 */
  	protected boolean validateCreditCardPrefixCheck(String cardNumber) {
  
  		int length = cardNumber.length();
  		if (length < 13) {
  			return false;
  		}
  
  		boolean valid = false;
  		int cardType = 0;
  
  		String prefix2 = cardNumber.substring(0, 2) + ",";
  
  		if (AX_PREFIX.indexOf(prefix2) != -1) {
  			cardType = 3;
  		}
  		if (cardNumber.substring(0, 1).equals(VS_PREFIX)) {
  			cardType = 4;
  		}
  		if (MC_PREFIX.indexOf(prefix2) != -1) {
  			cardType = 5;
  		}
  		if (cardNumber.substring(0, 4).equals(DS_PREFIX)) {
  			cardType = 6;
  		}
  
  		if ((cardType == 3) && (length == 15)) {
  			valid = true;
  		}
  		if ((cardType == 4) && ((length == 13) || (length == 16))) {
  			valid = true;
  		}
  		if ((cardType == 5) && (length == 16)) {
  			valid = true;
  		}
  		if ((cardType == 6) && (length == 16)) {
  			valid = true;
  		}
  
  		return valid;
  	}
  
  }
  
  
  

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


Mime
View raw message