libxcks  0.1.0.1
xmlparser.hpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 #ifndef INC_XMLPARSER_HPP_2D5675B8_341A_487A_9504_5A2CAB588986
25 #define INC_XMLPARSER_HPP_2D5675B8_341A_487A_9504_5A2CAB588986
26 
27 //---------------------------------------------------------------------------
28 #include <vector>
29 #include <utility>
30 #include <string>
31 
32 #include <expat.h>
33 //---------------------------------------------------------------------------
34 
35 
36 namespace libxcks
37 {
42 {
43  protected:
44  std::string message;
45 
46  public:
47  // Default constructor.
48  XMLParserException() noexcept;
49 
50  // Copy constructor.
51  XMLParserException(const XMLParserException& source) noexcept;
52 
53  // Copy assignment operator.
54  XMLParserException& operator=(const XMLParserException& source) noexcept;
55 
56  // Constructor with a message.
57  XMLParserException(const std::string& msg) noexcept;
58 
59  // Destructor.
60  virtual ~XMLParserException();
61 
62  // Returns the explanatory string.
63  virtual std::string what() const noexcept;
64 
65  // Returns the message.
66  virtual std::string getMessage() const noexcept;
67 
68  // Sets the message.
69  virtual void setMessage(const std::string& msg) noexcept;
70 };
71 //---------------------------------------------------------------------------
72 
73 
77 using XMLParserAttribute = std::pair<std::string, std::string>;
78 
79 
84 {
85  protected:
86  std::vector<XMLParserAttribute> attributes;
87 
88  public:
90  static constexpr int Not_Found = -1;
91 
94 
97 
100 
101  // Adds an attribute to the end of the list.
102  void addAttribute(const std::string& name, const std::string& value);
103 
104  // Adds an attribute to the end of the list.
105  void addAttribute(const XMLParserAttribute&& attribute);
106 
107  // Clears the attribute list for reuse.
108  void clear();
109 
110  // Looks up an attribute's index by name.
111  int getIndex(const std::string& name) const;
112 
113  // Returns the number of attributes in the list.
114  size_t getCount() const;
115 
116  // Returns an attribute's name.
117  bool getName(size_t index, std::string& name) const;
118 
119  // Returns an attribute's value by index.
120  bool getValue(size_t index, std::string& value) const;
121 
122  // Looks up an attribute's value by name.
123  bool getValue(const std::string& name, std::string& value) const;
124 };
125 //---------------------------------------------------------------------------
126 
127 
138 {
139  private:
140  XML_Parser parser;
141  int xmlDepth;
142 
143  protected:
144  // Default constructor.
145  XMLParser();
146 
147  // Destructor.
148  virtual ~XMLParser();
149 
150  // Initializes the parser for parsing a new document.
151  bool initParser();
152 
153  // Receives notification of the beginning of an element.
154  virtual void startElement(const std::string& name, const XMLParserAttributes& atts) noexcept(false);
155 
156  // Receives notification of the end of an element.
157  virtual void endElement(const std::string& name) noexcept(false);
158 
159  // Receives notification of character data.
160  virtual void characters(const std::string& chars) noexcept(false);
161 
162  // Receive notification of a non-recoverable error.
163  virtual void fatalError(XML_Error errorCode, const std::string& errorMessage, int line, int column) noexcept;
164 
165  private:
166  // Static methods for binding expat handlers
167  // Handler for the beginning of an element.
168  static void XMLCALL startElementHandler(void* userData, const XML_Char* name, const XML_Char** atts) noexcept(false);
169 
170  // Handler for the end of an element.
171  static void XMLCALL endElementHandler(void* userData, const XML_Char* name) noexcept(false);
172 
173  // Handler for character data.
174  static void XMLCALL characterDataHandler(void* userData, const XML_Char* s, int len) noexcept(false);
175 
176  private:
177  // Changes the current depth in XML tree structure.
178  void setDepth(int depth);
179 
180  protected:
181  // Returns the current depth in XML tree structure.
182  int getDepth() const;
183 
184  public:
185  // Parses the given file.
186  bool parse(const std::string& filename);
187 
188  // Parses the given input stream.
189  bool parse(std::istream& is);
190 };
191 //---------------------------------------------------------------------------
192 
193 
198 {
199  public:
202 
205 
208 
211 
220  virtual void startElement(const std::string& name, const XMLParserAttributes& atts) noexcept(false) {}
221 
229  virtual void endElement(const std::string& name) noexcept(false) {}
230 
238  virtual void characters(const std::string& chars) noexcept(false) {}
239 
250  virtual void fatalError(XML_Error errorCode, const std::string& errorMessage, int line, int column) noexcept {}
251 };
252 //---------------------------------------------------------------------------
253 } // namespace libxcks
254 //---------------------------------------------------------------------------
255 
256 #endif // INC_XMLPARSER_HPP_2D5675B8_341A_487A_9504_5A2CAB588986
Manages elements' attributes.
Definition: xmlparser.hpp:84
std::vector< XMLParserAttribute > attributes
Attributes.
Definition: xmlparser.hpp:86
XMLParserAttributes & operator=(const XMLParserAttributes &)=delete
Deleted copy assignment operator.
XMLParserAttributes()
Default constructor.
Definition: xmlparser.hpp:93
XMLParserAttributes(const XMLParserAttributes &)=delete
Deleted copy constructor.
Default XML parser handler.
Definition: xmlparser.hpp:198
XMLParserDefaultHandler()
Default constructor.
Definition: xmlparser.hpp:201
virtual void endElement(const std::string &name) noexcept(false)
Receives notification of the end of an element.
Definition: xmlparser.hpp:229
virtual void characters(const std::string &chars) noexcept(false)
Receives notification of character data.
Definition: xmlparser.hpp:238
XMLParserDefaultHandler(const XMLParserDefaultHandler &)=delete
Deleted copy constructor.
virtual ~XMLParserDefaultHandler()
Destructor.
Definition: xmlparser.hpp:210
virtual void startElement(const std::string &name, const XMLParserAttributes &atts) noexcept(false)
Receives notification of the beginning of an element.
Definition: xmlparser.hpp:220
virtual void fatalError(XML_Error errorCode, const std::string &errorMessage, int line, int column) noexcept
Receive notification of a non-recoverable error.
Definition: xmlparser.hpp:250
XMLParserDefaultHandler & operator=(const XMLParserDefaultHandler &)=delete
Deleted copy assignment operator.
An exception class for the XML parser.
Definition: xmlparser.hpp:42
XMLParserException() noexcept
Default constructor.
Definition: xmlparser.cpp:56
virtual void setMessage(const std::string &msg) noexcept
Sets the message.
Definition: xmlparser.cpp:148
virtual std::string getMessage() const noexcept
Returns the message.
Definition: xmlparser.cpp:136
virtual std::string what() const noexcept
Returns the explanatory string.
Definition: xmlparser.cpp:122
std::string message
The message of the exception.
Definition: xmlparser.hpp:44
A very simple XML parser.
Definition: xmlparser.hpp:138
std::pair< std::string, std::string > XMLParserAttribute
An attribute and its value.
Definition: xmlparser.hpp:77