summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-0.8.7/src/XMLNode.cpp
blob: f3e79dc2f2005c3aca7b0d72d88488e9223b9d88 (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
/*************************************************************************
** XMLNode.cpp                                                          **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2009 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 "macros.h"
#include "XMLNode.h"
#include "XMLString.h"

using namespace std;

bool XMLNode::emit (ostream &os, XMLNode *stopNode) {
	if (this == stopNode)
		return false;
	write(os);
	return true;
}


XMLElementNode::XMLElementNode (const string &n) : name(n), emitted(false) {
}


XMLElementNode::~XMLElementNode () {
	FORALL(children, ElementList::iterator, i)
		delete *i;
}


void XMLElementNode::addAttribute (const string &name, const string &value) {
	attributes[name] = value;
}


void XMLElementNode::addAttribute (const string &name, double value) {
	attributes[name] = XMLString(value);
}


void XMLElementNode::append (XMLNode *child) {
	if (!child)
		return;
	XMLTextNode *textNode1 = dynamic_cast<XMLTextNode*>(child);
	if (!textNode1 || children.empty())
		children.push_back(child);
	else {
		if (XMLTextNode *textNode2 = dynamic_cast<XMLTextNode*>(children.back()))
			textNode2->append(textNode1);  // merge two consecutive text nodes
		else
			children.push_back(child);
	}
}


void XMLElementNode::append (const string &str) {
	if (children.empty() || !dynamic_cast<XMLTextNode*>(children.back()))
		children.push_back(new XMLTextNode(str));
	else
		static_cast<XMLTextNode*>(children.back())->append(str);
}


void XMLElementNode::prepend (XMLNode *child) {
	if (!child)
		return;
	XMLTextNode *textNode1 = dynamic_cast<XMLTextNode*>(child);
	if (!textNode1 || children.empty())
		children.push_front(child);
	else {
		if (XMLTextNode *textNode2 = dynamic_cast<XMLTextNode*>(children.back()))
			textNode2->prepend(textNode1);  // merge two consecutive text nodes
		else
			children.push_front(child);
	}
}


ostream& XMLElementNode::write (ostream &os) const {
	os << '<' << name;
	FORALL(attributes, AttribMap::const_iterator, i)
		os << ' ' << i->first << "='" << i->second << '\'';
	if (children.empty())
		os << "/>\n";
	else {
		os << '>';
		if (dynamic_cast<XMLElementNode*>(children.front()))
			os << '\n';
		FORALL(children, ElementList::const_iterator, i)
			(*i)->write(os);
		os << "</" << name << ">\n";
	}
	return os;
}


/** Writes a part of the XML tree to the given output stream and removes
 *  the completely written nodes. The output stops when a stop node is reached
 *  (this node won't be printed at all). If a node was only partly emitted, i.e.
 *  its child was the stop node, a further call of emit will continue the output.
 *  @param[in] os stream to which the output is sent to
 *  @param[in] stopElement node where emitting stops (if 0 the whole tree will be emitted)
 *  @return true if node was completely emitted
 * */
bool XMLElementNode::emit (ostream &os, XMLNode *stopNode) {
	if (this == stopNode)
		return false;

	if (!emitted) {
		os << '<' << name;
		FORALL(attributes, AttribMap::iterator, i)
			os << ' ' << i->first << "='" << i->second << '\'';
		if (children.empty())
			os << "/>\n";
		else {
			os << '>';
			if (dynamic_cast<XMLElementNode*>(children.front()))
				os << '\n';
		}

		emitted = true;
	}
	if (!children.empty()) {
		FORALL(children, ElementList::iterator, i) {
			if ((*i)->emit(os, stopNode)) {
				ElementList::iterator it = i++;  // prevent i from being invalidated...
				children.erase(it);              // ... by erase
				--i;  // @@ what happens if i points to first child?
			}
			else
				return false;
		}
		os << "</" << name << ">\n";
	}
	return true;
}


bool XMLElementNode::hasAttribute (const string &name) const {
	return attributes.find(name) != attributes.end();
}


//////////////////////

void XMLTextNode::append (XMLNode *node) {
	if (XMLTextNode *tn = dynamic_cast<XMLTextNode*>(node))
		append(tn);
	else
		delete node;
}


void XMLTextNode::append (XMLTextNode *node) {
	if (node)
		text += node->text;
	delete node;
}


void XMLTextNode::append (const string &str) {
	text += str;
}


void XMLTextNode::prepend (XMLNode *node) {
	if (XMLTextNode *tn = dynamic_cast<XMLTextNode*>(node))
		prepend(tn);
	else
		delete node;
}


//////////////////////

XMLDeclarationNode::XMLDeclarationNode (const string &n, const string &p)
	: name(n), params(p), emitted(false)
{
}


XMLDeclarationNode::~XMLDeclarationNode () {
	FORALL(children, list<XMLDeclarationNode*>::iterator, i)
		delete *i;
}

void XMLDeclarationNode::append (XMLDeclarationNode *child) {
	if (child)
		children.push_back(child);
}

ostream& XMLDeclarationNode::write (ostream &os) const {
	os << "<!" << name << ' ' << params;
	if (children.empty())
		os << ">\n";
	else {
		os << "[\n";
		FORALL(children, list<XMLDeclarationNode*>::const_iterator, i)
			(*i)->write(os);
		os << "]>\n";
	}
	return os;
}


bool XMLDeclarationNode::emit (ostream &os, XMLNode *stopNode) {
	if (this == stopNode)
		return false;

	if (!emitted) {
		os << "<!" << name << ' ' << params;
		if (children.empty())
			os << ">\n";
		else
			os << "[\n";
		emitted = true;
	}
	if (!children.empty()) {
		FORALL(children, list<XMLDeclarationNode*>::iterator, i) {
			if ((*i)->emit(os, stopNode)) {
				list<XMLDeclarationNode*>::iterator it = i++;  // prevent i from being invalidated...
				children.erase(it);              // ... by erase
				--i;  // @@ what happens if i points to first child?
			}
			else
				return false;
		}
		os << "]>\n";
	}
	return true;

}

//////////////////////

ostream& XMLCDataNode::write (ostream &os) const {
	os << "<![CDATA[\n"
		<< data
		<< "]]>\n";
	return os;
}