blob: f8e02a48cc71b5a44fee8a6ca2df658cd0213757 (
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
|
/*
* 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/FormParser.h"
namespace Swift {
FormParser::FormParser() : level_(TopLevel) {
}
void FormParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
if (level_ == TopLevel) {
std::string type = attributes.getAttribute("type");
if (type == "form") {
getPayloadInternal()->setType(Form::FormType);
}
else if (type == "submit") {
getPayloadInternal()->setType(Form::SubmitType);
}
else if (type == "cancel") {
getPayloadInternal()->setType(Form::CancelType);
}
else if (type == "result") {
getPayloadInternal()->setType(Form::ResultType);
}
}
else if (level_ == PayloadLevel) {
if (element == "title") {
currentText_.clear();
}
else if (element == "instructions") {
currentText_.clear();
}
else if (element == "field") {
std::string type = attributes.getAttribute("type");
if (type == "boolean") {
currentFieldParseHelper_ = BooleanFormFieldParseHelper::create();
}
else if (type == "fixed") {
currentFieldParseHelper_ = FixedFormFieldParseHelper::create();
}
else if (type == "hidden") {
currentFieldParseHelper_ = HiddenFormFieldParseHelper::create();
}
else if (type == "jid-multi") {
currentFieldParseHelper_ = JIDMultiFormFieldParseHelper::create();
}
else if (type == "jid-single") {
currentFieldParseHelper_ = JIDSingleFormFieldParseHelper::create();
}
else if (type == "list-multi") {
currentFieldParseHelper_ = ListMultiFormFieldParseHelper::create();
}
else if (type == "list-single") {
currentFieldParseHelper_ = ListSingleFormFieldParseHelper::create();
}
else if (type == "text-multi") {
currentFieldParseHelper_ = TextMultiFormFieldParseHelper::create();
}
else if (type == "text-private") {
currentFieldParseHelper_ = TextPrivateFormFieldParseHelper::create();
}
else if (type == "text-single") {
currentFieldParseHelper_ = TextSingleFormFieldParseHelper::create();
}
else {
currentFieldParseHelper_ = UntypedFormFieldParseHelper::create();
}
if (currentFieldParseHelper_) {
currentFieldParseHelper_->getField()->setName(attributes.getAttribute("var"));
currentFieldParseHelper_->getField()->setLabel(attributes.getAttribute("label"));
}
}
}
else if (level_ == FieldLevel && currentFieldParseHelper_) {
currentText_.clear();
if (element == "option") {
currentOptionLabel_ = attributes.getAttribute("label");
}
}
++level_;
}
void FormParser::handleEndElement(const std::string& element, const std::string&) {
--level_;
if (level_ == PayloadLevel) {
if (element == "title") {
std::string currentTitle = getPayloadInternal()->getTitle();
if (currentTitle.empty()) {
getPayloadInternal()->setTitle(currentText_);
}
else {
getPayloadInternal()->setTitle(currentTitle + "\n" + currentText_);
}
}
else if (element == "instructions") {
std::string currentInstructions = getPayloadInternal()->getInstructions();
if (currentInstructions.empty()) {
getPayloadInternal()->setInstructions(currentText_);
}
else {
getPayloadInternal()->setInstructions(currentInstructions + "\n" + currentText_);
}
}
else if (element == "field") {
if (currentFieldParseHelper_) {
getPayloadInternal()->addField(currentFieldParseHelper_->getField());
currentFieldParseHelper_.reset();
}
}
}
else if (level_ == FieldLevel && currentFieldParseHelper_) {
if (element == "required") {
currentFieldParseHelper_->getField()->setRequired(true);
}
else if (element == "desc") {
currentFieldParseHelper_->getField()->setDescription(currentText_);
}
else if (element == "option") {
currentFieldParseHelper_->getField()->addOption(FormField::Option(currentOptionLabel_, currentText_));
}
else if (element == "value") {
currentFieldParseHelper_->addValue(currentText_);
}
}
}
void FormParser::handleCharacterData(const std::string& text) {
currentText_ += text;
}
}
|