MPD  0.20.15
ExpatParser.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_EXPAT_HXX
21 #define MPD_EXPAT_HXX
22 
23 #include "check.h"
24 #include "Compiler.h"
25 
26 #include <expat.h>
27 
28 #include <stdexcept>
29 
30 class InputStream;
31 
32 class ExpatError final : public std::runtime_error {
33 public:
34  explicit ExpatError(XML_Error code)
35  :std::runtime_error(XML_ErrorString(code)) {}
36 
37  explicit ExpatError(XML_Parser parser)
38  :ExpatError(XML_GetErrorCode(parser)) {}
39 };
40 
42  char separator;
43 };
44 
45 class ExpatParser final {
46  const XML_Parser parser;
47 
48 public:
49  explicit ExpatParser(void *userData)
50  :parser(XML_ParserCreate(nullptr)) {
51  XML_SetUserData(parser, userData);
52  }
53 
55  :parser(XML_ParserCreateNS(nullptr, ns.separator)) {
56  XML_SetUserData(parser, userData);
57  }
58 
60  XML_ParserFree(parser);
61  }
62 
63  ExpatParser(const ExpatParser &) = delete;
64  ExpatParser &operator=(const ExpatParser &) = delete;
65 
66  void SetElementHandler(XML_StartElementHandler start,
67  XML_EndElementHandler end) noexcept {
68  XML_SetElementHandler(parser, start, end);
69  }
70 
71  void SetCharacterDataHandler(XML_CharacterDataHandler charhndl) noexcept {
72  XML_SetCharacterDataHandler(parser, charhndl);
73  }
74 
75  void Parse(const char *data, size_t length, bool is_final);
76 
77  void Parse(InputStream &is);
78 
79  gcc_pure
80  static const char *GetAttribute(const XML_Char **atts,
81  const char *name) noexcept;
82 
83  gcc_pure
84  static const char *GetAttributeCase(const XML_Char **atts,
85  const char *name) noexcept;
86 };
87 
93  ExpatParser parser;
94 
95 public:
96  CommonExpatParser():parser(this) {
99  }
100 
102  :parser(ns, this) {
105  }
106 
107  void Parse(const char *data, size_t length, bool is_final) {
108  parser.Parse(data, length, is_final);
109  }
110 
111  void Parse(InputStream &is) {
112  parser.Parse(is);
113  }
114 
115  gcc_pure
116  static const char *GetAttribute(const XML_Char **atts,
117  const char *name) noexcept {
118  return ExpatParser::GetAttribute(atts, name);
119  }
120 
121  gcc_pure
122  static const char *GetAttributeCase(const XML_Char **atts,
123  const char *name) noexcept {
124  return ExpatParser::GetAttributeCase(atts, name);
125  }
126 
127 protected:
128  virtual void StartElement(const XML_Char *name,
129  const XML_Char **atts) = 0;
130  virtual void EndElement(const XML_Char *name) = 0;
131  virtual void CharacterData(const XML_Char *s, int len) = 0;
132 
133 private:
134  static void XMLCALL StartElement(void *user_data, const XML_Char *name,
135  const XML_Char **atts) {
136  CommonExpatParser &p = *(CommonExpatParser *)user_data;
137  p.StartElement(name, atts);
138  }
139 
140  static void XMLCALL EndElement(void *user_data, const XML_Char *name) {
141  CommonExpatParser &p = *(CommonExpatParser *)user_data;
142  p.EndElement(name);
143  }
144 
145  static void XMLCALL CharacterData(void *user_data,
146  const XML_Char *s, int len) {
147  CommonExpatParser &p = *(CommonExpatParser *)user_data;
148  p.CharacterData(s, len);
149  }
150 };
151 
152 #endif
CommonExpatParser(ExpatNamespaceSeparator ns)
A specialization of ExpatParser that provides the most common callbacks as virtual methods...
Definition: ExpatParser.hxx:92
virtual void StartElement(const XML_Char *name, const XML_Char **atts)=0
static gcc_pure const char * GetAttribute(const XML_Char **atts, const char *name) noexcept
void Parse(const char *data, size_t length, bool is_final)
virtual void EndElement(const XML_Char *name)=0
ExpatParser & operator=(const ExpatParser &)=delete
ExpatParser(ExpatNamespaceSeparator ns, void *userData)
Definition: ExpatParser.hxx:54
void SetElementHandler(XML_StartElementHandler start, XML_EndElementHandler end) noexcept
Definition: ExpatParser.hxx:66
virtual void CharacterData(const XML_Char *s, int len)=0
void Parse(InputStream &is)
void Parse(const char *data, size_t length, bool is_final)
static gcc_pure const char * GetAttributeCase(const XML_Char **atts, const char *name) noexcept
static gcc_pure const char * GetAttributeCase(const XML_Char **atts, const char *name) noexcept
ExpatError(XML_Error code)
Definition: ExpatParser.hxx:34
static gcc_pure const char * GetAttribute(const XML_Char **atts, const char *name) noexcept
ExpatParser(void *userData)
Definition: ExpatParser.hxx:49
#define gcc_pure
Definition: Compiler.h:116
void SetCharacterDataHandler(XML_CharacterDataHandler charhndl) noexcept
Definition: ExpatParser.hxx:71
ExpatError(XML_Parser parser)
Definition: ExpatParser.hxx:37
const Partition const char * name
Definition: Count.hxx:34