summaryrefslogtreecommitdiffstats
blob: 9422170065cfc0b870c16eb70fce400df5d2b3cb (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * 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/CommandParser.h"
#include "Swiften/Parser/PayloadParsers/FormParserFactory.h"
#include "Swiften/Parser/PayloadParsers/FormParser.h"

namespace Swift {

CommandParser::CommandParser() : level_(TopLevel), inNote_(false), inActions_(false), noteType_(Command::Note::Info), formParser_(0)  {
	formParserFactory_ = new FormParserFactory();
}

CommandParser::~CommandParser() {
	delete formParserFactory_;
}

void CommandParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
	++level_;
	if (level_ == PayloadLevel) {
		boost::optional<Command::Action> action = parseAction(attributes.getAttribute("action"));
		if (action) {
			getPayloadInternal()->setAction(*action);
		}

		std::string status = attributes.getAttribute("status");
		if (status == "executing") {
			getPayloadInternal()->setStatus(Command::Executing);
		}
		else if (status == "completed") {
			getPayloadInternal()->setStatus(Command::Completed);
		}
		else if (status == "canceled") {
			getPayloadInternal()->setStatus(Command::Canceled);
		}

		getPayloadInternal()->setNode(attributes.getAttribute("node"));
		getPayloadInternal()->setSessionID(attributes.getAttribute("sessionid"));
	}
	else if (level_ == FormOrNoteOrActionsLevel) {
		assert(!formParser_);
		if (formParserFactory_->canParse(element, ns, attributes)) {
			formParser_ = dynamic_cast<FormParser*>(formParserFactory_->createPayloadParser());
			assert(formParser_);
		}
		else if (element == "note") {
			inNote_ = true;
			currentText_.clear();
			std::string noteType = attributes.getAttribute("type");
			if (noteType == "info") {
				noteType_ = Command::Note::Info;
			}
			else if (noteType == "warn") {
				noteType_ = Command::Note::Warn;
			}
			else if (noteType == "error") {
				noteType_ = Command::Note::Error;
			}
			else {
				noteType_ = Command::Note::Info;
			}
		}
		else if (element == "actions") {
			inActions_ = true;
			boost::optional<Command::Action> action = parseAction(attributes.getAttribute("execute"));
			if (action) {
				getPayloadInternal()->setExecuteAction(*action);
			}
		}
	}
	else if (level_ == ActionsActionLevel) {
	}

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

void CommandParser::handleEndElement(const std::string& element, const std::string& ns) {
	if (formParser_) {
		formParser_->handleEndElement(element, ns);
	}

	if (level_ == FormOrNoteOrActionsLevel) {
		if (formParser_) {
			Form::ref form(boost::dynamic_pointer_cast<Form>(formParser_->getPayload()));
			assert(form);
			getPayloadInternal()->setForm(form);
			delete formParser_;
			formParser_ = 0;
		}
		else if (inNote_) {
			inNote_ = false;
			getPayloadInternal()->addNote(Command::Note(currentText_, noteType_));
		}
		else if (inActions_) {
			inActions_ = false;
		}
	}
	else if (level_ == ActionsActionLevel && inActions_) {
		boost::optional<Command::Action> action = parseAction(element);
		if (action) {
			getPayloadInternal()->addAvailableAction(*action);
		}
	}
	--level_;
}

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

boost::optional<Command::Action> CommandParser::parseAction(const std::string& action) {
	if (action == "execute") {
		return Command::Execute;
	}
	else if (action == "cancel") {
		return Command::Cancel;
	}
	else if (action == "prev") {
		return Command::Prev;
	}
	else if (action == "next") {
		return Command::Next;
	}
	else if (action == "complete") {
		return Command::Complete;
	}
	return boost::optional<Command::Action>();
}

}