summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/XMLParser.cpp
blob: c1da5373a6bf28a5576445321cd010748e790098 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*************************************************************************
** XMLParser.cpp                                                        **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2023 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/>. **
*************************************************************************/

#include "InputReader.hpp"
#include "XMLParser.hpp"

using namespace std;

XMLElement* XMLParser::setRootElement (XMLElement *root) {
	_elementStack.clear();
	if (root)
		_elementStack.push_back(root);
	else {
		_root = util::make_unique<XMLElement>("root");
		_elementStack.push_back(_root.get());
	}
	return _elementStack.back();
}


/** Parses an XML fragment from an input stream. */
void XMLParser::parse (istream &is) {
	while (is) {
		string buf;
		buf.resize(1024);
		is.read(&buf[0], 1024);
		buf.resize(is.gcount());
		parse(std::move(buf));
	}
	finish();
}


static string::size_type find_end_of_tag (const string &str, string::size_type startpos) {
	char attrval_delim = 0;
	bool expect_attrval = false;
	for (auto i=startpos; i < str.length(); i++) {
		if (attrval_delim) {  // inside attrubute value?
			if (str[i] == attrval_delim)  // end of attribute value?
				attrval_delim = 0;
		}
		else if (str[i] == '>')
			return i;
		else if (str[i] == '"' || str[i] == '\'') { // start of attribute value?
			if (expect_attrval)
				attrval_delim = str[i];
			else {
				ostringstream oss;
				oss << "misplaced " << str[i] << " inside tag";
				throw XMLParserException(oss.str());
			}
		}
		else if (str[i] == '=') {
			expect_attrval = true;
			continue;
		}
		else if (str[i] == '<')
			throw XMLParserException("invalid '<' inside tag");
		expect_attrval = false;
	}
	return string::npos;
}


/** Parses a fragment of XML code, creates corresponding XML nodes and adds them
 *  to an SVG tree. The code may be split and processed by several calls of this
 *  function. Incomplete chunks that can't be processed yet are stored and picked
 *  up again together with the next incoming XML fragment. If a call of this function
 *  is supposed to finish the parsing of an XML subtree, parameter 'finish' must be set.
 *  @param[in] xml XML fragment to parse
 *  @param[in] finish if true, no more XML is expected and parsing is finished */
void XMLParser::parse (string xml, bool finish) {
	if (_elementStack.empty())  // no root element set?
		return;
	// collect/extract an XML fragment that only contains complete tags
	// incomplete tags are held back
	if (_xmlbuf.empty())
		_xmlbuf.assign(std::move(xml));
	else
		_xmlbuf.append(xml);
	string::size_type left=0;
	try {
		while (left != string::npos) {
			auto right = _xmlbuf.find('<', left);
			if (left < right && left < _xmlbuf.length())  {// plain text found?
				string text = (right == string::npos ? _xmlbuf.substr(left) : _xmlbuf.substr(left, right-left));
				appendNode(util::make_unique<XMLText>(std::move(text)));
			}
			if (right != string::npos) {
				left = right;
				if (_xmlbuf.compare(left, 9, "<![CDATA[") == 0) {
					right = _xmlbuf.find("]]>", left+9);
					if (right == string::npos) {
						if (finish) throw XMLParserException("expected ']]>' at end of CDATA block");
						break;
					}
					appendNode(util::make_unique<XMLCData>(_xmlbuf.substr(left+9, right-left-9)));
					right += 2;
				}
				else if (_xmlbuf.compare(left, 4, "<!--") == 0) {
					right = _xmlbuf.find("-->", left+4);
					if (right == string::npos) {
						if (finish) throw XMLParserException("expected '-->' at end of comment");
						break;
					}
					appendNode(util::make_unique<XMLComment>(_xmlbuf.substr(left+4, right-left-4)));
					right += 2;
				}
				else if (_xmlbuf.compare(left, 2, "<?") == 0) {
					right = _xmlbuf.find("?>", left+2);
					if (right == string::npos) {
						if (finish) throw XMLParserException("expected '?>' at end of processing instruction");
						break;
					}
					appendNode(util::make_unique<XMLText>(_xmlbuf.substr(left, right-left+2)));
					right++;
				}
				else if (_xmlbuf.compare(left, 2, "</") == 0) {
					right = _xmlbuf.find('>', left+2);
					if (right == string::npos) {
						if (finish) throw XMLParserException("missing '>' at end of closing XML tag");
						break;
					}
					closeElement(_xmlbuf.substr(left+2, right-left-2));
				}
				else {
					right = find_end_of_tag(_xmlbuf, left+1);
					if (right == string::npos) {
						if (finish)	throw XMLParserException("missing '>' or '/>' at end of opening XML tag");
						break;
					}
					openElement(_xmlbuf.substr(left+1, right-left-1));
				}
			}
			left = right;
			if (right != string::npos)
				left++;
		}
	}
	catch (const XMLParserException &e) {
		_error = true;
		throw;
	}
	if (left == string::npos)
		_xmlbuf.clear();
	else
		_xmlbuf.erase(0, left);
}


void XMLParser::appendNode (unique_ptr<XMLNode> node) {
	context()->append(std::move(node));
}


XMLElement* XMLParser::finishPushContext (unique_ptr<XMLElement> elem) {
	XMLElement *elemPtr = elem.get();
	context()->append(std::move(elem));
	return elemPtr;
}


/** Processes an opening element tag.
 *  @param[in] tag tag without leading and trailing angle brackets */
XMLElement* XMLParser::openElement (const string &tag) {
	StringInputBuffer ib(tag);
	BufferInputReader ir(ib);
	string name = ir.getString("/ \t\n\r");
	ir.skipSpace();
	unique_ptr<XMLElement> elemNode{createElementPtr(name)};
	map<string, string> attribs;
	if (ir.parseAttributes(attribs, true, "\"'")) {
		for (const auto &attrpair : attribs)
			elemNode->addAttribute(attrpair.first, attrpair.second);
	}
	ir.skipSpace();
	XMLElement *elemPtr = elemNode.get();
	if (ir.peek() == '/')       // end of empty element tag
		appendNode(std::move(elemNode));
	else if (ir.peek() < 0)   // end of opening tag
		_elementStack.push_back(finishPushContext(std::move(elemNode)));
	else
		throw XMLParserException("'>' or '/>' expected at end of opening tag <"+name);
	if (_notifyElementOpened)
		_notifyElementOpened(elemPtr);
	if (ir.peek() == '/' && _notifyElementClosed)
		_notifyElementClosed(elemPtr);
	return elemPtr;
}


/** Processes a closing element tag.
 *  @param[in] tag tag without leading and trailing angle brackets */
void XMLParser::closeElement (const string &tag) {
	StringInputBuffer ib(tag);
	BufferInputReader ir(ib);
	string name = ir.getString(" \t\n\r");
	ir.skipSpace();
	if (ir.peek() >= 0)
		throw XMLParserException("'>' expected at end of closing tag </"+name);
	if (_elementStack.size() <= 1)
		throw XMLParserException("spurious closing tag </" + name + ">");
	if (_elementStack.back()->name() != name)
		throw XMLParserException("expected </" + _elementStack.back()->name() + "> but found </" + name + ">");
	finishPopContext();
	if (_notifyElementClosed)
		_notifyElementClosed(_elementStack.back());
	_elementStack.pop_back();
}


/** Processes any remaining XML fragments, checks for missing closing tags,
 *  and resets the parser state. */
void XMLParser::finish () {
	if (!_xmlbuf.empty()) {
		if (!_error)
			parse("", true);
		_xmlbuf.clear();
	}
	string tags;
	while (_elementStack.size() > 1) {
		tags += "</" + _elementStack.back()->name() + ">, ";
		_elementStack.pop_back();
	}
	if (!tags.empty() && !_error) {
		tags.resize(tags.length()-2);  // drop trailing ", "
		throw XMLParserException("missing closing tag(s): " + tags);
	}
}


XMLElement* XMLParser::createElementPtr (std::string name) const {
	return new XMLElement(std::move(name));
}


void XMLParser::setNotifyFuncs (NotifyFunc notifyElementOpened, NotifyFunc notifyElementClosed) {
	_notifyElementOpened = std::move(notifyElementOpened);
	_notifyElementClosed = std::move(notifyElementClosed);
}