belcard_params.cpp 9.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	belcard_params.cpp
	Copyright (C) 2015  Belledonne Communications SARL

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

19
#include "belcard/belcard_params.hpp"
20
#include <belr/parser-impl.cc>
21 22 23 24 25

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

Sylvain Berfini's avatar
Sylvain Berfini committed
26 27
template <typename T>
shared_ptr<T> BelCardParam::parseParam(const string& rule, const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
28 29 30
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
Sylvain Berfini's avatar
Sylvain Berfini committed
31 32 33 34 35 36 37
	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);
Sylvain Berfini's avatar
Sylvain Berfini committed
38 39 40
}

void BelCardParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
41
	parser->setHandler("any-param", make_fn(BelCardGeneric::create<BelCardParam>))
Sylvain Berfini's avatar
Sylvain Berfini committed
42 43
			->setCollector("param-name", make_sfn(&BelCardParam::setName))
			->setCollector("param-value", make_sfn(&BelCardParam::setValue));
Sylvain Berfini's avatar
Sylvain Berfini committed
44 45 46 47
}

void BelCardParam::setAllParamsHandlersAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	BelCardParam::setHandlerAndCollectors(parser);
48 49 50 51 52 53 54 55 56 57 58 59
	BelCardLabelParam::setHandlerAndCollectors(parser);
	BelCardValueParam::setHandlerAndCollectors(parser);
	BelCardPrefParam::setHandlerAndCollectors(parser);
	BelCardAlternativeIdParam::setHandlerAndCollectors(parser);
	BelCardParamIdParam::setHandlerAndCollectors(parser);
	BelCardTypeParam::setHandlerAndCollectors(parser);
	BelCardMediaTypeParam::setHandlerAndCollectors(parser);
	BelCardCALSCALEParam::setHandlerAndCollectors(parser);
	BelCardSortAsParam::setHandlerAndCollectors(parser);
	BelCardGeoParam::setHandlerAndCollectors(parser);
	BelCardTimezoneParam::setHandlerAndCollectors(parser);
	BelCardLabelParam::setHandlerAndCollectors(parser);
Sylvain Berfini's avatar
Sylvain Berfini committed
60 61 62 63 64 65
}

BelCardParam::BelCardParam() : BelCardGeneric() {
	
}

66 67 68 69
void BelCardParam::serialize(ostream& output) const {
	output << getName() << "=" << getValue();
}

Sylvain Berfini's avatar
Sylvain Berfini committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
void BelCardParam::setName(const string &name) {
	_name = name;
}
const string &BelCardParam::getName() const {
	return _name;
}

void BelCardParam::setValue(const string &value) {
	_value = value;
}
const string &BelCardParam::getValue() const {
	return _value;
}

84
shared_ptr<BelCardLanguageParam> BelCardLanguageParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
85
	return BelCardParam::parseParam<BelCardLanguageParam>("LANGUAGE-param", input);
86 87 88
}

void BelCardLanguageParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
89
	parser->setHandler("LANGUAGE-param", make_fn(BelCardGeneric::create<BelCardLanguageParam>))
90
			->setCollector("LANGUAGE-param-value", make_sfn(&BelCardLanguageParam::setValue));
91 92 93 94 95 96
}

BelCardLanguageParam::BelCardLanguageParam() : BelCardParam() {
	setName("LANGUAGE");
}

97 98 99 100 101 102 103 104 105 106
shared_ptr<BelCardValueParam> BelCardValueParam::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);
	shared_ptr<BelCardGeneric> ret = parser.parseInput("VALUE-param", input, NULL);
	return dynamic_pointer_cast<BelCardValueParam>(ret);
}

void BelCardValueParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
107
	parser->setHandler("VALUE-param", make_fn(BelCardGeneric::create<BelCardValueParam>))
108
			->setCollector("VALUE-param-value", make_sfn(&BelCardValueParam::setValue));
109 110 111 112
}

BelCardValueParam::BelCardValueParam() : BelCardParam() {
	setName("VALUE");
113 114 115
}

shared_ptr<BelCardPrefParam> BelCardPrefParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
116
	return BelCardParam::parseParam<BelCardPrefParam>("PREF-param", input);
117 118 119
}

void BelCardPrefParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
120
	parser->setHandler("PREF-param", make_fn(BelCardGeneric::create<BelCardPrefParam>))
121
			->setCollector("PREF-param-value", make_sfn(&BelCardPrefParam::setValue));
122 123 124 125 126 127 128
}

BelCardPrefParam::BelCardPrefParam() : BelCardParam() {
	setName("PREF");
}

shared_ptr<BelCardAlternativeIdParam> BelCardAlternativeIdParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
129
	return BelCardParam::parseParam<BelCardAlternativeIdParam>("ALTID-param", input);
130 131 132
}

void BelCardAlternativeIdParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
133
	parser->setHandler("ALTID-param", make_fn(BelCardGeneric::create<BelCardAlternativeIdParam>))
134
			->setCollector("ALTID-param-value", make_sfn(&BelCardAlternativeIdParam::setValue));
135 136 137 138 139 140 141
}

BelCardAlternativeIdParam::BelCardAlternativeIdParam() : BelCardParam() {
	setName("ALTID");
}

shared_ptr<BelCardParamIdParam> BelCardParamIdParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
142
	return BelCardParam::parseParam<BelCardParamIdParam>("PID-param", input);
143 144 145
}

void BelCardParamIdParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
146
	parser->setHandler("PID-param", make_fn(BelCardGeneric::create<BelCardParamIdParam>))
147
			->setCollector("PID-param-value", make_sfn(&BelCardParamIdParam::setValue));
148 149 150 151 152 153 154
}

BelCardParamIdParam::BelCardParamIdParam() : BelCardParam() {
	setName("PID");
}

shared_ptr<BelCardTypeParam> BelCardTypeParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
155
	return BelCardParam::parseParam<BelCardTypeParam>("TYPE-param", input);
156 157 158
}

void BelCardTypeParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
159
	parser->setHandler("TYPE-param", make_fn(BelCardGeneric::create<BelCardTypeParam>))
160
			->setCollector("TYPE-param-value", make_sfn(&BelCardTypeParam::setValue));
161 162 163 164 165 166 167
}

BelCardTypeParam::BelCardTypeParam() : BelCardParam() {
	setName("TYPE");
}

shared_ptr<BelCardMediaTypeParam> BelCardMediaTypeParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
168
	return BelCardParam::parseParam<BelCardMediaTypeParam>("MEDIATYPE-param", input);
169 170 171
}

void BelCardMediaTypeParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
172
	parser->setHandler("MEDIATYPE-param", make_fn(BelCardGeneric::create<BelCardMediaTypeParam>))
173
			->setCollector("MEDIATYPE-param-value", make_sfn(&BelCardMediaTypeParam::setValue));
174 175 176 177 178 179 180
}

BelCardMediaTypeParam::BelCardMediaTypeParam() : BelCardParam() {
	setName("MEDIATYPE");
}

shared_ptr<BelCardCALSCALEParam> BelCardCALSCALEParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
181
	return BelCardParam::parseParam<BelCardCALSCALEParam>("CALSCALE-param", input);
182 183 184
}

void BelCardCALSCALEParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
185
	parser->setHandler("CALSCALE-param", make_fn(BelCardGeneric::create<BelCardCALSCALEParam>))
186
			->setCollector("CALSCALE-param-value", make_sfn(&BelCardCALSCALEParam::setValue));
187 188 189 190 191 192 193
}

BelCardCALSCALEParam::BelCardCALSCALEParam() : BelCardParam() {
	setName("CALSCALE");
}

shared_ptr<BelCardSortAsParam> BelCardSortAsParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
194
	return BelCardParam::parseParam<BelCardSortAsParam>("SORT-AS-param", input);
195 196 197
}

void BelCardSortAsParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
198
	parser->setHandler("SORT-AS-param", make_fn(BelCardGeneric::create<BelCardSortAsParam>))
199
			->setCollector("SORT-AS-param-value", make_sfn(&BelCardSortAsParam::setValue));
200 201 202 203 204 205 206
}

BelCardSortAsParam::BelCardSortAsParam() : BelCardParam() {
	setName("SORT-AS");
}

shared_ptr<BelCardGeoParam> BelCardGeoParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
207
	return BelCardParam::parseParam<BelCardGeoParam>("GEO-PARAM-param", input);
208 209 210
}

void BelCardGeoParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
211
	parser->setHandler("GEO-PARAM-param", make_fn(BelCardGeneric::create<BelCardGeoParam>))
212
			->setCollector("GEO-PARAM-param-value", make_sfn(&BelCardGeoParam::setValue));
213 214 215 216 217 218 219
}

BelCardGeoParam::BelCardGeoParam() : BelCardParam() {
	setName("GEO");
}

shared_ptr<BelCardTimezoneParam> BelCardTimezoneParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
220
	return BelCardParam::parseParam<BelCardTimezoneParam>("TZ-PARAM-param", input);
221 222 223
}

void BelCardTimezoneParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
224
	parser->setHandler("TZ-PARAM-param", make_fn(BelCardGeneric::create<BelCardTimezoneParam>))
225
			->setCollector("TZ-PARAM-param-value", make_sfn(&BelCardTimezoneParam::setValue));
226 227 228 229
}

BelCardTimezoneParam::BelCardTimezoneParam() : BelCardParam() {
	setName("TZ");
230 231 232
}

shared_ptr<BelCardLabelParam> BelCardLabelParam::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
233
	return BelCardParam::parseParam<BelCardLabelParam>("LABEL-param", input);
234 235 236
}

void BelCardLabelParam::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
237
	parser->setHandler("LABEL-param", make_fn(BelCardGeneric::create<BelCardLabelParam>))
238
			->setCollector("LABEL-param-value", make_sfn(&BelCardLabelParam::setValue));
239 240 241 242
}

BelCardLabelParam::BelCardLabelParam() : BelCardParam() {
	setName("LABEL");
243
}