summaryrefslogtreecommitdiffstats
blob: 051f37e9b57c1f34961d08e3c12bfa6832683582 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swiften/Parser/StanzaParser.h"

#include <iostream>
#include <boost/optional.hpp>
#include <cassert>

#include "Swiften/Parser/PayloadParser.h"
#include "Swiften/Parser/PayloadParserFactory.h"
#include "Swiften/Parser/PayloadParserFactoryCollection.h"
#include "Swiften/Parser/UnknownPayloadParser.h"

namespace Swift {

StanzaParser::StanzaParser(PayloadParserFactoryCollection* factories) : 
		currentDepth_(0), factories_(factories) {
}

StanzaParser::~StanzaParser() {
}

void StanzaParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
	if (inStanza()) {
		if (!inPayload()) {
			assert(!currentPayloadParser_.get());
			PayloadParserFactory* payloadParserFactory = factories_->getPayloadParserFactory(element, ns, attributes);
			if (payloadParserFactory) {
				currentPayloadParser_.reset(payloadParserFactory->createPayloadParser());
			}
			else {
				currentPayloadParser_.reset(new UnknownPayloadParser());
			}
		}
		assert(currentPayloadParser_.get());
		currentPayloadParser_->handleStartElement(element, ns, attributes);
	}
	else {
		boost::optional<std::string> from = attributes.getAttributeValue("from");
		if (from) {
			getStanza()->setFrom(JID(*from));
		}
		boost::optional<std::string> to = attributes.getAttributeValue("to");
		if (to) {
			getStanza()->setTo(JID(*to));
		}
		boost::optional<std::string> id = attributes.getAttributeValue("id");
		if (id) {
			getStanza()->setID(*id);
		}
		handleStanzaAttributes(attributes);
	}
	++currentDepth_;
}

void StanzaParser::handleEndElement(const std::string& element, const std::string& ns) {
	assert(inStanza());
	if (inPayload()) {
		assert(currentPayloadParser_.get());
		currentPayloadParser_->handleEndElement(element, ns);
		--currentDepth_;
		if (!inPayload()) {
			boost::shared_ptr<Payload> payload(currentPayloadParser_->getPayload());
			if (payload) {
				getStanza()->addPayload(payload);
			}
			currentPayloadParser_.reset();
		}
	}
	else {
		--currentDepth_;
	}
}

void StanzaParser::handleCharacterData(const std::string& data) {
	if (currentPayloadParser_.get()) {
		currentPayloadParser_->handleCharacterData(data);
	}
}

}