summaryrefslogtreecommitdiffstats
blob: 7c8752ce5f9c53e79f4b54bca6bc094e0fad72ee (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
102
103
104
105
106
107
108
109
110
/*
 * 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/SearchPayloadParser.h"
#include "Swiften/Parser/PayloadParsers/FormParserFactory.h"
#include "Swiften/Parser/PayloadParsers/FormParser.h"

namespace Swift {

SearchPayloadParser::SearchPayloadParser() : level(TopLevel), formParser(NULL)  {
	formParserFactory = new FormParserFactory();
}

SearchPayloadParser::~SearchPayloadParser() {
	delete formParserFactory;
}

void SearchPayloadParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
	if (level == TopLevel) {
	}
	else if (level == PayloadLevel) {
		if (element == "x" && ns == "jabber:x:data") {
			assert(!formParser);
			formParser = dynamic_cast<FormParser*>(formParserFactory->createPayloadParser());
		}
		else if (element == "item") {
			assert(!currentItem);
			currentItem.reset(SearchPayload::Item());
			currentItem->jid = JID(attributes.getAttribute("jid"));
		}
		else {
			currentText.clear();
		}
	}
	else if (level == ItemLevel && currentItem) {
		currentText.clear();
	}

	if (formParser) {
		formParser->handleStartElement(element, ns, attributes);
	}

	++level;
}

void SearchPayloadParser::handleEndElement(const std::string& element, const std::string& ns) {
	--level;

	if (formParser) {
		formParser->handleEndElement(element, ns);
	}

	if (level == TopLevel) {
	}
	else if (level == PayloadLevel) {
		if (formParser) {
			getPayloadInternal()->setForm(formParser->getPayloadInternal());
			delete formParser;
			formParser = NULL;
		}
		else if (element == "item") {
			assert(currentItem);
			getPayloadInternal()->addItem(*currentItem);
			currentItem.reset();
		}
		else if (element == "instructions") {
			getPayloadInternal()->setInstructions(currentText);
		}
		else if (element == "nick") {
			getPayloadInternal()->setNick(currentText);
		}
		else if (element == "first") {
			getPayloadInternal()->setFirst(currentText);
		}
		else if (element == "last") {
			getPayloadInternal()->setLast(currentText);
		}
		else if (element == "email") {
			getPayloadInternal()->setEMail(currentText);
		}
	}
	else if (level == ItemLevel && currentItem) {
		if (element == "nick") {
			currentItem->nick = currentText;
		}
		else if (element == "first") {
			currentItem->first = currentText;
		}
		else if (element == "last") {
			currentItem->last = currentText;
		}
		else if (element == "email") {
			currentItem->email = currentText;
		}
	}
}

void SearchPayloadParser::handleCharacterData(const std::string& data) {
	if (formParser) {
		formParser->handleCharacterData(data);
	}
	else {
		currentText += data;
	}
}

}