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/04/02 03:59:11 UTC

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

dwinterfeldt    02/04/01 17:59:11

  Modified:    validator build.properties.sample build.xml
               validator/src/share/org/apache/commons/validator
                        GenericTypeValidator.java GenericValidator.java
  Log:
  Switched from Jakarta Regexp package to Jakarta ORO.
  
  Revision  Changes    Path
  1.5       +3 -1      jakarta-commons/validator/build.properties.sample
  
  Index: build.properties.sample
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/build.properties.sample,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- build.properties.sample	17 Mar 2002 00:16:37 -0000	1.4
  +++ build.properties.sample	2 Apr 2002 01:59:11 -0000	1.5
  @@ -20,7 +20,9 @@
   # from the Jakarta Commons project.
   commons-logging.jar=e:/software/commons-logging/commons-logging.jar
   
  -regexp.jar=e:/software/regexp/jakarta-regexp-1.2.jar
  +# The JAR file containing version 2.06 (or later) of 
  +# the the Jakarta ORO project.
  +oro.jar = e:/software/jakarta-oro/jakarta-oro.jar
   
   # The JAR file containing version 3.7 (or later) of
   # JUnit Unit Testing (http://www.junit.org)
  
  
  
  1.7       +1 -1      jakarta-commons/validator/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/build.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- build.xml	11 Mar 2002 05:12:02 -0000	1.6
  +++ build.xml	2 Apr 2002 01:59:11 -0000	1.7
  @@ -26,7 +26,7 @@
         <pathelement path="${commons-collections.jar}"/>
         <pathelement path="${commons-digester.jar}"/>
         <pathelement path="${commons-logging.jar}"/>
  -      <pathelement path="${regexp.jar}"/>
  +      <pathelement path="${oro.jar}"/>
      </path>
      
      <path id="test.classpath">
  
  
  
  1.2       +4 -6      jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java
  
  Index: GenericTypeValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- GenericTypeValidator.java	30 Mar 2002 04:25:59 -0000	1.1
  +++ GenericTypeValidator.java	2 Apr 2002 01:59:11 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $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 $
  + * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java,v 1.2 2002/04/02 01:59:11 dwinterfeldt Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/04/02 01:59:11 $
    *
    * ====================================================================
    *
  @@ -68,8 +68,6 @@
   import java.text.DateFormat;
   import java.text.SimpleDateFormat;
   import java.text.ParseException;
  -import org.apache.regexp.RE;
  -import org.apache.regexp.RESyntaxException;
   
   
   /**
  @@ -79,7 +77,7 @@
    * validation performed.</p>
    *
    * @author David Winterfeldt
  - * @version $Revision: 1.1 $ $Date: 2002/03/30 04:25:59 $
  + * @version $Revision: 1.2 $ $Date: 2002/04/02 01:59:11 $
   */
   public class GenericTypeValidator implements Serializable {
       
  
  
  
  1.5       +114 -102  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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- GenericValidator.java	30 Mar 2002 04:25:59 -0000	1.4
  +++ GenericValidator.java	2 Apr 2002 01:59:11 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $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 $
  + * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v 1.5 2002/04/02 01:59:11 dwinterfeldt Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/04/02 01:59:11 $
    *
    * ====================================================================
    *
  @@ -68,20 +68,24 @@
   import java.text.DateFormat;
   import java.text.SimpleDateFormat;
   import java.text.ParseException;
  -import org.apache.regexp.RE;
  -import org.apache.regexp.RESyntaxException;
  -
  +import org.apache.oro.text.perl.Perl5Util;
   
   /**
    * <p>This class contains basic methods for 
    * performing validations.</p>
    *
    * @author David Winterfeldt
  - * @version $Revision: 1.4 $ $Date: 2002/03/30 04:25:59 $
  + * @version $Revision: 1.5 $ $Date: 2002/04/02 01:59:11 $
   */
   public class GenericValidator implements Serializable {
   
       /**
  +     * Delimiter to put around a regular expression 
  +     * following Perl 5 syntax.
  +    */
  +    public final static String REGEXP_DELIM = "/";
  +
  +    /**
        * <p>Checks if the field isn't null and length of the field is greater than zero not 
        * including whitespace.</p>
        *
  @@ -97,13 +101,15 @@
        * @param 	value 		The value validation is being performed on.
        * @param 	regexp		The regular expression.
       */
  -    public static boolean matchRegexp(String value, String regexp) throws RESyntaxException {
  +    public static boolean matchRegexp(String value, String regexp) {
  +       boolean match = false;
  +       
          if (regexp != null && regexp.length() > 0) {
  -          RE r = new RE(regexp);
  -          return (r.match(value));
  -       } else {
  -       	  return false;
  +          Perl5Util r = new Perl5Util();
  +          match = r.match(getDelimittedRegexp(regexp), value);
          }
  +       
  +       return match;
       }        
       
   
  @@ -357,103 +363,102 @@
          boolean bValid = true;
          
          try {
  -          RE emailPat = new RE("^(.+)@(.+)$");
  -          String specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  -          // ??? '/' character was removed before '[' and ']'
  -          // I'm not sure if that was the right thing to do.
  -          String validChars = "[^\\s" + specialChars + "]";
  -          String quotedUser = "(\"[^\"]*\")";
  -          //RE ipDomainPat = new RE("^\\[(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\]$");
  -          RE ipDomainPat = new RE("^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$");
  -          String atom = validChars + '+';
  -          String word = "(" + atom + "|" + quotedUser + ")";
  -          RE userPat = new RE("^" + word + "(\\." + word + ")*$");
  -          RE domainPat = new RE("^" + atom + "(\\." + atom +")*$");
  -
  -	  boolean matchEmailPat = emailPat.match(value);
  -	  	      
  -          if (!matchEmailPat) {
  -             bValid = false;
  -          }
  -
  -          if (bValid) {
  -             if (matchEmailPat) {
  -                String user = emailPat.getParen(1);
  -                
  -                // See if "user" is valid 
  -                if (!userPat.match(user)) {
  -                   bValid = false;
  -                }
  -             } else {
  -                bValid = false;
  -             }
  -          }
  -          
  -          String domain = emailPat.getParen(2);
  -          
  -          if (bValid) {
  -             if (ipDomainPat.match(domain)) {
  -                 // this is an IP address
  +           String specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  +           String validChars = "[^\\s" + specialChars + "]";
  +           String quotedUser = "(\"[^\"]*\")";
  +           String atom = validChars + '+';
  +           String word = "(" + atom + "|" + quotedUser + ")";
  +
  +           // Each pattern must be surrounded by /
  +           String emailPat = getDelimittedRegexp("^(.+)@(.+)$");
  +           String ipDomainPat = getDelimittedRegexp("^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$");
  +           String userPat = getDelimittedRegexp("^" + word + "(\\." + word + ")*$");
  +           String domainPat = getDelimittedRegexp("^" + atom + "(\\." + atom +")*$");
  +           String atomPat = getDelimittedRegexp("(" + atom + ")");
  +         
  +           Perl5Util matchEmailPat = new Perl5Util();
  +           Perl5Util matchUserPat = new Perl5Util();
  +           Perl5Util matchIPPat = new Perl5Util();
  +           Perl5Util matchDomainPat = new Perl5Util();
  +           Perl5Util matchAtomPat = new Perl5Util();
  +         
  +           boolean ipAddress = false;
  +           boolean symbolic = false;
  +         
  +           // Check the whole email address structure
  +           bValid = matchEmailPat.match(emailPat, value);
  +
  +           // Check the user component of the email address
  +           if (bValid) {
  +              String user = matchEmailPat.group(1);
  +              
  +              // See if "user" is valid 
  +              bValid = matchUserPat.match(userPat, user);
  +           }
  +         
  +           // Check the domain component of the email address
  +           if (bValid) {
  +              String domain = matchEmailPat.group(2);
  +         
  +              // check if domain is IP address or symbolic
  +              ipAddress = matchIPPat.match(ipDomainPat, domain);
  +             
  +              if (ipAddress) {
  +                 // this is an IP address so check components
                    for (int i = 1; i <= 4; i++) {
  -                    String ipSegment = ipDomainPat.getParen(i);
  -                    
  -                    if (ipSegment != null && ipSegment.length() > 0) {
  -                       int iIpSegment = 0;
  -                       
  -                       try {
  -                       	  iIpSegment = Integer.parseInt(ipSegment);
  -                       } catch (Exception e) {
  -                          bValid = false;
  -                       }
  -                       
  -                       if (iIpSegment > 255) {
  -                          bValid = false;
  -                       }
  -                    } else {
  -                       bValid = false;
  -                    }
  -                 }
  -             } else {
  -               if (bValid) {
  -                  // Domain is symbolic name
  -                  if (!domainPat.match(domain)) {
  -                     bValid = false;
  -                  }
  -               }
  -               if (bValid) {
  -                  RE atomPat = new RE("(" + atom + ")");
  -                  boolean match = true;
  -                  int pos = 0;
  -                  int i = 0;
  -                  String[] domainSegment = new String[10];
  -                  
  -                  while (match) {
  -                     match = atomPat.match(domain, pos);
  -                     if (match) {
  -                        domainSegment[i] = atomPat.getParen(1);
  -                        pos += domainSegment[i].length() + 1;
  -                        i++;
  +                     String ipSegment = matchIPPat.group(i);
  +                     if (ipSegment != null && ipSegment.length() > 0) {
  +                         int iIpSegment = 0;
  +                         try {
  +                            iIpSegment = Integer.parseInt(ipSegment);
  +                         } catch (Exception e) {
  +                            bValid = false;
  +                         }
  +                     
  +                         if (iIpSegment > 255) {
  +                            bValid = false;
  +                         }
                        } else {
  +                        bValid = false;
                        }
  -                  }
  -                  int len = i;
  -                  if (domainSegment[len - 1].length() < 2 || domainSegment[len - 1].length() > 3) {
  -                     bValid = false;
  -                  }
  -               
  -                  // Make sure there's a host name preceding the domain.
  -                  if (len < 2) {
  +                 }
  +              } else {    
  +                 // Domain is symbolic name
  +                 symbolic = matchDomainPat.match(domainPat, domain);
  +              }
  +              
  +              if (symbolic) {
  +                 // this is a symbolic domain so check components
  +                 String[] domainSegment = new String[10];
  +                 boolean match = true;
  +                 int i = 0;
  +                 int l = 0;
  +                 
  +                 while (match) {
  +                    match = matchAtomPat.match(atomPat, domain);
  +                    if (match) {
  +                       domainSegment[i] = matchAtomPat.group(1);
  +                       l = domainSegment[i].length() + 1;
  +                       domain = (l >= domain.length())? "" : domain.substring(l);
  +                       i++;
  +                    }
  +                 }
  +                 
  +                 int len = i;
  +                 if (domainSegment[len - 1].length() < 2 || domainSegment[len - 1].length() > 4) {
                        bValid = false;
  -                  }
  -               }               	
  -	     }
  -          }
  -       } catch (RESyntaxException e) {
  -          bValid = false;
  +                 }
  +
  +                 // Make sure there's a host name preceding the domain.
  +                 if (len < 2) {
  +                    bValid = false;
  +                 }
  +              }               	
  +	   }
          } catch (Exception e) {
             bValid = false;
          }
  -       
  +
          return bValid;
       }
   
  @@ -477,4 +482,11 @@
          return (value.length() >= min);
       } 
   
  +
  +    /**
  +     * Adds a '/' on either side of the regular expression.
  +    */
  +    protected static String getDelimittedRegexp(String regexp) {
  +       return (REGEXP_DELIM + regexp + REGEXP_DELIM);
  +    }
   }
  
  
  

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