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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* Copyright (c) 2010 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Swiften/Serializer/PayloadSerializers/FormSerializer.h"
#include <boost/shared_ptr.hpp>
#include <iostream>
#include "Swiften/Base/foreach.h"
#include "Swiften/Base/String.h"
#include "Swiften/Serializer/XML/XMLTextNode.h"
#include "Swiften/Serializer/XML/XMLRawTextNode.h"
using namespace Swift;
namespace {
template<typename T> void serializeValueAsString(boost::shared_ptr<FormField> field, boost::shared_ptr<XMLElement> parent) {
String value = boost::dynamic_pointer_cast<T>(field)->getValue();
if (!value.isEmpty()) {
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(value));
parent->addNode(valueElement);
}
}
}
namespace Swift {
FormSerializer::FormSerializer() : GenericPayloadSerializer<Form>() {
}
String FormSerializer::serializePayload(boost::shared_ptr<Form> form) const {
boost::shared_ptr<XMLElement> formElement(new XMLElement("x", "jabber:x:data"));
String type;
switch (form->getType()) {
case Form::FormType: type = "form"; break;
case Form::SubmitType: type = "submit"; break;
case Form::CancelType: type = "cancel"; break;
case Form::ResultType: type = "result"; break;
}
formElement->setAttribute("type", type);
if (!form->getTitle().isEmpty()) {
multiLineify(form->getTitle(), "title", formElement);
}
if (!form->getInstructions().isEmpty()) {
multiLineify(form->getInstructions(), "instructions", formElement);
}
foreach(boost::shared_ptr<FormField> field, form->getFields()) {
formElement->addNode(fieldToXML(field));
}
return formElement->serialize();
}
boost::shared_ptr<XMLElement> FormSerializer::fieldToXML(boost::shared_ptr<FormField> field) const {
boost::shared_ptr<XMLElement> fieldElement(new XMLElement("field"));
if (!field->getName().isEmpty()) {
fieldElement->setAttribute("var", field->getName());
}
if (!field->getLabel().isEmpty()) {
fieldElement->setAttribute("label", field->getLabel());
}
if (field->getRequired()) {
fieldElement->addNode(boost::shared_ptr<XMLElement>(new XMLElement("required")));
}
if (!field->getDescription().isEmpty()) {
boost::shared_ptr<XMLElement> descriptionElement(new XMLElement("desc"));
descriptionElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(field->getDescription())));
fieldElement->addNode(descriptionElement);
}
// Set the value and type
String fieldType;
if (boost::dynamic_pointer_cast<BooleanFormField>(field)) {
fieldType = "boolean";
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(boost::dynamic_pointer_cast<BooleanFormField>(field)->getValue() ? "1" : "0"));
fieldElement->addNode(valueElement);
}
else if (boost::dynamic_pointer_cast<FixedFormField>(field)) {
fieldType = "fixed";
serializeValueAsString<FixedFormField>(field, fieldElement);
}
else if (boost::dynamic_pointer_cast<HiddenFormField>(field)) {
fieldType = "hidden";
serializeValueAsString<HiddenFormField>(field, fieldElement);
}
else if (boost::dynamic_pointer_cast<ListSingleFormField>(field)) {
fieldType = "list-single";
serializeValueAsString<ListSingleFormField>(field, fieldElement);
}
else if (boost::dynamic_pointer_cast<TextPrivateFormField>(field)) {
fieldType = "text-private";
serializeValueAsString<TextPrivateFormField>(field, fieldElement);
}
else if (boost::dynamic_pointer_cast<TextSingleFormField>(field)) {
fieldType = "text-single";
serializeValueAsString<TextSingleFormField>(field, fieldElement);
}
else if (boost::dynamic_pointer_cast<JIDMultiFormField>(field)) {
fieldType = "jid-multi";
std::vector<JID> jids = boost::dynamic_pointer_cast<JIDMultiFormField>(field)->getValue();
foreach(const JID& jid, jids) {
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(jid.toString()));
fieldElement->addNode(valueElement);
}
}
else if (boost::dynamic_pointer_cast<JIDSingleFormField>(field)) {
fieldType = "jid-single";
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(boost::dynamic_pointer_cast<JIDSingleFormField>(field)->getValue().toString()));
fieldElement->addNode(valueElement);
}
else if (boost::dynamic_pointer_cast<ListMultiFormField>(field)) {
fieldType = "list-multi";
std::vector<String> lines = boost::dynamic_pointer_cast<ListMultiFormField>(field)->getValue();
foreach(const String& line, lines) {
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(line));
fieldElement->addNode(valueElement);
}
}
else if (boost::dynamic_pointer_cast<TextMultiFormField>(field)) {
fieldType = "text-multi";
multiLineify(boost::dynamic_pointer_cast<TextMultiFormField>(field)->getValue(), "value", fieldElement);
}
else if (boost::dynamic_pointer_cast<UntypedFormField>(field)) {
std::vector<String> lines = boost::dynamic_pointer_cast<UntypedFormField>(field)->getValue();
foreach(const String& line, lines) {
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(line));
fieldElement->addNode(valueElement);
}
}
else {
assert(false);
}
if (!fieldType.isEmpty()) {
fieldElement->setAttribute("type", fieldType);
}
foreach (const FormField::Option& option, field->getOptions()) {
boost::shared_ptr<XMLElement> optionElement(new XMLElement("option"));
optionElement->setAttribute("label", option.label);
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(option.value));
optionElement->addNode(valueElement);
fieldElement->addNode(optionElement);
}
return fieldElement;
}
void FormSerializer::multiLineify(const String& text, const String& elementName, boost::shared_ptr<XMLElement> element) const {
String unRdText(text);
unRdText.removeAll('\r');
std::vector<String> lines = unRdText.split('\n');
foreach (String line, lines) {
boost::shared_ptr<XMLElement> lineElement(new XMLElement(elementName));
lineElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(line)));
element->addNode(lineElement);
}
}
}
|