cmXMLParser.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmXMLParser_h
  11. #define cmXMLParser_h
  12. #include "cmStandardIncludes.h"
  13. extern "C"
  14. {
  15. void cmXMLParserStartElement(void*, const char*, const char**);
  16. void cmXMLParserEndElement(void*, const char*);
  17. void cmXMLParserCharacterDataHandler(void*, const char*, int);
  18. }
  19. /** \class cmXMLParser
  20. * \brief Helper class for performing XML parsing
  21. *
  22. * Superclass for all XML parsers.
  23. */
  24. class cmXMLParser
  25. {
  26. public:
  27. cmXMLParser();
  28. virtual ~cmXMLParser();
  29. //! Parse given XML string
  30. virtual int Parse(const char* string);
  31. //! Parse given XML file
  32. virtual int ParseFile(const char* file);
  33. /**
  34. * When parsing fragments of XML or streaming XML, use the following
  35. * three methods. InitializeParser method initialize parser but does
  36. * not perform any actual parsing. ParseChunk parses framgent of
  37. * XML. This has to match to what was already parsed. CleanupParser
  38. * finishes parsing. If there were errors, CleanupParser will report
  39. * them.
  40. */
  41. virtual int InitializeParser();
  42. virtual int ParseChunk(const char* inputString,
  43. std::string::size_type length);
  44. virtual int CleanupParser();
  45. protected:
  46. //! This variable is true if there was a parse error while parsing in
  47. //chunks.
  48. int ParseError;
  49. //1 Expat parser structure. Exists only during call to Parse().
  50. void* Parser;
  51. /**
  52. * Called before each block of input is read from the stream to check if
  53. * parsing is complete. Can be replaced by subclasses to change the
  54. * terminating condition for parsing. Parsing always stops when the end of
  55. * file is reached in the stream.
  56. */
  57. virtual int ParsingComplete();
  58. /**
  59. * Called when a new element is opened in the XML source. Should be
  60. * replaced by subclasses to handle each element. name = Name of new
  61. * element. atts = Null-terminated array of attribute name/value pairs.
  62. * Even indices are attribute names, and odd indices are values.
  63. */
  64. virtual void StartElement(const char* name, const char** atts);
  65. //! Called at the end of an element in the XML source opened when
  66. //StartElement was called.
  67. virtual void EndElement(const char* name);
  68. //! Called when there is character data to handle.
  69. virtual void CharacterDataHandler(const char* data, int length);
  70. //! Called by Parse to report an XML syntax error.
  71. virtual void ReportXmlParseError();
  72. /** Called by ReportXmlParseError with basic error info. */
  73. virtual void ReportError(int line, int column, const char* msg);
  74. //! Utility for convenience of subclasses. Wraps isspace C library
  75. // routine.
  76. static int IsSpace(char c);
  77. //! Send the given buffer to the XML parser.
  78. virtual int ParseBuffer(const char* buffer,
  79. 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