From 1d6d6588e3affc9a736b29062107ca82ad6a5644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sat, 11 Jul 2009 17:51:42 +0200 Subject: Added Raw XML payload parser & serializer. diff --git a/Swiften/Elements/RawXMLPayload.h b/Swiften/Elements/RawXMLPayload.h new file mode 100644 index 0000000..c2ee439 --- /dev/null +++ b/Swiften/Elements/RawXMLPayload.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Swiften/Base/String.h" +#include "Swiften/Elements/Payload.h" + +namespace Swift { + class RawXMLPayload : public Payload { + public: + RawXMLPayload() {} + + void setRawXML(const String& data) { + rawXML_ = data; + } + + const String& getRawXML() const { + return rawXML_; + } + + private: + String rawXML_; + }; +} diff --git a/Swiften/Elements/UnknownElement.h b/Swiften/Elements/UnknownElement.h index a2ae406..3d2c219 100644 --- a/Swiften/Elements/UnknownElement.h +++ b/Swiften/Elements/UnknownElement.h @@ -4,10 +4,9 @@ #include "Swiften/Elements/Element.h" namespace Swift { - class UnknownElement : public Element - { - public: - UnknownElement() {} + class UnknownElement : public Element { + public: + UnknownElement() {} }; } diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index 32a8c25..8a7f468 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -16,6 +16,7 @@ #include "Swiften/Parser/PayloadParsers/SecurityLabelsCatalogParserFactory.h" #include "Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h" #include "Swiften/Parser/PayloadParsers/VCardParserFactory.h" +#include "Swiften/Parser/PayloadParsers/RawXMLPayloadParserFactory.h" using namespace boost; @@ -39,9 +40,13 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { foreach(shared_ptr factory, factories_) { addFactory(factory.get()); } + defaultFactory_ = new RawXMLPayloadParserFactory(); + setDefaultFactory(defaultFactory_); } FullPayloadParserFactoryCollection::~FullPayloadParserFactoryCollection() { + setDefaultFactory(NULL); + delete defaultFactory_; foreach(shared_ptr factory, factories_) { removeFactory(factory.get()); } diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h index 3c383ec..82e5a56 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h @@ -1,5 +1,4 @@ -#ifndef SWIFTEN_FULLPAYLOADPARSERFACTORYCOLLECTION_H -#define SWIFTEN_FULLPAYLOADPARSERFACTORYCOLLECTION_H +#pragma once #include #include @@ -15,7 +14,6 @@ namespace Swift { private: std::vector< boost::shared_ptr > factories_; + PayloadParserFactory* defaultFactory_; }; } - -#endif diff --git a/Swiften/Parser/PayloadParsers/Makefile.inc b/Swiften/Parser/PayloadParsers/Makefile.inc index b6e9954..55017e9 100644 --- a/Swiften/Parser/PayloadParsers/Makefile.inc +++ b/Swiften/Parser/PayloadParsers/Makefile.inc @@ -12,6 +12,7 @@ SWIFTEN_SOURCES += \ Swiften/Parser/PayloadParsers/ResourceBindParser.cpp \ Swiften/Parser/PayloadParsers/VCardUpdateParser.cpp \ Swiften/Parser/PayloadParsers/VCardParser.cpp \ + Swiften/Parser/PayloadParsers/RawXMLPayloadParser.cpp \ Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp include Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc diff --git a/Swiften/Parser/PayloadParsers/RawXMLPayloadParser.cpp b/Swiften/Parser/PayloadParsers/RawXMLPayloadParser.cpp new file mode 100644 index 0000000..c49af3e --- /dev/null +++ b/Swiften/Parser/PayloadParsers/RawXMLPayloadParser.cpp @@ -0,0 +1,26 @@ +#include "Swiften/Parser/PayloadParsers/RawXMLPayloadParser.h" +#include "Swiften/Parser/SerializingParser.h" + +namespace Swift { + +RawXMLPayloadParser::RawXMLPayloadParser() : level_(0) { +} + +void RawXMLPayloadParser::handleStartElement(const String& element, const String& ns, const AttributeMap& attributes) { + ++level_; + serializingParser_.handleStartElement(element, ns, attributes); +} + +void RawXMLPayloadParser::handleEndElement(const String& element, const String& ns) { + serializingParser_.handleEndElement(element, ns); + --level_; + if (level_ == 0) { + getPayloadInternal()->setRawXML(serializingParser_.getResult()); + } +} + +void RawXMLPayloadParser::handleCharacterData(const String& data) { + serializingParser_.handleCharacterData(data); +} + +} diff --git a/Swiften/Parser/PayloadParsers/RawXMLPayloadParser.h b/Swiften/Parser/PayloadParsers/RawXMLPayloadParser.h new file mode 100644 index 0000000..f636486 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/RawXMLPayloadParser.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Swiften/Elements/RawXMLPayload.h" +#include "Swiften/Parser/GenericPayloadParser.h" +#include "Swiften/Parser/SerializingParser.h" + +namespace Swift { + class SerializingParser; + + class RawXMLPayloadParser : public GenericPayloadParser { + public: + RawXMLPayloadParser(); + + 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: + int level_; + SerializingParser serializingParser_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/RawXMLPayloadParserFactory.h b/Swiften/Parser/PayloadParsers/RawXMLPayloadParserFactory.h new file mode 100644 index 0000000..46b1183 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/RawXMLPayloadParserFactory.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Swiften/Parser/PayloadParserFactory.h" +#include "Swiften/Parser/PayloadParsers/RawXMLPayloadParser.h" +#include "Swiften/Base/String.h" + +namespace Swift { + class RawXMLPayloadParserFactory : public PayloadParserFactory { + public: + RawXMLPayloadParserFactory() {} + + virtual bool canParse(const String&, const String&, const AttributeMap&) const { + return true; + } + + virtual PayloadParser* createPayloadParser() { + return new RawXMLPayloadParser(); + } + }; +} diff --git a/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc b/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc index 26f7b3c..d7d419e 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc +++ b/Swiften/Parser/PayloadParsers/UnitTest/Makefile.inc @@ -11,4 +11,5 @@ UNITTEST_SOURCES += \ Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelParserTest.cpp \ Swiften/Parser/PayloadParsers/UnitTest/SecurityLabelsCatalogParserTest.cpp \ Swiften/Parser/PayloadParsers/UnitTest/VCardUpdateParserTest.cpp \ - Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp + Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp \ + Swiften/Parser/PayloadParsers/UnitTest/RawXMLPayloadParserTest.cpp diff --git a/Swiften/Parser/PayloadParsers/UnitTest/RawXMLPayloadParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/RawXMLPayloadParserTest.cpp new file mode 100644 index 0000000..5a2be8d --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/RawXMLPayloadParserTest.cpp @@ -0,0 +1,34 @@ +#include +#include + +#include "Swiften/Parser/PayloadParsers/RawXMLPayloadParser.h" +#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h" + +using namespace Swift; + +class RawXMLPayloadParserTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(RawXMLPayloadParserTest); + CPPUNIT_TEST(testParse); + CPPUNIT_TEST_SUITE_END(); + + public: + RawXMLPayloadParserTest() {} + + void testParse() { + RawXMLPayloadParser testling; + PayloadParserTester parser(&testling); + + String xml = + "" + "" + "" + ""; + CPPUNIT_ASSERT(parser.parse(xml)); + + RawXMLPayload* payload = dynamic_cast(testling.getPayload().get()); + CPPUNIT_ASSERT_EQUAL(xml, payload->getRawXML()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(RawXMLPayloadParserTest); diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index 0f66b35..6a24008 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -17,6 +17,7 @@ #include "Swiften/Serializer/PayloadSerializers/SecurityLabelsCatalogSerializer.h" #include "Swiften/Serializer/PayloadSerializers/VCardSerializer.h" #include "Swiften/Serializer/PayloadSerializers/VCardUpdateSerializer.h" +#include "Swiften/Serializer/PayloadSerializers/RawXMLPayloadSerializer.h" namespace Swift { @@ -37,6 +38,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new SecurityLabelsCatalogSerializer()); serializers_.push_back(new VCardSerializer()); serializers_.push_back(new VCardUpdateSerializer()); + serializers_.push_back(new RawXMLPayloadSerializer()); foreach(PayloadSerializer* serializer, serializers_) { addSerializer(serializer); } diff --git a/Swiften/Serializer/PayloadSerializers/RawXMLPayloadSerializer.h b/Swiften/Serializer/PayloadSerializers/RawXMLPayloadSerializer.h new file mode 100644 index 0000000..f980174 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/RawXMLPayloadSerializer.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Swiften/Serializer/GenericPayloadSerializer.h" +#include "Swiften/Elements/RawXMLPayload.h" + +namespace Swift { + class RawXMLPayloadSerializer : public GenericPayloadSerializer { + public: + RawXMLPayloadSerializer() : GenericPayloadSerializer() {} + + virtual String serializePayload(boost::shared_ptr p) const { + return p->getRawXML(); + } + }; +} -- cgit v0.10.2-6-g49f6