1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
10 void cmXMLParserStartElement(void*, const char*, const char**);
11 void cmXMLParserEndElement(void*, const char*);
12 void cmXMLParserCharacterDataHandler(void*, const char*, int);
15 /** \class cmXMLParser
16 * \brief Helper class for performing XML parsing
18 * Superclass for all XML parsers.
24 cmXMLParser(const cmXMLParser& /*other*/) = default;
25 virtual ~cmXMLParser();
27 //! Parse given XML string
28 virtual int Parse(const char* string);
30 //! Parse given XML file
31 virtual int ParseFile(const char* file);
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
41 virtual int InitializeParser();
42 virtual int ParseChunk(const char* inputString,
43 std::string::size_type length);
44 virtual int CleanupParser();
45 using ReportFunction = void (*)(int, const char*, void*);
46 void SetErrorCallback(ReportFunction f, void* d)
48 this->ReportCallback = f;
49 this->ReportCallbackData = d;
53 //! This variable is true if there was a parse error while parsing in
56 ReportFunction ReportCallback;
57 void* ReportCallbackData;
59 // 1 Expat parser structure. Exists only during call to Parse().
63 * Called before each block of input is read from the stream to check if
64 * parsing is complete. Can be replaced by subclasses to change the
65 * terminating condition for parsing. Parsing always stops when the end of
66 * file is reached in the stream.
69 virtual int ParsingComplete();
72 * Called when a new element is opened in the XML source. Should be
73 * replaced by subclasses to handle each element. name = Name of new
74 * element. atts = Null-terminated array of attribute name/value pairs.
75 * Even indices are attribute names, and odd indices are values.
77 virtual void StartElement(const std::string& name, const char** atts);
79 //! Called at the end of an element in the XML source opened when
80 // StartElement was called.
81 virtual void EndElement(const std::string& name);
83 //! Called when there is character data to handle.
84 virtual void CharacterDataHandler(const char* data, int length);
86 //! Called by Parse to report an XML syntax error.
87 virtual void ReportXmlParseError();
89 /** Called by ReportXmlParseError with basic error info. */
90 virtual void ReportError(int line, int column, const char* msg);
92 //! Utility for convenience of subclasses. Wraps isspace C library
94 static int IsSpace(char c);
96 //! Send the given buffer to the XML parser.
97 virtual int ParseBuffer(const char* buffer, std::string::size_type length);
99 //! Send the given c-style string to the XML parser.
100 int ParseBuffer(const char* buffer);
102 /** Helps subclasses search for attributes on elements. */
103 static const char* FindAttribute(const char** atts, const char* attribute);
105 //! Callbacks for the expat
106 friend void cmXMLParserStartElement(void*, const char*, const char**);
107 friend void cmXMLParserEndElement(void*, const char*);
108 friend void cmXMLParserCharacterDataHandler(void*, const char*, int);