From a49e2a1d4d17c36fad6ac1d1313fac955f675a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Thu, 6 May 2010 20:50:32 +0200 Subject: Completed storage bookmark parser & serializer. Added URI element. diff --git a/Swiften/Elements/Storage.h b/Swiften/Elements/Storage.h index 515cd52..dcc4b85 100644 --- a/Swiften/Elements/Storage.h +++ b/Swiften/Elements/Storage.h @@ -25,6 +25,13 @@ namespace Swift { String password; }; + struct URL { + URL() {} + + String name; + String url; + }; + Storage() { } @@ -36,7 +43,16 @@ namespace Swift { conferences.push_back(conference); } + const std::vector& getURLs() const { + return urls; + } + + void addURL(const URL& url) { + urls.push_back(url); + } + private: std::vector conferences; + std::vector urls; }; } diff --git a/Swiften/Parser/PayloadParsers/StorageParser.cpp b/Swiften/Parser/PayloadParsers/StorageParser.cpp index f6fd29d..660bdd0 100644 --- a/Swiften/Parser/PayloadParsers/StorageParser.cpp +++ b/Swiften/Parser/PayloadParsers/StorageParser.cpp @@ -22,6 +22,12 @@ void StorageParser::handleStartElement(const String& element, const String&, con conference->jid = JID(attributes.getAttribute("jid")); conference->name = attributes.getAttribute("name"); } + else if (element == "url") { + assert(!url); + url = Storage::URL(); + url->name = attributes.getAttribute("name"); + url->url = attributes.getAttribute("url"); + } } else if (level == DetailLevel) { currentText = ""; @@ -37,6 +43,11 @@ void StorageParser::handleEndElement(const String& element, const String&) { getPayloadInternal()->addConference(*conference); conference.reset(); } + else if (element == "url") { + assert(url); + getPayloadInternal()->addURL(*url); + url.reset(); + } } else if (level == DetailLevel && conference) { if (element == "nick") { diff --git a/Swiften/Parser/PayloadParsers/StorageParser.h b/Swiften/Parser/PayloadParsers/StorageParser.h index 735513e..e8fcbd8 100644 --- a/Swiften/Parser/PayloadParsers/StorageParser.h +++ b/Swiften/Parser/PayloadParsers/StorageParser.h @@ -29,5 +29,6 @@ namespace Swift { int level; String currentText; boost::optional conference; + boost::optional url; }; } diff --git a/Swiften/Parser/PayloadParsers/UnitTest/StorageParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/StorageParserTest.cpp index 88dd125..03dc144 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/StorageParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/StorageParserTest.cpp @@ -16,6 +16,7 @@ class StorageParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(StorageParserTest); CPPUNIT_TEST(testParse_Conference); CPPUNIT_TEST(testParse_MultipleConferences); + CPPUNIT_TEST(testParse_URL); CPPUNIT_TEST_SUITE_END(); public: @@ -65,6 +66,22 @@ class StorageParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(String("Tea party"), conferences[1].name); CPPUNIT_ASSERT_EQUAL(JID("teaparty@wonderland.lit"), conferences[1].jid); } + + void testParse_URL() { + PayloadsParserTester parser; + + CPPUNIT_ASSERT(parser.parse( + "" + "" + "")); + + Storage* payload = dynamic_cast(parser.getPayload().get()); + std::vector urls = payload->getURLs(); + CPPUNIT_ASSERT_EQUAL(1, static_cast(urls.size())); + CPPUNIT_ASSERT_EQUAL(String("Complete Works of Shakespeare"), urls[0].name); + CPPUNIT_ASSERT_EQUAL(String("http://the-tech.mit.edu/Shakespeare/"), urls[0].url); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(StorageParserTest); diff --git a/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp b/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp index f7beecd..b4a2a49 100644 --- a/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/StorageSerializer.cpp @@ -19,6 +19,7 @@ StorageSerializer::StorageSerializer() : GenericPayloadSerializer() { String StorageSerializer::serializePayload(boost::shared_ptr storage) const { XMLElement storageElement("storage", "storage:bookmarks"); + foreach(const Storage::Conference& conference, storage->getConferences()) { boost::shared_ptr conferenceElement(new XMLElement("conference")); conferenceElement->setAttribute("name", conference.name); @@ -36,6 +37,14 @@ String StorageSerializer::serializePayload(boost::shared_ptr storage) c } storageElement.addNode(conferenceElement); } + + foreach(const Storage::URL& url, storage->getURLs()) { + boost::shared_ptr urlElement(new XMLElement("url")); + urlElement->setAttribute("name", url.name); + urlElement->setAttribute("url", url.url); + storageElement.addNode(urlElement); + } + return storageElement.serialize(); } diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp index 714de42..5888731 100644 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/StorageSerializerTest.cpp @@ -31,6 +31,10 @@ class StorageSerializerTest : public CppUnit::TestFixture { conference.nick = "Puck"; conference.password = "MyPass"; storage->addConference(conference); + Storage::URL url; + url.name = "Complete Works of Shakespeare"; + url.url = "http://the-tech.mit.edu/Shakespeare/"; + storage->addURL(url); CPPUNIT_ASSERT_EQUAL(String( "" @@ -41,6 +45,7 @@ class StorageSerializerTest : public CppUnit::TestFixture { "Puck" "MyPass" "" + "" ""), serializer.serialize(storage)); } -- cgit v0.10.2-6-g49f6