summaryrefslogtreecommitdiff
path: root/libs/pbd/xml++.cc
blob: 5507f658e9329f5ea6e751937c96856f2b183568 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* xml++.cc
 * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
 * are covered by the GNU Lesser General Public License, which should be
 * included with libxml++ as the file COPYING.
 */

#include <pbd/xml++.h>
#include <libxml/debugXML.h>

static XMLNode *readnode(xmlNodePtr);
static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int);

XMLTree::XMLTree() 
	: _filename(), 
	_root(0), 
	_compression(0)
{ 
}

XMLTree::XMLTree(const string &fn)
	: _filename(fn), 
	_root(0), 
	_compression(0)
{ 
	read(); 
}

XMLTree::XMLTree(const XMLTree * from)
{
	_filename = from->filename();
	_root = new XMLNode(*from->root());
	_compression = from->compression();
}

XMLTree::~XMLTree()
{
	if (_root) {
		delete _root;
	}
}

int 
XMLTree::set_compression(int c)
{
	if (c > 9) {
	    	c = 9;
	} else if (c < 0) {
	    	c = 0;
	}
	
	_compression = c;
	
	return _compression;
}

bool 
XMLTree::read(void)
{
	xmlDocPtr doc;
	
	if (_root) {
		delete _root;
		_root = 0;
	}
	
	xmlKeepBlanksDefault(0);
	
	doc = xmlParseFile(_filename.c_str());
	if (!doc) {
	 	return false;
	}
	
	_root = readnode(xmlDocGetRootElement(doc));
	xmlFreeDoc(doc);
	
	return true;
}

bool 
XMLTree::read_buffer(const string & buffer)
{
	xmlDocPtr doc;
	
	_filename = "";
	
	if (_root) {
		delete _root;
		_root = 0;
	}
	
	doc = xmlParseMemory((char *) buffer.c_str(), buffer.length());
	if (!doc) {
		return false;
	}
	
	_root = readnode(xmlDocGetRootElement(doc));
	xmlFreeDoc(doc);
	
	return true;
}

bool 
XMLTree::write(void) const
{
	xmlDocPtr doc;
	XMLNodeList children;
	int result;
	
	xmlKeepBlanksDefault(0);
	doc = xmlNewDoc((xmlChar *) "1.0");
	xmlSetDocCompressMode(doc, _compression);
	writenode(doc, _root, doc->children, 1);
	result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
	xmlFreeDoc(doc);
	
	if (result == -1) {
		return false;
	}
	
	return true;
}

void
XMLTree::debug(FILE* out) const
{
    xmlDocPtr doc;
    XMLNodeList children;

    xmlKeepBlanksDefault(0);
    doc = xmlNewDoc((xmlChar *) "1.0");
    xmlSetDocCompressMode(doc, _compression);
    writenode(doc, _root, doc->children, 1);
    xmlDebugDumpDocument (out, doc);
    xmlFreeDoc(doc);
}

const string & 
XMLTree::write_buffer(void) const
{
	static string retval;
	char *ptr;
	int len;
	xmlDocPtr doc;
	XMLNodeList children;
	
	xmlKeepBlanksDefault(0);
	doc = xmlNewDoc((xmlChar *) "1.0");
	xmlSetDocCompressMode(doc, _compression);
	writenode(doc, _root, doc->children, 1);
	xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
	xmlFreeDoc(doc);
	
	retval = ptr;
	
	free(ptr);
	
	return retval;
}

XMLNode::XMLNode(const string & n)
	:  _name(n), _is_content(false), _content(string())
{
}

XMLNode::XMLNode(const string & n, const string & c)
	:_name(n), _is_content(true), _content(c)
{
}

XMLNode::XMLNode(const XMLNode& from)
{
	XMLPropertyList props;
	XMLPropertyIterator curprop;
	XMLNodeList nodes;
	XMLNodeIterator curnode;
	
	_name = from.name();
	set_content(from.content());
	
	props = from.properties();
	for (curprop = props.begin(); curprop != props.end(); ++curprop) {
		add_property((*curprop)->name().c_str(), (*curprop)->value());
	}
	
	nodes = from.children();
	for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
		add_child_copy(**curnode);
	}
}

XMLNode::~XMLNode()
{
	XMLNodeIterator curchild;
	XMLPropertyIterator curprop;
	
	for (curchild = _children.begin(); curchild != _children.end();	++curchild) {
		delete *curchild;
	}
	    
	for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
		delete *curprop;
	}
}

const string & 
XMLNode::set_content(const string & c)
{
	if (c.empty()) {
		_is_content = false;
	} else {
		_is_content = true;
	}
	    
	_content = c;
	
	return _content;
}

XMLNode*
XMLNode::child (const char *name) const
{
	/* returns first child matching name */

	XMLNodeConstIterator cur;
	
	if (name == 0) {
		return 0;
	}
	    
	for (cur = _children.begin(); cur != _children.end(); ++cur) {
		if ((*cur)->name() == name) {
			return *cur;
		}
	}
	    
	return 0;
}

const XMLNodeList & 
XMLNode::children(const string& n) const
{
	/* returns all children matching name */

	static XMLNodeList retval;
	XMLNodeConstIterator cur;
	
	if (n.empty()) {
		return _children;
	}
	    
	retval.erase(retval.begin(), retval.end());
	
	for (cur = _children.begin(); cur != _children.end(); ++cur) {
		if ((*cur)->name() == n) {
			retval.insert(retval.end(), *cur);
		}
	}
	    
	return retval;
}

XMLNode *
XMLNode::add_child(const char * n)
{
	return add_child_copy(XMLNode (n));
}

void
XMLNode::add_child_nocopy (XMLNode& n)
{
	_children.insert(_children.end(), &n);
}

XMLNode *
XMLNode::add_child_copy(const XMLNode& n)
{
	XMLNode *copy = new XMLNode (n);
	_children.insert(_children.end(), copy);
	return copy;
}

XMLNode *
XMLNode::add_content(const string & c)
{
	return add_child_copy(XMLNode (string(), c));
}

XMLProperty *
XMLNode::property(const char * n)
{
	string ns(n);
	map<string,XMLProperty*>::iterator iter;

	if ((iter = _propmap.find(ns)) != _propmap.end()) {
		return iter->second;
	}

	return 0;
}

XMLProperty *
XMLNode::property(const string & ns)
{
	map<string,XMLProperty*>::iterator iter;

	if ((iter = _propmap.find(ns)) != _propmap.end()) {
		return iter->second;
	}
	
	return 0;
}

XMLProperty *
XMLNode::add_property(const char * n, const string & v)
{
	string ns(n);
	if(_propmap.find(ns) != _propmap.end()){
		remove_property(ns);
	}

	XMLProperty *tmp = new XMLProperty(ns, v);

	if (!tmp) {
		return 0;
	}

	_propmap[tmp->name()] = tmp;
	_proplist.insert(_proplist.end(), tmp);

	return tmp;
}

XMLProperty *
XMLNode::add_property(const char * n, const char * v)
{
	string vs(v);
	return add_property(n, vs);
}

void 
XMLNode::remove_property(const string & n)
{
	if (_propmap.find(n) != _propmap.end()) {
		_proplist.remove(_propmap[n]);
		_propmap.erase(n);
	}
}

void 
XMLNode::remove_nodes(const string & n)
{
	XMLNodeIterator i = _children.begin();
	XMLNodeIterator tmp;
	
	while (i != _children.end()) {
		tmp = i;
		++tmp;
		if ((*i)->name() == n) {
			_children.erase (i);
		}
		i = tmp;
	}
}

void 
XMLNode::remove_nodes_and_delete(const string & n)
{
	XMLNodeIterator i = _children.begin();
	XMLNodeIterator tmp;
	
	while (i != _children.end()) {
		tmp = i;
		++tmp;
		if ((*i)->name() == n) {
			delete *i;
			_children.erase (i);
		}
		i = tmp;
	}
}

XMLProperty::XMLProperty(const string &n, const string &v)
	: _name(n), 
	_value(v) 
{ 
}

XMLProperty::~XMLProperty()
{
}

static XMLNode *
readnode(xmlNodePtr node)
{
	string name, content;
	xmlNodePtr child;
	XMLNode *tmp;
	xmlAttrPtr attr;
	
	if (node->name) {
		name = (char *) node->name;
	}
	
	tmp = new XMLNode(name);
	
	for (attr = node->properties; attr; attr = attr->next) {
		content = "";
		if (attr->children) {
			content = (char *) attr->children->content;
		}
		tmp->add_property((char *) attr->name, content);
	}
	
	if (node->content) {
		tmp->set_content((char *) node->content);
	} else {
		tmp->set_content(string());
	}
	
	for (child = node->children; child; child = child->next) {
		tmp->add_child_nocopy (*readnode(child));
	}
	
	return tmp;
}

static void 
writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0)
{
	XMLPropertyList props;
	XMLPropertyIterator curprop;
	XMLNodeList children;
	XMLNodeIterator curchild;
	xmlNodePtr node;
	
	if (root) {
		node = doc->children = xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0);
	} else {
		node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0);
	}
	    
	if (n->is_content()) {
		node->type = XML_TEXT_NODE;
		xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(), n->content().length());
	}
	
	props = n->properties();
	for (curprop = props.begin(); curprop != props.end(); ++curprop) {
		xmlSetProp(node, (xmlChar *) (*curprop)->name().c_str(), (xmlChar *) (*curprop)->value().c_str());
	}
	    
	children = n->children();
	for (curchild = children.begin(); curchild != children.end(); ++curchild) {
		writenode(doc, *curchild, node);
	}
}