summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Parser')
-rw-r--r--Swiften/Parser/BOSHBodyExtractor.cpp2
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h6
-rw-r--r--Swiften/Parser/PlatformXMLParserFactory.cpp6
-rw-r--r--Swiften/Parser/PlatformXMLParserFactory.h2
-rw-r--r--Swiften/Parser/UnitTest/ParserTester.h9
-rw-r--r--Swiften/Parser/XMLParserFactory.h4
-rw-r--r--Swiften/Parser/XMPPParser.cpp1
-rw-r--r--Swiften/Parser/XMPPParser.h2
8 files changed, 12 insertions, 20 deletions
diff --git a/Swiften/Parser/BOSHBodyExtractor.cpp b/Swiften/Parser/BOSHBodyExtractor.cpp
index c45d338..803f16a 100644
--- a/Swiften/Parser/BOSHBodyExtractor.cpp
+++ b/Swiften/Parser/BOSHBodyExtractor.cpp
@@ -99,42 +99,42 @@ BOSHBodyExtractor::BOSHBodyExtractor(XMLParserFactory* parserFactory, const Byte
ByteArray::const_reverse_iterator j = data.rbegin();
if (!endElementSeen) {
while (isWhitespace(*j) && j < data.rend()) {
++j;
}
if (j == data.rend() || *j != '>') {
return;
}
++j;
while (j < data.rend() && isWhitespace(*j)) {
++j;
}
if (std::distance(j, data.rend()) < 6 || *(j+5) != '<' || *(j+4) != '/' || *(j+3) != 'b' || *(j+2) != 'o' || *(j+1) != 'd' || *j != 'y') {
return;
}
j += 6;
}
body = BOSHBody();
if (!endElementSeen) {
body->content = std::string(
reinterpret_cast<const char*>(vecptr(data) + std::distance(data.begin(), i)),
boost::numeric_cast<size_t>(std::distance(i, j.base())));
}
// Parse the body element
BOSHBodyParserClient parserClient(this);
- std::shared_ptr<XMLParser> parser(parserFactory->createXMLParser(&parserClient));
+ std::shared_ptr<XMLParser> parser(std::move(parserFactory->createXMLParser(&parserClient)));
if (!parser->parse(std::string(
reinterpret_cast<const char*>(vecptr(data)),
boost::numeric_cast<size_t>(std::distance(data.begin(), i))))) {
/* TODO: This needs to be only validating the BOSH <body> element, so that XMPP parsing errors are caught at
the correct higher layer */
body = boost::optional<BOSHBody>();
return;
}
}
}
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h b/Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h
index 2c1ff8e..dcdbffa 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h
+++ b/Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h
@@ -1,68 +1,64 @@
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
#include <cassert>
#include <Swiften/Elements/Payload.h>
#include <Swiften/Parser/PayloadParser.h>
#include <Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h>
#include <Swiften/Parser/PlatformXMLParserFactory.h>
#include <Swiften/Parser/XMLParser.h>
#include <Swiften/Parser/XMLParserClient.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) {
assert(!payloadParser.get());
PayloadParserFactory* payloadParserFactory = factories.getPayloadParserFactory(element, ns, attributes);
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);
}
std::shared_ptr<Payload> getPayload() const {
return payloadParser->getPayload();
}
template<typename T>
std::shared_ptr<T> getPayload() const {
return std::dynamic_pointer_cast<T>(payloadParser->getPayload());
}
private:
- XMLParser* xmlParser;
+ std::unique_ptr<XMLParser> xmlParser;
FullPayloadParserFactoryCollection factories;
std::shared_ptr<PayloadParser> payloadParser;
int level;
};
}
diff --git a/Swiften/Parser/PlatformXMLParserFactory.cpp b/Swiften/Parser/PlatformXMLParserFactory.cpp
index 87f70d1..97e1c9e 100644
--- a/Swiften/Parser/PlatformXMLParserFactory.cpp
+++ b/Swiften/Parser/PlatformXMLParserFactory.cpp
@@ -1,31 +1,31 @@
/*
* Copyright (c) 2010 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Parser/PlatformXMLParserFactory.h>
#include <cassert>
#ifdef HAVE_LIBXML
#include <Swiften/Parser/LibXMLParser.h>
#else
#include <Swiften/Parser/ExpatParser.h>
#endif
namespace Swift {
PlatformXMLParserFactory::PlatformXMLParserFactory() {
}
-XMLParser* PlatformXMLParserFactory::createXMLParser(XMLParserClient* client) {
+std::unique_ptr<XMLParser> PlatformXMLParserFactory::createXMLParser(XMLParserClient* client) {
#ifdef HAVE_LIBXML
- return new LibXMLParser(client);
+ return std::unique_ptr<XMLParser>(new LibXMLParser(client));
#else
- return new ExpatParser(client);
+ return std::unique_ptr<XMLParser>(new ExpatParser(client));
#endif
}
}
diff --git a/Swiften/Parser/PlatformXMLParserFactory.h b/Swiften/Parser/PlatformXMLParserFactory.h
index 82b8573..fa3ca19 100644
--- a/Swiften/Parser/PlatformXMLParserFactory.h
+++ b/Swiften/Parser/PlatformXMLParserFactory.h
@@ -1,19 +1,19 @@
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
#include <Swiften/Base/API.h>
#include <Swiften/Parser/XMLParserFactory.h>
namespace Swift {
class SWIFTEN_API PlatformXMLParserFactory : public XMLParserFactory {
public:
PlatformXMLParserFactory();
- virtual XMLParser* createXMLParser(XMLParserClient*);
+ virtual std::unique_ptr<XMLParser> createXMLParser(XMLParserClient*);
};
}
diff --git a/Swiften/Parser/UnitTest/ParserTester.h b/Swiften/Parser/UnitTest/ParserTester.h
index a98eb51..aa01d40 100644
--- a/Swiften/Parser/UnitTest/ParserTester.h
+++ b/Swiften/Parser/UnitTest/ParserTester.h
@@ -1,47 +1,42 @@
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
#include <Swiften/Parser/PlatformXMLParserFactory.h>
#include <Swiften/Parser/XMLParser.h>
#include <Swiften/Parser/XMLParserClient.h>
namespace Swift {
class XMLParser;
template<typename ParserType>
class ParserTester : public XMLParserClient {
public:
- ParserTester(ParserType* parser) : parser_(parser) {
- xmlParser_ = PlatformXMLParserFactory().createXMLParser(this);
- }
-
- ~ParserTester() {
- delete xmlParser_;
+ ParserTester(ParserType* parser) : xmlParser_(PlatformXMLParserFactory().createXMLParser(this)), parser_(parser) {
}
bool parse(const std::string& data) {
return xmlParser_->parse(data);
}
virtual void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
parser_->handleStartElement(element, ns, attributes);
}
virtual void handleEndElement(const std::string& element, const std::string& ns) {
parser_->handleEndElement(element, ns);
}
virtual void handleCharacterData(const std::string& data) {
parser_->handleCharacterData(data);
}
private:
- XMLParser* xmlParser_;
+ std::unique_ptr<XMLParser> xmlParser_;
ParserType* parser_;
};
}
diff --git a/Swiften/Parser/XMLParserFactory.h b/Swiften/Parser/XMLParserFactory.h
index 091f45b..595512b 100644
--- a/Swiften/Parser/XMLParserFactory.h
+++ b/Swiften/Parser/XMLParserFactory.h
@@ -1,21 +1,23 @@
/*
* Copyright (c) 2010 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
+#include <memory>
+
#include <Swiften/Base/API.h>
namespace Swift {
class XMLParser;
class XMLParserClient;
class SWIFTEN_API XMLParserFactory {
public:
virtual ~XMLParserFactory();
- virtual XMLParser* createXMLParser(XMLParserClient*) = 0;
+ virtual std::unique_ptr<XMLParser> createXMLParser(XMLParserClient*) = 0;
};
}
diff --git a/Swiften/Parser/XMPPParser.cpp b/Swiften/Parser/XMPPParser.cpp
index 2b45a12..d2a5a98 100644
--- a/Swiften/Parser/XMPPParser.cpp
+++ b/Swiften/Parser/XMPPParser.cpp
@@ -33,61 +33,60 @@
#include <Swiften/Parser/StreamManagementEnabledParser.h>
#include <Swiften/Parser/StreamManagementFailedParser.h>
#include <Swiften/Parser/StreamResumeParser.h>
#include <Swiften/Parser/StreamResumedParser.h>
#include <Swiften/Parser/TLSProceedParser.h>
#include <Swiften/Parser/UnknownElementParser.h>
#include <Swiften/Parser/XMLParser.h>
#include <Swiften/Parser/XMLParserFactory.h>
#include <Swiften/Parser/XMPPParserClient.h>
// TODO: Whenever an error occurs in the handlers, stop the parser by returing
// a bool value, and stopping the XML parser
namespace Swift {
XMPPParser::XMPPParser(
XMPPParserClient* client,
PayloadParserFactoryCollection* payloadParserFactories,
XMLParserFactory* xmlParserFactory) :
xmlParser_(nullptr),
client_(client),
payloadParserFactories_(payloadParserFactories),
level_(0),
currentElementParser_(nullptr),
parseErrorOccurred_(false) {
xmlParser_ = xmlParserFactory->createXMLParser(this);
}
XMPPParser::~XMPPParser() {
delete currentElementParser_;
- delete xmlParser_;
}
bool XMPPParser::parse(const std::string& data) {
bool xmlParseResult = xmlParser_->parse(data);
return xmlParseResult && !parseErrorOccurred_;
}
void XMPPParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
if (!parseErrorOccurred_) {
if (level_ == TopLevel) {
if (element == "stream" && ns == "http://etherx.jabber.org/streams") {
ProtocolHeader header;
header.setFrom(attributes.getAttribute("from"));
header.setTo(attributes.getAttribute("to"));
header.setID(attributes.getAttribute("id"));
header.setVersion(attributes.getAttribute("version"));
client_->handleStreamStart(header);
}
else {
parseErrorOccurred_ = true;
}
}
else {
if (level_ == StreamLevel) {
assert(!currentElementParser_);
currentElementParser_ = createElementParser(element, ns);
}
currentElementParser_->handleStartElement(element, ns, attributes);
}
}
diff --git a/Swiften/Parser/XMPPParser.h b/Swiften/Parser/XMPPParser.h
index 09fae38..6595b94 100644
--- a/Swiften/Parser/XMPPParser.h
+++ b/Swiften/Parser/XMPPParser.h
@@ -15,43 +15,43 @@
#include <Swiften/Parser/XMLParserClient.h>
namespace Swift {
class XMLParser;
class XMPPParserClient;
class XMLParserFactory;
class ElementParser;
class PayloadParserFactoryCollection;
class SWIFTEN_API XMPPParser : public XMLParserClient, boost::noncopyable {
public:
XMPPParser(
XMPPParserClient* parserClient,
PayloadParserFactoryCollection* payloadParserFactories,
XMLParserFactory* xmlParserFactory);
virtual ~XMPPParser();
bool parse(const std::string&);
private:
virtual void handleStartElement(
const std::string& element,
const std::string& ns,
const AttributeMap& attributes);
virtual void handleEndElement(const std::string& element, const std::string& ns);
virtual void handleCharacterData(const std::string& data);
ElementParser* createElementParser(const std::string& element, const std::string& xmlns);
private:
- XMLParser* xmlParser_;
+ std::unique_ptr<XMLParser> xmlParser_;
XMPPParserClient* client_;
PayloadParserFactoryCollection* payloadParserFactories_;
enum Level {
TopLevel = 0,
StreamLevel = 1,
ElementLevel = 2
};
int level_;
ElementParser* currentElementParser_;
bool parseErrorOccurred_;
};
}