-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvCardReader.cs
400 lines (329 loc) · 12.4 KB
/
vCardReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
// vCard Reader
namespace MyProject.vCard
{
/// <summary>
/// You may combine HomeWorkType and PhoneType, and FLAG them to reflect the attributes of vCard.
/// </summary>
public enum HomeWorkType
{
home, work
}
public enum PhoneType
{
VOICE,
FAX,
MSG,
CELL,
PAGER
}
/// <summary>
/// If you flag the enume types, you may use flags.
/// </summary>
public struct Phone
{
public string number;
public HomeWorkType homeWorkType;
public bool pref;
public PhoneType phoneType;
}
public struct Email
{
public string address;
public HomeWorkType homeWorkType;
public bool pref;
}
public struct Address
{
public string po;
public string ext;
public string street;
public string locality;
public string region;
public string postcode;
public string country;
public HomeWorkType homeWorkType;
}
public enum LabelType
{
DOM,
INTL,
POSTAL,
PARCEL
}
/// <summary>
/// Not used yet. You may use regular expressions or String.Replace() to replace =0D=0A to line breaks.
/// </summary>
public struct Label
{
public string address;
public LabelType labelType;
}
/// <summary>
/// Read text and create data fields of collections.
/// </summary>
public class vCardReader
{
#region Singlar Properties
private string formattedName;
public string FormattedName
{
get { return formattedName; }
set { formattedName = value; }
}
string surname;
public string Surname
{
get { return surname; }
set { surname = value; }
}
private string givenName;
public string GivenName
{
get { return givenName; }
set { givenName = value; }
}
private string middleName;
public string MiddleName
{
get { return middleName; }
set { middleName = value; }
}
private string prefix;
public string Prefix
{
get { return prefix; }
set { prefix = value; }
}
private string suffix;
public string Suffix
{
get { return suffix; }
set { suffix = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private DateTime? bday;
public DateTime? Birthday
{
get { return bday; }
set { bday = value; }
}
private DateTime? rev;
/// <summary>
/// If Rev in vCard is UTC, Rev will convert utc to local datetime.
/// </summary>
public DateTime? Rev
{
get { return rev; }
set { rev = value; }
}
private string org;
public string Org
{
get { return org; }
set { org = value; }
}
private string note;
public string Note
{
get { return note; }
set { note = value; }
}
#endregion
#region Property Collections with attribute
private List<Address> addresses = new List<Address>();
public List<Address> Addresses
{
get { return addresses; }
//set { addresses = value; }
}
private List<Phone> phones = new List<Phone>();
public List<Phone> Phones
{
get { return phones; }
//set { phones = value; }
}
private List<Email> emails = new List<Email>();
public List<Email> Emails
{
get { return emails; }
//set { emails = value; }
}
private List<string> urls = new List<string>();
public List<string> URLs
{
get { return urls; }
}
#endregion
/// <summary>
/// Analyze s into vCard structures.
/// </summary>
public void ParseLines(string s)
{
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
Regex regex;
Match m;
MatchCollection mc;
regex = new Regex(@"(?<strElement>(FN)) (:(?<strFN>[^\n\r]*))", options);
m = regex.Match(s);
if (m.Success)
FormattedName = m.Groups["strFN"].Value;
regex = new Regex(@"(\n(?<strElement>(N))) (:(?<strSurname>([^;]*))) (;(?<strGivenName>([^;]*))) (;(?<strMidName>([^;]*))) (;(?<strPrefix>([^;]*))) (;(?<strSuffix>[^\n\r]*))", options);
m = regex.Match(s);
if (m.Success)
{
Surname = m.Groups["strSurname"].Value;
GivenName = m.Groups["strGivenName"].Value;
MiddleName = m.Groups["strMidName"].Value;
Prefix = m.Groups["strPrefix"].Value;
Suffix = m.Groups["strSuffix"].Value;
}
///Title
regex = new Regex(@"(?<strElement>(TITLE)) (:(?<strTITLE>[^\n\r]*))", options);
m = regex.Match(s);
if (m.Success)
Title = m.Groups["strTITLE"].Value;
///ORG
regex = new Regex(@"(?<strElement>(ORG)) (:(?<strORG>[^\n\r]*))", options);
m = regex.Match(s);
if (m.Success)
Org = m.Groups["strORG"].Value;
///Note
regex = new Regex(@"((?<strElement>(NOTE)) (;*(?<strAttr>(ENCODING=QUOTED-PRINTABLE)))* ([^:]*)* (:(?<strValue> (([^\n\r]*=[\n\r]+)*[^\n\r]*[^=][\n\r]*) )))", options);
m = regex.Match(s);
if (m.Success)
{
Note = m.Groups["strValue"].Value;
//Remove connections and escape strings. The order is significant.
Note = Note.Replace("=" + Environment.NewLine, "");
Note = Note.Replace("=0D=0A" , Environment.NewLine);
Note = Note.Replace("=3D", "=");
}
///Birthday
regex = new Regex(@"(?<strElement>(BDAY)) [^:]* (:(?<strBDAY>[^\n\r]*))", options);
m = regex.Match(s);
if (m.Success)
{
string[] expectedFormats = { "yyyyMMdd", "yyMMdd", "yyyy-MM-dd" };
Birthday = DateTime.ParseExact(m.Groups["strBDAY"].Value, expectedFormats, null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
}
///Rev
regex = new Regex(@"(?<strElement>(REV)) (:(?<strREV>[^\n\r]*))", options);
m = regex.Match(s);
if (m.Success)
{
string[] expectedFormats = { "yyyyMMddHHmmss", "yyyyMMddTHHmmssZ" };
Rev = DateTime.ParseExact(m.Groups["strREV"].Value, expectedFormats, null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
}
///Emails
string ss;
regex = new Regex(@"(\.(?<strElement>(EMAIL)) (;*(?<strAttr>(HOME|WORK)))* (;(?<strPref>(PREF)))* (;[^:]*)* (:(?<strValue>[^\n\r]*)))", options);
mc = regex.Matches(s);
if (mc.Count > 0)
{
//Emails = new Email[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
m = mc[i];
Email email = new Email();
email.address = m.Groups["strValue"].Value;
ss = m.Groups["strAttr"].Value;
if (ss == "HOME")
email.homeWorkType = HomeWorkType.home;
else if (ss == "WORK")
email.homeWorkType = HomeWorkType.work;
if (m.Groups["strPref"].Value == "PREF")
email.pref = true;
emails.Add(email);
}
}
regex = new Regex(@"(\.(?<strElement>(URL)) (;type=(?<strPref>(PREF)))* (;[^:]*)* (:(?<strValue>[^\n\r]*)))", options);
mc = regex.Matches(s);
if (mc.Count > 0)
{
//Emails = new Email[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
m = mc[i];
urls.Add(m.Groups["strValue"].Value);
/*m = mc[i];
Email email = new Email();
email.address = m.Groups["strValue"].Value;
ss = m.Groups["strAttr"].Value;
if (ss == "HOME")
email.homeWorkType = HomeWorkType.home;
else if (ss == "WORK")
email.homeWorkType = HomeWorkType.work;
if (m.Groups["strPref"].Value == "PREF")
email.pref = true;
emails.Add(email);*/
}
}
///Phones
regex = new Regex(@"(\.(?<strElement>(TEL)) (;*(?<strAttr>(HOME|WORK)))* (;(?<strType>(VOICE|CELL|PAGER|MSG|FAX)))* (;(?<strPref>(PREF)))* (;[^:]*)* (:(?<strValue>[^\n\r]*)))", options);
mc = regex.Matches(s);
if (mc.Count > 0)
{
//Phones = new Phone[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
m = mc[i];
Phone phone = new Phone();
phone.number = m.Groups["strValue"].Value;
ss = m.Groups["strAttr"].Value;
if (ss == "HOME")
phone.homeWorkType = HomeWorkType.home;
else if (ss == "WORK")
phone.homeWorkType = HomeWorkType.work;
if (m.Groups["strPref"].Value == "PREF")
phone.pref = true;
ss = m.Groups["strType"].Value;
if (ss == "VOICE")
phone.phoneType = PhoneType.VOICE;
else if (ss == "CELL")
phone.phoneType = PhoneType.CELL;
else if (ss == "PAGER")
phone.phoneType = PhoneType.PAGER;
else if (ss == "MSG")
phone.phoneType = PhoneType.MSG;
else if (ss == "FAX")
phone.phoneType = PhoneType.FAX;
phones.Add(phone);
}
}
///Addresses
regex = new Regex(@"(\.(?<strElement>(ADR))) (;type=*(?<strAttr>(HOME|WORK)))* (;type=pref)* (.(?<strPo>([^;]*))) (;(?<strBlock>([^;]*))) (;(?<strStreet>([^;]*))) (;(?<strCity>([^;]*))) (;(?<strRegion>([^;]*))) (;(?<strPostcode>([^;]*)))(;(?<strNation>[^\n\r]*)) ", options);
mc = regex.Matches(s);
if (mc.Count > 0)
{
//Addresses = new Address[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
m = mc[i];
Address address = new Address();
ss = m.Groups["strAttr"].Value;
if (ss == "HOME")
address.homeWorkType = HomeWorkType.home;
else if (ss == "WORK")
address.homeWorkType = HomeWorkType.work;
address.po = m.Groups["strPo"].Value;
address.ext = m.Groups["strBlock"].Value;
address.street = m.Groups["strStreet"].Value;
address.locality = m.Groups["strCity"].Value;
address.region = m.Groups["strRegion"].Value;
address.postcode = m.Groups["strPostcode"].Value;
address.country = m.Groups["strNation"].Value;
addresses.Add(address);
}
}
}
}
}