Commit 8deddfdd authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Added Source & XML properties

parent 97024aad
......@@ -9,8 +9,6 @@
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
using namespace::belr;
......@@ -31,6 +29,8 @@ namespace belcard {
list<shared_ptr<BelCardEmail>> _emails;
list<shared_ptr<BelCardImpp>> _impp;
list<shared_ptr<BelCardLang>> _langs;
list<shared_ptr<BelCardSource>> _sources;
list<shared_ptr<BelCardXML>> _xml;
list<shared_ptr<BelCardProperty>> _properties;
public:
......@@ -52,7 +52,9 @@ namespace belcard {
->setCollector("TEL", make_sfn(&BelCard::addTel))
->setCollector("EMAIL", make_sfn(&BelCard::addEmail))
->setCollector("IMPP", make_sfn(&BelCard::addImpp))
->setCollector("LANG", make_sfn(&BelCard::addLang));
->setCollector("LANG", make_sfn(&BelCard::addLang))
->setCollector("SOURCE", make_sfn(&BelCard::addSource))
->setCollector("XML", make_sfn(&BelCard::addXML));
}
BelCard() {
......@@ -163,6 +165,22 @@ namespace belcard {
return _langs;
}
void addSource(const shared_ptr<BelCardSource> &source) {
_sources.push_back(source);
addProperty(source);
}
const list<shared_ptr<BelCardSource>> &getSource() const {
return _sources;
}
void addXML(const shared_ptr<BelCardXML> &xml) {
_xml.push_back(xml);
addProperty(xml);
}
const list<shared_ptr<BelCardXML>> &getXML() const {
return _xml;
}
void addProperty(const shared_ptr<BelCardProperty> &property) {
_properties.push_back(property);
}
......
......@@ -6,9 +6,6 @@
#include <belr/abnf.hh>
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
using namespace::belr;
......@@ -45,6 +42,70 @@ namespace belcard {
BelCardProperty::addParam(param);
}
};
class BelCardSource : public BelCardProperty {
public:
static shared_ptr<BelCardSource> create() {
return make_shared<BelCardSource>();
}
static shared_ptr<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);
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("SOURCE", make_fn(&BelCardSource::create))
->setCollector("group", make_sfn(&BelCardSource::setGroup))
->setCollector("any-param", make_sfn(&BelCardSource::addParam))
->setCollector("SOURCE-value", make_sfn(&BelCardSource::setValue));
}
BelCardSource() : BelCardProperty() {
setName("SOURCE");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
class BelCardXML : public BelCardProperty {
public:
static shared_ptr<BelCardXML> create() {
return make_shared<BelCardXML>();
}
static shared_ptr<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);
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("XML", make_fn(&BelCardXML::create))
->setCollector("group", make_sfn(&BelCardXML::setGroup))
->setCollector("any-param", make_sfn(&BelCardXML::addParam))
->setCollector("XML-value", make_sfn(&BelCardXML::setValue));
}
BelCardXML() : BelCardProperty() {
setName("XML");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
}
#endif
\ No newline at end of file
......@@ -33,6 +33,8 @@ shared_ptr<BelCard> BelCardParser::parse(const string &input) {
BelCardEmail::setHandlerAndCollectors(&parser);
BelCardImpp::setHandlerAndCollectors(&parser);
BelCardLang::setHandlerAndCollectors(&parser);
BelCardSource::setHandlerAndCollectors(&parser);
BelCardXML::setHandlerAndCollectors(&parser);
size_t parsedSize = 0;
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard, &parsedSize);
......
......@@ -5,12 +5,19 @@ vcard = "BEGIN:VCARD" CRLF
1*property
"END:VCARD" CRLF
property = KIND / FN / N / NICKNAME / BDAY / ANNIVERSARY / GENDER / PHOTO
property = KIND / SOURCE / XML
/ FN / N / NICKNAME / BDAY / ANNIVERSARY / GENDER / PHOTO
/ ADR / TEL / EMAIL / IMPP / LANG
KIND = [group "."] "KIND" *(";" any-param) ":" KIND-value CRLF
KIND-value = "individual" / "group" / "org" / "location" / iana-token / x-name
SOURCE = [group "."] "SOURCE" *(";" any-param) ":" SOURCE-value CRLF
SOURCE-value = URI
XML = [group "."] "XML" *(";" any-param) ":" XML-value CRLF
XML-value = text
FN = [group "."] "FN" *(";" any-param) ":" FN-value CRLF
FN-value = text
......
......@@ -15,8 +15,36 @@ static void kind_property(void) {
BC_ASSERT_EQUAL(input.compare(sstream.str()), 0, int, "%d");
}
static void source_property(void) {
string input = "SOURCE:ldap://ldap.example.com/cn=Babs%20Jensen,%20o=Babsco,%20c=US\r\n";
shared_ptr<BelCardSource> source = BelCardSource::parse(input);
BC_ASSERT_TRUE_FATAL(source != NULL);
stringstream sstream;
sstream << *source;
BC_ASSERT_EQUAL(input.compare(sstream.str()), 0, int, "%d");
input = "SOURCE:http://directory.example.com/addressbooks/jdoe/Jean%20Dupont.vcf\r\n";
source = BelCardSource::parse(input);
BC_ASSERT_TRUE_FATAL(source != NULL);
sstream.clear();
sstream.str(std::string());
sstream << *source;
BC_ASSERT_EQUAL(input.compare(sstream.str()), 0, int, "%d");
}
static void xml_property(void) {
string input = "XML:<config xmlns=\"http://www.linphone.org/xsds/lpconfig.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd\"></config>\r\n";
shared_ptr<BelCardXML> xml = BelCardXML::parse(input);
BC_ASSERT_TRUE_FATAL(xml != NULL);
stringstream sstream;
sstream << *xml;
BC_ASSERT_EQUAL(input.compare(sstream.str()), 0, int, "%d");
}
static test_t tests[] = {
{ "Kind", kind_property },
{ "Source", source_property },
{ "XML", xml_property },
};
test_suite_t vcard_general_properties_test_suite = {
......
......@@ -48,6 +48,7 @@ static void vcard_parsing(void) {
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
BC_ASSERT_TRUE(belCard != NULL);
//TODO: find a way to check the belCard object
delete parser;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment