1
2
3
4
5 package net.sourceforge.jsdp.util;
6
7 import java.io.Serializable;
8 import java.util.StringTokenizer;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12 import net.sourceforge.jsdp.SDPException;
13
14
15
16
17
18
19
20
21
22
23 public class Contact implements Cloneable, Serializable {
24
25
26 private static final long serialVersionUID = -1082887678662975345L;
27
28
29 private static final String EMAIL_REGEX = "([\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+(\\.[\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+)*)@([\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+(\\.[\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+)*)";
30
31
32 private static final String PERSONAL_REGEX = "[^\\(\\)<>\\r\\n]+";
33
34
35 private static final Pattern emailPattern = Pattern.compile(EMAIL_REGEX);
36
37
38 private static final Pattern personalPattern = Pattern.compile(PERSONAL_REGEX);
39
40
41 protected String address;
42
43
44 protected String personal;
45
46
47
48
49 protected Contact() {
50
51 super();
52 }
53
54
55
56
57
58
59
60
61 public Contact(final String info) throws SDPException {
62
63 super();
64
65 Matcher emailMatcher = emailPattern.matcher(info);
66 if (emailMatcher.lookingAt()) {
67
68 int position = emailMatcher.end();
69 if (position == info.length()) {
70 this.address = info;
71 }
72 else {
73 this.address = info.substring(0, position);
74
75 StringTokenizer tokenizer = new StringTokenizer(info.substring(position), "()");
76 StringBuffer temp = new StringBuffer();
77 while (tokenizer.hasMoreTokens()) {
78 temp.append(tokenizer.nextToken());
79 }
80
81 String name = temp.toString().trim();
82 if (name.length() > 0) {
83 setPersonal(name);
84 }
85 else {
86 throw new SDPException("Invalid e-mail address");
87 }
88 }
89 }
90 else {
91 StringTokenizer tokenizer = new StringTokenizer(info, "<>");
92 setPersonal(tokenizer.nextToken().trim());
93 setAddress(tokenizer.nextToken());
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106
107 public Contact(final String address, final String personal) throws SDPException {
108
109 super();
110 setAddress(address);
111 setPersonal(personal);
112 }
113
114
115
116
117
118
119 public Object clone() {
120
121 Contact email = new Contact();
122 email.address = new String(this.address);
123
124 if (this.personal != null) {
125 email.personal = new String(personal);
126 }
127 else {
128 email.personal = null;
129 }
130
131 return email;
132 }
133
134
135
136
137
138
139 public String getAddress() {
140
141 return address;
142 }
143
144
145
146
147
148
149 public String getPersonal() {
150
151 return personal;
152 }
153
154
155
156
157
158
159
160
161 public void setAddress(final String address) throws SDPException {
162
163 if (!emailPattern.matcher(address).matches()) {
164 throw new SDPException("Invalid e-mail address:" + address);
165 }
166
167 this.address = address;
168 }
169
170
171
172
173
174
175
176
177 public void setPersonal(final String personal) throws SDPException {
178
179 if (!personalPattern.matcher(personal).matches()) {
180 throw new SDPException("Invalid personal information:" + personal);
181 }
182
183 this.personal = personal.trim();
184 }
185
186
187
188
189
190
191
192 public String toString() {
193
194 StringBuffer result;
195
196 result = new StringBuffer();
197
198 if (personal != null) {
199 result.append(personal);
200 result.append(" <" + getAddress() + ">");
201 }
202 else {
203 result.append(getAddress());
204 }
205
206 return result.toString();
207 }
208 }