From 6310bc00ceeb65f0cf6fc0ada9d1b188517d8a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sat, 20 Jun 2009 10:17:59 +0200 Subject: Added vcard-temp update parser. diff --git a/Swiften/Elements/VCardUpdate.h b/Swiften/Elements/VCardUpdate.h new file mode 100644 index 0000000..b6c8ebd --- /dev/null +++ b/Swiften/Elements/VCardUpdate.h @@ -0,0 +1,17 @@ +#pragma once + +#include "Swiften/Base/String.h" +#include "Swiften/Elements/Payload.h" + +namespace Swift { + class VCardUpdate : public Payload { + public: + VCardUpdate() {} + + void setPhotoHash(const String& photoHash) { photoHash_ = photoHash; } + const String& getPhotoHash() { return photoHash_; } + + private: + String photoHash_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index fb2fea7..de66278 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -14,6 +14,7 @@ #include "Swiften/Parser/PayloadParsers/DiscoInfoParserFactory.h" #include "Swiften/Parser/PayloadParsers/SecurityLabelParserFactory.h" #include "Swiften/Parser/PayloadParsers/SecurityLabelsCatalogParserFactory.h" +#include "Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h" using namespace boost; @@ -32,6 +33,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(shared_ptr(new StartSessionParserFactory())); factories_.push_back(shared_ptr(new SecurityLabelParserFactory())); factories_.push_back(shared_ptr(new SecurityLabelsCatalogParserFactory())); + factories_.push_back(shared_ptr(new VCardUpdateParserFactory())); foreach(shared_ptr factory, factories_) { addFactory(factory.get()); } diff --git a/Swiften/Parser/PayloadParsers/Makefile.inc b/Swiften/Parser/PayloadParsers/Makefile.inc index 828dc5e..e7ea276 100644 --- a/Swiften/Parser/PayloadParsers/Makefile.inc +++ b/Swiften/Parser/PayloadParsers/Makefile.inc @@ -10,6 +10,7 @@ SWIFTEN_SOURCES += \ Swiften/Parser/PayloadParsers/ErrorParser.cpp \ Swiften/Parser/PayloadParsers/RosterParser.cpp \ Swiften/Parser/PayloadParsers/ResourceBindParser.cpp \ + Swiften/Parser/PayloadParsers/VCardUpdateParser.cpp \ Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp include Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc diff --git a/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc b/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc index f4d4cf9..6d492c1 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc +++ b/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc @@ -9,4 +9,5 @@ UNITTEST_SOURCES += \ Swiften/Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp \ Swiften/Parser/PayloadParsers/UnitTest/ResourceBindParserTest.cpp \ Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelParserTest.cpp \ - Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelsCatalogParserTest.cpp + Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelsCatalogParserTest.cpp \ + Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp diff --git a/Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp new file mode 100644 index 0000000..0d9ae64 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp @@ -0,0 +1,32 @@ +#include +#include + +#include "Swiften/Parser/PayloadParsers/VCardUpdateParser.h" +#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h" + +using namespace Swift; + +class VCardUpdateParserTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(VCardUpdateParserTest); + CPPUNIT_TEST(testParse); + CPPUNIT_TEST_SUITE_END(); + + public: + VCardUpdateParserTest() {} + + void testParse() { + VCardUpdateParser testling; + PayloadParserTester parser(&testling); + + CPPUNIT_ASSERT(parser.parse( + "" + "sha1-hash-of-image" + "")); + + VCardUpdate* payload = dynamic_cast(testling.getPayload().get()); + CPPUNIT_ASSERT_EQUAL(String("sha1-hash-of-image"), payload->getPhotoHash()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(VCardUpdateParserTest); diff --git a/Swiften/Parser/PayloadParsers/VCardUpdateParser.cpp b/Swiften/Parser/PayloadParsers/VCardUpdateParser.cpp new file mode 100644 index 0000000..855f9b0 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/VCardUpdateParser.cpp @@ -0,0 +1,26 @@ +#include "Swiften/Parser/PayloadParsers/VCardUpdateParser.h" + +namespace Swift { + +VCardUpdateParser::VCardUpdateParser() : level_(TopLevel) { +} + +void VCardUpdateParser::handleStartElement(const String& element, const String&, const AttributeMap& attributes) { + if (level_ == PayloadLevel) { + currentText_ = ""; + } + ++level_; +} + +void VCardUpdateParser::handleEndElement(const String& element, const String&) { + --level_; + if (level_ == PayloadLevel && element == "photo") { + getPayloadInternal()->setPhotoHash(currentText_); + } +} + +void VCardUpdateParser::handleCharacterData(const String& text) { + currentText_ += text; +} + +} diff --git a/Swiften/Parser/PayloadParsers/VCardUpdateParser.h b/Swiften/Parser/PayloadParsers/VCardUpdateParser.h new file mode 100644 index 0000000..3682ccf --- /dev/null +++ b/Swiften/Parser/PayloadParsers/VCardUpdateParser.h @@ -0,0 +1,25 @@ +#pragma once + +#include "Swiften/Elements/VCardUpdate.h" +#include "Swiften/Parser/GenericPayloadParser.h" + +namespace Swift { + class SerializingParser; + + class VCardUpdateParser : public GenericPayloadParser { + public: + VCardUpdateParser(); + + 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: + enum Level { + TopLevel = 0, + PayloadLevel = 1 + }; + int level_; + String currentText_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h b/Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h new file mode 100644 index 0000000..0a8dddf --- /dev/null +++ b/Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Swiften/Parser/GenericPayloadParserFactory.h" +#include "Swiften/Parser/PayloadParsers/VCardUpdateParser.h" + +namespace Swift { + class VCardUpdateParserFactory : public GenericPayloadParserFactory { + public: + VCardUpdateParserFactory() : GenericPayloadParserFactory("x", "vcard-temp:x:update") {} + }; +} -- cgit v0.10.2-6-g49f6