VCard1.fromPlainText constructor
VCard1.fromPlainText(
- String plainText
Constructor from the plain text.
Exmaple:
BEGIN:VCARD
VERSION:1.0
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK;PREF:;;100 Waters Edge;Baytown;LA;30314;United States of America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
URL:http://www.johndoe.com
REV:20080424T195243Z
END:VCARD
Implementation
factory VCard1.fromPlainText(String plainText) {
final texts = plainText.trim().split('\n');
if (texts.first != 'BEGIN:VCARD') {
throw VCardParsingError('vCard must starts with "BEGIN:VCARD"');
} else {
texts.removeAt(0);
}
if (texts.last != 'END:VCARD') {
throw VCardParsingError('vCard must ends with "END:VCARD"');
} else {
texts.removeLast();
}
if (!texts.contains('VERSION:1.0')) {
throw VCardParsingError('The vCard must have "VERSION:1.0" as the vCard specification');
}
final name = VCardName.fromPlainText(texts.firstWhere((e) => e.startsWith('N:')));
final tel = <VCardTelephone>[];
final adr = <VCardAddress>[];
final emails = <String>[];
VCardGeo? geo;
VCardOrganization? org;
String? title;
String? role;
String? url;
DateTime? rev;
for (String t in texts) {
if (t.startsWith('TEL')) {
tel.add(VCardTelephone.fromPlainText(t, VCardVersion.v1));
} else if (t.startsWith('ADR')) {
adr.add(VCardAddress.fromPlainText(t));
} else if (t.startsWith('GEO')) {
geo = VCardGeo.fromPlainText(t);
} else if (t.startsWith('EMAIL')) {
emails.add(t.substring(6));
} else if (t.startsWith('ORG')) {
org = VCardOrganization.fromPlainText(t);
} else if (t.startsWith('TITLE')) {
title = t.substring(6);
} else if (t.startsWith('ROLE')) {
role = t.substring(5);
} else if (t.startsWith('URL')) {
url = t.substring(4);
} else if (t.startsWith('REV')) {
rev = DateTime.tryParse(t.substring(4));
}
}
return VCard1(
name: name,
telephone: tel.isNotEmpty ? tel : null,
address: adr.isNotEmpty ? adr : null,
geo: geo,
emails: emails.isNotEmpty ? emails : null,
organization: org,
title: title,
role: role,
url: url,
revision: rev,
);
}