summaryrefslogtreecommitdiffstats
blob: 0da9f482a9fe21d34b74149573fed844577fadd2 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * 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/PayloadParsers/RosterParser.h"
#include "Swiften/Parser/SerializingParser.h"

namespace Swift {

RosterParser::RosterParser() : level_(TopLevel), inItem_(false), unknownContentParser_(0) {
}

void RosterParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
	if (level_ == TopLevel) {
		AttributeMap::const_iterator i = attributes.find("ver");
		if (i != attributes.end()) {
			getPayloadInternal()->setVersion(i->second);
		}
	}
	else if (level_ == PayloadLevel) {
		if (element == "item") {
			inItem_ = true;
			currentItem_ = RosterItemPayload();

			currentItem_.setJID(JID(attributes.getAttribute("jid")));
			currentItem_.setName(attributes.getAttribute("name"));

			std::string subscription = attributes.getAttribute("subscription");
			if (subscription == "both") {
				currentItem_.setSubscription(RosterItemPayload::Both);
			}
			else if (subscription == "to") {
				currentItem_.setSubscription(RosterItemPayload::To);
			}
			else if (subscription == "from") {
				currentItem_.setSubscription(RosterItemPayload::From);
			}
			else if (subscription == "remove") {
				currentItem_.setSubscription(RosterItemPayload::Remove);
			}
			else {
				currentItem_.setSubscription(RosterItemPayload::None);
			}

			if (attributes.getAttribute("ask") == "subscribe") {
				currentItem_.setSubscriptionRequested();
			}
		}
	}
	else if (level_ == ItemLevel) {
		if (element == "group") {
			currentText_ = "";
		}
		else {
			assert(!unknownContentParser_);
			unknownContentParser_ = new SerializingParser();
			unknownContentParser_->handleStartElement(element, ns, attributes);
		}
	}
	else if (unknownContentParser_) {
		unknownContentParser_->handleStartElement(element, ns, attributes);
	}
	++level_;
}

void RosterParser::handleEndElement(const std::string& element, const std::string& ns) {
	--level_;
	if (level_ == PayloadLevel) {
		if (inItem_) {
			getPayloadInternal()->addItem(currentItem_);
			inItem_ = false;
		}
	}
	else if (level_ == ItemLevel) {
		if (unknownContentParser_) {
			unknownContentParser_->handleEndElement(element, ns);
			currentItem_.addUnknownContent(unknownContentParser_->getResult());
			delete unknownContentParser_;
			unknownContentParser_ = NULL;
		}
		else if (element == "group") {
			currentItem_.addGroup(currentText_);
		}
	}
	else if (unknownContentParser_) {
		unknownContentParser_->handleEndElement(element, ns);
	}
}

void RosterParser::handleCharacterData(const std::string& data) {
	if (unknownContentParser_) {
		unknownContentParser_->handleCharacterData(data);
	}
	else {
		currentText_ += data;
	}
}

}