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, bool to_tree_doc)
132 doc = xmlParseMemory(const_cast<char*>(buffer.c_str()), buffer.length());
137 _root = readnode(xmlDocGetRootElement(doc));
152 XMLTree::write() const
155 XMLNodeList children;
158 xmlKeepBlanksDefault(0);
159 doc = xmlNewDoc(xml_version);
160 xmlSetDocCompressMode(doc, _compression);
161 writenode(doc, _root, doc->children, 1);
162 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
165 xmlErrorPtr xerr = xmlGetLastError ();
167 std::cerr << "unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
169 std::cerr << "xmlSaveFormatFileEnc: error"
170 << " domain: " << xerr->domain
171 << " code: " << xerr->code
172 << " msg: " << xerr->message
187 XMLTree::debug(FILE* out) const
189 #ifdef LIBXML_DEBUG_ENABLED
191 XMLNodeList children;
193 xmlKeepBlanksDefault(0);
194 doc = xmlNewDoc(xml_version);
195 xmlSetDocCompressMode(doc, _compression);
196 writenode(doc, _root, doc->children, 1);
197 xmlDebugDumpDocument (out, doc);
203 XMLTree::write_buffer() const
205 static string retval;
209 XMLNodeList children;
211 xmlKeepBlanksDefault(0);
212 doc = xmlNewDoc(xml_version);
213 xmlSetDocCompressMode(doc, _compression);
214 writenode(doc, _root, doc->children, 1);
215 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
225 static const int PROPERTY_RESERVE_COUNT = 16;
227 XMLNode::XMLNode(const string& n)
231 _proplist.reserve (PROPERTY_RESERVE_COUNT);
234 XMLNode::XMLNode(const string& n, const string& c)
239 _proplist.reserve (PROPERTY_RESERVE_COUNT);
242 XMLNode::XMLNode(const XMLNode& from)
244 _proplist.reserve (PROPERTY_RESERVE_COUNT);
254 XMLNode::clear_lists ()
256 XMLNodeIterator curchild;
257 XMLPropertyIterator curprop;
259 _selected_children.clear ();
261 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
267 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
275 XMLNode::operator= (const XMLNode& from)
283 _name = from.name ();
284 set_content (from.content ());
286 const XMLPropertyList& props = from.properties ();
288 for (XMLPropertyConstIterator prop_iter = props.begin (); prop_iter != props.end (); ++prop_iter) {
289 set_property ((*prop_iter)->name ().c_str (), (*prop_iter)->value ());
292 const XMLNodeList& nodes = from.children ();
293 for (XMLNodeConstIterator child_iter = nodes.begin (); child_iter != nodes.end (); ++child_iter) {
294 add_child_copy (**child_iter);
301 XMLNode::operator== (const XMLNode& other) const
303 if (is_content () != other.is_content ()) {
308 if (content () != other.content ()) {
312 if (name () != other.name ()) {
317 XMLPropertyList const& other_properties = other.properties ();
319 if (_proplist.size () != other_properties.size ()) {
323 XMLPropertyConstIterator our_prop_iter = _proplist.begin();
324 XMLPropertyConstIterator other_prop_iter = other_properties.begin();
326 while (our_prop_iter != _proplist.end ()) {
327 XMLProperty const* our_prop = *our_prop_iter;
328 XMLProperty const* other_prop = *other_prop_iter;
329 if (our_prop->name () != other_prop->name () || our_prop->value () != other_prop->value ()) {
336 XMLNodeList const& other_children = other.children();
338 if (_children.size() != other_children.size()) {
342 XMLNodeConstIterator our_child_iter = _children.begin ();
343 XMLNodeConstIterator other_child_iter = other_children.begin ();
345 while (our_child_iter != _children.end()) {
346 XMLNode const* our_child = *our_child_iter;
347 XMLNode const* other_child = *other_child_iter;
349 if (*our_child != *other_child) {
359 XMLNode::operator!= (const XMLNode& other) const
361 return !(*this == other);
365 XMLNode::set_content(const string& c)
379 XMLNode::child (const char* name) const
381 /* returns first child matching name */
383 XMLNodeConstIterator cur;
389 for (cur = _children.begin(); cur != _children.end(); ++cur) {
390 if ((*cur)->name() == name) {
399 XMLNode::children(const string& n) const
401 /* returns all children matching name */
403 XMLNodeConstIterator cur;
409 _selected_children.clear();
411 for (cur = _children.begin(); cur != _children.end(); ++cur) {
412 if ((*cur)->name() == n) {
413 _selected_children.insert(_selected_children.end(), *cur);
417 return _selected_children;
421 XMLNode::add_child(const char* n)
423 return add_child_copy(XMLNode (n));
427 XMLNode::add_child_nocopy(XMLNode& n)
429 _children.insert(_children.end(), &n);
433 XMLNode::add_child_copy(const XMLNode& n)
435 XMLNode *copy = new XMLNode(n);
436 _children.insert(_children.end(), copy);
440 boost::shared_ptr<XMLSharedNodeList>
441 XMLTree::find(const string xpath, XMLNode* node) const
443 xmlXPathContext* ctxt;
447 doc = xmlNewDoc(xml_version);
448 writenode(doc, node, doc->children, 1);
449 ctxt = xmlXPathNewContext(doc);
451 ctxt = xmlXPathNewContext(_doc);
454 boost::shared_ptr<XMLSharedNodeList> result =
455 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
457 xmlXPathFreeContext(ctxt);
466 XMLNode::attribute_value()
468 XMLNodeList children = this->children();
470 throw XMLException("XMLNode: attribute_value failed (is_content) for requested node: " + name());
472 if (children.size() != 1)
473 throw XMLException("XMLNode: attribute_value failed (children.size != 1) for requested node: " + name());
475 XMLNode* child = *(children.begin());
476 if (!child->is_content())
477 throw XMLException("XMLNode: attribute_value failed (!child->is_content()) for requested node: " + name());
479 return child->content();
483 XMLNode::add_content(const string& c)
486 /* this would add a "</>" child, leading to invalid XML.
487 * Also in XML, empty string content is equivalent to no content.
491 return add_child_copy(XMLNode (string(), c));
495 XMLNode::property(const char* name) const
497 XMLPropertyConstIterator iter = _proplist.begin();
499 while (iter != _proplist.end()) {
500 if ((*iter)->name() == name) {
510 XMLNode::property(const string& name) const
512 XMLPropertyConstIterator iter = _proplist.begin();
514 while (iter != _proplist.end()) {
515 if ((*iter)->name() == name) {
524 XMLNode::property(const char* name)
526 XMLPropertyIterator iter = _proplist.begin();
528 while (iter != _proplist.end()) {
529 if ((*iter)->name() == name) {
538 XMLNode::property(const string& name)
540 XMLPropertyIterator iter = _proplist.begin();
542 while (iter != _proplist.end()) {
543 if ((*iter)->name() == name) {
553 XMLNode::has_property_with_value (const string& name, const string& value) const
555 XMLPropertyConstIterator iter = _proplist.begin();
557 while (iter != _proplist.end()) {
558 if ((*iter)->name() == name && (*iter)->value() == value) {
567 XMLNode::set_property(const char* name, const string& value)
569 XMLPropertyIterator iter = _proplist.begin();
571 while (iter != _proplist.end()) {
572 if ((*iter)->name() == name) {
573 (*iter)->set_value (value);
579 XMLProperty* new_property = new XMLProperty(name, value);
585 _proplist.insert(_proplist.end(), new_property);
591 XMLNode::get_property(const char* name, std::string& value) const
593 XMLProperty const* const prop = property (name);
597 value = prop->value ();
603 XMLNode::remove_property(const string& name)
605 XMLPropertyIterator iter = _proplist.begin();
607 while (iter != _proplist.end()) {
608 if ((*iter)->name() == name) {
609 XMLProperty* property = *iter;
610 _proplist.erase (iter);
618 /** Remove any property with the given name from this node and its children */
620 XMLNode::remove_property_recursively(const string& n)
623 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
624 (*i)->remove_property_recursively (n);
629 XMLNode::remove_nodes(const string& n)
631 XMLNodeIterator i = _children.begin();
632 while (i != _children.end()) {
633 if ((*i)->name() == n) {
634 i = _children.erase (i);
642 XMLNode::remove_nodes_and_delete(const string& n)
644 XMLNodeIterator i = _children.begin();
646 while (i != _children.end()) {
647 if ((*i)->name() == n) {
649 i = _children.erase (i);
657 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
659 XMLNodeIterator i = _children.begin();
660 XMLProperty const * prop;
662 while (i != _children.end()) {
663 prop = (*i)->property(propname);
664 if (prop && prop->value() == val) {
666 i = _children.erase(i);
674 XMLNode::remove_node_and_delete(const string& n, const string& propname, const string& val)
676 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
677 if ((*i)->name() == n) {
678 XMLProperty const * prop = (*i)->property (propname);
679 if (prop && prop->value() == val) {
688 XMLProperty::XMLProperty(const string& n, const string& v)
694 XMLProperty::~XMLProperty()
699 readnode(xmlNodePtr node)
701 string name, content;
707 name = (const char*)node->name;
710 tmp = new XMLNode(name);
712 for (attr = node->properties; attr; attr = attr->next) {
714 if (attr->children) {
715 content = (char*)attr->children->content;
717 tmp->set_property((const char*)attr->name, content);
721 tmp->set_content((char*)node->content);
723 tmp->set_content(string());
726 for (child = node->children; child; child = child->next) {
727 tmp->add_child_nocopy (*readnode(child));
734 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
739 node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
741 node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
744 if (n->is_content()) {
745 node->type = XML_TEXT_NODE;
746 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
749 const XMLPropertyList& props = n->properties();
751 for (XMLPropertyConstIterator prop_iter = props.begin (); prop_iter != props.end ();
753 xmlSetProp (node, (const xmlChar*)(*prop_iter)->name ().c_str (),
754 (const xmlChar*)(*prop_iter)->value ().c_str ());
757 const XMLNodeList& children = n->children ();
758 for (XMLNodeConstIterator child_iter = children.begin (); child_iter != children.end ();
760 writenode (doc, *child_iter, node);
764 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
766 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
769 xmlXPathFreeContext(ctxt);
770 xmlFreeDoc(ctxt->doc);
772 throw XMLException("Invalid XPath: " + xpath);
775 if (result->type != XPATH_NODESET) {
776 xmlXPathFreeObject(result);
777 xmlXPathFreeContext(ctxt);
778 xmlFreeDoc(ctxt->doc);
780 throw XMLException("Only nodeset result types are supported.");
783 xmlNodeSet* nodeset = result->nodesetval;
784 XMLSharedNodeList* nodes = new XMLSharedNodeList();
786 for (int i = 0; i < nodeset->nodeNr; ++i) {
787 XMLNode* node = readnode(nodeset->nodeTab[i]);
788 nodes->push_back(boost::shared_ptr<XMLNode>(node));
794 xmlXPathFreeObject(result);
799 /** Dump a node, its properties and children to a stream */
801 XMLNode::dump (ostream& s, string p) const
804 s << p << " " << content() << "\n";
806 s << p << "<" << _name;
807 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
808 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
812 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
813 (*i)->dump (s, p + " ");
816 s << p << "</" << _name << ">\n";