diff options
author | Max Kellermann <max@duempel.org> | 2014-01-09 20:26:13 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-09 20:56:00 +0100 |
commit | 2ed1c2222735ce5fccd69b7eb4595d398c3a22f6 (patch) | |
tree | ecfb31b3082d65271f9016fc0c547eb7a7ed4c96 /src | |
parent | eb23ef1747d1a6bd4f9089c874c4acc5453cfac0 (diff) | |
download | mpd-2ed1c2222735ce5fccd69b7eb4595d398c3a22f6.tar.gz mpd-2ed1c2222735ce5fccd69b7eb4595d398c3a22f6.tar.xz mpd-2ed1c2222735ce5fccd69b7eb4595d398c3a22f6.zip |
ExpatParser: add helper class CommonExpatParser
Diffstat (limited to 'src')
-rw-r--r-- | src/Expat.hxx | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Expat.hxx b/src/Expat.hxx index e0de30e7e..0ea35c618 100644 --- a/src/Expat.hxx +++ b/src/Expat.hxx @@ -60,4 +60,52 @@ private: void SetError(Error &error); }; +/** + * A specialization of #ExpatParser that provides the most common + * callbacks as virtual methods. + */ +class CommonExpatParser { + ExpatParser parser; + +public: + CommonExpatParser():parser(this) { + parser.SetElementHandler(StartElement, EndElement); + parser.SetCharacterDataHandler(CharacterData); + } + + bool Parse(InputStream &is, Error &error) { + return parser.Parse(is, error); + } + + gcc_pure + static const char *GetAttributeCase(const XML_Char **atts, + const char *name) { + return ExpatParser::GetAttributeCase(atts, name); + } + +protected: + virtual void StartElement(const XML_Char *name, + const XML_Char **atts) = 0; + virtual void EndElement(const XML_Char *name) = 0; + virtual void CharacterData(const XML_Char *s, int len) = 0; + +private: + static void XMLCALL StartElement(void *user_data, const XML_Char *name, + const XML_Char **atts) { + CommonExpatParser &p = *(CommonExpatParser *)user_data; + p.StartElement(name, atts); + } + + static void XMLCALL EndElement(void *user_data, const XML_Char *name) { + CommonExpatParser &p = *(CommonExpatParser *)user_data; + p.EndElement(name); + } + + static void XMLCALL CharacterData(void *user_data, + const XML_Char *s, int len) { + CommonExpatParser &p = *(CommonExpatParser *)user_data; + p.CharacterData(s, len); + } +}; + #endif |