2 * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3 * are covered by the GNU Lesser General Public License, which should be
4 * included with libxml++ as the file COPYING.
5 * Modified for Ardour and released under the same terms.
9 #include <libxml/debugXML.h>
10 #include <libxml/xpath.h>
11 #include <libxml/xpathInternals.h>
13 #define XML_VERSION "1.0"
17 static XMLNode* readnode(xmlNodePtr);
18 static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int);
19 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
28 XMLTree::XMLTree(const string& fn, bool validate)
33 read_internal(validate);
36 XMLTree::XMLTree(const XMLTree* from)
37 : _filename(from->filename())
38 , _root(new XMLNode(*from->root()))
39 , _compression(from->compression())
49 XMLTree::set_compression(int c)
63 XMLTree::read_internal(bool validate)
65 //shouldnt be used anywhere ATM, remove if so!
71 xmlParserCtxtPtr ctxt = NULL; /* the parser context */
72 xmlDocPtr doc; /* the resulting document tree */
74 xmlKeepBlanksDefault(0);
75 /* parse the file, activating the DTD validation option */
77 /* create a parser context */
78 ctxt = xmlNewParserCtxt();
82 doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
84 doc = xmlParseFile(_filename.c_str());
87 /* check if parsing suceeded */
90 xmlFreeParserCtxt(ctxt);
94 /* check if validation suceeded */
95 if (validate && ctxt->valid == 0) {
96 xmlFreeParserCtxt(ctxt);
98 throw XMLException("Failed to validate document " + _filename);
102 _root = readnode(xmlDocGetRootElement(doc));
104 /* free up the parser context */
106 xmlFreeParserCtxt(ctxt);
114 XMLTree::read_buffer(const string& buffer)
123 doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
128 _root = readnode(xmlDocGetRootElement(doc));
136 XMLTree::write() const
139 XMLNodeList children;
142 xmlKeepBlanksDefault(0);
143 doc = xmlNewDoc((xmlChar*) XML_VERSION);
144 xmlSetDocCompressMode(doc, _compression);
145 writenode(doc, _root, doc->children, 1);
146 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
157 XMLTree::debug(FILE* out) const
160 XMLNodeList children;
162 xmlKeepBlanksDefault(0);
163 doc = xmlNewDoc((xmlChar*) XML_VERSION);
164 xmlSetDocCompressMode(doc, _compression);
165 writenode(doc, _root, doc->children, 1);
166 xmlDebugDumpDocument (out, doc);
171 XMLTree::write_buffer() const
173 static string retval;
177 XMLNodeList children;
179 xmlKeepBlanksDefault(0);
180 doc = xmlNewDoc((xmlChar*) XML_VERSION);
181 xmlSetDocCompressMode(doc, _compression);
182 writenode(doc, _root, doc->children, 1);
183 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
193 XMLNode::XMLNode(const string& n)
199 XMLNode::XMLNode(const string& n, const string& c)
206 XMLNode::XMLNode(const XMLNode& from)
208 XMLPropertyList props;
209 XMLPropertyIterator curprop;
211 XMLNodeIterator curnode;
214 set_content(from.content());
216 props = from.properties();
217 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
218 add_property((*curprop)->name().c_str(), (*curprop)->value());
221 nodes = from.children();
222 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
223 add_child_copy(**curnode);
229 XMLNodeIterator curchild;
230 XMLPropertyIterator curprop;
232 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
236 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
242 XMLNode::set_content(const string& c)
256 XMLNode::child (const char* name) const
258 /* returns first child matching name */
260 XMLNodeConstIterator cur;
266 for (cur = _children.begin(); cur != _children.end(); ++cur) {
267 if ((*cur)->name() == name) {
276 XMLNode::children(const string& n) const
278 /* returns all children matching name */
280 XMLNodeConstIterator cur;
286 _selected_children.clear();
288 for (cur = _children.begin(); cur != _children.end(); ++cur) {
289 if ((*cur)->name() == n) {
290 _selected_children.insert(_selected_children.end(), *cur);
294 return _selected_children;
298 XMLNode::add_child(const char* n)
300 return add_child_copy(XMLNode (n));
304 XMLNode::add_child_nocopy(XMLNode& n)
306 _children.insert(_children.end(), &n);
310 XMLNode::add_child_copy(const XMLNode& n)
312 XMLNode *copy = new XMLNode(n);
313 _children.insert(_children.end(), copy);
317 boost::shared_ptr<XMLSharedNodeList>
318 XMLNode::find(const string xpath) const
320 xmlDocPtr doc = xmlNewDoc((xmlChar*) XML_VERSION);
321 writenode(doc, (XMLNode*)this, doc->children, 1);
322 xmlXPathContext* ctxt = xmlXPathNewContext(doc);
324 boost::shared_ptr<XMLSharedNodeList> result =
325 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
327 xmlXPathFreeContext(ctxt);
334 XMLNode::attribute_value()
336 XMLNodeList children = this->children();
337 assert(!_is_content);
338 assert(children.size() == 1);
339 XMLNode* child = *(children.begin());
340 assert(child->is_content());
341 return child->content();
345 XMLNode::add_content(const string& c)
347 return add_child_copy(XMLNode (string(), c));
351 XMLNode::property(const char* n)
354 map<string,XMLProperty*>::iterator iter;
356 if ((iter = _propmap.find(ns)) != _propmap.end()) {
364 XMLNode::property(const string& ns)
366 map<string,XMLProperty*>::iterator iter;
368 if ((iter = _propmap.find(ns)) != _propmap.end()) {
376 XMLNode::add_property(const char* n, const string& v)
379 if (_propmap.find(ns) != _propmap.end()) {
383 XMLProperty* tmp = new XMLProperty(ns, v);
389 _propmap[tmp->name()] = tmp;
390 _proplist.insert(_proplist.end(), tmp);
396 XMLNode::add_property(const char* n, const char* v)
399 return add_property(n, vs);
403 XMLNode::add_property(const char* name, const long value)
405 static char str[1024];
406 snprintf(str, 1024, "%ld", value);
407 return add_property(name, str);
411 XMLNode::remove_property(const string& n)
413 if (_propmap.find(n) != _propmap.end()) {
414 XMLProperty* p = _propmap[n];
415 _proplist.remove (p);
422 XMLNode::remove_nodes(const string& n)
424 XMLNodeIterator i = _children.begin();
427 while (i != _children.end()) {
430 if ((*i)->name() == n) {
438 XMLNode::remove_nodes_and_delete(const string& n)
440 XMLNodeIterator i = _children.begin();
443 while (i != _children.end()) {
446 if ((*i)->name() == n) {
455 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
457 XMLNodeIterator i = _children.begin();
461 while (i != _children.end()) {
465 prop = (*i)->property(propname);
466 if (prop && prop->value() == val) {
475 XMLProperty::XMLProperty(const string& n, const string& v)
479 // Normalize property name (replace '_' with '-' as old session are inconsistent)
480 for (size_t i = 0; i < _name.length(); ++i) {
481 if (_name[i] == '_') {
487 XMLProperty::~XMLProperty()
492 readnode(xmlNodePtr node)
494 string name, content;
500 name = (char*)node->name;
503 tmp = new XMLNode(name);
505 for (attr = node->properties; attr; attr = attr->next) {
507 if (attr->children) {
508 content = (char*)attr->children->content;
510 tmp->add_property((char*)attr->name, content);
514 tmp->set_content((char*)node->content);
516 tmp->set_content(string());
519 for (child = node->children; child; child = child->next) {
520 tmp->add_child_nocopy (*readnode(child));
527 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
529 XMLPropertyList props;
530 XMLPropertyIterator curprop;
531 XMLNodeList children;
532 XMLNodeIterator curchild;
536 node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0);
538 node = xmlNewChild(p, 0, (xmlChar*) n->name().c_str(), 0);
541 if (n->is_content()) {
542 node->type = XML_TEXT_NODE;
543 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
546 props = n->properties();
547 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
548 xmlSetProp(node, (xmlChar*) (*curprop)->name().c_str(), (xmlChar*) (*curprop)->value().c_str());
551 children = n->children();
552 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
553 writenode(doc, *curchild, node);
557 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
559 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
562 xmlXPathFreeContext(ctxt);
563 xmlFreeDoc(ctxt->doc);
565 throw XMLException("Invalid XPath: " + xpath);
568 if (result->type != XPATH_NODESET) {
569 xmlXPathFreeObject(result);
570 xmlXPathFreeContext(ctxt);
571 xmlFreeDoc(ctxt->doc);
573 throw XMLException("Only nodeset result types are supported.");
576 xmlNodeSet* nodeset = result->nodesetval;
577 XMLSharedNodeList* nodes = new XMLSharedNodeList();
579 for (int i = 0; i < nodeset->nodeNr; ++i) {
580 XMLNode* node = readnode(nodeset->nodeTab[i]);
581 nodes->push_back(boost::shared_ptr<XMLNode>(node));
587 xmlXPathFreeObject(result);