summaryrefslogtreecommitdiffstats
blob: b5f0b220c24c73528747a6cbb43e8f14935d6526 (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
/*
 * Copyright (c) 2010-2014 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Serializer/StanzaSerializer.h>

#include <sstream>
#include <typeinfo>
#include <iostream>

#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/PayloadSerializer.h>
#include <Swiften/Serializer/PayloadSerializerCollection.h>
#include <Swiften/Elements/Stanza.h>

namespace Swift {

StanzaSerializer::StanzaSerializer(const std::string& tag, PayloadSerializerCollection* payloadSerializers) : tag_(tag), payloadSerializers_(payloadSerializers) {
}

SafeByteArray StanzaSerializer::serialize(boost::shared_ptr<Element> element) const {
	return serialize(element, "");
}

SafeByteArray StanzaSerializer::serialize(boost::shared_ptr<Element> element, const std::string& xmlns) const {
	boost::shared_ptr<Stanza> stanza(boost::dynamic_pointer_cast<Stanza>(element));

	XMLElement stanzaElement(tag_, xmlns);
	if (stanza->getFrom().isValid()) {
		stanzaElement.setAttribute("from", stanza->getFrom());
	}
	if (stanza->getTo().isValid()) {
		stanzaElement.setAttribute("to", stanza->getTo());
	}
	if (!stanza->getID().empty()) {
		stanzaElement.setAttribute("id", stanza->getID());
	}
	setStanzaSpecificAttributes(stanza, stanzaElement);

	std::string serializedPayloads;
	foreach (const boost::shared_ptr<Payload>& payload, stanza->getPayloads()) {
		PayloadSerializer* serializer = payloadSerializers_->getPayloadSerializer(payload);
		if (serializer) {
			serializedPayloads += serializer->serialize(payload);
		}
		else {
			std::cerr << "Could not find serializer for " << typeid(*(payload.get())).name() << std::endl;
		}
	}
	if (!serializedPayloads.empty()) {
		stanzaElement.addNode(boost::shared_ptr<XMLNode>(new XMLRawTextNode(serializedPayloads)));
	}

	return createSafeByteArray(stanzaElement.serialize());
}

}