Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EmailValidator |
|
| 4.125;4.125 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.validator; | |
18 | ||
19 | import org.apache.commons.validator.routines.InetAddressValidator; | |
20 | ||
21 | import java.util.regex.Matcher; | |
22 | import java.util.regex.Pattern; | |
23 | ||
24 | /** | |
25 | * <p>Perform email validations.</p> | |
26 | * <p> | |
27 | * This class is a Singleton; you can retrieve the instance via the getInstance() method. | |
28 | * </p> | |
29 | * <p> | |
30 | * Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a> | |
31 | * http://javascript.internet.com | |
32 | * </p> | |
33 | * <p> | |
34 | * This implementation is not guaranteed to catch all possible errors in an email address. | |
35 | * For example, an address like nobody@noplace.somedog will pass validator, even though there | |
36 | * is no TLD "somedog" | |
37 | * </p>. | |
38 | * | |
39 | * @version $Revision: 1739358 $ | |
40 | * @since Validator 1.1 | |
41 | * @deprecated Use the new EmailValidator in the routines package. This class | |
42 | * will be removed in a future release. | |
43 | */ | |
44 | @Deprecated | |
45 | public class EmailValidator { | |
46 | ||
47 | private static final String SPECIAL_CHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]"; | |
48 | private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]"; | |
49 | private static final String QUOTED_USER = "(\"[^\"]*\")"; | |
50 | private static final String ATOM = VALID_CHARS + '+'; | |
51 | private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")"; | |
52 | ||
53 | // NOT USED private static final Pattern LEGAL_ASCII_PATTERN = Pattern.compile("^\\p{ASCII}+$"); | |
54 | // NOT USED private static final Pattern EMAIL_PATTERN = Pattern.compile("^(.+)@(.+)$"); | |
55 | 1 | private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile("^\\[(.*)\\]$"); |
56 | 1 | private static final Pattern TLD_PATTERN = Pattern.compile("^([a-zA-Z]+)$"); |
57 | ||
58 | 1 | private static final Pattern USER_PATTERN = Pattern.compile("^\\s*" + WORD + "(\\." + WORD + ")*$"); |
59 | 1 | private static final Pattern DOMAIN_PATTERN = Pattern.compile("^" + ATOM + "(\\." + ATOM + ")*\\s*$"); |
60 | 1 | private static final Pattern ATOM_PATTERN = Pattern.compile("(" + ATOM + ")"); |
61 | ||
62 | /** | |
63 | * Singleton instance of this class. | |
64 | */ | |
65 | 1 | private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(); |
66 | ||
67 | /** | |
68 | * Returns the Singleton instance of this validator. | |
69 | * @return singleton instance of this validator. | |
70 | */ | |
71 | public static EmailValidator getInstance() { | |
72 | 0 | return EMAIL_VALIDATOR; |
73 | } | |
74 | ||
75 | /** | |
76 | * Protected constructor for subclasses to use. | |
77 | */ | |
78 | protected EmailValidator() { | |
79 | 2 | super(); |
80 | 2 | } |
81 | ||
82 | /** | |
83 | * <p>Checks if a field has a valid e-mail address.</p> | |
84 | * | |
85 | * @param email The value validation is being performed on. A <code>null</code> | |
86 | * value is considered invalid. | |
87 | * @return true if the email address is valid. | |
88 | */ | |
89 | public boolean isValid(String email) { | |
90 | 33 | return org.apache.commons.validator.routines.EmailValidator.getInstance().isValid(email); |
91 | } | |
92 | ||
93 | /** | |
94 | * Returns true if the domain component of an email address is valid. | |
95 | * @param domain being validated. | |
96 | * @return true if the email address's domain is valid. | |
97 | */ | |
98 | protected boolean isValidDomain(String domain) { | |
99 | 0 | boolean symbolic = false; |
100 | ||
101 | // see if domain is an IP address in brackets | |
102 | 0 | Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain); |
103 | ||
104 | 0 | if (ipDomainMatcher.matches()) { |
105 | 0 | InetAddressValidator inetAddressValidator = |
106 | InetAddressValidator.getInstance(); | |
107 | 0 | if (inetAddressValidator.isValid(ipDomainMatcher.group(1))) { |
108 | 0 | return true; |
109 | } | |
110 | 0 | } else { |
111 | // Domain is symbolic name | |
112 | 0 | symbolic = DOMAIN_PATTERN.matcher(domain).matches(); |
113 | } | |
114 | ||
115 | 0 | if (symbolic) { |
116 | 0 | if (!isValidSymbolicDomain(domain)) { |
117 | 0 | return false; |
118 | } | |
119 | } else { | |
120 | 0 | return false; |
121 | } | |
122 | ||
123 | 0 | return true; |
124 | } | |
125 | ||
126 | /** | |
127 | * Returns true if the user component of an email address is valid. | |
128 | * @param user being validated | |
129 | * @return true if the user name is valid. | |
130 | */ | |
131 | protected boolean isValidUser(String user) { | |
132 | 0 | return USER_PATTERN.matcher(user).matches(); |
133 | } | |
134 | ||
135 | /** | |
136 | * Validates an IP address. Returns true if valid. | |
137 | * @param ipAddress IP address | |
138 | * @return true if the ip address is valid. | |
139 | */ | |
140 | protected boolean isValidIpAddress(String ipAddress) { | |
141 | 0 | Matcher ipAddressMatcher = IP_DOMAIN_PATTERN.matcher(ipAddress); |
142 | 0 | for (int i = 1; i <= 4; i++) { // CHECKSTYLE IGNORE MagicNumber |
143 | 0 | String ipSegment = ipAddressMatcher.group(i); |
144 | 0 | if (ipSegment == null || ipSegment.length() <= 0) { |
145 | 0 | return false; |
146 | } | |
147 | ||
148 | 0 | int iIpSegment = 0; |
149 | ||
150 | try { | |
151 | 0 | iIpSegment = Integer.parseInt(ipSegment); |
152 | 0 | } catch(NumberFormatException e) { |
153 | 0 | return false; |
154 | 0 | } |
155 | ||
156 | 0 | if (iIpSegment > 255) { // CHECKSTYLE IGNORE MagicNumber |
157 | 0 | return false; |
158 | } | |
159 | ||
160 | } | |
161 | 0 | return true; |
162 | } | |
163 | ||
164 | /** | |
165 | * Validates a symbolic domain name. Returns true if it's valid. | |
166 | * @param domain symbolic domain name | |
167 | * @return true if the symbolic domain name is valid. | |
168 | */ | |
169 | protected boolean isValidSymbolicDomain(String domain) { | |
170 | 0 | String[] domainSegment = new String[10]; // CHECKSTYLE IGNORE MagicNumber |
171 | 0 | boolean match = true; |
172 | 0 | int i = 0; |
173 | 0 | Matcher atomMatcher = ATOM_PATTERN.matcher(domain); |
174 | 0 | while (match) { |
175 | 0 | match = atomMatcher.matches(); |
176 | 0 | if (match) { |
177 | 0 | domainSegment[i] = atomMatcher.group(1); |
178 | 0 | int l = domainSegment[i].length() + 1; |
179 | 0 | domain = |
180 | (l >= domain.length()) | |
181 | ? "" | |
182 | : domain.substring(l); | |
183 | ||
184 | 0 | i++; |
185 | 0 | } |
186 | } | |
187 | ||
188 | 0 | int len = i; |
189 | ||
190 | // Make sure there's a host name preceding the domain. | |
191 | 0 | if (len < 2) { |
192 | 0 | return false; |
193 | } | |
194 | ||
195 | // TODO: the tld should be checked against some sort of configurable | |
196 | // list | |
197 | 0 | String tld = domainSegment[len - 1]; |
198 | 0 | if (tld.length() > 1) { |
199 | 0 | if (! TLD_PATTERN.matcher(tld).matches()) { |
200 | 0 | return false; |
201 | } | |
202 | } else { | |
203 | 0 | return false; |
204 | } | |
205 | ||
206 | 0 | return true; |
207 | } | |
208 | /** | |
209 | * Recursively remove comments, and replace with a single space. The simpler | |
210 | * regexps in the Email Addressing FAQ are imperfect - they will miss escaped | |
211 | * chars in atoms, for example. | |
212 | * Derived From Mail::RFC822::Address | |
213 | * @param emailStr The email address | |
214 | * @return address with comments removed. | |
215 | */ | |
216 | protected String stripComments(String emailStr) { | |
217 | 0 | String result = emailStr; |
218 | 0 | String commentPat = "^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/"; |
219 | 0 | Pattern commentMatcher = Pattern.compile(commentPat); |
220 | ||
221 | 0 | while (commentMatcher.matcher(result).matches()) { |
222 | 0 | result = result.replaceFirst(commentPat, "\1 "); |
223 | } | |
224 | 0 | return result; |
225 | } | |
226 | } |