123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- //==============================================================================
- //
- // XMLParser - the XML parser class
- //
- // Copyright (C) 2018 Dick van Oudheusden
- //
- // This library is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Lesser General Public
- // License as published by the Free Software Foundation; either
- // version 3 of the License, or (at your option) any later version.
- //
- // This library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public
- // License along with this library; if not, write to the Free
- // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- //
- //==============================================================================
- #include <string>
- #include <cstring>
- #include "XMLRawParser.h"
- namespace gpxtools
- {
-
- XMLRawParser::XMLRawParser(XMLParserHandler *handler) :
- _handler(handler),
- _parser(nullptr),
- _path(""),
- _errorText(),
- _errorLineNumber(0),
- _errorColumnNumber(0),
- _lastXMLItem(NOTHING),
- _lastName(),
- _lastPath(),
- _lastAttributes(),
- _buffer(),
- _startByteIndex(0)
- {
- }
- XMLRawParser::~XMLRawParser()
- {
- if (_parser != nullptr)
- {
- stopExpat();
- }
- }
- // Parsing
- bool XMLRawParser::parse(const char *data, std::size_t length, bool isFinal)
- {
- bool ok = false;
- if (_parser == nullptr)
- {
- startExpat();
- if (_parser == nullptr) return false;
- _buffer.clear();
- _startByteIndex = 0;
- _path.clear();
- _errorText.clear();
- _errorLineNumber = 0;
- _errorColumnNumber = 0;
- }
- _buffer.append(data, length);
- ok = (XML_Parse(_parser, data, int(length), isFinal) != XML_STATUS_ERROR);
- if (!ok)
- {
- _errorText = XML_ErrorString(XML_GetErrorCode(_parser));
- _errorLineNumber = XML_GetCurrentLineNumber(_parser);
- _errorColumnNumber = XML_GetCurrentColumnNumber(_parser);
- }
- if (isFinal)
- {
- sendLast(_buffer);
- stopExpat();
- }
- return ok;
- }
- bool XMLRawParser::parse(const char *text, bool isFinal)
- {
- return parse(text, strlen(text), isFinal);
- }
- bool XMLRawParser::parse(const std::string &data, bool isFinal)
- {
- return parse(data.c_str(), data.length(), isFinal);
- }
- // Parse a stream
- bool XMLRawParser::parse(std::istream &stream)
- {
- bool ok = stream.good();
- if (ok)
- {
- char buffer[4096];
- while ((ok) && (stream.good()))
- {
- stream.read(buffer, sizeof(buffer));
- ok = parse(buffer, stream.gcount(), (stream.gcount() < sizeof(buffer)));
- }
- }
- return ok;
- }
- // Buffer
- std::string XMLRawParser::readBuffer()
- {
- if (_parser == nullptr) return std::string();
- std::size_t current = std::size_t(XML_GetCurrentByteIndex(_parser));
- if (current < _startByteIndex) return std::string();
- std::string text = _buffer.substr(0, current - _startByteIndex);
- _buffer.erase(0, current - _startByteIndex);
- _startByteIndex = current;
- return text;
- }
- // Helpers
- void XMLRawParser::trim(std::string &text)
- {
- std::size_t start = text.find_first_not_of(" \t\r\n");
- if (start == std::string::npos)
- {
- text.clear();
- }
- else
- {
- text.erase(0, start);
- std::size_t end = text.find_last_not_of(" \t\r\n");
- text.erase(end + 1);
- }
- }
- // Store XML items
- void XMLRawParser::storeStartElement(const XML_Char *name, const XML_Char **atts)
- {
- _lastXMLItem = START_ELEMENT;
- _lastName = std::string(name);
- _lastPath = _path;
- _lastAttributes.clear();
- for (int i = 0; atts[i] != nullptr; i+=2)
- {
- _lastAttributes[std::string(atts[i])] = atts[i+1];
- }
- }
- void XMLRawParser::storeEndElement(const XML_Char *name)
- {
- _lastXMLItem = END_ELEMENT;
- _lastName = std::string(name);
- _lastPath = _path;
- _lastAttributes.clear();
- }
- void XMLRawParser::storeText()
- {
- _lastXMLItem = TEXT;
- _lastName.clear();
- _lastPath = _path;
- _lastAttributes.clear();
- }
- void XMLRawParser::storeUnhandled()
- {
- _lastXMLItem = UNHANDLED;
- _lastName.clear();
- _lastPath = _path;
- _lastAttributes.clear();
- }
- void XMLRawParser::sendLast(const std::string &raw)
- {
- switch(_lastXMLItem)
- {
- case START_ELEMENT:
- _handler->startElement(_lastPath, raw, _lastName, _lastAttributes);
- break;
- case END_ELEMENT:
- _handler->endElement(_lastPath, raw, _lastName);
- break;
- case TEXT:
- _handler->text(_lastPath, raw);
- break;
- case UNHANDLED:
- _handler->unhandled(_lastPath, raw);
- break;
- case NOTHING:
- break;
- }
- _lastXMLItem = NOTHING;
- }
- // Privates
- void XMLRawParser::startExpat()
- {
- _parser = XML_ParserCreate(nullptr);
-
- XML_SetUserData(_parser, this);
-
- XML_SetElementHandler(_parser, startElementHandler, endElementHandler);
- XML_SetCharacterDataHandler(_parser, characterDataHandler);
- XML_SetDefaultHandler(_parser, unhandledHandler);
- }
-
- void XMLRawParser::stopExpat()
- {
- XML_ParserFree(_parser);
-
- _parser = nullptr;
- }
- void XMLRawParser::startElementHandler(void *userData, const XML_Char *name, const XML_Char **atts)
- {
- XMLRawParser *self = static_cast<XMLRawParser*>(userData);
-
- self->sendLast(self->readBuffer());
- self->_path.append("/");
- self->_path.append(name);
- self->storeStartElement(name, atts);
- }
- void XMLRawParser::endElementHandler(void *userData, const XML_Char *name)
- {
- XMLRawParser *self = static_cast<XMLRawParser*>(userData);
-
- self->sendLast(self->readBuffer());
- self->storeEndElement(name);
- size_t i = self->_path.find_last_of('/');
- if (i != std::string::npos)
- {
- self->_path.erase(i);
- }
- }
- void XMLRawParser::characterDataHandler(void *userData, const XML_Char *, int)
- {
- XMLRawParser *self = static_cast<XMLRawParser*>(userData);
- self->sendLast(self->readBuffer());
- self->storeText();
- }
- void XMLRawParser::unhandledHandler(void *userData, const XML_Char *, int)
- {
- XMLRawParser *self = static_cast<XMLRawParser*>(userData);
- self->sendLast(self->readBuffer());
- self->storeUnhandled();
- }
- }
|