Doxygen
Loading...
Searching...
No Matches
xml.h
Go to the documentation of this file.
1#ifndef XML_H
2#define XML_H
3
4/******************************************************************************
5 *
6 * Copyright (C) 1997-2021 by Dimitri van Heesch.
7 *
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
13 *
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
16 *
17 */
18
19#include <memory>
20#include <functional>
21#include <string>
22#include <unordered_map>
23
24/*! @brief Event handlers that can installed by the client and called while parsing a XML document.
25 */
27{
28 public:
29 using Attributes = std::unordered_map<std::string,std::string>;
30 using StartDocType = void();
31 using EndDocType = void();
32 using StartElementType = void(const std::string &,const Attributes &);
33 using EndElementType = void(const std::string &);
34 using ErrorType = void(const std::string,int,const std::string &);
35 using CharsType = void(const std::string &);
36
37 std::function<StartDocType> startDocument; /**< handler invoked at the start of the document */
38 std::function<EndDocType> endDocument; /**< handler invoked at the end of the document */
39 std::function<StartElementType> startElement; /**< handler invoked when an opening tag has been found */
40 std::function<EndElementType> endElement; /**< handler invoked when a closing tag has been found */
41 std::function<CharsType> characters; /**< handler invoked when content between tags has been found */
42 std::function<ErrorType> error; /**< handler invoked when the parser encounters an error */
43
44 static std::string value(const Attributes &attrib,const std::string &key)
45 {
46 auto it = attrib.find(key);
47 if (it!=attrib.end())
48 {
49 return it->second;
50 }
51 return "";
52 }
53};
54
56{
57 public:
58 XMLLocator() = default;
59 XMLLocator(const XMLLocator &) = delete;
60 XMLLocator &operator=(const XMLLocator &) = delete;
61 XMLLocator(XMLLocator &&) = delete;
63 virtual ~XMLLocator() = default;
64
65 virtual int lineNr() const = 0;
66 virtual std::string fileName() const = 0;
67};
68
69/*! Very basic SAX style parser to parse XML documents. */
70class XMLParser : public XMLLocator
71{
72 public:
73 /*! Creates an instance of the parser object. Different instances can run on different
74 * threads without interference.
75 *
76 * @param handlers The event handlers passed by the client.
77 */
78 XMLParser(const XMLHandlers &handlers);
79 /*! Destructor */
80 ~XMLParser() override;
81 XMLParser(const XMLParser &) = delete;
82 XMLParser &operator=(const XMLParser &) = delete;
83 XMLParser(XMLParser &&) = delete;
85
86 using Transcode = bool(std::string &,const char *);
87
88 /*! Parses a file gives the contents of the file as a string.
89 * @param fileName the name of the file, used for error reporting.
90 * @param inputString the contents of the file as a zero terminated UTF-8 string.
91 * @param debugEnabled indicates if debugging via -d lex is enabled or not.
92 * @param debugStart hook that is to be called before starting with parsing
93 * @param debugEnd hook that is to be called after finishing with parsing
94 * @param transcoder hook that is to be called when transcoding text to UTF-8
95 */
96 void parse(const char *fileName,
97 const char *inputString,
98 bool debugEnabled,
99 std::function<void()> debugStart,
100 std::function<void()> debugEnd,
101 std::function<Transcode> transcoder =
102 [](std::string&s,const char *){ return true; }
103 );
104
105 private:
106 int lineNr() const override;
107 std::string fileName() const override;
108 struct Private;
109 std::unique_ptr<Private> p;
110};
111
112#endif
Event handlers that can installed by the client and called while parsing a XML document.
Definition xml.h:27
std::unordered_map< std::string, std::string > Attributes
Definition xml.h:29
void(const std::string &) CharsType
Definition xml.h:35
void() EndDocType
Definition xml.h:31
std::function< EndElementType > endElement
handler invoked when a closing tag has been found
Definition xml.h:40
void(const std::string &, const Attributes &) StartElementType
Definition xml.h:32
std::function< StartElementType > startElement
handler invoked when an opening tag has been found
Definition xml.h:39
std::function< EndDocType > endDocument
handler invoked at the end of the document
Definition xml.h:38
void(const std::string, int, const std::string &) ErrorType
Definition xml.h:34
std::function< CharsType > characters
handler invoked when content between tags has been found
Definition xml.h:41
void() StartDocType
Definition xml.h:30
void(const std::string &) EndElementType
Definition xml.h:33
static std::string value(const Attributes &attrib, const std::string &key)
Definition xml.h:44
std::function< ErrorType > error
handler invoked when the parser encounters an error
Definition xml.h:42
std::function< StartDocType > startDocument
handler invoked at the start of the document
Definition xml.h:37
virtual ~XMLLocator()=default
XMLLocator & operator=(XMLLocator &&)=delete
XMLLocator()=default
virtual int lineNr() const =0
XMLLocator & operator=(const XMLLocator &)=delete
XMLLocator(XMLLocator &&)=delete
XMLLocator(const XMLLocator &)=delete
virtual std::string fileName() const =0
XMLParser & operator=(XMLParser &&)=delete
int lineNr() const override
Definition xml.l:501
bool(std::string &, const char *) Transcode
Definition xml.h:86
std::unique_ptr< Private > p
Definition xml.h:109
XMLParser & operator=(const XMLParser &)=delete
std::string fileName() const override
Definition xml.l:507
XMLParser(XMLParser &&)=delete
XMLParser(const XMLHandlers &handlers)
Definition xml.l:436
XMLParser(const XMLParser &)=delete
void parse(const char *fileName, const char *inputString, bool debugEnabled, std::function< void()> debugStart, std::function< void()> debugEnd, std::function< Transcode > transcoder=[](std::string &s, const char *){ return true;})
Definition xml.l:447
~XMLParser() override
Definition xml.l:442