001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import org.apache.oro.text.perl.Perl5Util;
020:
021: /**
022: * <p>Perform email validations.</p>
023: * <p>
024: * This class is a Singleton; you can retrieve the instance via the getInstance() method.
025: * </p>
026: * <p>
027: * Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
028: * http://javascript.internet.com
029: * </p>
030: * <p>
031: * This implementation is not guaranteed to catch all possible errors in an email address.
032: * For example, an address like nobody@noplace.somedog will pass validator, even though there
033: * is no TLD "somedog"
034: * </p>.
035: *
036: * @version $Revision: 478560 $ $Date: 2006-11-23 13:09:27 +0000 (Thu, 23 Nov 2006) $
037: * @since Validator 1.1
038: */
039: public class EmailValidator {
040:
041: private static final String SPECIAL_CHARS = "\\000-\\037\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]\\177";
042: private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS
043: + "]";
044: private static final String QUOTED_USER = "(\"[^\"]*\")";
045: private static final String ATOM = VALID_CHARS + '+';
046: private static final String WORD = "((" + VALID_CHARS + "|')+|"
047: + QUOTED_USER + ")";
048:
049: // Each pattern must be surrounded by /
050: private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
051: private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";
052: private static final String IP_DOMAIN_PATTERN = "/^\\[(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\\]$/";
053: private static final String TLD_PATTERN = "/^([a-zA-Z]+)$/";
054:
055: private static final String USER_PATTERN = "/^\\s*" + WORD + "(\\."
056: + WORD + ")*$/";
057: private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\."
058: + ATOM + ")*\\s*$/";
059: private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
060:
061: /**
062: * Singleton instance of this class.
063: */
064: private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
065:
066: /**
067: * Returns the Singleton instance of this validator.
068: * @return singleton instance of this validator.
069: */
070: public static EmailValidator getInstance() {
071: return EMAIL_VALIDATOR;
072: }
073:
074: /**
075: * Protected constructor for subclasses to use.
076: */
077: protected EmailValidator() {
078: super ();
079: }
080:
081: /**
082: * <p>Checks if a field has a valid e-mail address.</p>
083: *
084: * @param email The value validation is being performed on. A <code>null</code>
085: * value is considered invalid.
086: * @return true if the email address is valid.
087: */
088: public boolean isValid(String email) {
089: if (email == null) {
090: return false;
091: }
092:
093: Perl5Util matchAsciiPat = new Perl5Util();
094: if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) {
095: return false;
096: }
097:
098: email = stripComments(email);
099:
100: // Check the whole email address structure
101: Perl5Util emailMatcher = new Perl5Util();
102: if (!emailMatcher.match(EMAIL_PATTERN, email)) {
103: return false;
104: }
105:
106: if (email.endsWith(".")) {
107: return false;
108: }
109:
110: if (!isValidUser(emailMatcher.group(1))) {
111: return false;
112: }
113:
114: if (!isValidDomain(emailMatcher.group(2))) {
115: return false;
116: }
117:
118: return true;
119: }
120:
121: /**
122: * Returns true if the domain component of an email address is valid.
123: * @param domain being validatied.
124: * @return true if the email address's domain is valid.
125: */
126: protected boolean isValidDomain(String domain) {
127: boolean symbolic = false;
128: Perl5Util ipAddressMatcher = new Perl5Util();
129:
130: if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) {
131: if (!isValidIpAddress(ipAddressMatcher)) {
132: return false;
133: } else {
134: return true;
135: }
136: } else {
137: // Domain is symbolic name
138: Perl5Util domainMatcher = new Perl5Util();
139: symbolic = domainMatcher.match(DOMAIN_PATTERN, domain);
140: }
141:
142: if (symbolic) {
143: if (!isValidSymbolicDomain(domain)) {
144: return false;
145: }
146: } else {
147: return false;
148: }
149:
150: return true;
151: }
152:
153: /**
154: * Returns true if the user component of an email address is valid.
155: * @param user being validated
156: * @return true if the user name is valid.
157: */
158: protected boolean isValidUser(String user) {
159: Perl5Util userMatcher = new Perl5Util();
160: return userMatcher.match(USER_PATTERN, user);
161: }
162:
163: /**
164: * Validates an IP address. Returns true if valid.
165: * @param ipAddressMatcher Pattren matcher
166: * @return true if the ip address is valid.
167: */
168: protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) {
169: for (int i = 1; i <= 4; i++) {
170: String ipSegment = ipAddressMatcher.group(i);
171: if (ipSegment == null || ipSegment.length() <= 0) {
172: return false;
173: }
174:
175: int iIpSegment = 0;
176:
177: try {
178: iIpSegment = Integer.parseInt(ipSegment);
179: } catch (NumberFormatException e) {
180: return false;
181: }
182:
183: if (iIpSegment > 255) {
184: return false;
185: }
186:
187: }
188: return true;
189: }
190:
191: /**
192: * Validates a symbolic domain name. Returns true if it's valid.
193: * @param domain symbolic domain name
194: * @return true if the symbolic domain name is valid.
195: */
196: protected boolean isValidSymbolicDomain(String domain) {
197: String[] domainSegment = new String[10];
198: boolean match = true;
199: int i = 0;
200: Perl5Util atomMatcher = new Perl5Util();
201: while (match) {
202: match = atomMatcher.match(ATOM_PATTERN, domain);
203: if (match) {
204: domainSegment[i] = atomMatcher.group(1);
205: int l = domainSegment[i].length() + 1;
206: domain = (l >= domain.length()) ? "" : domain
207: .substring(l);
208:
209: i++;
210: }
211: }
212:
213: int len = i;
214:
215: // Make sure there's a host name preceding the domain.
216: if (len < 2) {
217: return false;
218: }
219:
220: // TODO: the tld should be checked against some sort of configurable
221: // list
222: String tld = domainSegment[len - 1];
223: if (tld.length() > 1) {
224: Perl5Util matchTldPat = new Perl5Util();
225: if (!matchTldPat.match(TLD_PATTERN, tld)) {
226: return false;
227: }
228: } else {
229: return false;
230: }
231:
232: return true;
233: }
234:
235: /**
236: * Recursively remove comments, and replace with a single space. The simpler
237: * regexps in the Email Addressing FAQ are imperfect - they will miss escaped
238: * chars in atoms, for example.
239: * Derived From Mail::RFC822::Address
240: * @param emailStr The email address
241: * @return address with comments removed.
242: */
243: protected String stripComments(String emailStr) {
244: String input = emailStr;
245: String result = emailStr;
246: String commentPat = "s/^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/$1 /osx";
247: Perl5Util commentMatcher = new Perl5Util();
248: result = commentMatcher.substitute(commentPat, input);
249: // This really needs to be =~ or Perl5Matcher comparison
250: while (!result.equals(input)) {
251: input = result;
252: result = commentMatcher.substitute(commentPat, input);
253: }
254: return result;
255:
256: }
257: }
|