KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > EmailUtils


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.util;
56
57 import java.util.StringTokenizer JavaDoc;
58
59 /**
60  * RFC 821 compliant email checking (I think... at the very least it
61  * seems to work on all valid email addresses I poke at it)
62  *
63  * @author J R Briggs
64  */

65 public final class EmailUtils implements Constants {
66   private static final String JavaDoc INVALID_CHARS = " <>()[]\\,;:\"@ ";
67   
68   private EmailUtils() {
69   }
70
71  /**
72   * returns true if the specified string is a valid (RFC821) email address
73   * @param email the string representation of an email address
74   * @returns true if the email address is valid
75   */

76   public static final boolean isValidEmail(String JavaDoc email) {
77     int pos = email.indexOf('@');
78     if (pos <= 0 || pos == email.length()-1) {
79       return false;
80     }
81     else {
82       String JavaDoc localpart = email.substring(0,pos);
83       String JavaDoc domain = email.substring(pos+1);
84     
85       if (!isValidLocalPart(localpart)) {
86         return false;
87       }
88       else {
89         return isValidDomain(domain);
90       }
91     }
92   }
93
94   private static final boolean isValidLocalPart(String JavaDoc localpart) {
95     if (localpart == null || localpart.length() < 1) {
96       return false;
97     }
98     else {
99       if (localpart.startsWith(DOT) || localpart.endsWith(DOT)) {
100         return false;
101       }
102       else {
103         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(localpart, DOT);
104         while (st.hasMoreTokens()) {
105           if (!isValidString(st.nextToken())) {
106             return false;
107           }
108         }
109         return true;
110       }
111     }
112   }
113
114   private static final boolean isValidDomain(String JavaDoc domain) {
115     if (domain.startsWith(DOT) || domain.endsWith(DOT)) {
116       return false;
117     }
118     else {
119       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(domain,DOT);
120       while (st.hasMoreTokens()) {
121         String JavaDoc token = st.nextToken();
122         // check for valid 'element' which is a name, number or dotnum
123
if (token.startsWith(SQ_LEFT_BRACKET)) {
124           while (!token.endsWith(SQ_RIGHT_BRACKET) && st.hasMoreTokens()) {
125             token += DOT + st.nextToken();
126           }
127
128           if (!isValidDotNum(token)) {
129             return false;
130           }
131         }
132         else if (!isValidName(token) && !isValidNumber(token)) {
133           return false;
134         }
135       }
136
137       return true;
138     }
139   }
140
141   private static final boolean isValidName(String JavaDoc name) {
142     int len = name.length();
143     if (len == 0) {
144       return false;
145     }
146     
147     char start = name.charAt(0);
148     char end = name.charAt(len-1);
149     
150     if (!isValidAlpha(start)) {
151       return false;
152     }
153     else if (!isValidAlpha(end) && !isValidDigit(end)) {
154       return false;
155     }
156     else {
157       for (int i = 1; i < name.length()-1; i++) {
158         char c = name.charAt(i);
159         if (!isValidAlpha(c) && !isValidDigit(c) && c != '-') {
160           return false;
161         }
162       }
163     }
164     return true;
165   }
166
167   private static final boolean isValidNumber(String JavaDoc number) {
168     int len = number.length();
169     if (!number.startsWith(HASH) || len <= 1) {
170       return false;
171     }
172     else {
173       for (int i = 1; i < len; i++) {
174         if (!isValidDigit(number.charAt(i))) {
175           return false;
176         }
177       }
178       return true;
179     }
180   }
181
182   private static final boolean isValidDotNum(String JavaDoc dotnum) {
183     int len = dotnum.length();
184
185     if (len <= 2 || dotnum.charAt(0) != '[' || dotnum.charAt(len-1) != ']') {
186       return false;
187     }
188     else {
189       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(dotnum, DOT);
190
191       while (st.hasMoreTokens()) {
192         String JavaDoc s = st.nextToken();
193
194         if (s.length() > 3) {
195           return false;
196         }
197         else {
198           for (int i = 0; i < s.length(); i++) {
199             if (!isValidDigit(s.charAt(i))) {
200               return false;
201             }
202           }
203         }
204       }
205       return true;
206     }
207   }
208
209   private static final boolean isValidString(String JavaDoc s) {
210     for (int i = 0; i < s.length(); i++) {
211       char c = s.charAt(i);
212       if (c == '\\') {
213         i++;
214         c = s.charAt(i);
215         if (c < 0 || c > 127) {
216           return false;
217         }
218       }
219       else {
220         if (c < 32 || c > 126 || INVALID_CHARS.indexOf(c) >= 0) {
221           return false;
222         }
223       }
224     }
225     return true;
226   }
227   
228   private static final boolean isValidAlpha(char c) {
229     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
230   }
231
232   private static final boolean isValidDigit(char c) {
233     return (c >= '0' && c <= '9');
234   }
235
236
237 }
Popular Tags