summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/XMLParser.hpp
blob: 51f6a62544ce8cf42526b795b1b3e57f2e5c1a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************************************
** XMLParser.hpp                                                        **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2022 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#ifndef XMLPARSER_HPP
#define XMLPARSER_HPP

#include <string>
#include "MessageException.hpp"
#include "SVGTree.hpp"

struct XMLParserException : MessageException {
	explicit XMLParserException (const std::string &msg) : MessageException(msg) {}
};

class XMLParser {
	using AppendFunc = void (SVGTree::*)(std::unique_ptr<XMLNode>);
	using PushFunc = void (SVGTree::*)(std::unique_ptr<SVGElement>);
	using PopFunc = void (SVGTree::*)();
	using NameStack = std::vector<std::string>;

	public:
		XMLParser (AppendFunc append, PushFunc push, PopFunc pop)
				: _append(append), _pushContext(push), _popContext(pop) {}

		void parse (const std::string &xml, SVGTree &svgTree, bool finish=false);
		void finish (SVGTree &svgTree);

	protected:
		void openElement (const std::string &tag, SVGTree &svgTree);
		void closeElement (const std::string &tag, SVGTree &svgTree);

	private:
		AppendFunc _append;
		PushFunc _pushContext;
		PopFunc _popContext;
		std::string _xmlbuf;
		NameStack _nameStack;  ///< names of nested elements still missing a closing tag
		bool _error=false;
};

#endif