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 UrlValidator.java
Date Sat, 03 May 2003 20:22:43 GMT
dgraham     2003/05/03 13:22:43

  Modified:    validator/src/test/org/apache/commons/validator UrlTest.java
               validator/src/share/org/apache/commons/validator
                        UrlValidator.java
  Log:
  Changed individual option booleans to use a Flags instance.
  This simplified setting options and uncovered a bug in UrlTest
  that demonstrates why setting options in this new manner
  is less error prone.
  
  Revision  Changes    Path
  1.4       +17 -11    jakarta-commons/validator/src/test/org/apache/commons/validator/UrlTest.java
  
  Index: UrlTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/UrlTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- UrlTest.java	2 May 2003 18:28:19 -0000	1.3
  +++ UrlTest.java	3 May 2003 20:22:43 -0000	1.4
  @@ -104,10 +104,15 @@
      }
   
      public void testIsValid() {
  -      testIsValid(testUrlParts, true,false,false);
  -      System.out.println("\nTesting options ....");
  -      setUp();
  -      testIsValid(testUrlPartsOptions, true,true,true);
  +    	testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
  +    	System.out.println("\nTesting options ....");
  +    	setUp();
  +    	int options =
  +    		UrlValidator.ALLOW_2_SLASHES
  +    			+ UrlValidator.ALLOW_ALL_SCHEMES
  +    			+ UrlValidator.NO_FRAGMENTS;
  +    
  +    	testIsValid(testUrlPartsOptions, options);
      }
   
      public void testIsValidScheme() {
  @@ -115,7 +120,8 @@
            System.out.print("\n testIsValidScheme() ");
         }
         String[] schemes = {"http", "gopher"};
  -      UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
  +      //UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
  +      UrlValidator urlVal = new UrlValidator(schemes, 0);
         for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
            TestPair testPair = testScheme[sIndex];
            boolean result = urlVal.isValidScheme(testPair.item);
  @@ -140,8 +146,8 @@
       *
       * @param testObjects Used to create a url.
       */
  -   public void testIsValid(Object[] testObjects, boolean allow2Slash, boolean noFragment,boolean
allScheme) {
  -      UrlValidator urlVal = new UrlValidator(null, allow2Slash,noFragment,allScheme);
  +   public void testIsValid(Object[] testObjects, int options) {
  +      UrlValidator urlVal = new UrlValidator(null, options);
         int statusPerLine = 60;
         int printed = 0;
         if (printIndex)  {
  
  
  
  1.10      +74 -47    jakarta-commons/validator/src/share/org/apache/commons/validator/UrlValidator.java
  
  Index: UrlValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/UrlValidator.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- UrlValidator.java	3 May 2003 15:53:53 -0000	1.9
  +++ UrlValidator.java	3 May 2003 20:22:43 -0000	1.10
  @@ -62,8 +62,11 @@
   package org.apache.commons.validator;
   
   import java.io.Serializable;
  +import java.util.Arrays;
   import java.util.HashSet;
  +import java.util.Set;
   
  +import org.apache.commons.validator.util.Flags;
   import org.apache.oro.text.perl.Perl5Util;
   
   /**
  @@ -109,6 +112,23 @@
    * @version $Revision$ $Date$
    */
   public class UrlValidator implements Serializable {
  +    
  +    /**
  +     * Allows all validly formatted schemes to pass validation instead of supplying a 
  +     * set of valid schemes.
  +     */
  +    public static final int ALLOW_ALL_SCHEMES = 1;
  +    
  +    /**
  +     * Allow two slashes in the path component of the URL.
  +     */
  +    public static final int ALLOW_2_SLASHES = 2;
  +    
  +    /**
  +     * Enabling this options disallows any URL fragments.
  +     */
  +    public static final int NO_FRAGMENTS = 4;
  +    
   	private static final String ALPHA_CHARS = "a-zA-Z";
       
   	private static final String ALPHA_NUMERIC_CHARS = ALPHA_CHARS + "\\d";
  @@ -183,13 +203,21 @@
   	private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
       
   	private static final String ALPHA_PATTERN = "/^[" + ALPHA_CHARS + "]/";
  -
  -	// Non static fields
  -	private boolean allow2Slash = false;
  -	private boolean allowAllScheme = false;
  -	private boolean noFragment = false;
  -	private HashSet allowedSchemeSet;
  -	protected String[] defaultSchemeSet = { "http", "https", "ftp" };
  +    
  +    /**
  +     * Holds the set of current validation options.
  +     */
  +    private Flags options = null;
  +
  +    /**
  +     * The set of schemes that are allowed to be in a URL.
  +     */
  +	private Set allowedSchemes = null;
  +    
  +    /**
  +     * If no schemes are provided, default to this set.
  +     */
  +	protected String[] defaultSchemes = { "http", "https", "ftp" };
   
   	/**
   	 * Create a UrlValidator with default properties.
  @@ -199,48 +227,46 @@
   	}
   
   	/**
  -	 * Behavour of validation is modified by passing in several strings options:
  -	 * @param schemes Pass in one or more url scheme to consider valid, passing in
  +	 * Behavior of validation is modified by passing in several strings options:
  +	 * @param schemes Pass in one or more url schemes to consider valid, passing in
   	 *        a null will default to "http,https,ftp" being valid.
   	 *        If a non-null schemes is specified then all valid schemes must
  -	 *        be specified. Setting the allowAllScheme option to true will
  +	 *        be specified. Setting the ALLOW_ALL_SCHEMES option will
   	 *        ignore the contents of schemes.
   	 */
   	public UrlValidator(String[] schemes) {
  -		this(schemes, false, false, false);
  +		this(schemes, 0);
   	}
  +    
  +    /**
  +     * Initialize a UrlValidator with the given validation options.
  +     * @param options The options should be set using the public constants declared in
  +     * this class.  To set multiple options you simply add them together.  For example,

  +     * ALLOW_2_SLASHES + NO_FRAGMENTS enables both of those options. 
  +     */
  +    public UrlValidator(int options) {
  +        this(null, options);
  +    }
   
   	/**
   	 * Behavour of validation is modified by passing in options:
  -	 *   @param allowAllScheme If true, allows all validly formatted schemes. [false]
  -	 *   @param allow2Slash If true, allows double '/' characters in the path  component,
[false]
  -	 *   @param noFragment If true, fragments are flagged as illegal.[false]
  -	 */
  -	public UrlValidator(
  -		String[] schemes,
  -		boolean allowAllScheme,
  -		boolean allow2Slash,
  -		boolean noFragment) {
  -            
  -		this.allowAllScheme = allowAllScheme;
  -		this.allow2Slash = allow2Slash;
  -		this.noFragment = noFragment;
  -
  -		if (!this.allowAllScheme) {
  -			if (schemes == null) {
  -				this.allowedSchemeSet = new HashSet(defaultSchemeSet.length);
  -				for (int sIndex = 0;
  -					sIndex < defaultSchemeSet.length;
  -					sIndex++) {
  -					this.allowedSchemeSet.add(defaultSchemeSet[sIndex]);
  -				}
  +     * @param schemes The set of valid schemes.
  +	 * @param options The options should be set using the public constants declared in
  +     * this class.  To set multiple options you simply add them together.  For example,

  +     * ALLOW_2_SLASHES + NO_FRAGMENTS enables both of those options. 
  +	 */
  +	public UrlValidator(String[] schemes, int options) {
  +		this.options = new Flags(options);
   
  -			} else if (schemes != null) {
  -				this.allowedSchemeSet = new HashSet(schemes.length);
  -				for (int sIndex = 0; sIndex < schemes.length; sIndex++) {
  -					this.allowedSchemeSet.add(schemes[sIndex]);
  -				}
  -			}
  +		if (this.options.isOn(ALLOW_ALL_SCHEMES)) {
  +			return;
  +		}
  +
  +		if (schemes == null) {
  +			this.allowedSchemes =
  +				new HashSet(Arrays.asList(this.defaultSchemes));
  +		} else {
  +			this.allowedSchemes = new HashSet(Arrays.asList(schemes));
   		}
   
   	}
  @@ -304,8 +330,8 @@
   		Perl5Util matchSchemePat = new Perl5Util();
   		boolean bValid = matchSchemePat.match(SCHEME_PATTERN, scheme);
   		if (bValid) {
  -			if (allowedSchemeSet != null) {
  -				bValid = allowedSchemeSet.contains(scheme);
  +			if (allowedSchemes != null) {
  +				bValid = allowedSchemes.contains(scheme);
   			}
   		}
   		return bValid;
  @@ -422,7 +448,8 @@
   
   		if (bValid) {
   			int slash2Count = countToken("//", path);
  -			if (!allow2Slash) {
  +
  +			if (this.options.isOff(ALLOW_2_SLASHES)) {
   				bValid = (slash2Count == 0);
   			}
   
  @@ -457,7 +484,7 @@
   			return true;
   		}
   
  -		return (noFragment == false);
  +        return this.options.isOff(NO_FRAGMENTS);
   	}
   
   	/**
  
  
  

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