summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/Base/String.h2
-rw-r--r--Swiften/Elements/Command.h66
-rw-r--r--Swiften/Elements/Form.h46
-rw-r--r--Swiften/Elements/FormField.h102
-rw-r--r--Swiften/Parser/PayloadParsers/DiscoInfoParser.h5
-rw-r--r--Swiften/Parser/PayloadParsers/FormParser.cpp132
-rw-r--r--Swiften/Parser/PayloadParsers/FormParser.h106
-rw-r--r--Swiften/Parser/PayloadParsers/FormParserFactory.h29
-rw-r--r--Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp2
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp112
-rw-r--r--Swiften/Parser/SConscript1
-rw-r--r--Swiften/SConscript4
-rw-r--r--Swiften/Serializer/PayloadSerializers/CommandSerializer.cpp98
-rw-r--r--Swiften/Serializer/PayloadSerializers/CommandSerializer.h25
-rw-r--r--Swiften/Serializer/PayloadSerializers/FormSerializer.cpp161
-rw-r--r--Swiften/Serializer/PayloadSerializers/FormSerializer.h27
-rw-r--r--Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp4
-rw-r--r--Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp139
-rw-r--r--Swiften/Serializer/XML/XMLTextNode.h6
19 files changed, 1063 insertions, 4 deletions
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h
index 0958795..03d9029 100644
--- a/Swiften/Base/String.h
+++ b/Swiften/Base/String.h
@@ -36,6 +36,8 @@ namespace Swift {
size_t getUTF8Size() const { return data_.size(); }
std::vector<unsigned int> getUnicodeCodePoints() const;
+ void clear() { data_.clear(); }
+
/**
* Returns the part before and after 'c'.
* If the given splitter does not occur in the string, the second
diff --git a/Swiften/Elements/Command.h b/Swiften/Elements/Command.h
new file mode 100644
index 0000000..c802035
--- /dev/null
+++ b/Swiften/Elements/Command.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/Payload.h"
+
+namespace Swift {
+ /**
+ * Ad-Hoc Command (XEP-0050).
+ */
+ class Command : public Payload {
+ public:
+ enum Status {Executing, Completed, Canceled, NoStatus};
+ enum Action {Cancel, Execute, Complete, Prev, Next, NoAction};
+
+ struct Note {
+ enum Type {Info, Warn, Error};
+
+ Note(String note, Type type) : note(note), type(type) {};
+
+ String note;
+ Type type;
+ };
+
+ public:
+ Command(const String& node, const String& sessionID, Status status) { constructor(node, sessionID, NoAction, status);}
+ Command(const String& node, const String& sessionID = "", Action action = Execute) { constructor(node, sessionID, action, NoStatus); }
+
+ const String& getNode() const { return node_; }
+ const String& getSessionID() const { return sessionID_; }
+ Action getPerformedAction() const { return performedAction_; }
+ void setExecuteAction(Action action) { executeAction_ = action; }
+ Action getExecuteAction() const { return executeAction_; }
+ Status getStatus() const { return status_; }
+ void addAvailableAction(Action action) { availableActions_.push_back(action); }
+ const std::vector<Action>& getAvailableActions() const { return availableActions_; }
+ void addNote(const Note& note) { notes_.push_back(note); }
+ const std::vector<Note>& getNotes() const { return notes_; }
+ boost::shared_ptr<Payload> getPayload() const { return payload_; }
+ void setPayload(boost::shared_ptr<Payload> payload) { payload_ = payload; }
+
+ private:
+ void constructor(const String& node, const String& sessionID, Action action, Status status) {
+ node_ = node;
+ sessionID_ = sessionID;
+ performedAction_ = action;
+ status_ = status;
+ executeAction_ = NoAction;
+ }
+
+ private:
+ String node_;
+ String sessionID_;
+ Action performedAction_;
+ Status status_;
+ Action executeAction_;
+ std::vector<Action> availableActions_;
+ std::vector<Note> notes_;
+ boost::shared_ptr<Payload> payload_;
+ };
+}
diff --git a/Swiften/Elements/Form.h b/Swiften/Elements/Form.h
new file mode 100644
index 0000000..ed77d2b
--- /dev/null
+++ b/Swiften/Elements/Form.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+
+#include "Swiften/Elements/Payload.h"
+#include "Swiften/Elements/FormField.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ /**
+ * XEP-0004 Data form.
+ * For the relevant Fields, the parsers and serialisers protect the API user against
+ * the strange multi-value instead of newline thing by transforming them.
+ */
+ class Form : public Payload {
+ public:
+ enum Type { FormType, SubmitType, CancelType, ResultType };
+
+ public:
+ Form(Type type = FormType) : type_(type) {}
+
+ void addField(boost::shared_ptr<FormField> field) { fields_.push_back(field); }
+ const std::vector<boost::shared_ptr<FormField> >& getFields() const { return fields_; }
+ void setTitle(const String& title) { title_ = title; }
+ const String& getTitle() { return title_; }
+
+ void setInstructions(const String& instructions) { instructions_ = instructions; }
+ const String& getInstructions() { return instructions_; }
+
+ Type getType() { return type_; }
+ void setType(Type type) { type_ = type; }
+
+ private:
+ std::vector<boost::shared_ptr<FormField> > fields_;
+ String title_;
+ String instructions_;
+ Type type_;
+ };
+}
diff --git a/Swiften/Elements/FormField.h b/Swiften/Elements/FormField.h
new file mode 100644
index 0000000..732203a
--- /dev/null
+++ b/Swiften/Elements/FormField.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <vector>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ class FormField {
+ public:
+ typedef boost::shared_ptr<FormField> ref;
+
+ virtual ~FormField() {}
+
+ struct Option {
+ Option(const String& label, const String& value) : label(label), value(value) {}
+ String label;
+ String value;
+ };
+
+ void setName(const String& name) { this->name = name; }
+ const String& getName() const { return name; }
+
+ void setLabel(const String& label) { this->label = label; }
+ const String& getLabel() const { return label; }
+
+ void setDescription(const String& description) { this->description = description; }
+ const String& getDescription() const { return description; }
+
+ void setRequired(bool required) { this->required = required; }
+ bool getRequired() const { return required; }
+
+ void addOption(const Option& option) {
+ options.push_back(option);
+ }
+
+ const std::vector<Option>& getOptions() const {
+ return options;
+ }
+
+ protected:
+ FormField() : required(false) {}
+
+ private:
+ String name;
+ String label;
+ String description;
+ bool required;
+ std::vector<Option> options;
+ };
+
+ template<typename T> class GenericFormField : public FormField {
+ public:
+ const T& getValue() const {
+ return value;
+ }
+
+ void setValue(const T& value) {
+ this->value = value;
+ }
+
+ protected:
+ GenericFormField() {}
+ GenericFormField(const T& value) : value(value) {}
+
+ private:
+ T value;
+ };
+
+#define SWIFTEN_DECLARE_FORM_FIELD(name, valueType) \
+ class name##FormField : public GenericFormField< valueType > { \
+ public: \
+ typedef boost::shared_ptr<name##FormField> ref; \
+ static ref create(const valueType& value) { \
+ return ref(new name##FormField(value)); \
+ } \
+ static ref create() { \
+ return ref(new name##FormField()); \
+ } \
+ private: \
+ name##FormField(valueType value) : GenericFormField< valueType >(value) {} \
+ name##FormField() : GenericFormField< valueType >() {} \
+ };
+
+ SWIFTEN_DECLARE_FORM_FIELD(Boolean, bool);
+ SWIFTEN_DECLARE_FORM_FIELD(Fixed, String);
+ SWIFTEN_DECLARE_FORM_FIELD(Hidden, String);
+ SWIFTEN_DECLARE_FORM_FIELD(ListSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD(TextMulti, String);
+ SWIFTEN_DECLARE_FORM_FIELD(TextPrivate, String);
+ SWIFTEN_DECLARE_FORM_FIELD(TextSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD(JIDSingle, JID);
+ SWIFTEN_DECLARE_FORM_FIELD(JIDMulti, std::vector<JID>);
+ SWIFTEN_DECLARE_FORM_FIELD(ListMulti, std::vector<String>);
+}
diff --git a/Swiften/Parser/PayloadParsers/DiscoInfoParser.h b/Swiften/Parser/PayloadParsers/DiscoInfoParser.h
index f1502fc..925d349 100644
--- a/Swiften/Parser/PayloadParsers/DiscoInfoParser.h
+++ b/Swiften/Parser/PayloadParsers/DiscoInfoParser.h
@@ -4,8 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_DiscoInfoParser_H
-#define SWIFTEN_DiscoInfoParser_H
+#pragma once
#include "Swiften/Elements/DiscoInfo.h"
#include "Swiften/Parser/GenericPayloadParser.h"
@@ -27,5 +26,3 @@ namespace Swift {
int level_;
};
}
-
-#endif
diff --git a/Swiften/Parser/PayloadParsers/FormParser.cpp b/Swiften/Parser/PayloadParsers/FormParser.cpp
new file mode 100644
index 0000000..f08e7a3
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/FormParser.cpp
@@ -0,0 +1,132 @@
+/*
+ * 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 String& element, const String&, const AttributeMap& attributes) {
+ if (level_ == TopLevel) {
+ 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") {
+ 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();
+ }
+ 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 String& element, const String&) {
+ --level_;
+ if (level_ == PayloadLevel) {
+ if (element == "title") {
+ String currentTitle = getPayloadInternal()->getTitle();
+ if (currentTitle.isEmpty()) {
+ getPayloadInternal()->setTitle(currentText_);
+ }
+ else {
+ getPayloadInternal()->setTitle(currentTitle + "\n" + currentText_);
+ }
+ }
+ else if (element == "instructions") {
+ String currentInstructions = getPayloadInternal()->getInstructions();
+ if (currentInstructions.isEmpty()) {
+ 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 String& text) {
+ currentText_ += text;
+}
+
+}
diff --git a/Swiften/Parser/PayloadParsers/FormParser.h b/Swiften/Parser/PayloadParsers/FormParser.h
new file mode 100644
index 0000000..76a54b9
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/FormParser.h
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Elements/Form.h"
+#include "Swiften/Parser/GenericPayloadParser.h"
+
+namespace Swift {
+ class FormParser : public GenericPayloadParser<Form> {
+ public:
+ FormParser();
+
+ virtual void handleStartElement(const String& element, const String&, const AttributeMap& attributes);
+ virtual void handleEndElement(const String& element, const String&);
+ virtual void handleCharacterData(const String& data);
+
+ private:
+ class FieldParseHelper {
+ public:
+ virtual ~FieldParseHelper() {}
+ virtual void addValue(const String&) = 0;
+ virtual boost::shared_ptr<FormField> getField() const {
+ return field;
+ }
+ protected:
+ boost::shared_ptr<FormField> field;
+ };
+ class BoolFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ boost::dynamic_pointer_cast< GenericFormField<bool> >(getField())->setValue(s == "1" || s == "true");
+ }
+ };
+ class StringFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ boost::shared_ptr<GenericFormField<String> > field = boost::dynamic_pointer_cast< GenericFormField<String> >(getField());
+ if (field->getValue().isEmpty()) {
+ field->setValue(s);
+ }
+ else {
+ field->setValue(field->getValue() + "\n" + s);
+ }
+ }
+ };
+ class JIDFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ boost::dynamic_pointer_cast< GenericFormField<JID> >(getField())->setValue(JID(s));
+ }
+ };
+ class StringListFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ // FIXME: Inefficient, but too much hassle to do efficiently
+ boost::shared_ptr<GenericFormField< std::vector<String> > > field = boost::dynamic_pointer_cast< GenericFormField<std::vector<String > > >(getField());
+ std::vector<String> l = field->getValue();
+ l.push_back(s);
+ field->setValue(l);
+ }
+ };
+ class JIDListFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ // FIXME: Inefficient, but too much hassle to do efficiently
+ boost::shared_ptr< GenericFormField< std::vector<JID> > > field = boost::dynamic_pointer_cast< GenericFormField<std::vector<JID > > >(getField());
+ std::vector<JID> l = field->getValue();
+ l.push_back(JID(s));
+ field->setValue(l);
+ }
+ };
+
+#define SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(name, baseParser) \
+ class name##FormFieldParseHelper : public baseParser##FieldParseHelper { \
+ public: \
+ typedef boost::shared_ptr<name##FormFieldParseHelper> ref; \
+ static ref create() { \
+ return ref(new name##FormFieldParseHelper()); \
+ } \
+ private: \
+ name##FormFieldParseHelper() : baseParser##FieldParseHelper() { \
+ field = name##FormField::create(); \
+ } \
+ };
+
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Boolean, Bool);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Fixed, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Hidden, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(ListSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(TextMulti, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(TextPrivate, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(TextSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(JIDSingle, JID);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(JIDMulti, JIDList);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(ListMulti, StringList);
+
+ enum Level {
+ TopLevel = 0,
+ PayloadLevel = 1,
+ FieldLevel = 2
+ };
+ int level_;
+ String currentText_;
+ String currentOptionLabel_;
+ boost::shared_ptr<FieldParseHelper> currentFieldParseHelper_;
+ };
+}
diff --git a/Swiften/Parser/PayloadParsers/FormParserFactory.h b/Swiften/Parser/PayloadParsers/FormParserFactory.h
new file mode 100644
index 0000000..805b0f1
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/FormParserFactory.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Parser/PayloadParserFactory.h"
+#include "Swiften/Parser/PayloadParsers/FormParser.h"
+
+namespace Swift {
+ class PayloadParserFactoryCollection;
+
+ class FormParserFactory : public PayloadParserFactory {
+ public:
+ FormParserFactory() {
+ }
+
+ virtual bool canParse(const String& /*element*/, const String& ns, const AttributeMap&) const {
+ return ns == "jabber:x:data";
+ }
+
+ virtual PayloadParser* createPayloadParser() {
+ return new FormParser();
+ }
+
+ };
+}
diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
index eace3d1..b115b10 100644
--- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
+++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
@@ -24,6 +24,7 @@
#include "Swiften/Parser/PayloadParsers/DiscoItemsParserFactory.h"
#include "Swiften/Parser/PayloadParsers/SecurityLabelParserFactory.h"
#include "Swiften/Parser/PayloadParsers/SecurityLabelsCatalogParserFactory.h"
+#include "Swiften/Parser/PayloadParsers/FormParserFactory.h"
#include "Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h"
#include "Swiften/Parser/PayloadParsers/VCardParserFactory.h"
#include "Swiften/Parser/PayloadParsers/RawXMLPayloadParserFactory.h"
@@ -51,6 +52,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() {
factories_.push_back(shared_ptr<PayloadParserFactory>(new StartSessionParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new SecurityLabelParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new SecurityLabelsCatalogParserFactory()));
+ factories_.push_back(shared_ptr<PayloadParserFactory>(new FormParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new VCardUpdateParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new VCardParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new PrivateStorageParserFactory(this)));
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
new file mode 100644
index 0000000..e7d80e3
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Parser/PayloadParsers/FormParser.h"
+#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h"
+
+using namespace Swift;
+
+class FormParserTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(FormParserTest);
+ CPPUNIT_TEST(testParse_FormInformation);
+ CPPUNIT_TEST(testParse);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testParse_FormInformation() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<x type=\"submit\" xmlns=\"jabber:x:data\">"
+ "<title>Bot Configuration</title>"
+ "<instructions>Hello!</instructions>"
+ "<instructions>Fill out this form to configure your new bot!</instructions>"
+ "</x>"
+ ));
+
+ Form* payload = dynamic_cast<Form*>(parser.getPayload().get());
+ CPPUNIT_ASSERT_EQUAL(String("Bot Configuration"), payload->getTitle());
+ CPPUNIT_ASSERT_EQUAL(String("Hello!\nFill out this form to configure your new bot!"), payload->getInstructions());
+ CPPUNIT_ASSERT_EQUAL(Form::SubmitType, payload->getType());
+ }
+
+ void testParse() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<x type=\"form\" xmlns=\"jabber:x:data\">"
+ "<field type=\"hidden\" var=\"FORM_TYPE\">"
+ "<value>jabber:bot</value>"
+ "</field>"
+ "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>"
+ "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>"
+ "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>"
+ "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">"
+ "<required/>"
+ "<value>1</value>"
+ "</field>"
+ "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>"
+ "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">"
+ "<value>news</value>"
+ "<value>search</value>"
+ "<option label=\"Contests\"><value>contests</value></option>"
+ "<option label=\"News\"><value>news</value></option>"
+ "<option label=\"Polls\"><value>polls</value></option>"
+ "<option label=\"Reminders\"><value>reminders</value></option>"
+ "<option label=\"Search\"><value>search</value></option>"
+ "</field>"
+ "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">"
+ "<value>20</value>"
+ "<option label=\"10\"><value>10</value></option>"
+ "<option label=\"20\"><value>20</value></option>"
+ "<option label=\"30\"><value>30</value></option>"
+ "<option label=\"50\"><value>50</value></option>"
+ "<option label=\"100\"><value>100</value></option>"
+ "<option label=\"None\"><value>none</value></option>"
+ "</field>"
+ "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">"
+ "<desc>Tell all your friends about your new bot!</desc>"
+ "<value>foo@bar.com</value>"
+ "<value>baz@fum.org</value>"
+ "</field>"
+ "</x>"));
+
+ Form* payload = dynamic_cast<Form*>(parser.getPayload().get());
+
+ CPPUNIT_ASSERT_EQUAL(9, static_cast<int>(payload->getFields().size()));
+ CPPUNIT_ASSERT_EQUAL(String("jabber:bot"), boost::dynamic_pointer_cast<HiddenFormField>(payload->getFields()[0])->getValue());
+ CPPUNIT_ASSERT_EQUAL(String("FORM_TYPE"), payload->getFields()[0]->getName());
+ CPPUNIT_ASSERT(!payload->getFields()[0]->getRequired());
+
+ CPPUNIT_ASSERT_EQUAL(String("Section 1: Bot Info"), boost::dynamic_pointer_cast<FixedFormField>(payload->getFields()[1])->getValue());
+
+ CPPUNIT_ASSERT_EQUAL(String("The name of your bot"), payload->getFields()[2]->getLabel());
+
+ CPPUNIT_ASSERT_EQUAL(String("This is a bot.\nA quite good one actually"), boost::dynamic_pointer_cast<TextMultiFormField>(payload->getFields()[3])->getValue());
+
+ CPPUNIT_ASSERT_EQUAL(true, boost::dynamic_pointer_cast<BooleanFormField>(payload->getFields()[4])->getValue());
+ CPPUNIT_ASSERT(payload->getFields()[4]->getRequired());
+
+ CPPUNIT_ASSERT_EQUAL(String("news"), boost::dynamic_pointer_cast<ListMultiFormField>(payload->getFields()[6])->getValue()[0]);
+ CPPUNIT_ASSERT_EQUAL(String("search"), boost::dynamic_pointer_cast<ListMultiFormField>(payload->getFields()[6])->getValue()[1]);
+ CPPUNIT_ASSERT_EQUAL(5, static_cast<int>(payload->getFields()[6]->getOptions().size()));
+ CPPUNIT_ASSERT_EQUAL(String("Contests"), payload->getFields()[6]->getOptions()[0].label);
+ CPPUNIT_ASSERT_EQUAL(String("contests"), payload->getFields()[6]->getOptions()[0].value);
+ CPPUNIT_ASSERT_EQUAL(String("News"), payload->getFields()[6]->getOptions()[1].label);
+ CPPUNIT_ASSERT_EQUAL(String("news"), payload->getFields()[6]->getOptions()[1].value);
+
+ CPPUNIT_ASSERT_EQUAL(String("20"), boost::dynamic_pointer_cast<ListSingleFormField>(payload->getFields()[7])->getValue());
+
+ CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com"), boost::dynamic_pointer_cast<JIDMultiFormField>(payload->getFields()[8])->getValue()[0]);
+ CPPUNIT_ASSERT_EQUAL(JID("baz@fum.org"), boost::dynamic_pointer_cast<JIDMultiFormField>(payload->getFields()[8])->getValue()[1]);
+ CPPUNIT_ASSERT_EQUAL(String("Tell all your friends about your new bot!"), payload->getFields()[8]->getDescription());
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(FormParserTest);
diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript
index 7b64ee3..1826432 100644
--- a/Swiften/Parser/SConscript
+++ b/Swiften/Parser/SConscript
@@ -23,6 +23,7 @@ sources = [
"PayloadParsers/DiscoInfoParser.cpp",
"PayloadParsers/DiscoItemsParser.cpp",
"PayloadParsers/ErrorParser.cpp",
+ "PayloadParsers/FormParser.cpp",
"PayloadParsers/FullPayloadParserFactoryCollection.cpp",
"PayloadParsers/PriorityParser.cpp",
"PayloadParsers/PrivateStorageParser.cpp",
diff --git a/Swiften/SConscript b/Swiften/SConscript
index 33a1a7e..f44d9a3 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -87,6 +87,8 @@ if env["SCONS_STAGE"] == "build" :
"Serializer/PayloadSerializers/StorageSerializer.cpp",
"Serializer/PayloadSerializers/PrivateStorageSerializer.cpp",
"Serializer/PayloadSerializers/DelaySerializer.cpp",
+ "Serializer/PayloadSerializers/CommandSerializer.cpp",
+ "Serializer/PayloadSerializers/FormSerializer.cpp",
"Serializer/PresenceSerializer.cpp",
"Serializer/StanzaSerializer.cpp",
"Serializer/StreamFeaturesSerializer.cpp",
@@ -164,6 +166,7 @@ if env["SCONS_STAGE"] == "build" :
File("Parser/PayloadParsers/UnitTest/BodyParserTest.cpp"),
File("Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp"),
File("Parser/PayloadParsers/UnitTest/ErrorParserTest.cpp"),
+ File("Parser/PayloadParsers/UnitTest/FormParserTest.cpp"),
File("Parser/PayloadParsers/UnitTest/PriorityParserTest.cpp"),
File("Parser/PayloadParsers/UnitTest/RawXMLPayloadParserTest.cpp"),
File("Parser/PayloadParsers/UnitTest/ResourceBindParserTest.cpp"),
@@ -199,6 +202,7 @@ if env["SCONS_STAGE"] == "build" :
File("Roster/UnitTest/XMPPRosterTest.cpp"),
File("Serializer/PayloadSerializers/UnitTest/PayloadsSerializer.cpp"),
File("Serializer/PayloadSerializers/UnitTest/CapsInfoSerializerTest.cpp"),
+ File("Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp"),
File("Serializer/PayloadSerializers/UnitTest/DiscoInfoSerializerTest.cpp"),
File("Serializer/PayloadSerializers/UnitTest/ErrorSerializerTest.cpp"),
File("Serializer/PayloadSerializers/UnitTest/PrioritySerializerTest.cpp"),
diff --git a/Swiften/Serializer/PayloadSerializers/CommandSerializer.cpp b/Swiften/Serializer/PayloadSerializers/CommandSerializer.cpp
new file mode 100644
index 0000000..3ac0c2c
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/CommandSerializer.cpp
@@ -0,0 +1,98 @@
+/*
+ * 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/CommandSerializer.h"
+
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Serializer/XML/XMLElement.h"
+#include "Swiften/Serializer/XML/XMLTextNode.h"
+#include "Swiften/Serializer/XML/XMLRawTextNode.h"
+#include "Swiften/Serializer/PayloadSerializerCollection.h"
+
+namespace Swift {
+
+CommandSerializer::CommandSerializer(PayloadSerializerCollection* serializers) : serializers(serializers) {
+}
+
+String CommandSerializer::serializePayload(boost::shared_ptr<Command> command) const {
+ XMLElement commandElement("command", "http://jabber.org/protocol/comands");
+ commandElement.setAttribute("node", command->getNode());
+
+ if (!command->getSessionID().isEmpty()) {
+ commandElement.setAttribute("sessionid", command->getSessionID());
+ }
+
+ String action = actionToString(command->getPerformedAction());
+ if (!action.isEmpty()) {
+ commandElement.setAttribute("action", action);
+ }
+
+ String status;
+ switch (command->getStatus()) {
+ case Command::Executing: status = "executing";break;
+ case Command::Completed: status = "completed";break;
+ case Command::Canceled: status = "canceled";break;
+ case Command::NoStatus: break;
+ }
+ if (!status.isEmpty()) {
+ commandElement.setAttribute("status", status);
+ }
+
+ if (command->getAvailableActions().size() > 0) {
+ String actions = "<actions";
+ String executeAction = actionToString(command->getExecuteAction());
+ if (!executeAction.isEmpty()) {
+ actions += " execute='" + executeAction + "'";
+ }
+ actions += ">";
+ foreach (Command::Action action, command->getAvailableActions()) {
+ actions += "<" + actionToString(action) + "/>";
+ }
+ actions += "</actions>";
+ commandElement.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(actions)));
+ }
+
+ foreach (Command::Note note, command->getNotes()) {
+ boost::shared_ptr<XMLElement> noteElement(new XMLElement("note"));
+ String type;
+ switch (note.type) {
+ case Command::Note::Info: type = "info";
+ case Command::Note::Warn: type = "warn";
+ case Command::Note::Error: type = "error";
+ }
+ if (!type.isEmpty()) {
+ noteElement->setAttribute("type", type);
+ }
+ noteElement->addNode(boost::shared_ptr<XMLTextNode>(new XMLTextNode(note.note)));
+ commandElement.addNode(noteElement);
+ }
+
+ boost::shared_ptr<Payload> payload = command->getPayload();
+ if (payload) {
+ PayloadSerializer* serializer = serializers->getPayloadSerializer(payload);
+ if (serializer) {
+ commandElement.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(serializer->serialize(payload))));
+ }
+ }
+ return commandElement.serialize();
+}
+
+String CommandSerializer::actionToString(Command::Action action) const {
+ String string;
+ switch (action) {
+ case Command::Cancel: string = "cancel"; break;
+ case Command::Execute: string = "execute"; break;
+ case Command::Complete: string = "complete"; break;
+ case Command::Prev: string = "prev"; break;
+ case Command::Next: string = "next"; break;
+ case Command::NoAction: break;
+ }
+ return string;
+}
+
+}
diff --git a/Swiften/Serializer/PayloadSerializers/CommandSerializer.h b/Swiften/Serializer/PayloadSerializers/CommandSerializer.h
new file mode 100644
index 0000000..6a0c067
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/CommandSerializer.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Serializer/GenericPayloadSerializer.h"
+#include "Swiften/Elements/Command.h"
+
+namespace Swift {
+ class PayloadSerializerCollection;
+
+ class CommandSerializer : public GenericPayloadSerializer<Command> {
+ public:
+ CommandSerializer(PayloadSerializerCollection* serializers);
+
+ virtual String serializePayload(boost::shared_ptr<Command>) const;
+
+ private:
+ PayloadSerializerCollection* serializers;
+ String actionToString(Command::Action action) const;
+ };
+}
diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp
new file mode 100644
index 0000000..e82f2d0
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp
@@ -0,0 +1,161 @@
+/*
+ * 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 {
+ assert(false);
+ }
+ 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);
+ }
+}
+
+}
diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.h b/Swiften/Serializer/PayloadSerializers/FormSerializer.h
new file mode 100644
index 0000000..1cdc7f2
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Serializer/GenericPayloadSerializer.h"
+#include "Swiften/Elements/Form.h"
+#include "Swiften/Elements/FormField.h"
+#include "Swiften/Serializer/XML/XMLElement.h"
+
+namespace Swift {
+ class FormSerializer : public GenericPayloadSerializer<Form> {
+ public:
+ FormSerializer();
+
+ virtual String serializePayload(boost::shared_ptr<Form>) const;
+
+ private:
+ boost::shared_ptr<XMLElement> fieldToXML(boost::shared_ptr<FormField> field) const;
+ void multiLineify(const String& text, const String& elementName, boost::shared_ptr<XMLElement> parent) const;
+ };
+}
+
+
diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
index 39e7d5b..04615a2 100644
--- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
+++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
@@ -31,6 +31,8 @@
#include "Swiften/Serializer/PayloadSerializers/StorageSerializer.h"
#include "Swiften/Serializer/PayloadSerializers/PrivateStorageSerializer.h"
#include "Swiften/Serializer/PayloadSerializers/DelaySerializer.h"
+#include "Swiften/Serializer/PayloadSerializers/FormSerializer.h"
+#include "Swiften/Serializer/PayloadSerializers/CommandSerializer.h"
namespace Swift {
@@ -57,7 +59,9 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() {
serializers_.push_back(new RawXMLPayloadSerializer());
serializers_.push_back(new StorageSerializer());
serializers_.push_back(new DelaySerializer());
+ serializers_.push_back(new FormSerializer());
serializers_.push_back(new PrivateStorageSerializer(this));
+ serializers_.push_back(new CommandSerializer(this));
foreach(PayloadSerializer* serializer, serializers_) {
addSerializer(serializer);
}
diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp
new file mode 100644
index 0000000..0d98dea
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Serializer/PayloadSerializers/FormSerializer.h"
+
+using namespace Swift;
+
+class FormSerializerTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(FormSerializerTest);
+ CPPUNIT_TEST(testSerializeFormInformation);
+ CPPUNIT_TEST(testSerializeFields);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testSerializeFormInformation() {
+ FormSerializer testling;
+ boost::shared_ptr<Form> form(new Form(Form::FormType));
+ form->setTitle("Bot Configuration");
+ form->setInstructions("Hello!\nFill out this form to configure your new bot!");
+
+ CPPUNIT_ASSERT_EQUAL(String(
+ "<x type=\"form\" xmlns=\"jabber:x:data\">"
+ "<title>Bot Configuration</title>"
+ "<instructions>Hello!</instructions>"
+ "<instructions>Fill out this form to configure your new bot!</instructions>"
+ "</x>"), testling.serialize(form));
+ }
+
+ void testSerializeFields() {
+ FormSerializer testling;
+ boost::shared_ptr<Form> form(new Form(Form::FormType));
+
+ FormField::ref field = HiddenFormField::create("jabber:bot");
+ field->setName("FORM_TYPE");
+ form->addField(field);
+
+ form->addField(FixedFormField::create("Section 1: Bot Info"));
+
+ field = TextSingleFormField::create();
+ field->setName("botname");
+ field->setLabel("The name of your bot");
+ form->addField(field);
+
+ field = TextMultiFormField::create("This is a bot.\nA quite good one actually");
+ field->setName("description");
+ field->setLabel("Helpful description of your bot");
+ form->addField(field);
+
+ field = BooleanFormField::create(true);
+ field->setName("public");
+ field->setLabel("Public bot?");
+ field->setRequired(true);
+ form->addField(field);
+
+ field = TextPrivateFormField::create();
+ field->setName("password");
+ field->setLabel("Password for special access");
+ form->addField(field);
+
+ std::vector<String> values;
+ values.push_back("news");
+ values.push_back("search");
+ field = ListMultiFormField::create(values);
+ field->setName("features");
+ field->setLabel("What features will the bot support?");
+ field->addOption(FormField::Option("Contests", "contests"));
+ field->addOption(FormField::Option("News", "news"));
+ field->addOption(FormField::Option("Polls", "polls"));
+ field->addOption(FormField::Option("Reminders", "reminders"));
+ field->addOption(FormField::Option("Search", "search"));
+ form->addField(field);
+
+ field = ListSingleFormField::create("20");
+ field->setName("maxsubs");
+ field->setLabel("Maximum number of subscribers");
+ field->addOption(FormField::Option("10", "10"));
+ field->addOption(FormField::Option("20", "20"));
+ field->addOption(FormField::Option("30", "30"));
+ field->addOption(FormField::Option("50", "50"));
+ field->addOption(FormField::Option("100", "100"));
+ field->addOption(FormField::Option("None", "none"));
+ form->addField(field);
+
+ std::vector<JID> jids;
+ jids.push_back(JID("foo@bar.com"));
+ jids.push_back(JID("baz@fum.org"));
+ field = JIDMultiFormField::create(jids);
+ field->setName("invitelist");
+ field->setLabel("People to invite");
+ field->setDescription("Tell all your friends about your new bot!");
+ form->addField(field);
+
+ CPPUNIT_ASSERT_EQUAL(String(
+ "<x type=\"form\" xmlns=\"jabber:x:data\">"
+ "<field type=\"hidden\" var=\"FORM_TYPE\">"
+ "<value>jabber:bot</value>"
+ "</field>"
+ "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>"
+ "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>"
+ "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>"
+ "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">"
+ "<required/>"
+ "<value>1</value>"
+ "</field>"
+ "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>"
+ "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">"
+ "<value>news</value>"
+ "<value>search</value>"
+ "<option label=\"Contests\"><value>contests</value></option>"
+ "<option label=\"News\"><value>news</value></option>"
+ "<option label=\"Polls\"><value>polls</value></option>"
+ "<option label=\"Reminders\"><value>reminders</value></option>"
+ "<option label=\"Search\"><value>search</value></option>"
+ "</field>"
+ "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">"
+ "<value>20</value>"
+ "<option label=\"10\"><value>10</value></option>"
+ "<option label=\"20\"><value>20</value></option>"
+ "<option label=\"30\"><value>30</value></option>"
+ "<option label=\"50\"><value>50</value></option>"
+ "<option label=\"100\"><value>100</value></option>"
+ "<option label=\"None\"><value>none</value></option>"
+ "</field>"
+ "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">"
+ "<desc>Tell all your friends about your new bot!</desc>"
+ "<value>foo@bar.com</value>"
+ "<value>baz@fum.org</value>"
+ "</field>"
+ "</x>"), testling.serialize(form));
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(FormSerializerTest);
diff --git a/Swiften/Serializer/XML/XMLTextNode.h b/Swiften/Serializer/XML/XMLTextNode.h
index 5f870ba..c1d13ef 100644
--- a/Swiften/Serializer/XML/XMLTextNode.h
+++ b/Swiften/Serializer/XML/XMLTextNode.h
@@ -11,6 +11,8 @@
namespace Swift {
class XMLTextNode : public XMLNode {
public:
+ typedef boost::shared_ptr<XMLTextNode> ref;
+
XMLTextNode(const String& text) : text_(text) {
text_.replaceAll('&', "&amp;"); // Should come first
text_.replaceAll('<', "&lt;");
@@ -21,6 +23,10 @@ namespace Swift {
return text_;
}
+ static ref create(const String& text) {
+ return ref(new XMLTextNode(text));
+ }
+
private:
String text_;
};