Coverage Report

Created: 2025-06-20 06:51

/src/CMake/Source/cmXMLParser.h
Line
Count
Source (jump to first uncovered line)
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <string>
8
9
extern "C" {
10
void cmXMLParserStartElement(void*, char const*, char const**);
11
void cmXMLParserEndElement(void*, char const*);
12
void cmXMLParserCharacterDataHandler(void*, char const*, int);
13
}
14
15
/** \class cmXMLParser
16
 * \brief Helper class for performing XML parsing
17
 *
18
 * Superclass for all XML parsers.
19
 */
20
class cmXMLParser
21
{
22
public:
23
  cmXMLParser();
24
  cmXMLParser(cmXMLParser const& /*other*/) = default;
25
  virtual ~cmXMLParser();
26
27
  //! Parse given XML string
28
  virtual int Parse(char const* string);
29
30
  //! Parse given XML file
31
  virtual int ParseFile(char const* file);
32
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 fragment 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(char const* inputString,
43
                         std::string::size_type length);
44
  virtual int CleanupParser();
45
  using ReportFunction = void (*)(int, char const*, void*);
46
  void SetErrorCallback(ReportFunction f, void* d)
47
0
  {
48
0
    this->ReportCallback = f;
49
0
    this->ReportCallbackData = d;
50
0
  }
51
52
protected:
53
  //! This variable is true if there was a parse error while parsing in
54
  // chunks.
55
  int ParseError;
56
  ReportFunction ReportCallback;
57
  void* ReportCallbackData;
58
59
  // 1 Expat parser structure.  Exists only during call to Parse().
60
  void* Parser;
61
62
  /**
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.
67
   */
68
69
  virtual int ParsingComplete();
70
71
  /**
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.
76
   */
77
  virtual void StartElement(std::string const& name, char const** atts);
78
79
  //! Called at the end of an element in the XML source opened when
80
  // StartElement was called.
81
  virtual void EndElement(std::string const& name);
82
83
  //! Called when there is character data to handle.
84
  virtual void CharacterDataHandler(char const* data, int length);
85
86
  //! Called by Parse to report an XML syntax error.
87
  virtual void ReportXmlParseError();
88
89
  /** Called by ReportXmlParseError with basic error info.  */
90
  virtual void ReportError(int line, int column, char const* msg);
91
92
  //! Send the given buffer to the XML parser.
93
  virtual int ParseBuffer(char const* buffer, std::string::size_type length);
94
95
  //! Send the given c-style string to the XML parser.
96
  int ParseBuffer(char const* buffer);
97
98
  /** Helps subclasses search for attributes on elements.  */
99
  static char const* FindAttribute(char const** atts, char const* attribute);
100
101
  //! Callbacks for the expat
102
  friend void cmXMLParserStartElement(void*, char const*, char const**);
103
  friend void cmXMLParserEndElement(void*, char const*);
104
  friend void cmXMLParserCharacterDataHandler(void*, char const*, int);
105
};