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.
10 #include "pbd/stacktrace.h"
11 #include "pbd/xml++.h"
13 #include <libxml/debugXML.h>
14 #include <libxml/xpath.h>
15 #include <libxml/xpathInternals.h>
17 xmlChar* xml_version = xmlCharStrdup("1.0");
21 static XMLNode* readnode(xmlNodePtr);
22 static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int);
23 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
33 XMLTree::XMLTree(const string& fn, bool validate)
39 read_internal(validate);
42 XMLTree::XMLTree(const XMLTree* from)
43 : _filename(from->filename())
44 , _root(new XMLNode(*from->root()))
45 , _doc (xmlCopyDoc (from->_doc, 1))
46 , _compression(from->compression())
61 XMLTree::set_compression(int c)
75 XMLTree::read_internal(bool validate)
77 //shouldnt be used anywhere ATM, remove if so!
88 /* create a parser context */
89 xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
94 xmlKeepBlanksDefault(0);
95 /* parse the file, activating the DTD validation option */
97 _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
99 _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_HUGE);
102 /* check if parsing suceeded */
104 xmlFreeParserCtxt(ctxt);
107 /* check if validation suceeded */
108 if (validate && ctxt->valid == 0) {
109 xmlFreeParserCtxt(ctxt);
110 throw XMLException("Failed to validate document " + _filename);
114 _root = readnode(xmlDocGetRootElement(_doc));
116 /* free up the parser context */
117 xmlFreeParserCtxt(ctxt);
123 XMLTree::read_buffer(const string& buffer)
132 doc = xmlParseMemory(const_cast<char*>(buffer.c_str()), buffer.length());
137 _root = readnode(xmlDocGetRootElement(doc));
145 XMLTree::write() const
148 XMLNodeList children;
151 xmlKeepBlanksDefault(0);
152 doc = xmlNewDoc(xml_version);
153 xmlSetDocCompressMode(doc, _compression);
154 writenode(doc, _root, doc->children, 1);
155 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
158 xmlErrorPtr xerr = xmlGetLastError ();
160 std::cerr << "unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
162 std::cerr << "xmlSaveFormatFileEnc: error"
163 << " domain: " << xerr->domain
164 << " code: " << xerr->code
165 << " msg: " << xerr->message
180 XMLTree::debug(FILE* out) const
182 #ifdef LIBXML_DEBUG_ENABLED
184 XMLNodeList children;
186 xmlKeepBlanksDefault(0);
187 doc = xmlNewDoc(xml_version);
188 xmlSetDocCompressMode(doc, _compression);
189 writenode(doc, _root, doc->children, 1);
190 xmlDebugDumpDocument (out, doc);
196 XMLTree::write_buffer() const
198 static string retval;
202 XMLNodeList children;
204 xmlKeepBlanksDefault(0);
205 doc = xmlNewDoc(xml_version);
206 xmlSetDocCompressMode(doc, _compression);
207 writenode(doc, _root, doc->children, 1);
208 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
218 XMLNode::XMLNode(const string& n)
224 XMLNode::XMLNode(const string& n, const string& c)
231 XMLNode::XMLNode(const XMLNode& from)
242 XMLNode::clear_lists ()
244 XMLNodeIterator curchild;
245 XMLPropertyIterator curprop;
247 _selected_children.clear ();
250 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
256 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
264 XMLNode::operator= (const XMLNode& from)
268 XMLPropertyList props;
269 XMLPropertyIterator curprop;
271 XMLNodeIterator curnode;
276 set_content(from.content());
278 props = from.properties();
279 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
280 add_property((*curprop)->name().c_str(), (*curprop)->value());
283 nodes = from.children();
284 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
285 add_child_copy(**curnode);
293 XMLNode::set_content(const string& c)
307 XMLNode::child (const char* name) const
309 /* returns first child matching name */
311 XMLNodeConstIterator cur;
317 for (cur = _children.begin(); cur != _children.end(); ++cur) {
318 if ((*cur)->name() == name) {
327 XMLNode::children(const string& n) const
329 /* returns all children matching name */
331 XMLNodeConstIterator cur;
337 _selected_children.clear();
339 for (cur = _children.begin(); cur != _children.end(); ++cur) {
340 if ((*cur)->name() == n) {
341 _selected_children.insert(_selected_children.end(), *cur);
345 return _selected_children;
349 XMLNode::add_child(const char* n)
351 return add_child_copy(XMLNode (n));
355 XMLNode::add_child_nocopy(XMLNode& n)
357 _children.insert(_children.end(), &n);
361 XMLNode::add_child_copy(const XMLNode& n)
363 XMLNode *copy = new XMLNode(n);
364 _children.insert(_children.end(), copy);
368 boost::shared_ptr<XMLSharedNodeList>
369 XMLTree::find(const string xpath, XMLNode* node) const
371 xmlXPathContext* ctxt;
375 doc = xmlNewDoc(xml_version);
376 writenode(doc, node, doc->children, 1);
377 ctxt = xmlXPathNewContext(doc);
379 ctxt = xmlXPathNewContext(_doc);
382 boost::shared_ptr<XMLSharedNodeList> result =
383 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
385 xmlXPathFreeContext(ctxt);
394 XMLNode::attribute_value()
396 XMLNodeList children = this->children();
397 assert(!_is_content);
398 assert(children.size() == 1);
399 XMLNode* child = *(children.begin());
400 assert(child->is_content());
401 return child->content();
405 XMLNode::add_content(const string& c)
407 return add_child_copy(XMLNode (string(), c));
411 XMLNode::property(const char* n) const
414 map<string,XMLProperty*>::const_iterator iter;
416 if ((iter = _propmap.find(ns)) != _propmap.end()) {
424 XMLNode::property(const string& ns) const
426 map<string,XMLProperty*>::const_iterator iter;
428 if ((iter = _propmap.find(ns)) != _propmap.end()) {
436 XMLNode::property(const char* n)
439 map<string,XMLProperty*>::iterator iter;
441 if ((iter = _propmap.find(ns)) != _propmap.end()) {
449 XMLNode::property(const string& ns)
451 map<string,XMLProperty*>::iterator iter;
453 if ((iter = _propmap.find(ns)) != _propmap.end()) {
461 XMLNode::has_property_with_value (const string& key, const string& value) const
463 map<string,XMLProperty*>::const_iterator iter = _propmap.find(key);
464 if (iter != _propmap.end()) {
465 const XMLProperty* p = (iter->second);
466 return (p && p->value() == value);
472 XMLNode::add_property(const char* n, const string& v)
475 map<string,XMLProperty*>::iterator iter;
477 if ((iter = _propmap.find(ns)) != _propmap.end()) {
478 iter->second->set_value (v);
482 XMLProperty* tmp = new XMLProperty(ns, v);
488 _propmap[tmp->name()] = tmp;
489 _proplist.insert(_proplist.end(), tmp);
495 XMLNode::add_property(const char* n, const char* v)
498 return add_property(n, vs);
502 XMLNode::add_property(const char* name, const long value)
505 snprintf(str, sizeof(str), "%ld", value);
506 return add_property(name, str);
510 XMLNode::remove_property(const string& n)
512 if (_propmap.find(n) != _propmap.end()) {
513 XMLProperty* p = _propmap[n];
514 XMLPropertyIterator i = std::find(_proplist.begin(), _proplist.end(), p);
515 if (i != _proplist.end ()) {
523 /** Remove any property with the given name from this node and its children */
525 XMLNode::remove_property_recursively(const string& n)
528 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
529 (*i)->remove_property_recursively (n);
534 XMLNode::remove_nodes(const string& n)
536 XMLNodeIterator i = _children.begin();
537 while (i != _children.end()) {
538 if ((*i)->name() == n) {
539 i = _children.erase (i);
547 XMLNode::remove_nodes_and_delete(const string& n)
549 XMLNodeIterator i = _children.begin();
551 while (i != _children.end()) {
552 if ((*i)->name() == n) {
554 i = _children.erase (i);
562 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
564 XMLNodeIterator i = _children.begin();
565 XMLProperty const * prop;
567 while (i != _children.end()) {
568 prop = (*i)->property(propname);
569 if (prop && prop->value() == val) {
571 i = _children.erase(i);
578 XMLProperty::XMLProperty(const string& n, const string& v)
582 // Normalize property name (replace '_' with '-' as old session are inconsistent)
583 for (size_t i = 0; i < _name.length(); ++i) {
584 if (_name[i] == '_') {
590 XMLProperty::~XMLProperty()
595 readnode(xmlNodePtr node)
597 string name, content;
603 name = (const char*)node->name;
606 tmp = new XMLNode(name);
608 for (attr = node->properties; attr; attr = attr->next) {
610 if (attr->children) {
611 content = (char*)attr->children->content;
613 tmp->add_property((const char*)attr->name, content);
617 tmp->set_content((char*)node->content);
619 tmp->set_content(string());
622 for (child = node->children; child; child = child->next) {
623 tmp->add_child_nocopy (*readnode(child));
630 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
632 XMLPropertyList props;
633 XMLPropertyIterator curprop;
634 XMLNodeList children;
635 XMLNodeIterator curchild;
639 node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
641 node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
644 if (n->is_content()) {
645 node->type = XML_TEXT_NODE;
646 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
649 props = n->properties();
650 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
651 xmlSetProp(node, (const xmlChar*) (*curprop)->name().c_str(), (const xmlChar*) (*curprop)->value().c_str());
654 children = n->children();
655 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
656 writenode(doc, *curchild, node);
660 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
662 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
665 xmlXPathFreeContext(ctxt);
666 xmlFreeDoc(ctxt->doc);
668 throw XMLException("Invalid XPath: " + xpath);
671 if (result->type != XPATH_NODESET) {
672 xmlXPathFreeObject(result);
673 xmlXPathFreeContext(ctxt);
674 xmlFreeDoc(ctxt->doc);
676 throw XMLException("Only nodeset result types are supported.");
679 xmlNodeSet* nodeset = result->nodesetval;
680 XMLSharedNodeList* nodes = new XMLSharedNodeList();
682 for (int i = 0; i < nodeset->nodeNr; ++i) {
683 XMLNode* node = readnode(nodeset->nodeTab[i]);
684 nodes->push_back(boost::shared_ptr<XMLNode>(node));
690 xmlXPathFreeObject(result);
695 /** Dump a node, its properties and children to a stream */
697 XMLNode::dump (ostream& s, string p) const
700 s << p << " " << content() << "\n";
702 s << p << "<" << _name;
703 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
704 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
708 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
709 (*i)->dump (s, p + " ");
712 s << p << "</" << _name << ">\n";