summaryrefslogtreecommitdiffstats
blob: 2c8895509981e076f1bf6317430affb57d11b74b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <cppunit/extensions/HelperMacros.h>

#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
#include "Swiften/Parser/XMLParser.h"
#include "Swiften/Parser/XMLParserClient.h"
#include "Swiften/Parser/PlatformXMLParserFactory.h"
#include "Swiften/Elements/Payload.h"
#include "Swiften/Parser/PayloadParser.h"

namespace Swift {
	class PayloadsParserTester : public XMLParserClient {
		public:
			PayloadsParserTester() : level(0) {
				xmlParser = PlatformXMLParserFactory().createXMLParser(this);
			}

			~PayloadsParserTester() {
				delete xmlParser;
			}

			bool parse(const std::string& data) {
				return xmlParser->parse(data);
			}

			virtual void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
				if (level == 0) {
					CPPUNIT_ASSERT(!payloadParser.get());
					PayloadParserFactory* payloadParserFactory = factories.getPayloadParserFactory(element, ns, attributes);
					CPPUNIT_ASSERT(payloadParserFactory);
					payloadParser.reset(payloadParserFactory->createPayloadParser());
				}
				payloadParser->handleStartElement(element, ns, attributes);
				level++;
			}

			virtual void handleEndElement(const std::string& element, const std::string& ns) {
				level--;
				payloadParser->handleEndElement(element, ns);
			}

			virtual void handleCharacterData(const std::string& data) {
				payloadParser->handleCharacterData(data);
			}

			boost::shared_ptr<Payload> getPayload() const {
				return payloadParser->getPayload();
			}

			template<typename T>
			boost::shared_ptr<T> getPayload() const {
				return boost::dynamic_pointer_cast<T>(payloadParser->getPayload());
			}

		private:
			XMLParser* xmlParser;
			FullPayloadParserFactoryCollection factories;
			std::auto_ptr<PayloadParser> payloadParser;
			int level;
	};
}