Added XHTMLIMPayload + parser and serializer

This commit is contained in:
HanzZ 2011-06-22 22:15:45 +02:00
parent 67c1817031
commit 64f1921f8d
7 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,14 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Elements/XHTMLIMPayload.h>
namespace Swift {
XHTMLIMPayload::XHTMLIMPayload(const std::string &body) : body_(body) {
}
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <vector>
#include <string>
#include <Swiften/Elements/Payload.h>
namespace Swift {
class XHTMLIMPayload : public Payload {
public:
XHTMLIMPayload(const std::string &body = "");
const std::string& getBody() const { return body_; }
void setBody(const std::string& body) {
body_ = body;
}
private:
std::string body_;
};
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/XHTMLIMParser.h>
#include <Swiften/Parser/SerializingParser.h>
namespace Swift {
XHTMLIMParser::XHTMLIMParser() : level_(TopLevel), bodyParser_(0) {
}
void XHTMLIMParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
++level_;
if (level_ == BodyLevel) {
if (element == "body") {
assert(!bodyParser_);
bodyParser_ = new SerializingParser();
}
}
else if (level_ >= InsideBodyLevel && bodyParser_) {
bodyParser_->handleStartElement(element, ns, attributes);
}
}
void XHTMLIMParser::handleEndElement(const std::string& element, const std::string& ns) {
if (level_ == BodyLevel) {
if (bodyParser_) {
if (element == "body") {
getPayloadInternal()->setBody(bodyParser_->getResult());
}
delete bodyParser_;
bodyParser_ = 0;
}
}
else if (bodyParser_ && level_ >= InsideBodyLevel) {
bodyParser_->handleEndElement(element, ns);
}
--level_;
}
void XHTMLIMParser::handleCharacterData(const std::string& data) {
if (bodyParser_) {
bodyParser_->handleCharacterData(data);
}
else {
currentText_ += data;
}
}
boost::shared_ptr<XHTMLIMPayload> XHTMLIMParser::getLabelPayload() const {
return getPayloadInternal();
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <Swiften/Elements/XHTMLIMPayload.h>
#include <Swiften/Parser/GenericPayloadParser.h>
namespace Swift {
class SerializingParser;
class XHTMLIMParser : public GenericPayloadParser<XHTMLIMPayload> {
public:
XHTMLIMParser();
virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes);
virtual void handleEndElement(const std::string& element, const std::string&);
virtual void handleCharacterData(const std::string& data);
boost::shared_ptr<XHTMLIMPayload> getLabelPayload() const;
private:
enum Level {
TopLevel = 0,
PayloadLevel = 1,
BodyLevel = 2,
InsideBodyLevel = 3
};
int level_;
SerializingParser* bodyParser_;
std::string currentText_;
};
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>
#include <Swiften/Serializer/XML/XMLElement.h>
namespace Swift {
XHTMLIMSerializer::XHTMLIMSerializer() : GenericPayloadSerializer<XHTMLIMPayload>() {
}
std::string XHTMLIMSerializer::serializePayload(boost::shared_ptr<XHTMLIMPayload> payload) const {
XMLElement html("html", "http://jabber.org/protocol/xhtml-im");
boost::shared_ptr<XMLElement> body(new XMLElement("body", "http://www.w3.org/1999/xhtml"));
body->addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(payload->getBody())));
html.addNode(body);
return html.serialize();
}
}

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <Swiften/Serializer/GenericPayloadSerializer.h>
#include <Swiften/Elements/XHTMLIMPayload.h>
namespace Swift {
class XHTMLIMSerializer : public GenericPayloadSerializer<XHTMLIMPayload> {
public:
XHTMLIMSerializer();
virtual std::string serializePayload(boost::shared_ptr<XHTMLIMPayload> xhtml) const;
};
}

View file

@ -30,6 +30,8 @@
#include "Swiften/TLS/OpenSSL/OpenSSLServerContextFactory.h"
#include "Swiften/Parser/PayloadParsers/AttentionParser.h"
#include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h"
#include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h"
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
#include "log4cxx/logger.h"
#include "log4cxx/consoleappender.h"
#include "log4cxx/patternlayout.h"
@ -104,8 +106,10 @@ Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory) {
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
m_server->addPayloadSerializer(new Swift::AttentionSerializer());
m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer());
m_server->onDataRead.connect(bind(&Component::handleDataRead, this, _1));
m_server->onDataWritten.connect(bind(&Component::handleDataWritten, this, _1));