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 ();
249 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
255 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
263 XMLNode::operator= (const XMLNode& from)
271 _name = from.name ();
272 set_content (from.content ());
274 const XMLPropertyList& props = from.properties ();
276 for (XMLPropertyConstIterator prop_iter = props.begin (); prop_iter != props.end (); ++prop_iter) {
277 add_property ((*prop_iter)->name ().c_str (), (*prop_iter)->value ());
280 const XMLNodeList& nodes = from.children ();
281 for (XMLNodeConstIterator child_iter = nodes.begin (); child_iter != nodes.end (); ++child_iter) {
282 add_child_copy (**child_iter);
289 XMLNode::operator== (const XMLNode& other) const
291 if (is_content () != other.is_content ()) {
296 if (content () != other.content ()) {
300 if (name () != other.name ()) {
305 XMLPropertyList const& other_properties = other.properties ();
307 if (_proplist.size () != other_properties.size ()) {
311 XMLPropertyConstIterator our_prop_iter = _proplist.begin();
312 XMLPropertyConstIterator other_prop_iter = other_properties.begin();
314 while (our_prop_iter != _proplist.end ()) {
315 XMLProperty const* our_prop = *our_prop_iter;
316 XMLProperty const* other_prop = *other_prop_iter;
317 if (our_prop->name () != other_prop->name () || our_prop->value () != other_prop->value ()) {
324 XMLNodeList const& other_children = other.children();
326 if (_children.size() != other_children.size()) {
330 XMLNodeConstIterator our_child_iter = _children.begin ();
331 XMLNodeConstIterator other_child_iter = other_children.begin ();
333 while (our_child_iter != _children.end()) {
334 XMLNode const* our_child = *our_child_iter;
335 XMLNode const* other_child = *other_child_iter;
337 if (*our_child != *other_child) {
347 XMLNode::operator!= (const XMLNode& other) const
349 return !(*this == other);
353 XMLNode::set_content(const string& c)
367 XMLNode::child (const char* name) const
369 /* returns first child matching name */
371 XMLNodeConstIterator cur;
377 for (cur = _children.begin(); cur != _children.end(); ++cur) {
378 if ((*cur)->name() == name) {
387 XMLNode::children(const string& n) const
389 /* returns all children matching name */
391 XMLNodeConstIterator cur;
397 _selected_children.clear();
399 for (cur = _children.begin(); cur != _children.end(); ++cur) {
400 if ((*cur)->name() == n) {
401 _selected_children.insert(_selected_children.end(), *cur);
405 return _selected_children;
409 XMLNode::add_child(const char* n)
411 return add_child_copy(XMLNode (n));
415 XMLNode::add_child_nocopy(XMLNode& n)
417 _children.insert(_children.end(), &n);
421 XMLNode::add_child_copy(const XMLNode& n)
423 XMLNode *copy = new XMLNode(n);
424 _children.insert(_children.end(), copy);
428 boost::shared_ptr<XMLSharedNodeList>
429 XMLTree::find(const string xpath, XMLNode* node) const
431 xmlXPathContext* ctxt;
435 doc = xmlNewDoc(xml_version);
436 writenode(doc, node, doc->children, 1);
437 ctxt = xmlXPathNewContext(doc);
439 ctxt = xmlXPathNewContext(_doc);
442 boost::shared_ptr<XMLSharedNodeList> result =
443 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
445 xmlXPathFreeContext(ctxt);
454 XMLNode::attribute_value()
456 XMLNodeList children = this->children();
457 assert(!_is_content);
458 assert(children.size() == 1);
459 XMLNode* child = *(children.begin());
460 assert(child->is_content());
461 return child->content();
465 XMLNode::add_content(const string& c)
467 return add_child_copy(XMLNode (string(), c));
471 XMLNode::property(const char* name) const
473 XMLPropertyConstIterator iter = _proplist.begin();
475 while (iter != _proplist.end()) {
476 if ((*iter)->name() == name) {
486 XMLNode::property(const string& name) const
488 XMLPropertyConstIterator iter = _proplist.begin();
490 while (iter != _proplist.end()) {
491 if ((*iter)->name() == name) {
500 XMLNode::property(const char* name)
502 XMLPropertyIterator iter = _proplist.begin();
504 while (iter != _proplist.end()) {
505 if ((*iter)->name() == name) {
514 XMLNode::property(const string& name)
516 XMLPropertyIterator iter = _proplist.begin();
518 while (iter != _proplist.end()) {
519 if ((*iter)->name() == name) {
529 XMLNode::has_property_with_value (const string& name, const string& value) const
531 XMLPropertyConstIterator iter = _proplist.begin();
533 while (iter != _proplist.end()) {
534 if ((*iter)->name() == name && (*iter)->value() == value) {
543 XMLNode::add_property(const char* name, const string& value)
545 XMLPropertyIterator iter = _proplist.begin();
547 while (iter != _proplist.end()) {
548 if ((*iter)->name() == name) {
549 (*iter)->set_value (value);
555 XMLProperty* new_property = new XMLProperty(name, value);
561 _proplist.insert(_proplist.end(), new_property);
567 XMLNode::add_property(const char* n, const char* v)
570 return add_property(n, vs);
574 XMLNode::add_property(const char* name, const long value)
577 snprintf(str, sizeof(str), "%ld", value);
578 return add_property(name, str);
582 XMLNode::remove_property(const string& name)
584 XMLPropertyIterator iter = _proplist.begin();
586 while (iter != _proplist.end()) {
587 if ((*iter)->name() == name) {
588 XMLProperty* property = *iter;
589 _proplist.erase (iter);
597 /** Remove any property with the given name from this node and its children */
599 XMLNode::remove_property_recursively(const string& n)
602 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
603 (*i)->remove_property_recursively (n);
608 XMLNode::remove_nodes(const string& n)
610 XMLNodeIterator i = _children.begin();
611 while (i != _children.end()) {
612 if ((*i)->name() == n) {
613 i = _children.erase (i);
621 XMLNode::remove_nodes_and_delete(const string& n)
623 XMLNodeIterator i = _children.begin();
625 while (i != _children.end()) {
626 if ((*i)->name() == n) {
628 i = _children.erase (i);
636 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
638 XMLNodeIterator i = _children.begin();
639 XMLProperty const * prop;
641 while (i != _children.end()) {
642 prop = (*i)->property(propname);
643 if (prop && prop->value() == val) {
645 i = _children.erase(i);
652 XMLProperty::XMLProperty(const string& n, const string& v)
656 // Normalize property name (replace '_' with '-' as old session are inconsistent)
657 for (size_t i = 0; i < _name.length(); ++i) {
658 if (_name[i] == '_') {
664 XMLProperty::~XMLProperty()
669 readnode(xmlNodePtr node)
671 string name, content;
677 name = (const char*)node->name;
680 tmp = new XMLNode(name);
682 for (attr = node->properties; attr; attr = attr->next) {
684 if (attr->children) {
685 content = (char*)attr->children->content;
687 tmp->add_property((const char*)attr->name, content);
691 tmp->set_content((char*)node->content);
693 tmp->set_content(string());
696 for (child = node->children; child; child = child->next) {
697 tmp->add_child_nocopy (*readnode(child));
704 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
709 node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
711 node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
714 if (n->is_content()) {
715 node->type = XML_TEXT_NODE;
716 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
719 const XMLPropertyList& props = n->properties();
721 for (XMLPropertyConstIterator prop_iter = props.begin (); prop_iter != props.end ();
723 xmlSetProp (node, (const xmlChar*)(*prop_iter)->name ().c_str (),
724 (const xmlChar*)(*prop_iter)->value ().c_str ());
727 const XMLNodeList& children = n->children ();
728 for (XMLNodeConstIterator child_iter = children.begin (); child_iter != children.end ();
730 writenode (doc, *child_iter, node);
734 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
736 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
739 xmlXPathFreeContext(ctxt);
740 xmlFreeDoc(ctxt->doc);
742 throw XMLException("Invalid XPath: " + xpath);
745 if (result->type != XPATH_NODESET) {
746 xmlXPathFreeObject(result);
747 xmlXPathFreeContext(ctxt);
748 xmlFreeDoc(ctxt->doc);
750 throw XMLException("Only nodeset result types are supported.");
753 xmlNodeSet* nodeset = result->nodesetval;
754 XMLSharedNodeList* nodes = new XMLSharedNodeList();
756 for (int i = 0; i < nodeset->nodeNr; ++i) {
757 XMLNode* node = readnode(nodeset->nodeTab[i]);
758 nodes->push_back(boost::shared_ptr<XMLNode>(node));
764 xmlXPathFreeObject(result);
769 /** Dump a node, its properties and children to a stream */
771 XMLNode::dump (ostream& s, string p) const
774 s << p << " " << content() << "\n";
776 s << p << "<" << _name;
777 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
778 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
782 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
783 (*i)->dump (s, p + " ");
786 s << p << "</" << _name << ">\n";