Doxygen
xml.h
浏览该文件的文档.
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  virtual ~XMLLocator() {}
59  virtual int lineNr() const = 0;
60  virtual std::string fileName() const = 0;
61 };
62 
63 /*! Very basic SAX style parser to parse XML documents. */
64 class XMLParser : public XMLLocator
65 {
66  public:
67  /*! Creates an instance of the parser object. Different instances can run on different
68  * threads without interference.
69  *
70  * @param handlers The event handlers passed by the client.
71  */
72  XMLParser(const XMLHandlers &handlers);
73  /*! Destructor */
74  ~XMLParser();
75 
76  /*! Parses a file gives the contents of the file as a string.
77  * @param fileName the name of the file, used for error reporting.
78  * @param inputString the contents of the file as a zero terminated UTF-8 string.
79  * @param debugEnabled indicates if debugging via -d lex is enabled or not.
80  */
81  void parse(const char *fileName,const char *inputString,bool debugEnabled);
82 
83  private:
84  virtual int lineNr() const override;
85  virtual std::string fileName() const override;
86  struct Private;
87  std::unique_ptr<Private> p;
88 };
89 
90 #endif
XMLParser
Definition: xml.h:64
XMLHandlers::startElement
std::function< StartElementType > startElement
handler invoked when an opening tag has been found
Definition: xml.h:52
XMLHandlers::Attributes
std::unordered_map< std::string, std::string > Attributes
Definition: xml.h:42
Private
@ Private
Definition: types.h:26
XMLParser::lineNr
virtual int lineNr() const override
XMLLocator::fileName
virtual std::string fileName() const =0
XMLHandlers::StartElementType
void(const std::string &, const Attributes &) StartElementType
Definition: xml.h:45
XMLParser::p
std::unique_ptr< Private > p
Definition: xml.h:86
XMLParser::fileName
virtual std::string fileName() const override
XMLHandlers::ErrorType
void(const std::string, int, const std::string &) ErrorType
Definition: xml.h:47
XMLLocator::lineNr
virtual int lineNr() const =0
XMLHandlers::error
std::function< ErrorType > error
handler invoked when the parser encounters an error
Definition: xml.h:55
XMLHandlers::StartDocType
void() StartDocType
Definition: xml.h:43
XMLHandlers::endElement
std::function< EndElementType > endElement
handler invoked when a closing tag has been found
Definition: xml.h:53
XMLLocator
Definition: xml.h:55
XMLParser::parse
void parse(const char *fileName, const char *inputString, bool debugEnabled)
XMLHandlers::endDocument
std::function< EndDocType > endDocument
handler invoked at the end of the document
Definition: xml.h:51
XMLHandlers::characters
std::function< CharsType > characters
handler invoked when content between tags has been found
Definition: xml.h:54
XMLLocator::~XMLLocator
virtual ~XMLLocator()
Definition: xml.h:58
XMLHandlers::EndElementType
void(const std::string &) EndElementType
Definition: xml.h:46
XMLParser::XMLParser
XMLParser(const XMLHandlers &handlers)
XMLHandlers
Event handlers that can installed by the client and called while parsing a XML document.
Definition: xml.h:26
XMLHandlers::CharsType
void(const std::string &) CharsType
Definition: xml.h:48
XMLHandlers::startDocument
std::function< StartDocType > startDocument
handler invoked at the start of the document
Definition: xml.h:50
XMLParser::~XMLParser
~XMLParser()
XMLHandlers::EndDocType
void() EndDocType
Definition: xml.h:44
XMLHandlers::value
static std::string value(const Attributes &attrib, const std::string &key)
Definition: xml.h:57