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 xmlChar* xml_version = xmlCharStrdup("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(const_cast<char*>(buffer.c_str()), buffer.length());
139 _root = readnode(xmlDocGetRootElement(doc));
147 XMLTree::write() const
150 XMLNodeList children;
153 xmlKeepBlanksDefault(0);
154 doc = xmlNewDoc(xml_version);
155 xmlSetDocCompressMode(doc, _compression);
156 writenode(doc, _root, doc->children, 1);
157 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
160 xmlErrorPtr xerr = xmlGetLastError ();
162 std::cerr << "unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
164 std::cerr << "xmlSaveFormatFileEnc: error"
165 << " domain: " << xerr->domain
166 << " code: " << xerr->code
167 << " msg: " << xerr->message
182 XMLTree::debug(FILE* out) const
184 #ifdef LIBXML_DEBUG_ENABLED
186 XMLNodeList children;
188 xmlKeepBlanksDefault(0);
189 doc = xmlNewDoc(xml_version);
190 xmlSetDocCompressMode(doc, _compression);
191 writenode(doc, _root, doc->children, 1);
192 xmlDebugDumpDocument (out, doc);
198 XMLTree::write_buffer() const
200 static string retval;
204 XMLNodeList children;
206 xmlKeepBlanksDefault(0);
207 doc = xmlNewDoc(xml_version);
208 xmlSetDocCompressMode(doc, _compression);
209 writenode(doc, _root, doc->children, 1);
210 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
220 XMLNode::XMLNode(const string& n)
226 XMLNode::XMLNode(const string& n, const string& c)
233 XMLNode::XMLNode(const XMLNode& from)
244 XMLNode::clear_lists ()
246 XMLNodeIterator curchild;
247 XMLPropertyIterator curprop;
249 _selected_children.clear ();
252 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
258 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
266 XMLNode::operator= (const XMLNode& from)
270 XMLPropertyList props;
271 XMLPropertyIterator curprop;
273 XMLNodeIterator curnode;
278 set_content(from.content());
280 props = from.properties();
281 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
282 add_property((*curprop)->name().c_str(), (*curprop)->value());
285 nodes = from.children();
286 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
287 add_child_copy(**curnode);
295 XMLNode::set_content(const string& c)
309 XMLNode::child (const char* name) const
311 /* returns first child matching name */
313 XMLNodeConstIterator cur;
319 for (cur = _children.begin(); cur != _children.end(); ++cur) {
320 if ((*cur)->name() == name) {
329 XMLNode::children(const string& n) const
331 /* returns all children matching name */
333 XMLNodeConstIterator cur;
339 _selected_children.clear();
341 for (cur = _children.begin(); cur != _children.end(); ++cur) {
342 if ((*cur)->name() == n) {
343 _selected_children.insert(_selected_children.end(), *cur);
347 return _selected_children;
351 XMLNode::add_child(const char* n)
353 return add_child_copy(XMLNode (n));
357 XMLNode::add_child_nocopy(XMLNode& n)
359 _children.insert(_children.end(), &n);
363 XMLNode::add_child_copy(const XMLNode& n)
365 XMLNode *copy = new XMLNode(n);
366 _children.insert(_children.end(), copy);
370 boost::shared_ptr<XMLSharedNodeList>
371 XMLTree::find(const string xpath, XMLNode* node) const
373 xmlXPathContext* ctxt;
377 doc = xmlNewDoc(xml_version);
378 writenode(doc, node, doc->children, 1);
379 ctxt = xmlXPathNewContext(doc);
381 ctxt = xmlXPathNewContext(_doc);
384 boost::shared_ptr<XMLSharedNodeList> result =
385 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
387 xmlXPathFreeContext(ctxt);
396 XMLNode::attribute_value()
398 XMLNodeList children = this->children();
399 assert(!_is_content);
400 assert(children.size() == 1);
401 XMLNode* child = *(children.begin());
402 assert(child->is_content());
403 return child->content();
407 XMLNode::add_content(const string& c)
409 return add_child_copy(XMLNode (string(), c));
413 XMLNode::property(const char* n)
416 map<string,XMLProperty*>::iterator iter;
418 if ((iter = _propmap.find(ns)) != _propmap.end()) {
426 XMLNode::property(const string& ns)
428 map<string,XMLProperty*>::iterator iter;
430 if ((iter = _propmap.find(ns)) != _propmap.end()) {
438 XMLNode::add_property(const char* n, const string& v)
441 map<string,XMLProperty*>::iterator iter;
443 if ((iter = _propmap.find(ns)) != _propmap.end()) {
444 iter->second->set_value (v);
448 XMLProperty* tmp = new XMLProperty(ns, v);
454 _propmap[tmp->name()] = tmp;
455 _proplist.insert(_proplist.end(), tmp);
461 XMLNode::add_property(const char* n, const char* v)
464 return add_property(n, vs);
468 XMLNode::add_property(const char* name, const long value)
471 snprintf(str, sizeof(str), "%ld", value);
472 return add_property(name, str);
476 XMLNode::remove_property(const string& n)
478 if (_propmap.find(n) != _propmap.end()) {
479 XMLProperty* p = _propmap[n];
480 _proplist.remove (p);
486 /** Remove any property with the given name from this node and its children */
488 XMLNode::remove_property_recursively(const string& n)
491 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
492 (*i)->remove_property_recursively (n);
497 XMLNode::remove_nodes(const string& n)
499 XMLNodeIterator i = _children.begin();
502 while (i != _children.end()) {
505 if ((*i)->name() == n) {
513 XMLNode::remove_nodes_and_delete(const string& n)
515 XMLNodeIterator i = _children.begin();
518 while (i != _children.end()) {
521 if ((*i)->name() == n) {
530 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
532 XMLNodeIterator i = _children.begin();
536 while (i != _children.end()) {
540 prop = (*i)->property(propname);
541 if (prop && prop->value() == val) {
550 XMLProperty::XMLProperty(const string& n, const string& v)
554 // Normalize property name (replace '_' with '-' as old session are inconsistent)
555 for (size_t i = 0; i < _name.length(); ++i) {
556 if (_name[i] == '_') {
562 XMLProperty::~XMLProperty()
567 readnode(xmlNodePtr node)
569 string name, content;
575 name = (const char*)node->name;
578 tmp = new XMLNode(name);
580 for (attr = node->properties; attr; attr = attr->next) {
582 if (attr->children) {
583 content = (char*)attr->children->content;
585 tmp->add_property((const char*)attr->name, content);
589 tmp->set_content((char*)node->content);
591 tmp->set_content(string());
594 for (child = node->children; child; child = child->next) {
595 tmp->add_child_nocopy (*readnode(child));
602 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
604 XMLPropertyList props;
605 XMLPropertyIterator curprop;
606 XMLNodeList children;
607 XMLNodeIterator curchild;
611 node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
613 node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
616 if (n->is_content()) {
617 node->type = XML_TEXT_NODE;
618 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
621 props = n->properties();
622 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
623 xmlSetProp(node, (const xmlChar*) (*curprop)->name().c_str(), (const xmlChar*) (*curprop)->value().c_str());
626 children = n->children();
627 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
628 writenode(doc, *curchild, node);
632 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
634 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
637 xmlXPathFreeContext(ctxt);
638 xmlFreeDoc(ctxt->doc);
640 throw XMLException("Invalid XPath: " + xpath);
643 if (result->type != XPATH_NODESET) {
644 xmlXPathFreeObject(result);
645 xmlXPathFreeContext(ctxt);
646 xmlFreeDoc(ctxt->doc);
648 throw XMLException("Only nodeset result types are supported.");
651 xmlNodeSet* nodeset = result->nodesetval;
652 XMLSharedNodeList* nodes = new XMLSharedNodeList();
654 for (int i = 0; i < nodeset->nodeNr; ++i) {
655 XMLNode* node = readnode(nodeset->nodeTab[i]);
656 nodes->push_back(boost::shared_ptr<XMLNode>(node));
662 xmlXPathFreeObject(result);
667 /** Dump a node, its properties and children to a stream */
669 XMLNode::dump (ostream& s, string p) const
672 s << p << " " << content() << "\n";
674 s << p << "<" << _name;
675 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
676 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
680 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
681 (*i)->dump (s, p + " ");
684 s << p << "</" << _name << ">\n";