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 DateValidator.java GenericValidator.java
Date Thu, 01 May 2003 02:42:30 GMT
dgraham     2003/04/30 19:42:30

  Modified:    validator/src/share/org/apache/commons/validator
                        GenericValidator.java
  Added:       validator/src/share/org/apache/commons/validator
                        DateValidator.java
  Log:
  Refactored date validations in to new DateValidator class.
  
  Revision  Changes    Path
  1.22      +36 -84    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.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- GenericValidator.java	1 May 2003 02:15:16 -0000	1.21
  +++ GenericValidator.java	1 May 2003 02:42:30 -0000	1.22
  @@ -63,9 +63,6 @@
   
   import java.io.Serializable;
   import java.util.Locale;
  -import java.text.DateFormat;
  -import java.text.SimpleDateFormat;
  -import java.text.ParseException;
   
   import org.apache.oro.text.perl.Perl5Util;
   
  @@ -91,6 +88,7 @@
       * UrlValidator used in wrapper method, lazy initialization.
       */
      private static UrlValidator urlValidator = null;
  +   
      /**
       * <p>Checks if the field isn't null and length of the field is greater than zero
not
       * including whitespace.</p>
  @@ -104,18 +102,17 @@
      /**
       * <p>Checks if the value matches the regular expression.</p>
       *
  -    * @param 	value 		The value validation is being performed on.
  -    * @param 	regexp		The regular expression.
  +    * @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;
   
  -      if (regexp != null && regexp.length() > 0) {
  -         Perl5Util r = new Perl5Util();
  -         match = r.match( ValidatorUtil.getDelimitedRegExp(regexp), value);
  +      if (regexp == null || regexp.length() <= 0) {
  +         return false;
         }
  -
  -      return match;
  +      
  +      Perl5Util matcher = new Perl5Util();
  +      return matcher.match(ValidatorUtil.getDelimitedRegExp(regexp), value);
      }
   
      /**
  @@ -148,7 +145,7 @@
      /**
       * <p>Checks if the value can safely be converted to a long primitive.</p>
       *
  -    * @param 	value 		The value validation is being performed on.
  +    * @param value The value validation is being performed on.
       */
      public static boolean isLong(String value) {
         return (GenericTypeValidator.formatLong(value) != null);
  @@ -157,7 +154,7 @@
      /**
       * <p>Checks if the value can safely be converted to a float primitive.</p>
       *
  -    * @param 	value 		The value validation is being performed on.
  +    * @param value The value validation is being performed on.
       */
      public static boolean isFloat(String value) {
         return (GenericTypeValidator.formatFloat(value) != null);
  @@ -166,47 +163,24 @@
      /**
       * <p>Checks if the value can safely be converted to a double primitive.</p>
       *
  -    * @param 	value 		The value validation is being performed on.
  +    * @param value The value validation is being performed on.
       */
      public static boolean isDouble(String value) {
         return (GenericTypeValidator.formatDouble(value) != null);
      }
   
  -   /**
  -    * <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 	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;
  -
  -      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);
  -
  -            formatter.parse(value);
  -         } catch (ParseException e) {
  -            bValid = false;
  -         }
  -      } else {
  -         bValid = false;
  -      }
  -
  -      return bValid;
  -   }
  +    /**
  +     * <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 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) {
  +    	return DateValidator.getInstance().isValid(value, locale);
  +    }
   
      /**
       * <p>Checks if the field is a valid date.  The pattern is used with
  @@ -215,35 +189,12 @@
       * 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.
  +    * @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;
  -
  -      if (value != null && datePattern != null && datePattern.length()
> 0) {
  -         try {
  -            SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
  -            formatter.setLenient(false);
  -
  -            formatter.parse(value);
  -
  -            if (strict) {
  -               if (datePattern.length() != value.length()) {
  -                  bValid = false;
  -               }
  -            }
  -
  -         } catch (ParseException e) {
  -            bValid = false;
  -         }
  -      } else {
  -         bValid = false;
  -      }
  -
  -      return bValid;
  +        return DateValidator.getInstance().isValid(value, datePattern, strict);
      }
   
      /**
  @@ -342,16 +293,17 @@
       * @param value The value validation is being performed on.
       */
      public static boolean isUrl(String value) {
  -      if (urlValidator == null)
  +      if (urlValidator == null) {
            urlValidator = new UrlValidator();
  -      return (urlValidator.isValid(value));
  +      }
  +      return urlValidator.isValid(value);
      }
   
      /**
       * <p>Checks if the value's length is less than or equal to the max.</p>
       *
  -    * @param 	value 		The value validation is being performed on.
  -    * @param 	max		The maximum length.
  +    * @param value The value validation is being performed on.
  +    * @param max The maximum length.
       */
      public static boolean maxLength(String value, int max) {
         return (value.length() <= max);
  
  
  
  1.1                  jakarta-commons/validator/src/share/org/apache/commons/validator/DateValidator.java
  
  Index: DateValidator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/DateValidator.java,v
1.1 2003/05/01 02:42:30 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/05/01 02:42:30 $
   *
   * ====================================================================
   *
   * 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;
  
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Locale;
  
  /**
   * <p>Perform date validations.</p>
   * <p>
   * This class is a Singleton; you can retrieve the instance via the getInstance() method.
   * </p>
   *
   * @author David Winterfeldt
   * @author James Turner
   * @author <a href="mailto:husted@apache.org">Ted Husted</a>
   * @author David Graham
   * @version $Revision: 1.1 $
   */
  public class DateValidator {
  
  	/**
  	 * Singleton instance of this class.
  	 */
  	private static final DateValidator instance = new DateValidator();
  
  	/**
  	 * Returns the Singleton instance of this validator.
  	 */
  	public static DateValidator getInstance() {
  		return instance;
  	}
  
  	/**
  	 * Protected constructor for subclasses to use.
  	 */
  	protected DateValidator() {
  		super();
  	}
  
  	/**
  	 * <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 boolean isValid(String value, String datePattern, boolean strict) {
  
  		if (value == null
  			|| datePattern == null
  			|| datePattern.length() <= 0) {
                  
  			return false;
  		}
  
  		SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
  		formatter.setLenient(false);
  
  		try {
  			formatter.parse(value);
  		} catch (ParseException e) {
  			return false;
  		}
  
  		if (strict && (datePattern.length() != value.length())) {
  			return false;
  		}
  
  		return true;
  	}
  
  	/**
  	 * <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 locale The locale to use for the date format, defaults to the default 
  	 * system default if null.
  	 */
  	public boolean isValid(String value, Locale locale) {
  
  		if (value == null) {
  			return false;
  		}
  
  		DateFormat formatter = null;
  		if (locale != null) {
  			formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
  		} else {
  			formatter =
  				DateFormat.getDateInstance(
  					DateFormat.SHORT,
  					Locale.getDefault());
  		}
  
  		formatter.setLenient(false);
  
  		try {
  			formatter.parse(value);
  		} catch (ParseException e) {
  			return false;
  		}
  
  		return true;
  	}
  
  }
  
  
  

---------------------------------------------------------------------
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