PubSub Parsers

This commit is contained in:
HanzZ 2012-02-22 19:23:43 +01:00
parent ff27ec0233
commit 7c90087b17
19 changed files with 349 additions and 18 deletions

View file

@ -8,7 +8,7 @@
namespace Swift {
PubSubItem::PubSubItem(const std::string &body) : body_(body) {
PubSubItem::PubSubItem() {
}
}

View file

@ -14,12 +14,41 @@
namespace Swift {
class PubSubItem : public Payload {
public:
PubSubItem(const std::string &body = "");
PubSubItem();
const std::string& getData() const { return body_; }
void addPayload(boost::shared_ptr<Payload> payload) {
payloads.push_back(payload);
}
void setData(const std::string& body) {
body_ = body;
const std::vector<boost::shared_ptr<Payload> > getPayloads() const {
return payloads;
}
template<typename T>
const std::vector<boost::shared_ptr<T> > getPayloads() const {
std::vector<boost::shared_ptr<T> > matched_payloads;
for (std::vector<boost::shared_ptr<Payload> >::const_iterator i = payloads.begin(); i != payloads.end(); ++i) {
boost::shared_ptr<T> result = boost::dynamic_pointer_cast<T>(*i);
if (result) {
matched_payloads.push_back(result);
}
}
return matched_payloads;
}
template<typename T>
const boost::shared_ptr<T> getPayload() const {
boost::shared_ptr<T> result;
for (std::vector<boost::shared_ptr<Payload> >::const_iterator i = payloads.begin(); i != payloads.end(); ++i) {
result = boost::dynamic_pointer_cast<T>(*i);
if (result) {
return result;
}
}
return result;
}
const std::string& getId() const { return id; }
@ -29,7 +58,7 @@ namespace Swift {
}
private:
std::string body_;
std::vector<boost::shared_ptr<Payload> > payloads;
std::string id;
};
}

View file

@ -16,10 +16,14 @@ namespace Swift {
public:
PubSubPayload();
void addPayload(boost::shared_ptr<Payload> payload) {
payloads.push_back(payload);
}
const std::vector<boost::shared_ptr<Payload> > getPayloads() const {
return payloads;
}
template<typename T>
const std::vector<boost::shared_ptr<T> > getPayloads() const {
std::vector<boost::shared_ptr<T> > matched_payloads;

View file

@ -28,16 +28,16 @@ namespace Swift {
return node;
}
void addItem(const boost::shared_ptr<PubSubItem> &item) {
void addItem(const boost::shared_ptr<Payload> &item) {
items.push_back(item);
}
const std::vector<boost::shared_ptr<PubSubItem> > &getItems() const {
const std::vector<boost::shared_ptr<Payload> > &getItems() const {
return items;
}
private:
std::string node;
std::vector<boost::shared_ptr<PubSubItem> > items;
std::vector<boost::shared_ptr<Payload> > items;
};
}

View file

@ -15,7 +15,7 @@
namespace Swift {
class PubSubSubscribePayload : public Payload {
public:
PubSubSubscribePayload(const JID &jid, const std::string &node = "");
PubSubSubscribePayload(const JID &jid = JID(), const std::string &node = "");
void setJID(const JID &jid) {
this->jid = jid;

View file

@ -17,7 +17,7 @@ namespace Swift {
public:
enum Type { None, Pending, Subscribed, Unconfigured };
PubSubSubscriptionPayload(const JID &jid, const std::string &node = "");
PubSubSubscriptionPayload(const JID &jid = JID(), const std::string &node = "");
void setJID(const JID &jid) {
this->jid = jid;

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/PubSubItemParser.h>
#include <boost/lexical_cast.hpp>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/MUCOccupant.h>
#include <Swiften/Parser/Tree/TreeReparser.h>
namespace Swift {
void PubSubItemParser::handleTree(ParserElement::ref root) {
std::string id = root->getAttributes().getAttribute("id");
if (!id.empty()) {
getPayloadInternal()->setId(id);
}
foreach (ParserElement::ref child, root->getAllChildren()) {
getPayloadInternal()->addPayload(TreeReparser::parseTree(child, factories));
}
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <boost/optional.hpp>
#include <Swiften/Elements/PubSubItem.h>
#include <Swiften/Parser/GenericPayloadTreeParser.h>
#include <Swiften/Parser/PayloadParsers/MUCItemParser.h>
namespace Swift {
class PayloadParserFactoryCollection;
class PubSubItemParser : public GenericPayloadTreeParser<PubSubItem> {
public:
PubSubItemParser(PayloadParserFactoryCollection* collection) : factories(collection) {}
virtual void handleTree(ParserElement::ref root);
private:
PayloadParserFactoryCollection* factories;
};
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/PubSubPayloadParser.h>
#include <boost/lexical_cast.hpp>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/MUCOccupant.h>
#include <Swiften/Parser/Tree/TreeReparser.h>
namespace Swift {
void PubSubPayloadParser::handleTree(ParserElement::ref root) {
foreach (ParserElement::ref child, root->getAllChildren()) {
getPayloadInternal()->addPayload(TreeReparser::parseTree(child, factories));
}
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <boost/optional.hpp>
#include <Swiften/Elements/PubSubPayload.h>
#include <Swiften/Parser/GenericPayloadTreeParser.h>
#include <Swiften/Parser/PayloadParsers/MUCItemParser.h>
namespace Swift {
class PayloadParserFactoryCollection;
class PubSubPayloadParser : public GenericPayloadTreeParser<PubSubPayload> {
public:
PubSubPayloadParser(PayloadParserFactoryCollection* collection) : factories(collection) {}
virtual void handleTree(ParserElement::ref root);
private:
PayloadParserFactoryCollection* factories;
};
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/PubSubPublishPayloadParser.h>
#include <boost/lexical_cast.hpp>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/MUCOccupant.h>
#include <Swiften/Parser/Tree/TreeReparser.h>
namespace Swift {
void PubSubPublishPayloadParser::handleTree(ParserElement::ref root) {
std::string node = root->getAttributes().getAttribute("node");
if (!node.empty()) {
getPayloadInternal()->setNode(node);
}
foreach (ParserElement::ref child, root->getAllChildren()) {
getPayloadInternal()->addItem(TreeReparser::parseTree(child, factories));
}
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <boost/optional.hpp>
#include <Swiften/Elements/PubSubPublishPayload.h>
#include <Swiften/Parser/GenericPayloadTreeParser.h>
#include <Swiften/Parser/PayloadParsers/MUCItemParser.h>
namespace Swift {
class PayloadParserFactoryCollection;
class PubSubPublishPayloadParser : public GenericPayloadTreeParser<PubSubPublishPayload> {
public:
PubSubPublishPayloadParser(PayloadParserFactoryCollection* collection) : factories(collection) {}
virtual void handleTree(ParserElement::ref root);
private:
PayloadParserFactoryCollection* factories;
};
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/PubSubSubscribePayloadParser.h>
#include <boost/lexical_cast.hpp>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/MUCOccupant.h>
#include <Swiften/Parser/Tree/TreeReparser.h>
namespace Swift {
void PubSubSubscribePayloadParser::handleTree(ParserElement::ref root) {
std::string node = root->getAttributes().getAttribute("node");
if (!node.empty()) {
getPayloadInternal()->setNode(node);
}
std::string jid = root->getAttributes().getAttribute("jid");
if (!jid.empty()) {
getPayloadInternal()->setJID(jid);
}
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <boost/optional.hpp>
#include <Swiften/Elements/PubSubSubscribePayload.h>
#include <Swiften/Parser/GenericPayloadTreeParser.h>
#include <Swiften/Parser/PayloadParsers/MUCItemParser.h>
namespace Swift {
class PayloadParserFactoryCollection;
class PubSubSubscribePayloadParser : public GenericPayloadTreeParser<PubSubSubscribePayload> {
public:
PubSubSubscribePayloadParser(PayloadParserFactoryCollection* collection) : factories(collection) {}
virtual void handleTree(ParserElement::ref root);
private:
PayloadParserFactoryCollection* factories;
};
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/PubSubSubscriptionPayloadParser.h>
#include <boost/lexical_cast.hpp>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/MUCOccupant.h>
#include <Swiften/Parser/Tree/TreeReparser.h>
namespace Swift {
void PubSubSubscriptionPayloadParser::handleTree(ParserElement::ref root) {
std::string node = root->getAttributes().getAttribute("node");
if (!node.empty()) {
getPayloadInternal()->setNode(node);
}
std::string jid = root->getAttributes().getAttribute("jid");
if (!jid.empty()) {
getPayloadInternal()->setJID(jid);
}
std::string id = root->getAttributes().getAttribute("subid");
if (!id.empty()) {
getPayloadInternal()->setId(id);
}
std::string type = root->getAttributes().getAttribute("subscription");
if (type == "none") {
getPayloadInternal()->setType(PubSubSubscriptionPayload::None);
}
else if (type == "subscribed") {
getPayloadInternal()->setType(PubSubSubscriptionPayload::Subscribed);
}
else if (type == "pending") {
getPayloadInternal()->setType(PubSubSubscriptionPayload::Pending);
}
else if (type == "unconfigured") {
getPayloadInternal()->setType(PubSubSubscriptionPayload::Unconfigured);
}
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2012 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <boost/optional.hpp>
#include <Swiften/Elements/PubSubSubscriptionPayload.h>
#include <Swiften/Parser/GenericPayloadTreeParser.h>
#include <Swiften/Parser/PayloadParsers/MUCItemParser.h>
namespace Swift {
class PayloadParserFactoryCollection;
class PubSubSubscriptionPayloadParser : public GenericPayloadTreeParser<PubSubSubscriptionPayload> {
public:
PubSubSubscriptionPayloadParser(PayloadParserFactoryCollection* collection) : factories(collection) {}
virtual void handleTree(ParserElement::ref root);
private:
PayloadParserFactoryCollection* factories;
};
}

View file

@ -9,10 +9,12 @@
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/PayloadSerializerCollection.h>
namespace Swift {
PubSubItemSerializer::PubSubItemSerializer() : GenericPayloadSerializer<PubSubItem>() {
PubSubItemSerializer::PubSubItemSerializer(PayloadSerializerCollection *serializers) :
GenericPayloadSerializer<PubSubItem>(), serializers(serializers) {
}
std::string PubSubItemSerializer::serializePayload(boost::shared_ptr<PubSubItem> payload) const {
@ -21,9 +23,14 @@ std::string PubSubItemSerializer::serializePayload(boost::shared_ptr<PubSubItem>
item.setAttribute("id", payload->getId());
}
boost::shared_ptr<XMLElement> body(new XMLElement("body", "http://www.w3.org/1999/xhtml"));
body->addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(payload->getData())));
item.addNode(body);
if (!payload->getPayloads().empty()) {
foreach(boost::shared_ptr<Payload> subPayload, payload->getPayloads()) {
PayloadSerializer* serializer = serializers->getPayloadSerializer(subPayload);
if (serializer) {
item.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(serializer->serialize(subPayload))));
}
}
}
return item.serialize();
}

View file

@ -10,10 +10,13 @@
#include <Swiften/Elements/PubSubItem.h>
namespace Swift {
class PayloadSerializerCollection;
class PubSubItemSerializer : public GenericPayloadSerializer<PubSubItem> {
public:
PubSubItemSerializer();
PubSubItemSerializer(PayloadSerializerCollection *serializers);
virtual std::string serializePayload(boost::shared_ptr<PubSubItem> item) const;
private:
PayloadSerializerCollection *serializers;
};
}

View file

@ -26,7 +26,7 @@ std::string PubSubPublishPayloadSerializer::serializePayload(boost::shared_ptr<P
}
if (!payload->getItems().empty()) {
foreach(boost::shared_ptr<PubSubItem> subPayload, payload->getItems()) {
foreach(boost::shared_ptr<Payload> subPayload, payload->getItems()) {
PayloadSerializer* serializer = serializers->getPayloadSerializer(subPayload);
if (serializer) {
publish.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(serializer->serialize(subPayload))));