belcard_parser.cpp 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	belcard_parser.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/>.
*/

Sylvain Berfini's avatar
Sylvain Berfini committed
19 20
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard.hpp"
21
#include "belcard/belcard_utils.hpp"
22

Sylvain Berfini's avatar
Sylvain Berfini committed
23

24 25 26 27
#include <iostream>
#include <fstream>
#include <sstream>

Sylvain Berfini's avatar
Sylvain Berfini committed
28 29 30 31
using namespace::std;
using namespace::belr;
using namespace::belcard;

32 33 34 35 36
shared_ptr<BelCardParser> BelCardParser::getInstance() {
	static shared_ptr<BelCardParser> parser(new BelCardParser);
	return parser;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
37
BelCardParser::BelCardParser() {
38 39 40
	ABNFGrammarBuilder grammar_builder;
	shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
	_parser = new Parser<shared_ptr<BelCardGeneric>>(grammar);
41
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
42 43 44 45
	BelCardList::setHandlerAndCollectors(_parser);
	BelCard::setHandlerAndCollectors(_parser);
	BelCardParam::setAllParamsHandlersAndCollectors(_parser);
	BelCardProperty::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
46
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
47 48 49
	BelCardSource::setHandlerAndCollectors(_parser);
	BelCardKind::setHandlerAndCollectors(_parser);
	BelCardXML::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
50
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
51 52 53 54 55 56 57
	BelCardFullName::setHandlerAndCollectors(_parser);
	BelCardName::setHandlerAndCollectors(_parser);
	BelCardNickname::setHandlerAndCollectors(_parser);
	BelCardPhoto::setHandlerAndCollectors(_parser);
	BelCardBirthday::setHandlerAndCollectors(_parser);
	BelCardAnniversary::setHandlerAndCollectors(_parser);
	BelCardGender::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
58
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
59
	BelCardAddress::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
60
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
61 62 63 64
	BelCardPhoneNumber::setHandlerAndCollectors(_parser);
	BelCardEmail::setHandlerAndCollectors(_parser);
	BelCardImpp::setHandlerAndCollectors(_parser);
	BelCardLang::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
65
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
66 67
	BelCardTimezone::setHandlerAndCollectors(_parser);
	BelCardGeo::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
68
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
69 70 71 72 73 74
	BelCardTitle::setHandlerAndCollectors(_parser);
	BelCardRole::setHandlerAndCollectors(_parser);
	BelCardLogo::setHandlerAndCollectors(_parser);
	BelCardOrganization::setHandlerAndCollectors(_parser);
	BelCardMember::setHandlerAndCollectors(_parser);
	BelCardRelated::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
75
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
76 77 78 79 80 81 82 83
	BelCardCategories::setHandlerAndCollectors(_parser);
	BelCardNote::setHandlerAndCollectors(_parser);
	BelCardProductId::setHandlerAndCollectors(_parser);
	BelCardRevision::setHandlerAndCollectors(_parser);
	BelCardSound::setHandlerAndCollectors(_parser);
	BelCardUniqueId::setHandlerAndCollectors(_parser);
	BelCardClientProductIdMap::setHandlerAndCollectors(_parser);
	BelCardURL::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
84
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
85
	BelCardKey::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
86
	
BenRejeb Iyed's avatar
BenRejeb Iyed committed
87 88 89
	BelCardFBURL::setHandlerAndCollectors(_parser);
	BelCardCALADRURI::setHandlerAndCollectors(_parser);
	BelCardCALURI::setHandlerAndCollectors(_parser);
Ronan's avatar
Ronan committed
90
	
91 92 93 94 95 96 97 98 99 100
	BelCardBirthPlace::setHandlerAndCollectors(_parser);
	BelCardDeathPlace::setHandlerAndCollectors(_parser);
	BelCardDeathDate::setHandlerAndCollectors(_parser);
}

BelCardParser::~BelCardParser() {
	delete _parser;
}

shared_ptr<BelCardGeneric> BelCardParser::_parse(const string &input, const string &rule) {
Sylvain Berfini's avatar
Sylvain Berfini committed
101
	size_t parsedSize = 0;
102
	shared_ptr<BelCardGeneric> ret = _parser->parseInput(rule, input, &parsedSize);
103
	if (parsedSize < input.size()){
104
		bctbx_error("[belcard] Parsing ended prematuraly at pos %llu", (unsigned long long)parsedSize);
105
	}
106 107 108 109 110 111
	return ret;
}

shared_ptr<BelCard> BelCardParser::parseOne(const string &input) {
	string vcard = belcard_unfold(input);
	shared_ptr<BelCardGeneric> ret = _parse(vcard, "vcard");
Sylvain Berfini's avatar
Sylvain Berfini committed
112 113
	shared_ptr<BelCard> belCard = dynamic_pointer_cast<BelCard>(ret);
	return belCard;
114 115 116 117 118 119 120
}

shared_ptr<BelCardList> BelCardParser::parse(const string &input) {
	string vcards = belcard_unfold(input);
	shared_ptr<BelCardGeneric> ret = _parse(vcards, "vcard-list");
	shared_ptr<BelCardList> belCards = dynamic_pointer_cast<BelCardList>(ret);
	return belCards;
121 122 123
}

shared_ptr<BelCardList> BelCardParser::parseFile(const string &filename) {
124 125
	string vcard = belcard_read_file(filename);
	return parse(vcard);
126
}