一、前言
基于apache的commons-validator源码包中的org.apache.commons.validator.EmailValidator类,对邮件地址进行验证校验isValid、验证域名是否有效isValidDomain、验证用户是否有效isValidUser、验证IP地址是否有效isValidIpAddress等。
二、源码说明
package org.apache.commons.validator;@b@@b@import org.apache.oro.text.perl.Perl5Util;@b@@b@public class EmailValidator@b@{@b@ private static final String SPECIAL_CHARS = "\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]";@b@ private static final String VALID_CHARS = "[^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]";@b@ private static final String QUOTED_USER = "(\"[^\"]*\")";@b@ private static final String ATOM = "[^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+";@b@ private static final String WORD = "(([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]|')+|(\"[^\"]*\"))";@b@ private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";@b@ private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";@b@ private static final String IP_DOMAIN_PATTERN = "/^\\[(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\\]$/";@b@ private static final String TLD_PATTERN = "/^([a-zA-Z]+)$/";@b@ private static final String USER_PATTERN = "/^\\s*(([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]|')+|(\"[^\"]*\"))(\\.(([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]|')+|(\"[^\"]*\")))*$/";@b@ private static final String DOMAIN_PATTERN = "/^[^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+(\\.[^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+)*\\s*$/";@b@ private static final String ATOM_PATTERN = "/([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+)/";@b@ private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();@b@@b@ public static EmailValidator getInstance()@b@ {@b@ return EMAIL_VALIDATOR;@b@ }@b@@b@ public boolean isValid(String email)@b@ {@b@ if (email == null) {@b@ return false;@b@ }@b@@b@ Perl5Util matchAsciiPat = new Perl5Util();@b@ if (!(matchAsciiPat.match("/^[\\000-\\177]+$/", email))) {@b@ return false;@b@ }@b@@b@ email = stripComments(email);@b@@b@ Perl5Util emailMatcher = new Perl5Util();@b@ if (!(emailMatcher.match("/^(.+)@(.+)$/", email))) {@b@ return false;@b@ }@b@@b@ if (email.endsWith(".")) {@b@ return false;@b@ }@b@@b@ if (!(isValidUser(emailMatcher.group(1)))) {@b@ return false;@b@ }@b@@b@ return (isValidDomain(emailMatcher.group(2)));@b@ }@b@@b@ protected boolean isValidDomain(String domain)@b@ {@b@ boolean symbolic = false;@b@ Perl5Util ipAddressMatcher = new Perl5Util();@b@@b@ if (ipAddressMatcher.match("/^\\[(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\\]$/", domain))@b@ {@b@ return (isValidIpAddress(ipAddressMatcher));@b@ }@b@@b@ Perl5Util domainMatcher = new Perl5Util();@b@ symbolic = domainMatcher.match("/^[^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+(\\.[^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+)*\\s*$/", domain);@b@@b@ if (symbolic) {@b@ if (isValidSymbolicDomain(domain)) break label66;@b@ return false;@b@ }@b@@b@ return false;@b@@b@ label66: return true;@b@ }@b@@b@ protected boolean isValidUser(String user)@b@ {@b@ Perl5Util userMatcher = new Perl5Util();@b@ return userMatcher.match("/^\\s*(([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]|')+|(\"[^\"]*\"))(\\.(([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]|')+|(\"[^\"]*\")))*$/", user);@b@ }@b@@b@ protected boolean isValidIpAddress(Perl5Util ipAddressMatcher)@b@ {@b@ for (int i = 1; i <= 4; ++i) {@b@ String ipSegment = ipAddressMatcher.group(i);@b@ if ((ipSegment == null) || (ipSegment.length() <= 0)) {@b@ return false;@b@ }@b@@b@ int iIpSegment = 0;@b@ try@b@ {@b@ iIpSegment = Integer.parseInt(ipSegment);@b@ } catch (NumberFormatException e) {@b@ return false;@b@ }@b@@b@ if (iIpSegment > 255)@b@ return false;@b@@b@ }@b@@b@ return true;@b@ }@b@@b@ protected boolean isValidSymbolicDomain(String domain)@b@ {@b@ String[] domainSegment = new String[10];@b@ boolean match = true;@b@ int i = 0;@b@ Perl5Util atomMatcher = new Perl5Util();@b@ while (match) {@b@ match = atomMatcher.match("/([^\\s\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]]+)/", domain);@b@ if (match) {@b@ domainSegment[i] = atomMatcher.group(1);@b@ int l = domainSegment[i].length() + 1;@b@ domain = (l >= domain.length()) ? "" : domain.substring(l);@b@@b@ ++i;@b@ }@b@ }@b@@b@ int len = i;@b@@b@ if (len < 2) {@b@ return false;@b@ }@b@@b@ String tld = domainSegment[(len - 1)];@b@ if (tld.length() > 1) {@b@ Perl5Util matchTldPat = new Perl5Util();@b@ if (!(matchTldPat.match("/^([a-zA-Z]+)$/", tld)))@b@ return false;@b@ }@b@ else {@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ protected String stripComments(String emailStr)@b@ {@b@ String input = emailStr;@b@ String result = emailStr;@b@ String commentPat = "s/^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|I111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/$1 /osx";@b@ Perl5Util commentMatcher = new Perl5Util();@b@ result = commentMatcher.substitute(commentPat, input);@b@@b@ while (!(result.equals(input))) {@b@ input = result;@b@ result = commentMatcher.substitute(commentPat, input);@b@ }@b@ return result;@b@ }@b@}