Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 21592 invoked from network); 3 May 2003 00:08:05 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 3 May 2003 00:08:05 -0000 Received: (qmail 28752 invoked by uid 97); 3 May 2003 00:10:14 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 28745 invoked from network); 3 May 2003 00:10:13 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 3 May 2003 00:10:13 -0000 Received: (qmail 21336 invoked by uid 500); 3 May 2003 00:08:03 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 21321 invoked by uid 500); 3 May 2003 00:08:03 -0000 Received: (qmail 21318 invoked from network); 3 May 2003 00:08:03 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 3 May 2003 00:08:03 -0000 Received: (qmail 65110 invoked by uid 1581); 3 May 2003 00:08:02 -0000 Date: 3 May 2003 00:08:02 -0000 Message-ID: <20030503000802.65109.qmail@icarus.apache.org> From: dgraham@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator EmailValidator.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N dgraham 2003/05/02 17:08:02 Modified: validator/src/share/org/apache/commons/validator EmailValidator.java Log: More refactoring into smaller methods. Revision Changes Path 1.5 +47 -33 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.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- EmailValidator.java 2 May 2003 23:48:54 -0000 1.4 +++ EmailValidator.java 3 May 2003 00:08:02 -0000 1.5 @@ -121,51 +121,54 @@ * * @param value The value validation is being performed on. */ - public boolean isValid(String value) { - boolean symbolic = false; - + public boolean isValid(String email) { Perl5Util matchAsciiPat = new Perl5Util(); - if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, value)) { + if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) { return false; } // Check the whole email address structure - Perl5Util matchEmailPat = new Perl5Util(); - if (!matchEmailPat.match(EMAIL_PATTERN, value)) { + Perl5Util emailMatcher = new Perl5Util(); + if (!emailMatcher.match(EMAIL_PATTERN, email)) { return false; } - if (value.endsWith(".")) { + if (email.endsWith(".")) { return false; } - // Check the user component of the email address - - String user = matchEmailPat.group(1); - - // See if "user" is valid - Perl5Util matchUserPat = new Perl5Util(); - if (!matchUserPat.match(USER_PATTERN, user)) { - return false; + if(!isValidUser(emailMatcher.group(1))){ + return false; } // Check the domain component of the email address - String domain = matchEmailPat.group(2); + String domain = emailMatcher.group(2); // check if domain is IP address or symbolic - Perl5Util matchIPPat = new Perl5Util(); - boolean ipAddress = matchIPPat.match(IP_DOMAIN_PATTERN, domain); + if(!isValidDomain(domain)){ + return false; + } - if (ipAddress) { - if (!isValidIpAddress(matchIPPat)) { + return true; + } + + /** + * Returns true if the domain component of an email address is valid. + */ + private boolean isValidDomain(String domain) { + boolean symbolic = false; + Perl5Util ipAddressMatcher = new Perl5Util(); + + if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) { + if (!isValidIpAddress(ipAddressMatcher)) { return false; } } else { // Domain is symbolic name - Perl5Util matchDomainPat = new Perl5Util(); - symbolic = matchDomainPat.match(DOMAIN_PATTERN, domain); + Perl5Util domainMatcher = new Perl5Util(); + symbolic = domainMatcher.match(DOMAIN_PATTERN, domain); } - + if (symbolic) { if (!isValidSymbolicDomain(domain)) { return false; @@ -173,16 +176,27 @@ } else { return false; } - + return true; } /** + * Returns true if the user component of an email address is valid. + */ + private boolean isValidUser(String user) { + Perl5Util userMatcher = new Perl5Util(); + if (userMatcher.match(USER_PATTERN, user)) { + return true; + } + return false; + } + + /** * Validates an IP address. Returns true if valid. */ - private boolean isValidIpAddress(Perl5Util matchIPPat) { + private boolean isValidIpAddress(Perl5Util ipAddressMatcher) { for (int i = 1; i <= 4; i++) { - String ipSegment = matchIPPat.group(i); + String ipSegment = ipAddressMatcher.group(i); if (ipSegment == null || ipSegment.length() <= 0) { return false; } @@ -208,12 +222,12 @@ String[] domainSegment = new String[10]; boolean match = true; int i = 0; - Perl5Util matchAtomPat = new Perl5Util(); + Perl5Util atomMatcher = new Perl5Util(); while (match) { - match = matchAtomPat.match(ATOM_PATTERN, domain); + match = atomMatcher.match(ATOM_PATTERN, domain); if (match) { - domainSegment[i] = matchAtomPat.group(1); + domainSegment[i] = atomMatcher.group(1); int l = domainSegment[i].length() + 1; domain = (l >= domain.length()) --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org