summaryrefslogtreecommitdiffstats
blob: 0030af7f4bf806ce2c4fbd1b034868b976253bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
/*
 * Copyright (c) 2011 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <deque>

#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <Swiften/Parser/GenericPayloadParser.h>
#include <Swiften/Parser/Tree/ParserElement.h>
#include <Swiften/Parser/Tree/NullParserElement.h>

#include <iostream>

namespace Swift {


	/**
	 * Generic parser offering something a bit like a DOM to work with.
	 */
	template<typename PAYLOAD_TYPE>
	class GenericPayloadTreeParser : public GenericPayloadParser<PAYLOAD_TYPE> {
		public:
			virtual void handleStartElement(const std::string& element, const std::string& xmlns, const AttributeMap& attributes) {
				//std::cerr << element << ", " << xmlns << ", " << attributes.getEntries().size();
				if (!root_) {
					root_ = boost::make_shared<ParserElement>(element, xmlns, attributes);
					elementStack_.push_back(root_);
				} else {
					ParserElement::ref current = *elementStack_.rbegin();
					elementStack_.push_back(current->addChild(element, xmlns, attributes));
				}
			}

			virtual void handleEndElement(const std::string& /*element*/, const std::string&) {
				elementStack_.pop_back();
				if (elementStack_.empty()) {
					handleTree(root_);
				}
			}

			virtual void handleCharacterData(const std::string& data) {
				ParserElement::ref current = *elementStack_.rbegin();
				current->appendCharacterData(data);
			}

			virtual void handleTree(ParserElement::ref root) = 0;

		private:
			std::deque<ParserElement::ref> elementStack_;
			ParserElement::ref root_;
	};
}