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 05:10:32 GMT
dgraham     2003/05/02 22:10:32

  Modified:    validator/src/share/org/apache/commons/validator
                        UrlValidator.java
  Log:
  Formatting cleanup only.
  
  Revision  Changes    Path
  1.8       +351 -318  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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- UrlValidator.java	3 May 2003 05:08:01 -0000	1.7
  +++ UrlValidator.java	3 May 2003 05:10:32 -0000	1.8
  @@ -108,304 +108,338 @@
    * @version $Revision$ $Date$
    */
   public class UrlValidator implements Serializable {
  -   private static final String alphaChars = "a-zA-Z"; 
  -   private static final String alphaNumChars = alphaChars + "\\d"; 
  -   private static final String specialChars = ";/@&=,.?:+$";
  -   private static final String validChars = "[^\\s" + specialChars + "]";
  -
  -   private static final String schemeChars = alphaChars; // Drop numeric, and  "+-." for
now
  -   private static final String authorityChars = alphaNumChars + "\\-\\.";
  -   private static final String atom = validChars + '+';
  -
  -   /**
  -    * This expression derived/taken from the BNF for URI (RFC2396).
  -    */
  -   private static final String urlPat = "/^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?/";
  -   //                                                                      12         
  3  4          5       6   7        8 9
  -   
  -   /**
  -    * Schema/Protocol (ie. http:, ftp:, file:, etc).
  -    */
  -   private static final int PARSE_URL_SCHEME = 2; 
  -   
  -   /**
  -    * Includes hostname/ip and port number.
  -    */
  -   private static final int PARSE_URL_AUTHORITY = 4;
  -    
  -   private static final int PARSE_URL_PATH = 5;
  -   private static final int PARSE_URL_QUERY = 7;
  -   private static final int PARSE_URL_FRAGMENT = 9;
  -   
  -   /**
  -    * Protocol (ie. http:, ftp:,https:).
  -    */
  -   private static final String schemePat = "/^[" + schemeChars + "]/";
  -   private static final String authorityPat = "/^([" + authorityChars + "]*)(:\\d*)?(.*)?/";
  -   //                                                                            1    
                     2  3       4
  -   private static final int PARSE_AUTHORITY_HOST_IP = 1;
  -   private static final int PARSE_AUTHORITY_PORT = 2;
  -   
  -   /**
  -    * Should always be empty.
  -    */
  -   private static final int PARSE_AUTHORITY_EXTRA = 3;
  -
  -   private static final String pathPat = "/^(/[-a-zA-Z0-9_:@&?=+,.!/~*'%$]*)$/";
  -   private static final String queryPat = "/^(.*)$/";
  -   private static final String legalAsciiPat = "/^[\\000-\\177]+$/";
  -   private static final String ipV4DomainPat = "/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/";
  -   private static final String domainPat = "/^" + atom + "(\\." + atom + ")*$/";
  -   private static final String portPat = "/^:(\\d{1,5})$/";
  -   private static final String atomPat = "/(" + atom + ")/";
  -   private static final String alphaPat = "/^[" + alphaChars + "]/";
  -
  -   // Non static fields
  -   private boolean allow2Slash = false;
  -   private boolean allowAllScheme = false;
  -   private boolean noFragment = false;
  -   private HashSet allowedSchemeSet;
  -   protected String[] defaultSchemeSet = {"http", "https", "ftp"};
  -
  -   /**
  -    * Create a UrlValidator with default properties.
  -    */
  -   public UrlValidator() {
  -      this(null);
  -   }
  -
  -   /**
  -    * 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
  -    *        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
  -    *        ignore the contents of schemes.
  -    */
  -   public UrlValidator(String[] schemes) {
  -      this(schemes, false, false, false);
  -   }
  -
  -   /**
  -    * 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]);
  -            }
  -
  -         } else if (schemes != null) {
  -            this.allowedSchemeSet = new HashSet(schemes.length);
  -            for (int sIndex = 0; sIndex < schemes.length; sIndex++) {
  -               this.allowedSchemeSet.add(schemes[sIndex]);
  -            }
  -         }
  -      }
  -
  -   }
  -
  -    /**
  -     * <p>Checks if a field has a valid url address.</p>
  -     *
  -     * @param value The value validation is being performed on.
  -     * @return true if the url is valid.
  -     */
  -    public boolean isValid(String value) {
  -    	try {
  -    		Perl5Util matchUrlPat = new Perl5Util();
  -    		Perl5Util matchAsciiPat = new Perl5Util();
  -    
  -    		if (!matchAsciiPat.match(legalAsciiPat, value)) {
  -    			return false;
  -    		}
  -    
  -    		// Check the whole url address structure
  -    		if (!matchUrlPat.match(urlPat, value)) {
  -    			return false;
  -    		}
  -    
  -    		if (!isValidScheme(matchUrlPat.group(PARSE_URL_SCHEME))) {
  -    			return false;
  -    		}
  -    
  -    		if (!isValidAuthority(matchUrlPat.group(PARSE_URL_AUTHORITY))) {
  -    			return false;
  -    		}
  -    
  -    		if (!isValidPath(matchUrlPat.group(PARSE_URL_PATH))) {
  -    			return false;
  -    		}
  -    
  -    		if (!isValidQuery(matchUrlPat.group(PARSE_URL_QUERY))) {
  -    			return false;
  -    		}
  -    
  -    		if (!isValidFragment(matchUrlPat.group(PARSE_URL_FRAGMENT))) {
  -    			return false;
  -    		}
  -    
  -    	} catch (Exception e) {
  -            // TODO Do we need to catch Exception?
  -    		return false;
  -    	}
  -    
  -    	return true;
  -    }
  -
  -   /**
  -    * Validate scheme. If schemes[] was initialized to a non null,
  -    * then only those scheme's are allowed.  Note this is slightly different
  -    * than for the Constructor.
  -    * @param scheme The scheme to validate.
  -    * @return   true is valid.
  -    */
  -   protected boolean isValidScheme(String scheme) {
  -      Perl5Util matchSchemePat = new Perl5Util();
  -      boolean bValid = matchSchemePat.match(schemePat, scheme);
  -      if (bValid) {
  -         if (allowedSchemeSet != null) {
  -            bValid = allowedSchemeSet.contains(scheme);
  -         }
  -      }
  -      return bValid;
  -   }
  -
  -    /**
  -     * Returns true if the authority is properly formatted.  An authority is the combination
  -     * of hostname and port.
  -     */
  -   protected boolean isValidAuthority(String authority) {
  -      boolean bValid = true;
  -      Perl5Util matchAuthorityPat = new Perl5Util();
  -      Perl5Util matchIPV4Pat = new Perl5Util();
  -      Perl5Util matchDomainPat = new Perl5Util();
  -      Perl5Util matchAtomPat = new Perl5Util();
  -      Perl5Util matchPortPat = new Perl5Util();
  -      Perl5Util matchAlphaPat = new Perl5Util();
  -
  -      bValid = matchAuthorityPat.match(authorityPat, authority);
  -
  -      if (bValid) {
  -         boolean ipV4Address = false;
  -         boolean hostname = false;
  -         // check if authority is IP address or hostname
  -         String hostIP = matchAuthorityPat.group(PARSE_AUTHORITY_HOST_IP);
  -         ipV4Address = matchIPV4Pat.match(ipV4DomainPat, hostIP);
  -
  -         if (ipV4Address) {
  -            // this is an IP address so check components
  -            for (int i = 1; i <= 4; i++) {
  -               String ipSegment = matchIPV4Pat.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;
  -               }
  -            }
  -         } else {
  -            // Domain is hostname name
  -            hostname = matchDomainPat.match(domainPat, hostIP);
  -         }
  -         //rightmost hostname will never start with a digit.
  -         if (hostname) {
  -            // this is a hostname authority so check components
  -            String[] domainSegment = new String[10];
  -            boolean match = true;
  -            int segmentCount = 0;
  -            int segmentLength = 0;
  -
  -            while (match) {
  -               match = matchAtomPat.match(atomPat, hostIP);
  -               if (match) {
  -                  domainSegment[segmentCount] = matchAtomPat.group(1);
  -                  segmentLength = domainSegment[segmentCount].length() + 1;
  -                  hostIP = (segmentLength >= hostIP.length()) ? "" : hostIP.substring(segmentLength);
  -                  segmentCount++;
  -               }
  -            }
  -            String topLevel = domainSegment[segmentCount - 1];
  -            if (topLevel.length() < 2 || topLevel.length() > 4) {
  -               bValid = false;
  -            }
  -
  -            // First letter of top level must be a alpha
  -            boolean isAlpha;
  -            isAlpha = matchAlphaPat.match(alphaPat, topLevel.substring(0, 1));
  -            if (!isAlpha) {
  -               bValid = false;
  -            }
  -
  -            // Make sure there's a host name preceding the authority.
  -            if (segmentCount < 2) {
  -               bValid = false;
  -            }
  -         }
  -
  -         if (bValid) {
  -            bValid = (hostname || ipV4Address);
  -         }
  -
  -         if (bValid) {
  -            String port = matchAuthorityPat.group(PARSE_AUTHORITY_PORT);
  -            if (port != null) {
  -               bValid = matchPortPat.match(portPat, port);
  -            }
  -         }
  -
  -         if (bValid) {
  -            String extra = matchAuthorityPat.group(PARSE_AUTHORITY_EXTRA);
  -            bValid = ((extra == null) || (extra.length() == 0));
  -         }
  -
  -
  -      }
  -      return bValid;
  -   }
  -
  -   protected boolean isValidPath(String path) {
  -      Perl5Util matchPathPat = new Perl5Util();
  -      boolean bValid = true;
  -
  -      bValid = matchPathPat.match(pathPat, path);
  -      if (bValid) {  //Shouldn't end with a '/'
  -         bValid = (path.lastIndexOf("/") < (path.length() - 1));
  -      }
  -      
  -      if (bValid) {
  -         int slash2Count = countToken("//", path);
  -         if (!allow2Slash) {
  -            bValid = (slash2Count == 0);
  -         }
  -         
  -         if (bValid) {
  -            int slashCount = countToken("/", path);
  -            int dot2Count = countToken("..", path);
  -            if (dot2Count > 0) {
  -               bValid = ((slashCount - slash2Count - 1) > dot2Count);
  -            }
  -         }
  -      }
  -      return bValid;
  -   }
  +	private static final String alphaChars = "a-zA-Z";
  +    
  +	private static final String alphaNumChars = alphaChars + "\\d";
  +    
  +	private static final String specialChars = ";/@&=,.?:+$";
  +    
  +	private static final String validChars = "[^\\s" + specialChars + "]";
  +
  +	private static final String schemeChars = alphaChars;
  +    
  +	// Drop numeric, and  "+-." for now
  +	private static final String authorityChars = alphaNumChars + "\\-\\.";
  +    
  +	private static final String atom = validChars + '+';
  +
  +	/**
  +	 * This expression derived/taken from the BNF for URI (RFC2396).
  +	 */
  +	private static final String urlPat =
  +		"/^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?/";
  +	//                                                                      12           
3  4          5       6   7        8 9
  +
  +	/**
  +	 * Schema/Protocol (ie. http:, ftp:, file:, etc).
  +	 */
  +	private static final int PARSE_URL_SCHEME = 2;
  +
  +	/**
  +	 * Includes hostname/ip and port number.
  +	 */
  +	private static final int PARSE_URL_AUTHORITY = 4;
  +
  +	private static final int PARSE_URL_PATH = 5;
  +    
  +	private static final int PARSE_URL_QUERY = 7;
  +    
  +	private static final int PARSE_URL_FRAGMENT = 9;
  +
  +	/**
  +	 * Protocol (ie. http:, ftp:,https:).
  +	 */
  +	private static final String schemePat = "/^[" + schemeChars + "]/";
  +    
  +	private static final String authorityPat =
  +		"/^([" + authorityChars + "]*)(:\\d*)?(.*)?/";
  +	//                                                                            1      
                   2  3       4
  +    
  +	private static final int PARSE_AUTHORITY_HOST_IP = 1;
  +    
  +	private static final int PARSE_AUTHORITY_PORT = 2;
  +
  +	/**
  +	 * Should always be empty.
  +	 */
  +	private static final int PARSE_AUTHORITY_EXTRA = 3;
  +
  +	private static final String pathPat =
  +		"/^(/[-a-zA-Z0-9_:@&?=+,.!/~*'%$]*)$/";
  +        
  +	private static final String queryPat = "/^(.*)$/";
  +    
  +	private static final String legalAsciiPat = "/^[\\000-\\177]+$/";
  +    
  +	private static final String ipV4DomainPat =
  +		"/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/";
  +        
  +	private static final String domainPat =
  +		"/^" + atom + "(\\." + atom + ")*$/";
  +        
  +	private static final String portPat = "/^:(\\d{1,5})$/";
  +    
  +	private static final String atomPat = "/(" + atom + ")/";
  +    
  +	private static final String alphaPat = "/^[" + alphaChars + "]/";
  +
  +	// Non static fields
  +	private boolean allow2Slash = false;
  +	private boolean allowAllScheme = false;
  +	private boolean noFragment = false;
  +	private HashSet allowedSchemeSet;
  +	protected String[] defaultSchemeSet = { "http", "https", "ftp" };
  +
  +	/**
  +	 * Create a UrlValidator with default properties.
  +	 */
  +	public UrlValidator() {
  +		this(null);
  +	}
  +
  +	/**
  +	 * 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
  +	 *        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
  +	 *        ignore the contents of schemes.
  +	 */
  +	public UrlValidator(String[] schemes) {
  +		this(schemes, false, false, false);
  +	}
  +
  +	/**
  +	 * 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]);
  +				}
  +
  +			} else if (schemes != null) {
  +				this.allowedSchemeSet = new HashSet(schemes.length);
  +				for (int sIndex = 0; sIndex < schemes.length; sIndex++) {
  +					this.allowedSchemeSet.add(schemes[sIndex]);
  +				}
  +			}
  +		}
  +
  +	}
  +
  +	/**
  +	 * <p>Checks if a field has a valid url address.</p>
  +	 *
  +	 * @param value The value validation is being performed on.
  +	 * @return true if the url is valid.
  +	 */
  +	public boolean isValid(String value) {
  +		try {
  +			Perl5Util matchUrlPat = new Perl5Util();
  +			Perl5Util matchAsciiPat = new Perl5Util();
  +
  +			if (!matchAsciiPat.match(legalAsciiPat, value)) {
  +				return false;
  +			}
  +
  +			// Check the whole url address structure
  +			if (!matchUrlPat.match(urlPat, value)) {
  +				return false;
  +			}
  +
  +			if (!isValidScheme(matchUrlPat.group(PARSE_URL_SCHEME))) {
  +				return false;
  +			}
  +
  +			if (!isValidAuthority(matchUrlPat.group(PARSE_URL_AUTHORITY))) {
  +				return false;
  +			}
  +
  +			if (!isValidPath(matchUrlPat.group(PARSE_URL_PATH))) {
  +				return false;
  +			}
  +
  +			if (!isValidQuery(matchUrlPat.group(PARSE_URL_QUERY))) {
  +				return false;
  +			}
  +
  +			if (!isValidFragment(matchUrlPat.group(PARSE_URL_FRAGMENT))) {
  +				return false;
  +			}
  +
  +		} catch (Exception e) {
  +			// TODO Do we need to catch Exception?
  +			return false;
  +		}
  +
  +		return true;
  +	}
  +
  +	/**
  +	 * Validate scheme. If schemes[] was initialized to a non null,
  +	 * then only those scheme's are allowed.  Note this is slightly different
  +	 * than for the Constructor.
  +	 * @param scheme The scheme to validate.
  +	 * @return   true is valid.
  +	 */
  +	protected boolean isValidScheme(String scheme) {
  +		Perl5Util matchSchemePat = new Perl5Util();
  +		boolean bValid = matchSchemePat.match(schemePat, scheme);
  +		if (bValid) {
  +			if (allowedSchemeSet != null) {
  +				bValid = allowedSchemeSet.contains(scheme);
  +			}
  +		}
  +		return bValid;
  +	}
  +
  +	/**
  +	 * Returns true if the authority is properly formatted.  An authority is the combination
  +	 * of hostname and port.
  +	 */
  +	protected boolean isValidAuthority(String authority) {
  +		boolean bValid = true;
  +		Perl5Util matchAuthorityPat = new Perl5Util();
  +		Perl5Util matchIPV4Pat = new Perl5Util();
  +		Perl5Util matchDomainPat = new Perl5Util();
  +		Perl5Util matchAtomPat = new Perl5Util();
  +		Perl5Util matchPortPat = new Perl5Util();
  +		Perl5Util matchAlphaPat = new Perl5Util();
  +
  +		bValid = matchAuthorityPat.match(authorityPat, authority);
  +
  +		if (bValid) {
  +			boolean ipV4Address = false;
  +			boolean hostname = false;
  +			// check if authority is IP address or hostname
  +			String hostIP = matchAuthorityPat.group(PARSE_AUTHORITY_HOST_IP);
  +			ipV4Address = matchIPV4Pat.match(ipV4DomainPat, hostIP);
  +
  +			if (ipV4Address) {
  +				// this is an IP address so check components
  +				for (int i = 1; i <= 4; i++) {
  +					String ipSegment = matchIPV4Pat.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;
  +					}
  +				}
  +			} else {
  +				// Domain is hostname name
  +				hostname = matchDomainPat.match(domainPat, hostIP);
  +			}
  +			//rightmost hostname will never start with a digit.
  +			if (hostname) {
  +				// this is a hostname authority so check components
  +				String[] domainSegment = new String[10];
  +				boolean match = true;
  +				int segmentCount = 0;
  +				int segmentLength = 0;
  +
  +				while (match) {
  +					match = matchAtomPat.match(atomPat, hostIP);
  +					if (match) {
  +						domainSegment[segmentCount] = matchAtomPat.group(1);
  +						segmentLength =
  +							domainSegment[segmentCount].length() + 1;
  +						hostIP =
  +							(segmentLength >= hostIP.length())
  +								? ""
  +								: hostIP.substring(segmentLength);
  +						segmentCount++;
  +					}
  +				}
  +				String topLevel = domainSegment[segmentCount - 1];
  +				if (topLevel.length() < 2 || topLevel.length() > 4) {
  +					bValid = false;
  +				}
  +
  +				// First letter of top level must be a alpha
  +				boolean isAlpha;
  +				isAlpha =
  +					matchAlphaPat.match(alphaPat, topLevel.substring(0, 1));
  +				if (!isAlpha) {
  +					bValid = false;
  +				}
  +
  +				// Make sure there's a host name preceding the authority.
  +				if (segmentCount < 2) {
  +					bValid = false;
  +				}
  +			}
  +
  +			if (bValid) {
  +				bValid = (hostname || ipV4Address);
  +			}
  +
  +			if (bValid) {
  +				String port = matchAuthorityPat.group(PARSE_AUTHORITY_PORT);
  +				if (port != null) {
  +					bValid = matchPortPat.match(portPat, port);
  +				}
  +			}
  +
  +			if (bValid) {
  +				String extra = matchAuthorityPat.group(PARSE_AUTHORITY_EXTRA);
  +				bValid = ((extra == null) || (extra.length() == 0));
  +			}
  +
  +		}
  +		return bValid;
  +	}
  +
  +	protected boolean isValidPath(String path) {
  +		Perl5Util matchPathPat = new Perl5Util();
  +		boolean bValid = true;
  +
  +		bValid = matchPathPat.match(pathPat, path);
  +		if (bValid) { //Shouldn't end with a '/'
  +			bValid = (path.lastIndexOf("/") < (path.length() - 1));
  +		}
  +
  +		if (bValid) {
  +			int slash2Count = countToken("//", path);
  +			if (!allow2Slash) {
  +				bValid = (slash2Count == 0);
  +			}
  +
  +			if (bValid) {
  +				int slashCount = countToken("/", path);
  +				int dot2Count = countToken("..", path);
  +				if (dot2Count > 0) {
  +					bValid = ((slashCount - slash2Count - 1) > dot2Count);
  +				}
  +			}
  +		}
  +		return bValid;
  +	}
   
   	/**
   	 * Returns true if the query is null or it's a properly formatted query string.
  @@ -430,20 +464,19 @@
   		return (noFragment == false);
   	}
   
  -    
  -    /**
  -     * Returns the number of times the token appears in the target.
  -     */
  -    protected int countToken(String token, String target) {
  -    	int tokenIndex = 0;
  -    	int count = 0;
  -    	while (tokenIndex != -1) {
  -    		tokenIndex = target.indexOf(token, tokenIndex);
  -    		if (tokenIndex > -1) {
  -    			tokenIndex++;
  -    			count++;
  -    		}
  -    	}
  -    	return count;
  -    }
  +	/**
  +	 * Returns the number of times the token appears in the target.
  +	 */
  +	protected int countToken(String token, String target) {
  +		int tokenIndex = 0;
  +		int count = 0;
  +		while (tokenIndex != -1) {
  +			tokenIndex = target.indexOf(token, tokenIndex);
  +			if (tokenIndex > -1) {
  +				tokenIndex++;
  +				count++;
  +			}
  +		}
  +		return count;
  +	}
   }
  
  
  

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