belcard_property.cpp 2.21 KB
Newer Older
Sylvain Berfini's avatar
Sylvain Berfini committed
1
#include "belcard/belcard_property.hpp"
2 3 4 5 6 7 8 9 10 11

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

shared_ptr<BelCardProperty> BelCardProperty::create() {
	return BelCardGeneric::create<BelCardProperty>();
}

shared_ptr<BelCardProperty> BelCardProperty::parse(const string& input) {
12 13 14 15
	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);
16
	shared_ptr<BelCardGeneric> ret = parser.parseInput("X-PROPERTY", input, NULL);
17
	return dynamic_pointer_cast<BelCardProperty>(ret);
18 19 20
}

void BelCardProperty::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
21
	parser->setHandler("X-PROPERTY", make_fn(&BelCardProperty::create))
22 23
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
24 25
			->setCollector("X-PROPERTY-name", make_sfn(&BelCardProperty::setName))
			->setCollector("X-PROPERTY-value", make_sfn(&BelCardProperty::setValue));
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
}

BelCardProperty::BelCardProperty() : BelCardGeneric() {
	
}

void BelCardProperty::setGroup(const string &group) {
	_group = group;
}
const string &BelCardProperty::getGroup() const {
	return _group;
}

void BelCardProperty::setName(const string &name) {
	_name = name;
}
const string &BelCardProperty::getName() const {
	return _name;
}

void BelCardProperty::setValue(const string &value) {
	_value = value;
}
const string &BelCardProperty::getValue() const {
	return _value;
}
Sylvain Berfini's avatar
Sylvain Berfini committed
52 53 54 55 56
		
void BelCardProperty::addValueParam(const shared_ptr<BelCardValueParam> &param) {
	_value_param = param;
	_params.push_back(_value_param);
}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

void BelCardProperty::addParam(const shared_ptr<BelCardParam> &param) {
	_params.push_back(param);
}
const list<shared_ptr<BelCardParam>> &BelCardProperty::getParams() const {
	return _params;
}

string BelCardProperty::serialize() const {
	stringstream output;
	
	if (getGroup().length() > 0) {
		output << getGroup() << ".";
	}
	
	output << getName();
	for (auto it = getParams().begin(); it != getParams().end(); ++it) {
		output << ";" << (**it); 
	}
	output << ":" << getValue() << "\r\n";
	
	return output.str();
}