Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.validator.EmailValidator |
89% |
89% |

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.oro.text.perl.Perl5Util;
20 |  |
21 |  |
     /**
22 |  |
      * <p>Perform email validations.</p>
23 |  |
      * <p>
24 |  |
      * This class is a Singleton; you can retrieve the instance via the getInstance() method.
25 |  |
      * </p>
26 |  |
      * <p>
27 |  |
      * Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
28 |  |
      * http://javascript.internet.com
29 |  |
      * </p>
30 |  |
      * <p>
31 |  |
      * This implementation is not guaranteed to catch all possible errors in an email address.
32 |  |
      * For example, an address like nobody@noplace.somedog will pass validator, even though there
33 |  |
      * is no TLD "somedog"
34 |  |
      * </p>.
35 |  |
      *
36 |  |
      * @version $Revision: 478560 $ $Date: 2006-11-23 13:09:27 +0000 (Thu, 23 Nov 2006) $
37 |  |
      * @since Validator 1.1
38 |  |
      */
39 |  |
     public class EmailValidator {
40 |  |
41 |  |
         private static final String SPECIAL_CHARS = "\\000-\\037\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]\\177";
42 |  |
         private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
43 |  |
         private static final String QUOTED_USER = "(\"[^\"]*\")";
44 |  |
         private static final String ATOM = VALID_CHARS + '+';
45 |  |
         private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")";
46 |  |
47 |  |
         // Each pattern must be surrounded by /
48 |  |
         private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
49 |  |
         private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";
50 |  |
         private static final String IP_DOMAIN_PATTERN =
51 |  |
                 "/^\\[(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\\]$/";
52 |  |
         private static final String TLD_PATTERN = "/^([a-zA-Z]+)$/";
53 |  |
54 |  |
         private static final String USER_PATTERN = "/^\\s*" + WORD + "(\\." + WORD + ")*$/";
55 |  |
         private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*\\s*$/";
56 |  |
         private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
57 |  |
58 |  |
         /**
59 |  |
          * Singleton instance of this class.
60 |  |
          */
61 | 1 |
         private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
62 |  |
63 |  |
         /**
64 |  |
          * Returns the Singleton instance of this validator.
65 |  |
          * @return singleton instance of this validator.
66 |  |
          */
67 |  |
         public static EmailValidator getInstance() {
68 | 29 |
             return EMAIL_VALIDATOR;
69 |  |
         }
70 |  |
71 |  |
         /**
72 |  |
          * Protected constructor for subclasses to use.
73 |  |
          */
74 |  |
         protected EmailValidator() {
75 | 2 |
             super();
76 | 2 |
         }
77 |  |
78 |  |
         /**
79 |  |
          * <p>Checks if a field has a valid e-mail address.</p>
80 |  |
          *
81 |  |
          * @param email The value validation is being performed on.  A <code>null</code>
82 |  |
          * value is considered invalid.
83 |  |
          * @return true if the email address is valid.
84 |  |
          */
85 |  |
         public boolean isValid(String email) {
86 | 62 |
             if (email == null) {
87 | 0 |
                 return false;
88 |  |
             }
89 |  |
90 | 62 |
             Perl5Util matchAsciiPat = new Perl5Util();
91 | 62 |
             if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) {
92 | 1 |
                 return false;
93 |  |
             }
94 |  |
95 | 61 |
             email = stripComments(email);
96 |  |
97 |  |
             // Check the whole email address structure
98 | 61 |
             Perl5Util emailMatcher = new Perl5Util();
99 | 61 |
             if (!emailMatcher.match(EMAIL_PATTERN, email)) {
100 | 1 |
                 return false;
101 |  |
             }
102 |  |
103 | 60 |
             if (email.endsWith(".")) {
104 | 2 |
                 return false;
105 |  |
             }
106 |  |
107 | 58 |
             if (!isValidUser(emailMatcher.group(1))) {
108 | 34 |
                 return false;
109 |  |
             }
110 |  |
111 | 24 |
             if (!isValidDomain(emailMatcher.group(2))) {
112 | 11 |
                 return false;
113 |  |
             }
114 |  |
115 | 13 |
             return true;
116 |  |
         }
117 |  |
118 |  |
         /**
119 |  |
          * Returns true if the domain component of an email address is valid.
120 |  |
          * @param domain being validatied.
121 |  |
          * @return true if the email address's domain is valid.
122 |  |
          */
123 |  |
         protected boolean isValidDomain(String domain) {
124 | 24 |
             boolean symbolic = false;
125 | 24 |
             Perl5Util ipAddressMatcher = new Perl5Util();
126 |  |
127 | 24 |
             if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) {
128 | 1 |
                 if (!isValidIpAddress(ipAddressMatcher)) {
129 | 0 |
                     return false;
130 |  |
                 } else {
131 | 1 |
                     return true;
132 |  |
                 }
133 |  |
             } else {
134 |  |
                 // Domain is symbolic name
135 | 23 |
                 Perl5Util domainMatcher = new Perl5Util();
136 | 23 |
                 symbolic = domainMatcher.match(DOMAIN_PATTERN, domain);
137 |  |
             }
138 |  |
139 | 23 |
             if (symbolic) {
140 | 17 |
                 if (!isValidSymbolicDomain(domain)) {
141 | 5 |
                     return false;
142 |  |
                 }
143 |  |
             } else {
144 | 6 |
                 return false;
145 |  |
             }
146 |  |
147 | 12 |
             return true;
148 |  |
         }
149 |  |
150 |  |
         /**
151 |  |
          * Returns true if the user component of an email address is valid.
152 |  |
          * @param user being validated
153 |  |
          * @return true if the user name is valid.
154 |  |
          */
155 |  |
         protected boolean isValidUser(String user) {
156 | 58 |
             Perl5Util userMatcher = new Perl5Util();
157 | 58 |
             return userMatcher.match(USER_PATTERN, user);
158 |  |
         }
159 |  |
160 |  |
         /**
161 |  |
          * Validates an IP address. Returns true if valid.
162 |  |
          * @param ipAddressMatcher Pattren matcher
163 |  |
          * @return true if the ip address is valid.
164 |  |
          */
165 |  |
         protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) {
166 | 5 |
             for (int i = 1; i <= 4; i++) {
167 | 4 |
                 String ipSegment = ipAddressMatcher.group(i);
168 | 4 |
                 if (ipSegment == null || ipSegment.length() <= 0) {
169 | 0 |
                     return false;
170 |  |
                 }
171 |  |
172 | 4 |
                 int iIpSegment = 0;
173 |  |
174 |  |
                 try {
175 | 4 |
                     iIpSegment = Integer.parseInt(ipSegment);
176 | 4 |
                 } catch(NumberFormatException e) {
177 | 0 |
                     return false;
178 |  |
                 }
179 |  |
180 | 4 |
                 if (iIpSegment > 255) {
181 | 0 |
                     return false;
182 |  |
                 }
183 |  |
184 |  |
             }
185 | 1 |
             return true;
186 |  |
         }
187 |  |
188 |  |
         /**
189 |  |
          * Validates a symbolic domain name.  Returns true if it's valid.
190 |  |
          * @param domain symbolic domain name
191 |  |
          * @return true if the symbolic domain name is valid.
192 |  |
          */
193 |  |
         protected boolean isValidSymbolicDomain(String domain) {
194 | 17 |
             String[] domainSegment = new String[10];
195 | 17 |
             boolean match = true;
196 | 17 |
             int i = 0;
197 | 17 |
             Perl5Util atomMatcher = new Perl5Util();
198 | 90 |
             while (match) {
199 | 56 |
                 match = atomMatcher.match(ATOM_PATTERN, domain);
200 | 56 |
                 if (match) {
201 | 39 |
                     domainSegment[i] = atomMatcher.group(1);
202 | 39 |
                     int l = domainSegment[i].length() + 1;
203 | 39 |
                     domain =
204 |  |
                             (l >= domain.length())
205 |  |
                             ? ""
206 |  |
                             : domain.substring(l);
207 |  |
208 | 39 |
                     i++;
209 |  |
                 }
210 |  |
             }
211 |  |
212 | 17 |
             int len = i;
213 |  |
214 |  |
             // Make sure there's a host name preceding the domain.
215 | 17 |
             if (len < 2) {
216 | 0 |
                 return false;
217 |  |
             }
218 |  |
219 |  |
             // TODO: the tld should be checked against some sort of configurable
220 |  |
             // list
221 | 17 |
             String tld = domainSegment[len - 1];
222 | 17 |
             if (tld.length() > 1) {
223 | 16 |
                 Perl5Util matchTldPat = new Perl5Util();
224 | 16 |
                 if (!matchTldPat.match(TLD_PATTERN, tld)) {
225 | 4 |
                     return false;
226 |  |
                 }
227 |  |
             } else {
228 | 1 |
                 return false;
229 |  |
             }
230 |  |
231 | 12 |
             return true;
232 |  |
         }
233 |  |
         /**
234 |  |
          *   Recursively remove comments, and replace with a single space.  The simpler
235 |  |
          *   regexps in the Email Addressing FAQ are imperfect - they will miss escaped
236 |  |
          *   chars in atoms, for example.
237 |  |
          *   Derived From    Mail::RFC822::Address
238 |  |
          * @param emailStr The email address
239 |  |
          * @return address with comments removed.
240 |  |
         */
241 |  |
         protected String stripComments(String emailStr)  {
242 | 61 |
          String input = emailStr;
243 | 61 |
          String result = emailStr;
244 | 61 |
          String commentPat = "s/^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/$1 /osx";
245 | 61 |
          Perl5Util commentMatcher = new Perl5Util();
246 | 61 |
          result = commentMatcher.substitute(commentPat,input);
247 |  |
          // This really needs to be =~ or Perl5Matcher comparison
248 | 122 |
          while (!result.equals(input)) {
249 | 0 |
             input = result;
250 | 0 |
             result = commentMatcher.substitute(commentPat,input);
251 |  |
          }
252 | 61 |
          return result;
253 |  |
254 |  |
         }
255 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.