XMLRawParser.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef XMLRAWPARSER_H
  2. #define XMLRAWPARSER_H
  3. //==============================================================================
  4. //
  5. // XMLRawParser - the parser class in the gpxtools library
  6. //
  7. // Copyright (C) 2018 Dick van Oudheusden
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Lesser General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 3 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Lesser General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Lesser General Public
  20. // License along with this library; if not, write to the Free
  21. // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. //
  23. //==============================================================================
  24. #include <iostream>
  25. #include <map>
  26. #include "expat.h"
  27. namespace gpxtools
  28. {
  29. ///
  30. /// @class XMLParserHandler
  31. ///
  32. /// @brief The xml parser handler class.
  33. ///
  34. class XMLParserHandler
  35. {
  36. public:
  37. XMLParserHandler() {}
  38. virtual ~XMLParserHandler() {}
  39. typedef std::map<std::string, std::string> Attributes;
  40. virtual void startElement(const std::string &path, const std::string &text, const std::string &name, const Attributes &attributes) = 0;
  41. virtual void endElement(const std::string &path, const std::string &text, const std::string &name) = 0;
  42. virtual void text(const std::string &path, const std::string &text) = 0;
  43. virtual void unhandled(const std::string &path, const std::string &text) = 0;
  44. };
  45. ///
  46. /// @class XMLParser
  47. ///
  48. /// @brief The xml parser class.
  49. ///
  50. class XMLRawParser
  51. {
  52. public:
  53. ///
  54. /// Constructor
  55. ///
  56. ///
  57. XMLRawParser(XMLParserHandler *handler);
  58. ///
  59. /// Deconstructor
  60. ///
  61. virtual ~XMLRawParser();
  62. // Properties
  63. ///
  64. /// Get the last error text
  65. ///
  66. /// @return the last error text
  67. ///
  68. const std::string &errorText() const { return _errorText; }
  69. ///
  70. /// Get the last error line number
  71. ///
  72. /// @return the last error line number
  73. ///
  74. int errorLineNumber() const { return _errorLineNumber; }
  75. ///
  76. /// Get the last error column number
  77. ///
  78. /// @return the last error column number
  79. ///
  80. int errorColumnNumber() const { return _errorColumnNumber; }
  81. public:
  82. // Parsing methods
  83. ///
  84. /// Parse data
  85. ///
  86. /// @param data the data to be parsed
  87. /// @param length the length of the data
  88. /// @param isFinal is this the last data ?
  89. ///
  90. /// @return success
  91. ///
  92. bool parse(const char *data, std::size_t length, bool isFinal);
  93. ///
  94. /// Parse text
  95. ///
  96. /// @param text the zero terminated text to be parsed
  97. /// @param isFinal is this the last data ?
  98. ///
  99. /// @return success
  100. ///
  101. bool parse(const char *text, bool isFinal);
  102. ///
  103. /// Parse string
  104. ///
  105. /// @param data the data to be parsed
  106. /// @param isFinal is this the last data ?
  107. ///
  108. /// @return success
  109. ///
  110. bool parse(const std::string &data, bool isFinal);
  111. ///
  112. /// Parse an input stream
  113. ///
  114. /// @param stream the stream to be parsed
  115. ///
  116. /// @return success
  117. ///
  118. bool parse(std::istream &stream);
  119. ///
  120. /// Trim a string
  121. ///
  122. /// @param text the string to be trimmed
  123. ///
  124. static void trim(std::string &text);
  125. private:
  126. // Store start element
  127. void storeStartElement(const XML_Char *name, const XML_Char **atts);
  128. void storeEndElement(const XML_Char *name);
  129. void storeText();
  130. void storeUnhandled();
  131. void sendLast(const std::string &raw);
  132. private:
  133. // Start using expat
  134. void startExpat();
  135. // Stop using expat
  136. void stopExpat();
  137. // Statics for expat
  138. static void startElementHandler (void *userData, const XML_Char *name, const XML_Char **atts);
  139. static void endElementHandler (void *userData, const XML_Char *name);
  140. static void characterDataHandler (void *userData, const XML_Char *s, int len);
  141. static void unhandledHandler (void *userData, const XML_Char *s, int len);
  142. // Return the text from the buffer
  143. std::string readBuffer();
  144. // Members
  145. XMLParserHandler *_handler;
  146. XML_Parser _parser;
  147. std::string _path;
  148. std::string _errorText;
  149. int _errorLineNumber;
  150. int _errorColumnNumber;
  151. enum XMLItem
  152. {
  153. NOTHING,
  154. START_ELEMENT,
  155. END_ELEMENT,
  156. TEXT,
  157. UNHANDLED
  158. };
  159. XMLItem _lastXMLItem;
  160. std::string _lastPath;
  161. std::string _lastName;
  162. XMLParserHandler::Attributes _lastAttributes;
  163. std::string _buffer;
  164. std::size_t _startByteIndex;
  165. // Disable copy constructors
  166. XMLRawParser(const XMLRawParser &);
  167. XMLRawParser& operator=(const XMLRawParser &);
  168. };
  169. }
  170. #endif