You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dw...@apache.org on 2002/03/30 05:25:59 UTC

cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator GenericTypeValidator.java GenericValidator.java

dwinterfeldt    02/03/29 20:25:59

  Modified:    validator/src/share/org/apache/commons/validator
                        GenericValidator.java
  Added:       validator/src/share/org/apache/commons/validator
                        GenericTypeValidator.java
  Log:
  Modified GenericValidtor to use GenericTypeValidator.  GenericValidator returns boolean from validation methods and GenericTypeValidator returns a correctly typed object if the validation was successful and null if it wasn't.
  
  Revision  Changes    Path
  1.4       +12 -42    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- GenericValidator.java	13 Mar 2002 05:39:32 -0000	1.3
  +++ GenericValidator.java	30 Mar 2002 04:25:59 -0000	1.4
  @@ -1,4 +1,9 @@
   /*
  + * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v 1.4 2002/03/30 04:25:59 dwinterfeldt Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/03/30 04:25:59 $
  + *
  + * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  @@ -72,6 +77,7 @@
    * performing validations.</p>
    *
    * @author David Winterfeldt
  + * @version $Revision: 1.4 $ $Date: 2002/03/30 04:25:59 $
   */
   public class GenericValidator implements Serializable {
   
  @@ -107,13 +113,7 @@
        * @param 	value 		The value validation is being performed on.
       */
       public static boolean isByte(String value) {
  -       try {
  -          Byte.parseByte(value);
  -       } catch (Exception e) {
  -          return false;
  -       }
  -       
  -       return true;
  +       return (GenericTypeValidator.formatByte(value) != null);
       }
   
       /**
  @@ -122,13 +122,7 @@
        * @param 	value 		The value validation is being performed on.
       */
       public static boolean isShort(String value) {
  -       try {
  -          Short.parseShort(value);
  -       } catch (Exception e) {
  -          return false;
  -       }
  -       
  -       return true;
  +       return (GenericTypeValidator.formatShort(value) != null);
       }
   
       /**
  @@ -137,13 +131,7 @@
        * @param 	value 		The value validation is being performed on.
       */
       public static boolean isInt(String value) {
  -       try {
  -          Integer.parseInt(value);
  -       } catch (Exception e) {
  -          return false;
  -       }
  -       
  -       return true;
  +       return (GenericTypeValidator.formatInt(value) != null);
       }
   
       /**
  @@ -152,13 +140,7 @@
        * @param 	value 		The value validation is being performed on.
       */
       public static boolean isLong(String value) {
  -       try {
  -          Long.parseLong(value);
  -       } catch (Exception e) {
  -          return false;
  -       }
  -       
  -       return true;
  +       return (GenericTypeValidator.formatLong(value) != null);
       }
   
       /**
  @@ -167,13 +149,7 @@
        * @param 	value 		The value validation is being performed on.
       */
       public static boolean isFloat(String value) {
  -       try {
  -          Float.parseFloat(value);
  -       } catch (Exception e) {
  -          return false;
  -       }
  -       
  -       return true;
  +       return (GenericTypeValidator.formatFloat(value) != null);
       }
   
       /**
  @@ -182,13 +158,7 @@
        * @param 	value 		The value validation is being performed on.
       */
       public static boolean isDouble(String value) {
  -       try {
  -          Double.parseDouble(value);
  -       } catch (Exception e) {
  -          return false;
  -       }
  -       
  -       return true;
  +       return (GenericTypeValidator.formatDouble(value) != null);
       }
   
       /**
  
  
  
  1.1                  jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java
  
  Index: GenericTypeValidator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java,v 1.1 2002/03/30 04:25:59 dwinterfeldt Exp $
   * $Revision: 1.1 $
   * $Date: 2002/03/30 04:25:59 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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;
  
  import java.io.Serializable;
  import java.util.Date;
  import java.util.Locale;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.text.ParseException;
  import org.apache.regexp.RE;
  import org.apache.regexp.RESyntaxException;
  
  
  /**
   * <p>This class contains basic methods for 
   * performing validations that return the 
   * correctly typed class based on the 
   * validation performed.</p>
   *
   * @author David Winterfeldt
   * @version $Revision: 1.1 $ $Date: 2002/03/30 04:25:59 $
  */
  public class GenericTypeValidator implements Serializable {
      
      /**
       * <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 Byte formatByte(String value) {
         Byte result = null;
         
         try {
            result = new Byte(value);
         } catch (Exception e) {
         }
         
         return result;
      }
  
      /**
       * <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 Short formatShort(String value) {
         Short result = null;
         
         try {
            result = new Short(value);
         } catch (Exception e) {
         }
         
         return result;
      }
  
      /**
       * <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 Integer formatInt(String value) {
         Integer result = null;
         
         try {
            result = new Integer(value);
         } catch (Exception e) {
         }
         
         return result;
      }
  
      /**
       * <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 Long formatLong(String value) {
         Long result = null;
         
         try {
            result = new Long(value);
         } catch (Exception e) {
         }
         
         return result;
      }
  
      /**
       * <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 Float formatFloat(String value) {
         Float result = null;
         
         try {
            result = new Float(value);
         } catch (Exception e) {
         }
         
         return result;
      }
  
      /**
       * <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 Double formatDouble(String value) {
         Double result = null;
         
         try {
            result = new Double(value);
         } catch (Exception e) {
         }
         
         return result;
      }
  
      /**
       * <p>Checks if the field is a valid date.  The <code>Locale</code> is 
       * used with <code>java.text.DateFormat</code>.  The setLenient method 
       * is set to <code>false</code> for all.</p>
       *
       * @param 	value 		The value validation is being performed on.
       * @param 	datePattern	The pattern passed to <code>SimpleDateFormat</code>.
      */
      public static Date formatDate(String value, Locale locale) {
  	Date date = null;
  
  	if (value != null) {
  	   try {
  	      DateFormat formatter = null;
  	      if (locale != null) {
  	         formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
  	      } else {
  	         formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  	      }
  	         
                formatter.setLenient(false);
                
                date = formatter.parse(value);
      	   } catch (ParseException e) {
             }
          }
  
          return date;
      }	
      
      /**
       * <p>Checks if the field is a valid date.  The pattern is used with 
       * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the 
       * length will be checked so '2/12/1999' will not pass validation with 
       * the format 'MM/dd/yyyy' because the month isn't two digits. 
       * The setLenient method is set to <code>false</code> for all.</p>
       *
       * @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 Date formatDate(String value, String datePattern, boolean strict) {
          Date date = null;
  
  	if (value != null && datePattern != null && datePattern.length() > 0) {
  	   try {
                SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
                formatter.setLenient(false);
                
                date = formatter.parse(value);
                
                if (strict) {
                   if (datePattern.length() != value.length()) {
                      date = null;
                   }
                }
      	   } catch (ParseException e) {
             }
          }
  
          return date;
      }	
  
      /**
       * <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>
       *
       * @param 	value 		The value validation is being performed on.
      */
      public static Integer formatCreditCard(String value) {
         Integer result = null;
         
         if (GenericValidator.validateCreditCardLuhnCheck(value) && GenericValidator.validateCreditCardPrefixCheck(value)) {
            try {
               result = new Integer(value);
            } catch (Exception e) {
            }
         }
         
         return result;
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>