diff options
| -rw-r--r-- | Swiften/Parser/ExpatParser.cpp | 6 | ||||
| -rw-r--r-- | Swiften/Parser/ExpatParser.h | 4 | ||||
| -rw-r--r-- | Swiften/Parser/LibXMLParser.cpp | 6 | ||||
| -rw-r--r-- | Swiften/Parser/LibXMLParser.h | 4 | ||||
| -rw-r--r-- | Swiften/Parser/UnitTest/XMLParserTest.cpp | 9 | ||||
| -rw-r--r-- | Swiften/Parser/XMLParser.h | 4 |
6 files changed, 21 insertions, 12 deletions
diff --git a/Swiften/Parser/ExpatParser.cpp b/Swiften/Parser/ExpatParser.cpp index 8415c42..e4e66f2 100644 --- a/Swiften/Parser/ExpatParser.cpp +++ b/Swiften/Parser/ExpatParser.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Parser/ExpatParser.h> #include <cassert> #include <limits> #include <memory> @@ -76,23 +76,23 @@ ExpatParser::ExpatParser(XMLParserClient* client) : XMLParser(client), p(new Pri XML_SetCharacterDataHandler(p->parser_, handleCharacterData); XML_SetXmlDeclHandler(p->parser_, handleXMLDeclaration); XML_SetEntityDeclHandler(p->parser_, handleEntityDeclaration); } ExpatParser::~ExpatParser() { XML_ParserFree(p->parser_); } -bool ExpatParser::parse(const std::string& data) { +bool ExpatParser::parse(const std::string& data, bool finalData) { if (data.size() > std::numeric_limits<int>::max()) { return false; } - bool success = XML_Parse(p->parser_, data.c_str(), static_cast<int>(data.size()), false) == XML_STATUS_OK; + bool success = XML_Parse(p->parser_, data.c_str(), static_cast<int>(data.size()), finalData) == XML_STATUS_OK; /*if (!success) { std::cout << "ERROR: " << XML_ErrorString(XML_GetErrorCode(p->parser_)) << " while parsing " << data << std::endl; }*/ return success; } void ExpatParser::stopParser() { XML_StopParser(p->parser_, static_cast<XML_Bool>(0)); } diff --git a/Swiften/Parser/ExpatParser.h b/Swiften/Parser/ExpatParser.h index 12df463..7583339 100644 --- a/Swiften/Parser/ExpatParser.h +++ b/Swiften/Parser/ExpatParser.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <memory> #include <boost/noncopyable.hpp> @@ -13,18 +13,18 @@ #include <Swiften/Base/API.h> #include <Swiften/Parser/XMLParser.h> namespace Swift { class SWIFTEN_API ExpatParser : public XMLParser, public boost::noncopyable { public: ExpatParser(XMLParserClient* client); ~ExpatParser(); - bool parse(const std::string& data); + bool parse(const std::string& data, bool finalData = false); void stopParser(); private: struct Private; const std::unique_ptr<Private> p; }; } diff --git a/Swiften/Parser/LibXMLParser.cpp b/Swiften/Parser/LibXMLParser.cpp index 5bd3737..c9f3a07 100644 --- a/Swiften/Parser/LibXMLParser.cpp +++ b/Swiften/Parser/LibXMLParser.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2018 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Parser/LibXMLParser.h> #include <cassert> #include <cstring> #include <limits> @@ -88,23 +88,23 @@ LibXMLParser::LibXMLParser(XMLParserClient* client) : XMLParser(client), p(new P assert(p->context_); } LibXMLParser::~LibXMLParser() { if (p->context_) { xmlFreeParserCtxt(p->context_); } } -bool LibXMLParser::parse(const std::string& data) { +bool LibXMLParser::parse(const std::string& data, bool finalData) { if (data.size() > std::numeric_limits<int>::max()) { return false; } - if (xmlParseChunk(p->context_, data.c_str(), static_cast<int>(data.size()), false) == XML_ERR_OK) { + if (xmlParseChunk(p->context_, data.c_str(), static_cast<int>(data.size()), finalData) == XML_ERR_OK) { return true; } xmlError* error = xmlCtxtGetLastError(p->context_); if (error->code == XML_WAR_NS_URI || error->code == XML_WAR_NS_URI_RELATIVE) { xmlCtxtResetLastError(p->context_); p->context_->errNo = XML_ERR_OK; return true; } return false; diff --git a/Swiften/Parser/LibXMLParser.h b/Swiften/Parser/LibXMLParser.h index 9f752ce..a863867 100644 --- a/Swiften/Parser/LibXMLParser.h +++ b/Swiften/Parser/LibXMLParser.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <memory> #include <boost/noncopyable.hpp> @@ -16,18 +16,18 @@ namespace Swift { /** * Warning: This constructor is not thread-safe, because it depends on global state to * check whether it is initialized. */ class LibXMLParser : public XMLParser, public boost::noncopyable { public: LibXMLParser(XMLParserClient* client); virtual ~LibXMLParser(); - bool parse(const std::string& data); + bool parse(const std::string& data, bool finalData = false); private: static bool initialized; struct Private; const std::unique_ptr<Private> p; }; } diff --git a/Swiften/Parser/UnitTest/XMLParserTest.cpp b/Swiften/Parser/UnitTest/XMLParserTest.cpp index b593aa7..9e9012b 100644 --- a/Swiften/Parser/UnitTest/XMLParserTest.cpp +++ b/Swiften/Parser/UnitTest/XMLParserTest.cpp @@ -211,18 +211,27 @@ class XMLParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), client_.events.size()); CPPUNIT_ASSERT_EQUAL(Client::StartElement, client_.events[0].type); CPPUNIT_ASSERT_EQUAL(std::string("iq"), client_.events[0].data); CPPUNIT_ASSERT_EQUAL(Client::EndElement, client_.events[1].type); CPPUNIT_ASSERT_EQUAL(std::string("iq"), client_.events[1].data); } + void testParse_CompleteDocument() { + ParserType testling(&client_); + + CPPUNIT_ASSERT(!testling.parse("<iq", true)); + CPPUNIT_ASSERT(!testling.parse("<iq>", true)); + CPPUNIT_ASSERT(!testling.parse("<iq><child>foo</child>", true)); + CPPUNIT_ASSERT(testling.parse("<iq><child>foo</child></iq>", true)); + } + void testParse_WhitespaceInAttribute() { ParserType testling(&client_); CPPUNIT_ASSERT(testling.parse( "<query xmlns='http://www.xmpp.org/extensions/xep-0084.html#ns-data '>")); CPPUNIT_ASSERT(testling.parse( "<presence/>")); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), client_.events.size()); CPPUNIT_ASSERT_EQUAL(Client::StartElement, client_.events[0].type); diff --git a/Swiften/Parser/XMLParser.h b/Swiften/Parser/XMLParser.h index 8a73c3f..ad79b2d 100644 --- a/Swiften/Parser/XMLParser.h +++ b/Swiften/Parser/XMLParser.h @@ -1,30 +1,30 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <string> #include <Swiften/Base/API.h> namespace Swift { class XMLParserClient; class SWIFTEN_API XMLParser { public: XMLParser(XMLParserClient* client); virtual ~XMLParser(); - virtual bool parse(const std::string& data) = 0; + virtual bool parse(const std::string& data, bool finalData = false) = 0; XMLParserClient* getClient() const { return client_; } private: XMLParserClient* client_; }; } |
Swift