cmXMLParser.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmXMLParser_h
  4. #define cmXMLParser_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <string>
  7. extern "C" {
  8. void cmXMLParserStartElement(void*, const char*, const char**);
  9. void cmXMLParserEndElement(void*, const char*);
  10. void cmXMLParserCharacterDataHandler(void*, const char*, int);
  11. }
  12. /** \class cmXMLParser
  13. * \brief Helper class for performing XML parsing
  14. *
  15. * Superclass for all XML parsers.
  16. */
  17. class cmXMLParser
  18. {
  19. public:
  20. cmXMLParser();
  21. virtual ~cmXMLParser();
  22. //! Parse given XML string
  23. virtual int Parse(const char* string);
  24. //! Parse given XML file
  25. virtual int ParseFile(const char* file);
  26. /**
  27. * When parsing fragments of XML or streaming XML, use the following
  28. * three methods. InitializeParser method initialize parser but does
  29. * not perform any actual parsing. ParseChunk parses framgent of
  30. * XML. This has to match to what was already parsed. CleanupParser
  31. * finishes parsing. If there were errors, CleanupParser will report
  32. * them.
  33. */
  34. virtual int InitializeParser();
  35. virtual int ParseChunk(const char* inputString,
  36. std::string::size_type length);
  37. virtual int CleanupParser();
  38. typedef void (*ReportFunction)(int, const char*, void*);
  39. void SetErrorCallback(ReportFunction f, void* d)
  40. {
  41. this->ReportCallback = f;
  42. this->ReportCallbackData = d;
  43. }
  44. protected:
  45. //! This variable is true if there was a parse error while parsing in
  46. // chunks.
  47. int ParseError;
  48. ReportFunction ReportCallback;
  49. void* ReportCallbackData;
  50. // 1 Expat parser structure. Exists only during call to Parse().
  51. void* Parser;
  52. /**
  53. * Called before each block of input is read from the stream to check if
  54. * parsing is complete. Can be replaced by subclasses to change the
  55. * terminating condition for parsing. Parsing always stops when the end of
  56. * file is reached in the stream.
  57. */
  58. virtual int ParsingComplete();
  59. /**
  60. * Called when a new element is opened in the XML source. Should be
  61. * replaced by subclasses to handle each element. name = Name of new
  62. * element. atts = Null-terminated array of attribute name/value pairs.
  63. * Even indices are attribute names, and odd indices are values.
  64. */
  65. virtual void StartElement(const std::string& name, const char** atts);
  66. //! Called at the end of an element in the XML source opened when
  67. // StartElement was called.
  68. virtual void EndElement(const std::string& name);
  69. //! Called when there is character data to handle.
  70. virtual void CharacterDataHandler(const char* data, int length);
  71. //! Called by Parse to report an XML syntax error.
  72. virtual void ReportXmlParseError();
  73. /** Called by ReportXmlParseError with basic error info. */
  74. virtual void ReportError(int line, int column, const char* msg);
  75. //! Utility for convenience of subclasses. Wraps isspace C library
  76. // routine.
  77. static int IsSpace(char c);
  78. //! Send the given buffer to the XML parser.
  79. virtual int ParseBuffer(const char* buffer, std::string::size_type length);
  80. //! Send the given c-style string to the XML parser.
  81. int ParseBuffer(const char* buffer);
  82. /** Helps subclasses search for attributes on elements. */
  83. static const char* FindAttribute(const char** atts, const char* attribute);
  84. //! Callbacks for the expat
  85. friend void cmXMLParserStartElement(void*, const char*, const char**);
  86. friend void cmXMLParserEndElement(void*, const char*);
  87. friend void cmXMLParserCharacterDataHandler(void*, const char*, int);
  88. };
  89. #endif