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 <libxml/debugXML.h>
11 #include <libxml/xpath.h>
12 #include <libxml/xpathInternals.h>
14 #define XML_VERSION "1.0"
18 static XMLNode* readnode(xmlNodePtr);
19 static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int);
20 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
30 XMLTree::XMLTree(const string& fn, bool validate)
36 read_internal(validate);
39 XMLTree::XMLTree(const XMLTree* from)
40 : _filename(from->filename())
41 , _root(new XMLNode(*from->root()))
42 , _doc (xmlCopyDoc (from->_doc, 1))
43 , _compression(from->compression())
58 XMLTree::set_compression(int c)
72 XMLTree::read_internal(bool validate)
74 //shouldnt be used anywhere ATM, remove if so!
85 xmlParserCtxtPtr ctxt = NULL; /* the parser context */
87 xmlKeepBlanksDefault(0);
88 /* parse the file, activating the DTD validation option */
90 /* create a parser context */
91 ctxt = xmlNewParserCtxt();
95 _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
97 _doc = xmlParseFile(_filename.c_str());
100 /* check if parsing suceeded */
103 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 */
118 xmlFreeParserCtxt(ctxt);
125 XMLTree::read_buffer(const string& buffer)
134 doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
139 _root = readnode(xmlDocGetRootElement(doc));
147 XMLTree::write() const
150 XMLNodeList children;
153 xmlKeepBlanksDefault(0);
154 doc = xmlNewDoc((xmlChar*) XML_VERSION);
155 xmlSetDocCompressMode(doc, _compression);
156 writenode(doc, _root, doc->children, 1);
157 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
168 XMLTree::debug(FILE* out) const
171 XMLNodeList children;
173 xmlKeepBlanksDefault(0);
174 doc = xmlNewDoc((xmlChar*) XML_VERSION);
175 xmlSetDocCompressMode(doc, _compression);
176 writenode(doc, _root, doc->children, 1);
177 xmlDebugDumpDocument (out, doc);
182 XMLTree::write_buffer() const
184 static string retval;
188 XMLNodeList children;
190 xmlKeepBlanksDefault(0);
191 doc = xmlNewDoc((xmlChar*) XML_VERSION);
192 xmlSetDocCompressMode(doc, _compression);
193 writenode(doc, _root, doc->children, 1);
194 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
204 XMLNode::XMLNode(const string& n)
210 XMLNode::XMLNode(const string& n, const string& c)
217 XMLNode::XMLNode(const XMLNode& from)
228 XMLNode::clear_lists ()
230 XMLNodeIterator curchild;
231 XMLPropertyIterator curprop;
233 _selected_children.clear ();
236 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
242 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
250 XMLNode::operator= (const XMLNode& from)
254 XMLPropertyList props;
255 XMLPropertyIterator curprop;
257 XMLNodeIterator curnode;
262 set_content(from.content());
264 props = from.properties();
265 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
266 add_property((*curprop)->name().c_str(), (*curprop)->value());
269 nodes = from.children();
270 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
271 add_child_copy(**curnode);
279 XMLNode::set_content(const string& c)
293 XMLNode::child (const char* name) const
295 /* returns first child matching name */
297 XMLNodeConstIterator cur;
303 for (cur = _children.begin(); cur != _children.end(); ++cur) {
304 if ((*cur)->name() == name) {
313 XMLNode::children(const string& n) const
315 /* returns all children matching name */
317 XMLNodeConstIterator cur;
323 _selected_children.clear();
325 for (cur = _children.begin(); cur != _children.end(); ++cur) {
326 if ((*cur)->name() == n) {
327 _selected_children.insert(_selected_children.end(), *cur);
331 return _selected_children;
335 XMLNode::add_child(const char* n)
337 return add_child_copy(XMLNode (n));
341 XMLNode::add_child_nocopy(XMLNode& n)
343 _children.insert(_children.end(), &n);
347 XMLNode::add_child_copy(const XMLNode& n)
349 XMLNode *copy = new XMLNode(n);
350 _children.insert(_children.end(), copy);
354 boost::shared_ptr<XMLSharedNodeList>
355 XMLTree::find(const string xpath, XMLNode* node) const
357 xmlXPathContext* ctxt;
361 doc = xmlNewDoc((xmlChar*) XML_VERSION);
362 writenode(doc, node, doc->children, 1);
363 ctxt = xmlXPathNewContext(doc);
365 ctxt = xmlXPathNewContext(_doc);
368 boost::shared_ptr<XMLSharedNodeList> result =
369 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
371 xmlXPathFreeContext(ctxt);
380 XMLNode::attribute_value()
382 XMLNodeList children = this->children();
383 assert(!_is_content);
384 assert(children.size() == 1);
385 XMLNode* child = *(children.begin());
386 assert(child->is_content());
387 return child->content();
391 XMLNode::add_content(const string& c)
393 return add_child_copy(XMLNode (string(), c));
397 XMLNode::property(const char* n)
400 map<string,XMLProperty*>::iterator iter;
402 if ((iter = _propmap.find(ns)) != _propmap.end()) {
410 XMLNode::property(const string& ns)
412 map<string,XMLProperty*>::iterator iter;
414 if ((iter = _propmap.find(ns)) != _propmap.end()) {
422 XMLNode::add_property(const char* n, const string& v)
425 map<string,XMLProperty*>::iterator iter;
427 if ((iter = _propmap.find(ns)) != _propmap.end()) {
428 iter->second->set_value (v);
432 XMLProperty* tmp = new XMLProperty(ns, v);
438 _propmap[tmp->name()] = tmp;
439 _proplist.insert(_proplist.end(), tmp);
445 XMLNode::add_property(const char* n, const char* v)
448 return add_property(n, vs);
452 XMLNode::add_property(const char* name, const long value)
455 snprintf(str, sizeof(str), "%ld", value);
456 return add_property(name, str);
460 XMLNode::remove_property(const string& n)
462 if (_propmap.find(n) != _propmap.end()) {
463 XMLProperty* p = _propmap[n];
464 _proplist.remove (p);
470 /** Remove any property with the given name from this node and its children */
472 XMLNode::remove_property_recursively(const string& n)
475 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
476 (*i)->remove_property_recursively (n);
481 XMLNode::remove_nodes(const string& n)
483 XMLNodeIterator i = _children.begin();
486 while (i != _children.end()) {
489 if ((*i)->name() == n) {
497 XMLNode::remove_nodes_and_delete(const string& n)
499 XMLNodeIterator i = _children.begin();
502 while (i != _children.end()) {
505 if ((*i)->name() == n) {
514 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
516 XMLNodeIterator i = _children.begin();
520 while (i != _children.end()) {
524 prop = (*i)->property(propname);
525 if (prop && prop->value() == val) {
534 XMLProperty::XMLProperty(const string& n, const string& v)
538 // Normalize property name (replace '_' with '-' as old session are inconsistent)
539 for (size_t i = 0; i < _name.length(); ++i) {
540 if (_name[i] == '_') {
546 XMLProperty::~XMLProperty()
551 readnode(xmlNodePtr node)
553 string name, content;
559 name = (char*)node->name;
562 tmp = new XMLNode(name);
564 for (attr = node->properties; attr; attr = attr->next) {
566 if (attr->children) {
567 content = (char*)attr->children->content;
569 tmp->add_property((char*)attr->name, content);
573 tmp->set_content((char*)node->content);
575 tmp->set_content(string());
578 for (child = node->children; child; child = child->next) {
579 tmp->add_child_nocopy (*readnode(child));
586 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
588 XMLPropertyList props;
589 XMLPropertyIterator curprop;
590 XMLNodeList children;
591 XMLNodeIterator curchild;
595 node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0);
597 node = xmlNewChild(p, 0, (xmlChar*) n->name().c_str(), 0);
600 if (n->is_content()) {
601 node->type = XML_TEXT_NODE;
602 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
605 props = n->properties();
606 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
607 xmlSetProp(node, (xmlChar*) (*curprop)->name().c_str(), (xmlChar*) (*curprop)->value().c_str());
610 children = n->children();
611 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
612 writenode(doc, *curchild, node);
616 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
618 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
621 xmlXPathFreeContext(ctxt);
622 xmlFreeDoc(ctxt->doc);
624 throw XMLException("Invalid XPath: " + xpath);
627 if (result->type != XPATH_NODESET) {
628 xmlXPathFreeObject(result);
629 xmlXPathFreeContext(ctxt);
630 xmlFreeDoc(ctxt->doc);
632 throw XMLException("Only nodeset result types are supported.");
635 xmlNodeSet* nodeset = result->nodesetval;
636 XMLSharedNodeList* nodes = new XMLSharedNodeList();
638 for (int i = 0; i < nodeset->nodeNr; ++i) {
639 XMLNode* node = readnode(nodeset->nodeTab[i]);
640 nodes->push_back(boost::shared_ptr<XMLNode>(node));
646 xmlXPathFreeObject(result);
651 /** Dump a node, its properties and children to a stream */
653 XMLNode::dump (ostream& s, string p) const
656 s << p << " " << content() << "\n";
658 s << p << "<" << _name;
659 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
660 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
664 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
665 (*i)->dump (s, p + " ");
668 s << p << "</" << _name << ">\n";