belcard_params.cpp 9.07 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 "belcard/vcard_grammar.hpp"
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) {
28
	shared_ptr<Grammar> grammar = loadVcardGrammar();
Sylvain Berfini's avatar
Sylvain Berfini committed
29
	Parser<shared_ptr<BelCardGeneric>> parser(grammar);
Sylvain Berfini's avatar
Sylvain Berfini committed
30 31 32 33 34 35 36
	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
37 38 39
}

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

void BelCardParam::setAllParamsHandlersAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
	BelCardParam::setHandlerAndCollectors(parser);
47 48 49 50 51 52 53 54 55 56 57 58
	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
59 60 61 62 63 64
}

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

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

Sylvain Berfini's avatar
Sylvain Berfini committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
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;
}

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

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

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

96
shared_ptr<BelCardValueParam> BelCardValueParam::parse(const string& input) {
97
	shared_ptr<Grammar> grammar = loadVcardGrammar();
98 99 100 101 102 103 104
	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
105
	parser->setHandler("VALUE-param", make_fn(BelCardGeneric::create<BelCardValueParam>))
106
			->setCollector("VALUE-param-value", make_sfn(&BelCardValueParam::setValue));
107 108 109 110
}

BelCardValueParam::BelCardValueParam() : BelCardParam() {
	setName("VALUE");
111 112 113
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

BelCardTimezoneParam::BelCardTimezoneParam() : BelCardParam() {
	setName("TZ");
228 229 230
}

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

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

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