MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
41 struct ExpatNamespaceSeparator {
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
54 ExpatParser(ExpatNamespaceSeparator ns, void *userData)
55 :parser(XML_ParserCreateNS(nullptr, ns.separator)) {
56 XML_SetUserData(parser, userData);
57 }
58
59 ~ExpatParser() {
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) {
68 XML_SetElementHandler(parser, start, end);
69 }
70
71 void SetCharacterDataHandler(XML_CharacterDataHandler charhndl) {
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);
82
83 gcc_pure
84 static const char *GetAttributeCase(const XML_Char **atts,
85 const char *name);
86 };
87
92 class CommonExpatParser {
93 ExpatParser parser;
94
95 public:
96 CommonExpatParser():parser(this) {
97 parser.SetElementHandler(StartElement, EndElement);
98 parser.SetCharacterDataHandler(CharacterData);
99 }
100
101 explicit CommonExpatParser(ExpatNamespaceSeparator ns)
102 :parser(ns, this) {
103 parser.SetElementHandler(StartElement, EndElement);
104 parser.SetCharacterDataHandler(CharacterData);
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) {
118 return ExpatParser::GetAttribute(atts, name);
119 }
120
121 gcc_pure
122 static const char *GetAttributeCase(const XML_Char **atts,
123 const char *name) {
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
check.h
CommonExpatParser::CommonExpatParser
CommonExpatParser(ExpatNamespaceSeparator ns)
Definition: ExpatParser.hxx:101
CommonExpatParser
A specialization of ExpatParser that provides the most common callbacks as virtual methods...
Definition: ExpatParser.hxx:92
CommonExpatParser::GetAttribute
static gcc_pure const char * GetAttribute(const XML_Char **atts, const char *name)
Definition: ExpatParser.hxx:116
ExpatError
Definition: ExpatParser.hxx:32
CommonExpatParser::StartElement
virtual void StartElement(const XML_Char *name, const XML_Char **atts)=0
ExpatParser
Definition: ExpatParser.hxx:45
CommonExpatParser::Parse
void Parse(const char *data, size_t length, bool is_final)
Definition: ExpatParser.hxx:107
CommonExpatParser::CommonExpatParser
CommonExpatParser()
Definition: ExpatParser.hxx:96
InputStream
Definition: InputStream.hxx:36
ExpatParser::GetAttributeCase
static gcc_pure const char * GetAttributeCase(const XML_Char **atts, const char *name)
ExpatParser::SetElementHandler
void SetElementHandler(XML_StartElementHandler start, XML_EndElementHandler end)
Definition: ExpatParser.hxx:66
CommonExpatParser::EndElement
virtual void EndElement(const XML_Char *name)=0
ExpatParser::operator=
ExpatParser & operator=(const ExpatParser &)=delete
ExpatNamespaceSeparator
Definition: ExpatParser.hxx:41
ExpatParser::ExpatParser
ExpatParser(ExpatNamespaceSeparator ns, void *userData)
Definition: ExpatParser.hxx:54
Compiler.h
CommonExpatParser::CharacterData
virtual void CharacterData(const XML_Char *s, int len)=0
CommonExpatParser::Parse
void Parse(InputStream &is)
Definition: ExpatParser.hxx:111
ExpatNamespaceSeparator::separator
char separator
Definition: ExpatParser.hxx:42
ExpatParser::Parse
void Parse(const char *data, size_t length, bool is_final)
ExpatParser::SetCharacterDataHandler
void SetCharacterDataHandler(XML_CharacterDataHandler charhndl)
Definition: ExpatParser.hxx:71
ExpatParser::~ExpatParser
~ExpatParser()
Definition: ExpatParser.hxx:59
CommonExpatParser::GetAttributeCase
static gcc_pure const char * GetAttributeCase(const XML_Char **atts, const char *name)
Definition: ExpatParser.hxx:122
ExpatError::ExpatError
ExpatError(XML_Error code)
Definition: ExpatParser.hxx:34
ExpatParser::ExpatParser
ExpatParser(void *userData)
Definition: ExpatParser.hxx:49
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
ExpatParser::GetAttribute
static gcc_pure const char * GetAttribute(const XML_Char **atts, const char *name)
ExpatError::ExpatError
ExpatError(XML_Parser parser)
Definition: ExpatParser.hxx:37
name
const Partition const char * name
Definition: Count.hxx:34