belcard_identification.cpp 11 KB
Newer Older
1 2 3 4 5 6
#include "belcard/belcard_identification.hpp"

using namespace::std;
using namespace::belr;
using namespace::belcard;

Sylvain Berfini's avatar
Sylvain Berfini committed
7 8
shared_ptr<BelCardFullName> BelCardFullName::create() {
	return BelCardGeneric::create<BelCardFullName>();
9 10
}

Sylvain Berfini's avatar
Sylvain Berfini committed
11
shared_ptr<BelCardFullName> BelCardFullName::parse(const string& input) {
12 13 14 15 16 17
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("FN", input, NULL);
Sylvain Berfini's avatar
Sylvain Berfini committed
18
	return dynamic_pointer_cast<BelCardFullName>(ret);
19 20
}

Sylvain Berfini's avatar
Sylvain Berfini committed
21 22
void BelCardFullName::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("FN", make_fn(&BelCardFullName::create))
23 24 25 26 27 28 29 30 31
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("LANGUAGE-param", make_sfn(&BelCardProperty::setLanguageParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("FN-value", make_sfn(&BelCardProperty::setValue));
32 33
}

Sylvain Berfini's avatar
Sylvain Berfini committed
34
BelCardFullName::BelCardFullName() : BelCardProperty() {
35 36 37
	setName("FN");
}

Sylvain Berfini's avatar
Sylvain Berfini committed
38 39
shared_ptr<BelCardName> BelCardName::create() {
	return BelCardGeneric::create<BelCardName>();
40 41
}

Sylvain Berfini's avatar
Sylvain Berfini committed
42
shared_ptr<BelCardName> BelCardName::parse(const string& input) {
43 44 45 46 47 48
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("N", input, NULL);
Sylvain Berfini's avatar
Sylvain Berfini committed
49
	return dynamic_pointer_cast<BelCardName>(ret);
50 51
}

Sylvain Berfini's avatar
Sylvain Berfini committed
52 53
void BelCardName::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("N", make_fn(&BelCardName::create))
54 55 56 57 58 59
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("SORT-AS-param", make_sfn(&BelCardProperty::setSortAsParam))
			->setCollector("LANGUAGE-param", make_sfn(&BelCardProperty::setLanguageParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
Sylvain Berfini's avatar
Sylvain Berfini committed
60 61 62 63 64
			->setCollector("N-fn", make_sfn(&BelCardName::setFamilyName))
			->setCollector("N-gn", make_sfn(&BelCardName::setGivenName))
			->setCollector("N-an", make_sfn(&BelCardName::setAdditionalName))
			->setCollector("N-prefixes", make_sfn(&BelCardName::setPrefixes))
			->setCollector("N-suffixes", make_sfn(&BelCardName::setSuffixes));
65 66
}

Sylvain Berfini's avatar
Sylvain Berfini committed
67
BelCardName::BelCardName() : BelCardProperty() {
68 69 70
	setName("N");
}

Sylvain Berfini's avatar
Sylvain Berfini committed
71
void BelCardName::setFamilyName(const string &value) {
72 73
	_family_name = value;
}
Sylvain Berfini's avatar
Sylvain Berfini committed
74
const string &BelCardName::getFamilyName() const {
75 76 77
	return _family_name;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
78
void BelCardName::setGivenName(const string &value) {
79 80
	_given_name = value;
}
Sylvain Berfini's avatar
Sylvain Berfini committed
81
const string &BelCardName::getGivenName() const {
82 83 84
	return _given_name;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
85
void BelCardName::setAdditionalName(const string &value) {
86 87
	_additional_name = value;
}
Sylvain Berfini's avatar
Sylvain Berfini committed
88
const string &BelCardName::getAdditionalName() const {
89 90 91
	return _additional_name;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
92
void BelCardName::setPrefixes(const string &value) {
93 94
	_prefixes = value;
}
Sylvain Berfini's avatar
Sylvain Berfini committed
95
const string &BelCardName::getPrefixes() const {
96 97 98
	return _prefixes;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
99
void BelCardName::setSuffixes(const string &value) {
100 101
	_suffixes = value;
}
Sylvain Berfini's avatar
Sylvain Berfini committed
102
const string &BelCardName::getSuffixes() const {
103 104 105
	return _suffixes;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
106
string BelCardName::serialize() const {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	stringstream output;
	
	if (getGroup().length() > 0) {
		output << getGroup() << ".";
	}
	
	output << getName();
	for (auto it = getParams().begin(); it != getParams().end(); ++it) {
		output << ";" << (**it); 
	}
	output << ":" << getFamilyName() + ";" + getGivenName() + ";" + getAdditionalName() + ";" + getPrefixes() + ";" + getSuffixes() << "\r\n";
	
	return output.str();            
}

shared_ptr<BelCardNickname> BelCardNickname::create() {
123
	return BelCardGeneric::create<BelCardNickname>();
124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

shared_ptr<BelCardNickname> BelCardNickname::parse(const string& input) {
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("NICKNAME", input, NULL);
	return dynamic_pointer_cast<BelCardNickname>(ret);
}

void BelCardNickname::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("NICKNAME", make_fn(&BelCardNickname::create))
138 139 140 141 142 143 144 145 146
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("LANGUAGE-param", make_sfn(&BelCardProperty::setLanguageParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("NICKNAME-value", make_sfn(&BelCardProperty::setValue));
147 148 149 150 151 152 153
}

BelCardNickname::BelCardNickname() : BelCardProperty() {
	setName("NICKNAME");
}

shared_ptr<BelCardBirthday> BelCardBirthday::create() {
154
	return BelCardGeneric::create<BelCardBirthday>();
155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

shared_ptr<BelCardBirthday> BelCardBirthday::parse(const string& input) {
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("BDAY", input, NULL);
	return dynamic_pointer_cast<BelCardBirthday>(ret);
}

void BelCardBirthday::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("BDAY", make_fn(&BelCardBirthday::create))
169 170 171 172 173 174 175
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("LANGUAGE-param", make_sfn(&BelCardProperty::setLanguageParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("CALSCALE-param", make_sfn(&BelCardProperty::setCALSCALEParam))
			->setCollector("BDAY-value", make_sfn(&BelCardProperty::setValue));
176 177 178 179 180 181 182
}

BelCardBirthday::BelCardBirthday() : BelCardProperty() {
	setName("BDAY");
}

shared_ptr<BelCardAnniversary> BelCardAnniversary::create() {
183
	return BelCardGeneric::create<BelCardAnniversary>();
184 185 186 187 188 189 190 191 192 193 194 195 196 197
}

shared_ptr<BelCardAnniversary> BelCardAnniversary::parse(const string& input) {
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("ANNIVERSARY", input, NULL);
	return dynamic_pointer_cast<BelCardAnniversary>(ret);
}

void BelCardAnniversary::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("ANNIVERSARY", make_fn(&BelCardAnniversary::create))
198 199 200 201 202 203
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("CALSCALE-param", make_sfn(&BelCardProperty::setCALSCALEParam))
			->setCollector("ANNIVERSARY-value", make_sfn(&BelCardProperty::setValue));
204 205 206 207 208 209 210
}

BelCardAnniversary::BelCardAnniversary() : BelCardProperty() {
	setName("ANNIVERSARY");
}

shared_ptr<BelCardGender> BelCardGender::create() {
211
	return BelCardGeneric::create<BelCardGender>();
212 213 214 215 216 217 218 219 220 221 222 223 224 225
}

shared_ptr<BelCardGender> BelCardGender::parse(const string& input) {
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("GENDER", input, NULL);
	return dynamic_pointer_cast<BelCardGender>(ret);
}

void BelCardGender::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("GENDER", make_fn(&BelCardGender::create))
226 227 228 229
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("GENDER-value", make_sfn(&BelCardProperty::setValue));
230 231 232 233 234 235 236
}

BelCardGender::BelCardGender() : BelCardProperty() {
	setName("GENDER");
}

shared_ptr<BelCardPhoto> BelCardPhoto::create() {
237
	return BelCardGeneric::create<BelCardPhoto>();
238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

shared_ptr<BelCardPhoto> BelCardPhoto::parse(const string& input) {
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
	setHandlerAndCollectors(&parser);
	BelCardParam::setHandlerAndCollectors(&parser);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("PHOTO", input, NULL);
	return dynamic_pointer_cast<BelCardPhoto>(ret);
}

void BelCardPhoto::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	parser->setHandler("PHOTO", make_fn(&BelCardPhoto::create))
252 253 254 255 256 257 258 259 260
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("MEDIATYPE-param", make_sfn(&BelCardProperty::setMediaTypeParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PHOTO-value", make_sfn(&BelCardProperty::setValue));
261 262 263 264 265
}

BelCardPhoto::BelCardPhoto() : BelCardProperty() {
	setName("PHOTO");
}