Commit 716f427b authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

More code factorized

parent c408866e
......@@ -53,7 +53,21 @@ namespace belcard {
void setLabelParam(const shared_ptr<BelCardLabelParam> &param);
const shared_ptr<BelCardLabelParam> &getLabelParam() const;
string serialize() const;
friend ostream &operator<<(ostream &output, const BelCardAddress &addr) {
if (addr.getGroup().length() > 0) {
output << addr.getGroup() << ".";
}
output << addr.getName();
for (auto it = addr.getParams().begin(); it != addr.getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << addr.getPostOfficeBox() << ";" << addr.getExtendedAddress()
<< ";" << addr.getStreet() << ";" << addr.getLocality() << ";" << addr.getRegion()
<< ";" << addr.getPostalCode() << ";" << addr.getCountry() << "\r\n";
return output;
}
};
}
......
......@@ -50,7 +50,19 @@ namespace belcard {
void setSuffixes(const string &value);
const string &getSuffixes() const;
string serialize() const;
friend ostream &operator<<(ostream &output, const BelCardName &name) {
if (name.getGroup().length() > 0) {
output << name.getGroup() << ".";
}
output << name.getName();
for (auto it = name.getParams().begin(); it != name.getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << name.getFamilyName() + ";" + name.getGivenName() + ";" + name.getAdditionalName() + ";" + name.getPrefixes() + ";" + name.getSuffixes() << "\r\n";
return output;
}
};
class BelCardNickname : public BelCardProperty {
......
......@@ -18,16 +18,14 @@ namespace belcard {
string _name;
string _value;
public:
template <typename T>
static shared_ptr<T> parseParam(const string& rule, const string& input);
static shared_ptr<BelCardParam> parse(const string& input);
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser);
static void setAllParamsHandlersAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser);
BelCardParam();
friend ostream &operator<<(ostream &output, const BelCardParam &param) {
output << param.serialize();
return output;
}
virtual void setName(const string &name);
virtual const string &getName() const;
......@@ -35,6 +33,11 @@ namespace belcard {
virtual const string &getValue() const;
virtual string serialize() const;
friend ostream &operator<<(ostream &output, const BelCardParam &param) {
output << param.getName() << "=" << param.getValue();;
return output;
}
};
class BelCardLanguageParam : public BelCardParam {
......
......@@ -34,16 +34,22 @@ namespace belcard {
list<shared_ptr<BelCardParam>> _params;
public:
template <typename T>
static shared_ptr<T> parseProperty(const string& rule, 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);
BelCardParam::setAllParamsHandlersAndCollectors(&parser);
T::setHandlerAndCollectors(&parser);
shared_ptr<BelCardGeneric> ret = parser.parseInput(rule, input, NULL);
return dynamic_pointer_cast<T>(ret);
}
static shared_ptr<BelCardProperty> parse(const string& input);
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser);
BelCardProperty();
friend ostream &operator<<(ostream &output, const BelCardProperty &prop) {
output << prop.serialize();
return output;
}
virtual void setGroup(const string &group);
virtual const string &getGroup() const;
......@@ -90,6 +96,11 @@ namespace belcard {
virtual const list<shared_ptr<BelCardParam>> &getParams() const;
virtual string serialize() const;
friend ostream &operator<<(ostream &output, const BelCardProperty &prop) {
output << prop.serialize();
return output;
}
};
}
#endif
\ No newline at end of file
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardAddress> BelCardAddress::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("ADR", input, NULL);
return dynamic_pointer_cast<BelCardAddress>(ret);
return BelCardProperty::parseProperty<BelCardAddress>("ADR", input);
}
void BelCardAddress::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -95,22 +89,4 @@ void BelCardAddress::setLabelParam(const shared_ptr<BelCardLabelParam> &param) {
}
const shared_ptr<BelCardLabelParam> &BelCardAddress::getLabelParam() const {
return _label_param;
}
string BelCardAddress::serialize() const {
stringstream output;
if (getGroup().length() > 0) {
output << getGroup() << ".";
}
output << getName();
for (auto it = getParams().begin(); it != getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << getPostOfficeBox() << ";" << getExtendedAddress()
<< ";" << getStreet() << ";" << getLocality() << ";" << getRegion()
<< ";" << getPostalCode() << ";" << getCountry() << "\r\n";
return output.str();
}
\ No newline at end of file
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardFBURL> BelCardFBURL::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("FBURL", input, NULL);
return dynamic_pointer_cast<BelCardFBURL>(ret);
return BelCardProperty::parseProperty<BelCardFBURL>("FBURL", input);
}
void BelCardFBURL::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -32,13 +26,7 @@ BelCardFBURL::BelCardFBURL() : BelCardProperty() {
}
shared_ptr<BelCardCALADRURI> BelCardCALADRURI::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("CALADRURI", input, NULL);
return dynamic_pointer_cast<BelCardCALADRURI>(ret);
return BelCardProperty::parseProperty<BelCardCALADRURI>("CALADRURI", input);
}
void BelCardCALADRURI::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -59,13 +47,7 @@ BelCardCALADRURI::BelCardCALADRURI() : BelCardProperty() {
}
shared_ptr<BelCardCALURI> BelCardCALURI::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("CALURI", input, NULL);
return dynamic_pointer_cast<BelCardCALURI>(ret);
return BelCardProperty::parseProperty<BelCardCALURI>("CALURI", input);
}
void BelCardCALURI::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardPhoneNumber> BelCardPhoneNumber::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("TEL", input, NULL);
return dynamic_pointer_cast<BelCardPhoneNumber>(ret);
return BelCardProperty::parseProperty<BelCardPhoneNumber>("TEL", input);
}
void BelCardPhoneNumber::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -31,13 +25,7 @@ BelCardPhoneNumber::BelCardPhoneNumber() : BelCardProperty() {
}
shared_ptr<BelCardEmail> BelCardEmail::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("EMAIL", input, NULL);
return dynamic_pointer_cast<BelCardEmail>(ret);
return BelCardProperty::parseProperty<BelCardEmail>("EMAIL", input);
}
void BelCardEmail::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -57,13 +45,7 @@ BelCardEmail::BelCardEmail() : BelCardProperty() {
}
shared_ptr<BelCardImpp> BelCardImpp::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("IMPP", input, NULL);
return dynamic_pointer_cast<BelCardImpp>(ret);
return BelCardProperty::parseProperty<BelCardImpp>("IMPP", input);
}
void BelCardImpp::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -84,13 +66,7 @@ BelCardImpp::BelCardImpp() : BelCardProperty() {
}
shared_ptr<BelCardLang> BelCardLang::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("LANG", input, NULL);
return dynamic_pointer_cast<BelCardLang>(ret);
return BelCardProperty::parseProperty<BelCardLang>("LANG", input);
}
void BelCardLang::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardCategories> BelCardCategories::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("CATEGORIES", input, NULL);
return dynamic_pointer_cast<BelCardCategories>(ret);
return BelCardProperty::parseProperty<BelCardCategories>("CATEGORIES", input);
}
void BelCardCategories::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -31,13 +25,7 @@ BelCardCategories::BelCardCategories() : BelCardProperty() {
}
shared_ptr<BelCardNote> BelCardNote::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("NOTE", input, NULL);
return dynamic_pointer_cast<BelCardNote>(ret);
return BelCardProperty::parseProperty<BelCardNote>("NOTE", input);
}
void BelCardNote::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -58,13 +46,7 @@ BelCardNote::BelCardNote() : BelCardProperty() {
}
shared_ptr<BelCardProductId> BelCardProductId::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("PRODID", input, NULL);
return dynamic_pointer_cast<BelCardProductId>(ret);
return BelCardProperty::parseProperty<BelCardProductId>("PRODID", input);
}
void BelCardProductId::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -80,13 +62,7 @@ BelCardProductId::BelCardProductId() : BelCardProperty() {
}
shared_ptr<BelCardRevision> BelCardRevision::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("REV", input, NULL);
return dynamic_pointer_cast<BelCardRevision>(ret);
return BelCardProperty::parseProperty<BelCardRevision>("REV", input);
}
void BelCardRevision::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -102,13 +78,7 @@ BelCardRevision::BelCardRevision() : BelCardProperty() {
}
shared_ptr<BelCardSound> BelCardSound::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("SOUND", input, NULL);
return dynamic_pointer_cast<BelCardSound>(ret);
return BelCardProperty::parseProperty<BelCardSound>("SOUND", input);
}
void BelCardSound::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -130,13 +100,7 @@ BelCardSound::BelCardSound() : BelCardProperty() {
}
shared_ptr<BelCardUniqueId> BelCardUniqueId::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("UID", input, NULL);
return dynamic_pointer_cast<BelCardUniqueId>(ret);
return BelCardProperty::parseProperty<BelCardUniqueId>("UID", input);
}
void BelCardUniqueId::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -152,13 +116,7 @@ BelCardUniqueId::BelCardUniqueId() : BelCardProperty() {
}
shared_ptr<BelCardClientProductIdMap> BelCardClientProductIdMap::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("CLIENTPIDMAP", input, NULL);
return dynamic_pointer_cast<BelCardClientProductIdMap>(ret);
return BelCardProperty::parseProperty<BelCardClientProductIdMap>("CLIENTPIDMAP", input);
}
void BelCardClientProductIdMap::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -173,13 +131,7 @@ BelCardClientProductIdMap::BelCardClientProductIdMap() : BelCardProperty() {
}
shared_ptr<BelCardURL> BelCardURL::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("URL", input, NULL);
return dynamic_pointer_cast<BelCardURL>(ret);
return BelCardProperty::parseProperty<BelCardURL>("URL", input);
}
void BelCardURL::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardSource> BelCardSource::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("SOURCE", input, NULL);
return dynamic_pointer_cast<BelCardSource>(ret);
return BelCardProperty::parseProperty<BelCardSource>("SOURCE", input);
}
void BelCardSource::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -31,13 +25,7 @@ BelCardSource::BelCardSource() : BelCardProperty() {
}
shared_ptr<BelCardKind> BelCardKind::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("KIND", input, NULL);
return dynamic_pointer_cast<BelCardKind>(ret);
return BelCardProperty::parseProperty<BelCardKind>("KIND", input);
}
void BelCardKind::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -53,13 +41,7 @@ BelCardKind::BelCardKind() : BelCardProperty() {
}
shared_ptr<BelCardXML> BelCardXML::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("XML", input, NULL);
return dynamic_pointer_cast<BelCardXML>(ret);
return BelCardProperty::parseProperty<BelCardXML>("XML", input);
}
void BelCardXML::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardTimezone> BelCardTimezone::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("TZ", input, NULL);
return dynamic_pointer_cast<BelCardTimezone>(ret);
return BelCardProperty::parseProperty<BelCardTimezone>("TZ", input);
}
void BelCardTimezone::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -32,13 +26,7 @@ BelCardTimezone::BelCardTimezone() : BelCardProperty() {
}
shared_ptr<BelCardGeo> BelCardGeo::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("GEO", input, NULL);
return dynamic_pointer_cast<BelCardGeo>(ret);
return BelCardProperty::parseProperty<BelCardGeo>("GEO", input);
}
void BelCardGeo::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardFullName> BelCardFullName::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("FN", input, NULL);
return dynamic_pointer_cast<BelCardFullName>(ret);
return BelCardProperty::parseProperty<BelCardFullName>("FN", input);
}
void BelCardFullName::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -32,13 +26,7 @@ BelCardFullName::BelCardFullName() : BelCardProperty() {
}
shared_ptr<BelCardName> BelCardName::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("N", input, NULL);
return dynamic_pointer_cast<BelCardName>(ret);
return BelCardProperty::parseProperty<BelCardName>("N", input);
}
void BelCardName::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -95,30 +83,8 @@ const string &BelCardName::getSuffixes() const {
return _suffixes;
}
string BelCardName::serialize() const {
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::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);
return BelCardProperty::parseProperty<BelCardNickname>("NICKNAME", input);
}
void BelCardNickname::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -139,13 +105,7 @@ BelCardNickname::BelCardNickname() : BelCardProperty() {
}
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);
return BelCardProperty::parseProperty<BelCardBirthday>("BDAY", input);
}
void BelCardBirthday::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -164,13 +124,7 @@ BelCardBirthday::BelCardBirthday() : BelCardProperty() {
}
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);
return BelCardProperty::parseProperty<BelCardAnniversary>("ANNIVERSARY", input);
}
void BelCardAnniversary::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -188,13 +142,7 @@ BelCardAnniversary::BelCardAnniversary() : BelCardProperty() {
}
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);
return BelCardProperty::parseProperty<BelCardGender>("GENDER", input);
}
void BelCardGender::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -210,13 +158,7 @@ BelCardGender::BelCardGender() : BelCardProperty() {
}
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);
return BelCardProperty::parseProperty<BelCardPhoto>("PHOTO", input);
}
void BelCardPhoto::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -5,13 +5,7 @@ using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardTitle> BelCardTitle::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("TITLE", input, NULL);
return dynamic_pointer_cast<BelCardTitle>(ret);
return BelCardProperty::parseProperty<BelCardTitle>("TITLE", input);
}
void BelCardTitle::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -32,13 +26,7 @@ BelCardTitle::BelCardTitle() : BelCardProperty() {
}
shared_ptr<BelCardRole> BelCardRole::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("ROLE", input, NULL);
return dynamic_pointer_cast<BelCardRole>(ret);
return BelCardProperty::parseProperty<BelCardRole>("ROLE", input);
}
void BelCardRole::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -59,13 +47,7 @@ BelCardRole::BelCardRole() : BelCardProperty() {
}
shared_ptr<BelCardLogo> BelCardLogo::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("LOGO", input, NULL);
return dynamic_pointer_cast<BelCardLogo>(ret);
return BelCardProperty::parseProperty<BelCardLogo>("LOGO", input);
}
void BelCardLogo::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -87,13 +69,7 @@ BelCardLogo::BelCardLogo() : BelCardProperty() {
}
shared_ptr<BelCardOrganization> BelCardOrganization::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("ORG", input, NULL);
return dynamic_pointer_cast<BelCardOrganization>(ret);
return BelCardProperty::parseProperty<BelCardOrganization>("ORG", input);
}
void BelCardOrganization::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -115,13 +91,7 @@ BelCardOrganization::BelCardOrganization() : BelCardProperty() {
}
shared_ptr<BelCardMember> BelCardMember::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("MEMBER", input, NULL);
return dynamic_pointer_cast<BelCardMember>(ret);
return BelCardProperty::parseProperty<BelCardMember>("MEMBER", input);
}
void BelCardMember::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......@@ -141,13 +111,7 @@ BelCardMember::BelCardMember() : BelCardProperty() {
}
shared_ptr<BelCardRelated> BelCardRelated::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("RELATED", input, NULL);
return dynamic_pointer_cast<BelCardRelated>(ret);
return BelCardProperty::parseProperty<BelCardRelated>("RELATED", input);
}
void BelCardRelated::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
......
......@@ -4,19 +4,28 @@ using namespace::std;
using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardParam> BelCardParam::parse(const string& input) {
template <typename T>
shared_ptr<T> BelCardParam::parseParam(const string& rule, 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);
shared_ptr<BelCardGeneric> ret = parser.parseInput("any-param", input, NULL);
return dynamic_pointer_cast<BelCardParam>(ret);
T::setHandlerAndCollectors(&parser);
shared_ptr<BelCardGeneric> ret = parser.parseInput(rule, input, NULL);
return dynamic_pointer_cast<T>(ret);
}
shared_ptr<BelCardParam> BelCardParam::parse(const string& input) {
return BelCardParam::parseParam<BelCardParam>("any-param", input);
}
void BelCardParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("any-param", make_fn(BelCardGeneric::create<BelCardParam>))
->setCollector("param-name", make_sfn(&BelCardParam::setName))
->setCollector("param-value", make_sfn(&BelCardParam::setValue));
}
void BelCardParam::setAllParamsHandlersAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
BelCardParam::setHandlerAndCollectors(parser);
BelCardLabelParam::setHandlerAndCollectors(parser);
BelCardValueParam::setHandlerAndCollectors(parser);
BelCardPrefParam::setHandlerAndCollectors(parser);
......@@ -51,17 +60,12 @@ const string &BelCardParam::getValue() const {