summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-08-08 18:41:53 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-08-08 18:41:53 (GMT)
commit19df82042f44c201e5a2821b4fa35465e33a1c90 (patch)
tree1746bddfa31ce2a6488ef5186036a049a255c9da /Swiften/Serializer
parent4a5a0977f661bf5c7c34ee7aa48b35073a682203 (diff)
downloadswift-contrib-19df82042f44c201e5a2821b4fa35465e33a1c90.zip
swift-contrib-19df82042f44c201e5a2821b4fa35465e33a1c90.tar.bz2
Added XEP-0004 data forms parsing & serializing.
Diffstat (limited to 'Swiften/Serializer')
-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
7 files changed, 460 insertions, 0 deletions
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_;
};