diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-07-11 15:51:42 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-07-11 15:56:10 (GMT) |
commit | 1d6d6588e3affc9a736b29062107ca82ad6a5644 (patch) | |
tree | 54a96dd3f652df321ed5017148811d241baa1dd8 /Swiften/Parser | |
parent | e1a2d258479cd915e8c30c5c996b58da3da3e55d (diff) | |
download | swift-1d6d6588e3affc9a736b29062107ca82ad6a5644.zip swift-1d6d6588e3affc9a736b29062107ca82ad6a5644.tar.bz2 |
Added Raw XML payload parser & serializer.
Diffstat (limited to 'Swiften/Parser')
8 files changed, 112 insertions, 5 deletions
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<PayloadParserFactory> factory, factories_) { addFactory(factory.get()); } + defaultFactory_ = new RawXMLPayloadParserFactory(); + setDefaultFactory(defaultFactory_); } FullPayloadParserFactoryCollection::~FullPayloadParserFactoryCollection() { + setDefaultFactory(NULL); + delete defaultFactory_; foreach(shared_ptr<PayloadParserFactory> 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 <boost/shared_ptr.hpp> #include <vector> @@ -15,7 +14,6 @@ namespace Swift { private: std::vector< boost::shared_ptr<PayloadParserFactory> > 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<RawXMLPayload> { + 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 <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#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 = + "<foo foo-attr=\"foo-val\" xmlns=\"foo-ns\">" + "<bar bar-attr=\"bar-val\" xmlns=\"bar-ns\"/>" + "<baz baz-attr=\"baz-val\" xmlns=\"baz-ns\"/>" + "</foo>"; + CPPUNIT_ASSERT(parser.parse(xml)); + + RawXMLPayload* payload = dynamic_cast<RawXMLPayload*>(testling.getPayload().get()); + CPPUNIT_ASSERT_EQUAL(xml, payload->getRawXML()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(RawXMLPayloadParserTest); |