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 */
017package org.apache.commons.validator;
018
019import org.apache.commons.validator.routines.InetAddressValidator;
020
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024/**
025 * <p>Perform email validations.</p>
026 * <p>
027 * This class is a Singleton; you can retrieve the instance via the getInstance() method.
028 * </p>
029 * <p>
030 * Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
031 * http://javascript.internet.com
032 * </p>
033 * <p>
034 * This implementation is not guaranteed to catch all possible errors in an email address.
035 * For example, an address like nobody@noplace.somedog will pass validator, even though there
036 * is no TLD "somedog"
037 * </p>.
038 *
039 * @version $Revision: 1441674 $ $Date: 2013-02-02 02:02:24 +0100 (Sa, 02 Feb 2013) $
040 * @since Validator 1.1
041 * @deprecated Use the new EmailValidator in the routines package. This class
042 * will be removed in a future release.
043 */
044public class EmailValidator {
045
046    private static final String SPECIAL_CHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]";
047    private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
048    private static final String QUOTED_USER = "(\"[^\"]*\")";
049    private static final String ATOM = VALID_CHARS + '+';
050    private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")";
051
052// NOT USED   private static final Pattern LEGAL_ASCII_PATTERN = Pattern.compile("^\\p{ASCII}+$");
053// NOT USED   private static final Pattern EMAIL_PATTERN = Pattern.compile("^(.+)@(.+)$");
054    private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile("^\\[(.*)\\]$");
055    private static final Pattern TLD_PATTERN = Pattern.compile("^([a-zA-Z]+)$");
056            
057    private static final Pattern USER_PATTERN = Pattern.compile("^\\s*" + WORD + "(\\." + WORD + ")*$");
058    private static final Pattern DOMAIN_PATTERN = Pattern.compile("^" + ATOM + "(\\." + ATOM + ")*\\s*$");
059    private static final Pattern ATOM_PATTERN = Pattern.compile("(" + 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        return org.apache.commons.validator.routines.EmailValidator.getInstance().isValid(email);
090    }
091
092    /**
093     * Returns true if the domain component of an email address is valid.
094     * @param domain being validated.
095     * @return true if the email address's domain is valid.
096     */
097    protected boolean isValidDomain(String domain) {
098        boolean symbolic = false;
099
100        // see if domain is an IP address in brackets
101        Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);
102
103        if (ipDomainMatcher.matches()) {
104            InetAddressValidator inetAddressValidator =
105                    InetAddressValidator.getInstance();
106            if (inetAddressValidator.isValid(ipDomainMatcher.group(1))) {
107                return true;
108            }
109        } else {
110            // Domain is symbolic name
111            symbolic = DOMAIN_PATTERN.matcher(domain).matches();
112        }
113
114        if (symbolic) {
115            if (!isValidSymbolicDomain(domain)) {
116                return false;
117            }
118        } else {
119            return false;
120        }
121
122        return true;
123    }
124
125    /**
126     * Returns true if the user component of an email address is valid.
127     * @param user being validated
128     * @return true if the user name is valid.
129     */
130    protected boolean isValidUser(String user) {
131        return USER_PATTERN.matcher(user).matches(); 
132    }
133
134    /**
135     * Validates an IP address. Returns true if valid.
136     * @param ipAddress IP address
137     * @return true if the ip address is valid.
138     */
139    protected boolean isValidIpAddress(String ipAddress) {
140        Matcher ipAddressMatcher = IP_DOMAIN_PATTERN.matcher(ipAddress);
141        for (int i = 1; i <= 4; i++) {
142            String ipSegment = ipAddressMatcher.group(i);
143            if (ipSegment == null || ipSegment.length() <= 0) {
144                return false;
145            }
146
147            int iIpSegment = 0;
148
149            try {
150                iIpSegment = Integer.parseInt(ipSegment);
151            } catch(NumberFormatException e) {
152                return false;
153            }
154
155            if (iIpSegment > 255) {
156                return false;
157            }
158
159        }
160        return true;
161    }
162
163    /**
164     * Validates a symbolic domain name.  Returns true if it's valid.
165     * @param domain symbolic domain name
166     * @return true if the symbolic domain name is valid.
167     */
168    protected boolean isValidSymbolicDomain(String domain) {
169        String[] domainSegment = new String[10];
170        boolean match = true;
171        int i = 0;
172        Matcher atomMatcher = ATOM_PATTERN.matcher(domain);
173        while (match) {
174            match = atomMatcher.matches();
175            if (match) {
176                domainSegment[i] = atomMatcher.group(1);
177                int l = domainSegment[i].length() + 1;
178                domain =
179                        (l >= domain.length())
180                        ? ""
181                        : domain.substring(l);
182
183                i++;
184            } 
185        }
186
187        int len = i;
188        
189        // Make sure there's a host name preceding the domain.
190        if (len < 2) {
191            return false;
192        }
193        
194        // TODO: the tld should be checked against some sort of configurable 
195        // list
196        String tld = domainSegment[len - 1];
197        if (tld.length() > 1) {
198            if (! TLD_PATTERN.matcher(tld).matches()) {
199                return false;
200            }
201        } else {
202            return false;
203        }
204
205        return true;
206    }
207    /**
208     *   Recursively remove comments, and replace with a single space.  The simpler
209     *   regexps in the Email Addressing FAQ are imperfect - they will miss escaped
210     *   chars in atoms, for example.
211     *   Derived From    Mail::RFC822::Address
212     * @param emailStr The email address
213     * @return address with comments removed.
214    */
215    protected String stripComments(String emailStr)  {
216     String result = emailStr;
217     String commentPat = "^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/";
218     Pattern commentMatcher = Pattern.compile(commentPat);
219     
220     while (commentMatcher.matcher(result).matches()) {
221        result = result.replaceFirst(commentPat, "\1 ");
222     }
223     return result;
224    }
225}