XMLParser.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //==============================================================================
  2. //
  3. // XMLParser - the XML parser class
  4. //
  5. // Copyright (C) 2018 Dick van Oudheusden
  6. //
  7. // This library is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU Lesser General Public
  9. // License as published by the Free Software Foundation; either
  10. // version 3 of the License, or (at your option) any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. // Lesser General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public
  18. // License along with this library; if not, write to the Free
  19. // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. //
  21. //==============================================================================
  22. #include <string>
  23. #include <cstring>
  24. #include "XMLParser.h"
  25. namespace gpxtools
  26. {
  27. XMLParser::XMLParser(XMLParserHandler *handler) :
  28. _handler(handler),
  29. _parser(0),
  30. _path(""),
  31. _errorText(),
  32. _errorLineNumber(0),
  33. _errorColumnNumber(0)
  34. {
  35. }
  36. XMLParser::~XMLParser()
  37. {
  38. if (_parser != 0)
  39. {
  40. stopExpat();
  41. }
  42. }
  43. // Parsing
  44. bool XMLParser::parse(const char *data, int length, bool isFinal)
  45. {
  46. bool ok = false;
  47. if (_parser == 0)
  48. {
  49. startExpat();
  50. if (_parser == 0)
  51. {
  52. return false;
  53. }
  54. _path = "";
  55. _errorText = "";
  56. _errorLineNumber = 0;
  57. _errorColumnNumber = 0;
  58. }
  59. ok = (XML_Parse(_parser, data, length, isFinal) != XML_STATUS_ERROR);
  60. if (!ok)
  61. {
  62. _errorText = XML_ErrorString(XML_GetErrorCode(_parser));
  63. _errorLineNumber = XML_GetCurrentLineNumber(_parser);
  64. _errorColumnNumber = XML_GetCurrentColumnNumber(_parser);
  65. }
  66. if (isFinal)
  67. {
  68. stopExpat();
  69. }
  70. return ok;
  71. }
  72. bool XMLParser::parse(const char *text, bool isFinal)
  73. {
  74. return parse(text, strlen(text), isFinal);
  75. }
  76. bool XMLParser::parse(const std::string &data, bool isFinal)
  77. {
  78. return parse(data.c_str(), data.length(), isFinal);
  79. }
  80. // Parse a stream
  81. bool XMLParser::parse(std::istream &stream)
  82. {
  83. bool ok = stream.good();
  84. if (ok)
  85. {
  86. char buffer[4096];
  87. while ((ok) && (stream.good()))
  88. {
  89. stream.read(buffer, sizeof(buffer));
  90. ok = parse(buffer, (int) stream.gcount(), (stream.gcount() < sizeof(buffer)));
  91. }
  92. }
  93. return ok;
  94. }
  95. // Helpers
  96. void XMLParser::trim(std::string &text)
  97. {
  98. std::size_t start = text.find_first_not_of(" \t\r\n");
  99. if (start == std::string::npos)
  100. {
  101. text.clear();
  102. }
  103. else
  104. {
  105. text.erase(0, start);
  106. std::size_t end = text.find_last_not_of(" \t\r\n");
  107. text.erase(end + 1);
  108. }
  109. }
  110. // Privates
  111. void XMLParser::startExpat()
  112. {
  113. _parser = XML_ParserCreate(NULL);
  114. XML_SetUserData(_parser, this);
  115. XML_SetElementHandler(_parser, startElementHandler, endElementHandler);
  116. XML_SetCharacterDataHandler(_parser, characterDataHandler);
  117. }
  118. void XMLParser::stopExpat()
  119. {
  120. XML_ParserFree(_parser);
  121. _parser = 0;
  122. }
  123. void XMLParser::startElementHandler(void *userData, const XML_Char *name, const XML_Char **atts)
  124. {
  125. XMLParser *self = static_cast<XMLParser*>(userData);
  126. XMLParserHandler::Attributes attributes;
  127. for (int i = 0; atts[i] != NULL; i+=2)
  128. {
  129. attributes[std::string(atts[i])] = atts[i+1];
  130. }
  131. self->_path.append("/");
  132. self->_path.append(name);
  133. self->_handler->startElement(self->_path, std::string(name), attributes);
  134. }
  135. void XMLParser::endElementHandler(void *userData, const XML_Char *name)
  136. {
  137. XMLParser *self = static_cast<XMLParser*>(userData);
  138. self->_handler->endElement(self->_path, std::string(name));
  139. size_t i = self->_path.find_last_of('/');
  140. if (i != std::string::npos)
  141. {
  142. self->_path.erase(i);
  143. }
  144. }
  145. void XMLParser::characterDataHandler(void *userData, const XML_Char *s, int len)
  146. {
  147. XMLParser *self = static_cast<XMLParser*>(userData);
  148. self->_handler->text(self->_path, std::string(s, len));
  149. }
  150. }