From fcd2d7cfb5a05be0fd13bfa3582b46c1a1b595aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be> Date: Sat, 20 Jun 2009 11:00:52 +0200 Subject: Added beginnings of a vCard parser. Only implemented PHOTO subtag, because it's the only thing we need right now. diff --git a/Swiften/Elements/VCard.h b/Swiften/Elements/VCard.h new file mode 100644 index 0000000..53be318 --- /dev/null +++ b/Swiften/Elements/VCard.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Swiften/Base/String.h" +#include "Swiften/Base/ByteArray.h" +#include "Swiften/Elements/Payload.h" + +namespace Swift { + class VCard : public Payload { + public: + VCard() {} + + void setPhoto(const ByteArray& photo) { photo_ = photo; } + const ByteArray& getPhoto() { return photo_; } + + void setPhotoType(const String& photoType) { photoType_ = photoType; } + const String& getPhotoType() { return photoType_; } + + private: + ByteArray photo_; + String photoType_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index de66278..32a8c25 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -15,6 +15,7 @@ #include "Swiften/Parser/PayloadParsers/SecurityLabelParserFactory.h" #include "Swiften/Parser/PayloadParsers/SecurityLabelsCatalogParserFactory.h" #include "Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h" +#include "Swiften/Parser/PayloadParsers/VCardParserFactory.h" using namespace boost; @@ -34,6 +35,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(shared_ptr<PayloadParserFactory>(new SecurityLabelParserFactory())); factories_.push_back(shared_ptr<PayloadParserFactory>(new SecurityLabelsCatalogParserFactory())); factories_.push_back(shared_ptr<PayloadParserFactory>(new VCardUpdateParserFactory())); + factories_.push_back(shared_ptr<PayloadParserFactory>(new VCardParserFactory())); foreach(shared_ptr<PayloadParserFactory> factory, factories_) { addFactory(factory.get()); } diff --git a/Swiften/Parser/PayloadParsers/Makefile.inc b/Swiften/Parser/PayloadParsers/Makefile.inc index e7ea276..b6e9954 100644 --- a/Swiften/Parser/PayloadParsers/Makefile.inc +++ b/Swiften/Parser/PayloadParsers/Makefile.inc @@ -11,6 +11,7 @@ SWIFTEN_SOURCES += \ Swiften/Parser/PayloadParsers/RosterParser.cpp \ Swiften/Parser/PayloadParsers/ResourceBindParser.cpp \ Swiften/Parser/PayloadParsers/VCardUpdateParser.cpp \ + Swiften/Parser/PayloadParsers/VCardParser.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 6d492c1..26f7b3c 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc +++ b/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc @@ -10,4 +10,5 @@ UNITTEST_SOURCES += \ Swiften/Parser/PayloadParsers/UnitTest/ResourceBindParserTest.cpp \ Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelParserTest.cpp \ Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelsCatalogParserTest.cpp \ - Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp + Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp \ + Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp diff --git a/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp new file mode 100644 index 0000000..2d16636 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp @@ -0,0 +1,39 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/Parser/PayloadParsers/VCardParser.h" +#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h" + +using namespace Swift; + +class VCardParserTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(VCardParserTest); + CPPUNIT_TEST(testParse_Photo); + CPPUNIT_TEST_SUITE_END(); + + public: + VCardParserTest() {} + + void testParse_Photo() { + VCardParser testling; + PayloadParserTester parser(&testling); + + CPPUNIT_ASSERT(parser.parse( + "<vCard xmlns='vcard-temp'>" + "<PHOTO>" + "<TYPE>image/jpeg</TYPE>" + "<BINVAL>" + "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ej" + "EyMzQ1Njc4OTA=" + "</BINVAL>" + "</PHOTO>" + "</vCard>")); + + VCard* payload = dynamic_cast<VCard*>(testling.getPayload().get()); + CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), payload->getPhotoType()); + CPPUNIT_ASSERT_EQUAL(ByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"), payload->getPhoto()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(VCardParserTest); diff --git a/Swiften/Parser/PayloadParsers/VCardParser.cpp b/Swiften/Parser/PayloadParsers/VCardParser.cpp new file mode 100644 index 0000000..a6fe2be --- /dev/null +++ b/Swiften/Parser/PayloadParsers/VCardParser.cpp @@ -0,0 +1,38 @@ +#include "Swiften/Parser/PayloadParsers/VCardParser.h" +#include "Swiften/Base/foreach.h" +#include "Swiften/StringCodecs/Base64.h" + +namespace Swift { + +VCardParser::VCardParser() { +} + +void VCardParser::handleStartElement(const String& element, const String&, const AttributeMap&) { + elementStack_.push_back(element); + currentText_ = ""; +} + +void VCardParser::handleEndElement(const String&, const String&) { + String elementHierarchy = getElementHierarchy(); + if (elementHierarchy == "/vCard/PHOTO/TYPE") { + getPayloadInternal()->setPhotoType(currentText_); + } + else if (elementHierarchy == "/vCard/PHOTO/BINVAL") { + getPayloadInternal()->setPhoto(Base64::decode(currentText_)); + } + elementStack_.pop_back(); +} + +void VCardParser::handleCharacterData(const String& text) { + currentText_ += text; +} + +String VCardParser::getElementHierarchy() const { + String result; + foreach(const String& element, elementStack_) { + result += "/" + element; + } + return result; +} + +} diff --git a/Swiften/Parser/PayloadParsers/VCardParser.h b/Swiften/Parser/PayloadParsers/VCardParser.h new file mode 100644 index 0000000..c1ed46b --- /dev/null +++ b/Swiften/Parser/PayloadParsers/VCardParser.h @@ -0,0 +1,24 @@ +#pragma once + +#include "Swiften/Elements/VCard.h" +#include "Swiften/Parser/GenericPayloadParser.h" + +namespace Swift { + class SerializingParser; + + class VCardParser : public GenericPayloadParser<VCard> { + public: + VCardParser(); + + 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: + String getElementHierarchy() const; + + private: + std::vector<String> elementStack_; + String currentText_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/VCardParserFactory.h b/Swiften/Parser/PayloadParsers/VCardParserFactory.h new file mode 100644 index 0000000..2d5302b --- /dev/null +++ b/Swiften/Parser/PayloadParsers/VCardParserFactory.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Swiften/Parser/GenericPayloadParserFactory.h" +#include "Swiften/Parser/PayloadParsers/VCardParser.h" + +namespace Swift { + class VCardParserFactory : public GenericPayloadParserFactory<VCardParser> { + public: + VCardParserFactory() : GenericPayloadParserFactory<VCardParser>("vCard", "vcard-temp") {} + }; +} -- cgit v0.10.2-6-g49f6