68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.util.mail;
|
|
24
|
|
25 import java.util.regex.Pattern;
|
|
26
|
|
27 public class MailAddress {
|
|
28
|
|
29 public static final Pattern EMAIL_PATTERN =
|
|
30 Pattern.compile("\\b(\\w[-+~.\\w]*)@[-\\w]+(\\.[-\\w]+)*\\.[a-zA-Z]+\\b");
|
|
31
|
|
32 private final String addrSpec;
|
|
33 private final String displayName;
|
|
34
|
|
35 public MailAddress(String addrSpec) {
|
|
36 this(addrSpec,null);
|
|
37 }
|
|
38
|
|
39 public MailAddress(String addrSpec,String displayName) {
|
|
40 this.addrSpec = addrSpec;
|
|
41 this.displayName = displayName;
|
|
42 }
|
|
43
|
|
44 public String getAddrSpec() {
|
|
45 return addrSpec;
|
|
46 }
|
|
47
|
|
48 public String getDisplayName() {
|
|
49 return displayName;
|
|
50 }
|
|
51
|
|
52 public String toString() {
|
|
53 return displayName==null ? addrSpec : displayName+" <"+addrSpec+">";
|
|
54 }
|
|
55
|
|
56 public boolean equals(Object o) {
|
|
57 if( !(o instanceof MailAddress) )
|
|
58 return false;
|
|
59 MailAddress ma = (MailAddress)o;
|
|
60 return addrSpec.equals(ma.addrSpec);
|
|
61 }
|
|
62
|
|
63 public int hashCode() {
|
|
64 return addrSpec.hashCode();
|
|
65 }
|
|
66
|
|
67 public boolean isValid() {
|
|
68 return EMAIL_PATTERN.matcher(addrSpec).matches() && addrSpec.indexOf("..") == -1;
|
|
69 }
|
|
70
|
|
71 // Test Code ----------------------------------------------------------------------------------
|
|
72
|
|
73 public static void main(String[] args) {
|
|
74 String[] valid = {
|
|
75 "none@none.net",
|
|
76 "none-owner@none.net",
|
|
77 "none+owner@none.net",
|
|
78 "none-the-owner@none.net.us",
|
|
79 "none.owner@none.net.us",
|
|
80 "fwd+lists~2B1264539414801-166331~40in.nabble.com+pharo-project-request~40lists.gforge.inria.fr+-679136591@in.nabble.com"
|
|
81 };
|
|
82
|
|
83 String[] invalid = {
|
|
84 // "none-@none.net", // not sure
|
|
85 "none...@none.net",
|
|
86 "none@none.net-",
|
|
87 "...none@none.net.us...",
|
|
88 ".-.@---",
|
|
89 "none@none+plus.com",
|
|
90 "none @none.net",
|
|
91 "none@ none.net",
|
|
92 "abc.example.com",
|
|
93 // "abc.@example.com", // not sure
|
|
94 "abc..123@example.com",
|
|
95 "a@b@c@example.com",
|
|
96 "()[]\\;:,<>@example.com"
|
|
97 };
|
|
98
|
|
99 System.out.println("--- Valid (Only TRUE)");
|
|
100 printEmailValidation(valid);
|
|
101
|
|
102 System.out.println("--- Invalid (Only FALSE)");
|
|
103 printEmailValidation(invalid);
|
|
104 }
|
|
105
|
|
106 private static void printEmailValidation(String[] valid) {
|
|
107 for (String s : valid) {
|
|
108 MailAddress mailAddress = new MailAddress(s);
|
|
109 System.out.println(s + " (" + mailAddress.isValid() + ')');
|
|
110 }
|
|
111 }
|
|
112 }
|