diff options
Diffstat (limited to 'Swiften/Serializer')
6 files changed, 199 insertions, 3 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index 0dc4b2d..f57411b 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -37,6 +37,7 @@ #include "Swiften/Serializer/PayloadSerializers/DelaySerializer.h" #include "Swiften/Serializer/PayloadSerializers/FormSerializer.h" #include "Swiften/Serializer/PayloadSerializers/CommandSerializer.h" +#include "Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.h" #include "Swiften/Serializer/PayloadSerializers/NicknameSerializer.h" namespace Swift { @@ -72,6 +73,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new FormSerializer()); serializers_.push_back(new PrivateStorageSerializer(this)); serializers_.push_back(new CommandSerializer()); + serializers_.push_back(new InBandRegistrationPayloadSerializer()); serializers_.push_back(new NicknameSerializer()); foreach(PayloadSerializer* serializer, serializers_) { addSerializer(serializer); diff --git a/Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.cpp b/Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.cpp new file mode 100644 index 0000000..8e8bcf2 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.h" + +#include <boost/shared_ptr.hpp> + +#include "Swiften/Base/foreach.h" +#include "Swiften/Serializer/XML/XMLElement.h" +#include "Swiften/Serializer/XML/XMLRawTextNode.h" +#include "Swiften/Serializer/PayloadSerializers/FormSerializer.h" + +namespace Swift { + +InBandRegistrationPayloadSerializer::InBandRegistrationPayloadSerializer() { +} + +String InBandRegistrationPayloadSerializer::serializePayload(boost::shared_ptr<InBandRegistrationPayload> registration) const { + XMLElement registerElement("query", "jabber:iq:register"); + + if (registration->isRegistered()) { + registerElement.addNode(XMLElement::ref(new XMLElement("registered"))); + } + + if (registration->getInstructions()) { + registerElement.addNode(XMLElement::ref(new XMLElement("instructions", "", *registration->getInstructions()))); + } + + + if (registration->getUsername()) { + registerElement.addNode(XMLElement::ref(new XMLElement("username", "", *registration->getUsername()))); + } + + if (registration->getNick()) { + registerElement.addNode(XMLElement::ref(new XMLElement("nick", "", *registration->getNick()))); + } + + if (registration->getPassword()) { + registerElement.addNode(XMLElement::ref(new XMLElement("password", "", *registration->getPassword()))); + } + + if (registration->getName()) { + registerElement.addNode(XMLElement::ref(new XMLElement("name", "", *registration->getName()))); + } + + if (registration->getFirst()) { + registerElement.addNode(XMLElement::ref(new XMLElement("first", "", *registration->getFirst()))); + } + + if (registration->getLast()) { + registerElement.addNode(XMLElement::ref(new XMLElement("last", "", *registration->getLast()))); + } + + if (registration->getEMail()) { + registerElement.addNode(XMLElement::ref(new XMLElement("email", "", *registration->getEMail()))); + } + + if (registration->getAddress()) { + registerElement.addNode(XMLElement::ref(new XMLElement("address", "", *registration->getAddress()))); + } + + if (registration->getCity()) { + registerElement.addNode(XMLElement::ref(new XMLElement("city", "", *registration->getCity()))); + } + + if (registration->getState()) { + registerElement.addNode(XMLElement::ref(new XMLElement("state", "", *registration->getState()))); + } + + if (registration->getZip()) { + registerElement.addNode(XMLElement::ref(new XMLElement("zip", "", *registration->getZip()))); + } + + if (registration->getPhone()) { + registerElement.addNode(XMLElement::ref(new XMLElement("phone", "", *registration->getPhone()))); + } + + if (registration->getURL()) { + registerElement.addNode(XMLElement::ref(new XMLElement("url", "", *registration->getURL()))); + } + + if (registration->getDate()) { + registerElement.addNode(XMLElement::ref(new XMLElement("date", "", *registration->getDate()))); + } + + if (registration->getMisc()) { + registerElement.addNode(XMLElement::ref(new XMLElement("misc", "", *registration->getMisc()))); + } + + if (registration->getText()) { + registerElement.addNode(XMLElement::ref(new XMLElement("text", "", *registration->getText()))); + } + + if (registration->getKey()) { + registerElement.addNode(XMLElement::ref(new XMLElement("key", "", *registration->getKey()))); + } + + if (Form::ref form = registration->getForm()) { + registerElement.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(FormSerializer().serialize(form)))); + } + + return registerElement.serialize(); +} + +} diff --git a/Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.h b/Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.h new file mode 100644 index 0000000..168aa3a --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/InBandRegistrationPayloadSerializer.h @@ -0,0 +1,22 @@ +/* + * 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/Serializer/GenericPayloadSerializer.h" +#include "Swiften/Elements/InBandRegistrationPayload.h" + +namespace Swift { + class PayloadSerializerCollection; + + class InBandRegistrationPayloadSerializer : public GenericPayloadSerializer<InBandRegistrationPayload> { + public: + InBandRegistrationPayloadSerializer(); + + virtual String serializePayload(boost::shared_ptr<InBandRegistrationPayload>) const; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp new file mode 100644 index 0000000..1654abc --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/InBandRegistrationPayloadSerializerTest.cpp @@ -0,0 +1,61 @@ +/* + * 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/InBandRegistrationPayloadSerializer.h" + +using namespace Swift; + +class InBandRegistrationPayloadSerializerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(InBandRegistrationPayloadSerializerTest); + CPPUNIT_TEST(testSerialize); + CPPUNIT_TEST(testSerialize_Form); + CPPUNIT_TEST_SUITE_END(); + + public: + void testSerialize() { + InBandRegistrationPayloadSerializer testling; + boost::shared_ptr<InBandRegistrationPayload> registration(new InBandRegistrationPayload()); + registration->setRegistered(true); + + String expectedResult = + "<query xmlns=\"jabber:iq:register\">" + "<registered/>" + "</query>"; + + CPPUNIT_ASSERT_EQUAL(expectedResult, testling.serialize(registration)); + } + void testSerialize_Form() { + InBandRegistrationPayloadSerializer testling; + boost::shared_ptr<InBandRegistrationPayload> registration(new InBandRegistrationPayload()); + registration->setInstructions("Use the enclosed form to register."); + + boost::shared_ptr<Form> form(new Form(Form::FormType)); + form->setTitle("Contest Registration"); + + FormField::ref field = HiddenFormField::create("jabber:iq:register"); + field->setName("FORM_TYPE"); + form->addField(field); + registration->setForm(form); + + String expectedResult = + "<query xmlns=\"jabber:iq:register\">" + "<instructions>Use the enclosed form to register.</instructions>" + "<x type=\"form\" xmlns=\"jabber:x:data\">" + "<title>Contest Registration</title>" + "<field type=\"hidden\" var=\"FORM_TYPE\">" + "<value>jabber:iq:register</value>" + "</field>" + "</x>" + "</query>"; + + CPPUNIT_ASSERT_EQUAL(expectedResult, testling.serialize(registration)); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(InBandRegistrationPayloadSerializerTest); diff --git a/Swiften/Serializer/XML/XMLElement.cpp b/Swiften/Serializer/XML/XMLElement.cpp index d88ff08..289ce31 100644 --- a/Swiften/Serializer/XML/XMLElement.cpp +++ b/Swiften/Serializer/XML/XMLElement.cpp @@ -7,14 +7,17 @@ #include "Swiften/Serializer/XML/XMLElement.h" #include "Swiften/Base/foreach.h" +#include "Swiften/Serializer/XML/XMLTextNode.h" namespace Swift { -XMLElement::XMLElement(const String& tag, const String& xmlns) : - tag_(tag) { +XMLElement::XMLElement(const String& tag, const String& xmlns, const String& text) : tag_(tag) { if (!xmlns.isEmpty()) { setAttribute("xmlns", xmlns); } + if (!text.isEmpty()) { + addNode(XMLTextNode::ref(new XMLTextNode(text))); + } } String XMLElement::serialize() { diff --git a/Swiften/Serializer/XML/XMLElement.h b/Swiften/Serializer/XML/XMLElement.h index cac29f1..b350344 100644 --- a/Swiften/Serializer/XML/XMLElement.h +++ b/Swiften/Serializer/XML/XMLElement.h @@ -19,7 +19,7 @@ namespace Swift { public: typedef boost::shared_ptr<XMLElement> ref; - XMLElement(const String& tag, const String& xmlns = ""); + XMLElement(const String& tag, const String& xmlns = "", const String& text = ""); void setAttribute(const String& attribute, const String& value); void addNode(boost::shared_ptr<XMLNode> node); |