XMLParser.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef XMLPARSER_H
  2. #define XMLPARSER_H
  3. //==============================================================================
  4. //
  5. // XMLParser - 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 &name, const Attributes &attributes) = 0;
  41. virtual void endElement(const std::string &path, const std::string &name) = 0;
  42. virtual void text(const std::string &path, const std::string &text) = 0;
  43. };
  44. ///
  45. /// @class XMLParser
  46. ///
  47. /// @brief The xml parser class.
  48. ///
  49. class XMLParser
  50. {
  51. public:
  52. ///
  53. /// Constructor
  54. ///
  55. ///
  56. XMLParser(XMLParserHandler *handler);
  57. ///
  58. /// Deconstructor
  59. ///
  60. virtual ~XMLParser();
  61. // Properties
  62. ///
  63. /// Get the last error text
  64. ///
  65. /// @return the last error text
  66. ///
  67. const std::string &errorText() const { return _errorText; }
  68. ///
  69. /// Get the last error line number
  70. ///
  71. /// @return the last error line number
  72. ///
  73. int errorLineNumber() const { return _errorLineNumber; }
  74. ///
  75. /// Get the last error column number
  76. ///
  77. /// @return the last error column number
  78. ///
  79. int errorColumnNumber() const { return _errorColumnNumber; }
  80. public:
  81. // Parsing methods
  82. ///
  83. /// Parse data
  84. ///
  85. /// @param data the data to be parsed
  86. /// @param length the length of the data
  87. /// @param isFinal is this the last data ?
  88. ///
  89. /// @return success
  90. ///
  91. bool parse(const char *data, int length, bool isFinal);
  92. ///
  93. /// Parse text
  94. ///
  95. /// @param text the zero terminated text to be parsed
  96. /// @param isFinal is this the last data ?
  97. ///
  98. /// @return success
  99. ///
  100. bool parse(const char *text, bool isFinal);
  101. ///
  102. /// Parse string
  103. ///
  104. /// @param data the data to be parsed
  105. /// @param isFinal is this the last data ?
  106. ///
  107. /// @return success
  108. ///
  109. bool parse(const std::string &data, bool isFinal);
  110. ///
  111. /// Parse an input stream
  112. ///
  113. /// @param stream the stream to be parsed
  114. ///
  115. /// @return success
  116. ///
  117. bool parse(std::istream &stream);
  118. ///
  119. /// Trim a string
  120. ///
  121. /// @param text the string to be trimmed
  122. ///
  123. static void trim(std::string &text);
  124. private:
  125. // Start using expat
  126. void startExpat();
  127. // Stop using expat
  128. void stopExpat();
  129. // Statics for expat
  130. static void startElementHandler (void *userData, const XML_Char *name, const XML_Char **atts);
  131. static void endElementHandler (void *userData, const XML_Char *name);
  132. static void characterDataHandler (void *userData, const XML_Char *s, int len);
  133. // Members
  134. XMLParserHandler *_handler;
  135. XML_Parser _parser;
  136. std::string _path;
  137. std::string _errorText;
  138. int _errorLineNumber;
  139. int _errorColumnNumber;
  140. // Disable copy constructors
  141. XMLParser(const XMLParser &);
  142. XMLParser& operator=(const XMLParser &);
  143. };
  144. }
  145. #endif