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 EmailValidator.java
Date Fri, 02 May 2003 23:48:54 GMT
dgraham     2003/05/02 16:48:54

  Modified:    validator/src/share/org/apache/commons/validator
                        EmailValidator.java
  Log:
  Renamed constants to follow Java standards.  Removed catch
  Exception block.
  
  Revision  Changes    Path
  1.4       +54 -59    jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java
  
  Index: EmailValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EmailValidator.java	2 May 2003 05:25:28 -0000	1.3
  +++ EmailValidator.java	2 May 2003 23:48:54 -0000	1.4
  @@ -81,21 +81,21 @@
    */
   public class EmailValidator {
       
  -    private static final String specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  -    private static final String validChars = "[^\\s" + specialChars + "]";
  -    private static final String quotedUser = "(\"[^\"]*\")";
  -    private static final String atom = validChars + '+';
  -    private static final String word = "(" + atom + "|" + quotedUser + ")";
  +    private static final String SPECIAL_CHARS = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  +    private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
  +    private static final String QUOTED_USER = "(\"[^\"]*\")";
  +    private static final String ATOM = VALID_CHARS + '+';
  +    private static final String WORD = "(" + ATOM + "|" + QUOTED_USER + ")";
       
       // Each pattern must be surrounded by /
  -    private static final String legalAsciiPat = "/^[\\000-\\177]+$/";
  -    private static final String emailPat = "/^(.+)@(.+)$/";
  -    private static final String ipDomainPat =
  +    private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
  +    private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";
  +    private static final String IP_DOMAIN_PATTERN =
       	"/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/";
           
  -    private static final String userPat = "/^" + word + "(\\." + word + ")*$/";
  -    private static final String domainPat = "/^" + atom + "(\\." + atom + ")*$/";
  -    private static final String atomPat = "/(" + atom + ")/";
  +    private static final String USER_PATTERN = "/^" + WORD + "(\\." + WORD + ")*$/";
  +    private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*$/";
  +    private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
   
   	/**
   	 * Singleton instance of this class.
  @@ -124,58 +124,53 @@
   	public boolean isValid(String value) {
   		boolean symbolic = false;
   
  -		try {
  -			Perl5Util matchAsciiPat = new Perl5Util();
  -			if (!matchAsciiPat.match(legalAsciiPat, value)) {
  -				return false;
  -			}
  +		Perl5Util matchAsciiPat = new Perl5Util();
  +		if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, value)) {
  +			return false;
  +		}
   
  -			// Check the whole email address structure
  -			Perl5Util matchEmailPat = new Perl5Util();
  -			if (!matchEmailPat.match(emailPat, value)) {
  -				return false;
  -			}
  +		// Check the whole email address structure
  +		Perl5Util matchEmailPat = new Perl5Util();
  +		if (!matchEmailPat.match(EMAIL_PATTERN, value)) {
  +			return false;
  +		}
   
  -			if (value.endsWith(".")) {
  -				return false;
  -			}
  +		if (value.endsWith(".")) {
  +			return false;
  +		}
   
  -			// Check the user component of the email address
  +		// Check the user component of the email address
   
  -			String user = matchEmailPat.group(1);
  +		String user = matchEmailPat.group(1);
   
  -			// See if "user" is valid
  -			Perl5Util matchUserPat = new Perl5Util();
  -			if (!matchUserPat.match(userPat, user)) {
  -				return false;
  -			}
  +		// See if "user" is valid
  +		Perl5Util matchUserPat = new Perl5Util();
  +		if (!matchUserPat.match(USER_PATTERN, user)) {
  +			return false;
  +		}
   
  -			// Check the domain component of the email address
  -			String domain = matchEmailPat.group(2);
  +		// Check the domain component of the email address
  +		String domain = matchEmailPat.group(2);
   
  -			// check if domain is IP address or symbolic
  -			Perl5Util matchIPPat = new Perl5Util();
  -			boolean ipAddress = matchIPPat.match(ipDomainPat, domain);
  -
  -			if (ipAddress) {
  -				if (!checkIpAddress(matchIPPat)) {
  -					return false;
  -				}
  -			} else {
  -				// Domain is symbolic name
  -				Perl5Util matchDomainPat = new Perl5Util();
  -				symbolic = matchDomainPat.match(domainPat, domain);
  -			}
  +		// check if domain is IP address or symbolic
  +		Perl5Util matchIPPat = new Perl5Util();
  +		boolean ipAddress = matchIPPat.match(IP_DOMAIN_PATTERN, domain);
   
  -			if (symbolic) {
  -				if (!checkSymbolicDomain(domain)) {
  -					return false;
  -				}
  -			} else {
  +		if (ipAddress) {
  +			if (!isValidIpAddress(matchIPPat)) {
   				return false;
   			}
  +		} else {
  +			// Domain is symbolic name
  +			Perl5Util matchDomainPat = new Perl5Util();
  +			symbolic = matchDomainPat.match(DOMAIN_PATTERN, domain);
  +		}
   
  -		} catch (Exception e) {
  +		if (symbolic) {
  +			if (!isValidSymbolicDomain(domain)) {
  +				return false;
  +			}
  +		} else {
   			return false;
   		}
   
  @@ -185,7 +180,7 @@
       /**
        * Validates an IP address. Returns true if valid.
        */
  -	private boolean checkIpAddress(Perl5Util matchIPPat) {
  +	private boolean isValidIpAddress(Perl5Util matchIPPat) {
   		for (int i = 1; i <= 4; i++) {
   			String ipSegment = matchIPPat.group(i);
   			if (ipSegment == null || ipSegment.length() <= 0) {
  @@ -209,14 +204,14 @@
       /**
        * Validates a symbolic domain name.  Returns true if it's valid.
        */
  -	private boolean checkSymbolicDomain(String domain) {
  +	private boolean isValidSymbolicDomain(String domain) {
       	String[] domainSegment = new String[10];
       	boolean match = true;
       	int i = 0;
       	Perl5Util matchAtomPat = new Perl5Util();
       
       	while (match) {
  -    		match = matchAtomPat.match(atomPat, domain);
  +    		match = matchAtomPat.match(ATOM_PATTERN, domain);
       		if (match) {
       			domainSegment[i] = matchAtomPat.group(1);
       			int l = domainSegment[i].length() + 1;
  
  
  

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