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 /* create a parser context */
86 xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
91 xmlKeepBlanksDefault(0);
92 /* parse the file, activating the DTD validation option */
94 _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
96 _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_HUGE);
99 /* check if parsing suceeded */
101 xmlFreeParserCtxt(ctxt);
104 /* check if validation suceeded */
105 if (validate && ctxt->valid == 0) {
106 xmlFreeParserCtxt(ctxt);
107 throw XMLException("Failed to validate document " + _filename);
111 _root = readnode(xmlDocGetRootElement(_doc));
113 /* free up the parser context */
114 xmlFreeParserCtxt(ctxt);
120 XMLTree::read_buffer(const string& buffer)
129 doc = xmlParseMemory(const_cast<char*>(buffer.c_str()), buffer.length());
134 _root = readnode(xmlDocGetRootElement(doc));
142 XMLTree::write() const
145 XMLNodeList children;
148 xmlKeepBlanksDefault(0);
149 doc = xmlNewDoc(xml_version);
150 xmlSetDocCompressMode(doc, _compression);
151 writenode(doc, _root, doc->children, 1);
152 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
155 xmlErrorPtr xerr = xmlGetLastError ();
157 std::cerr << "unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
159 std::cerr << "xmlSaveFormatFileEnc: error"
160 << " domain: " << xerr->domain
161 << " code: " << xerr->code
162 << " msg: " << xerr->message
177 XMLTree::debug(FILE* out) const
179 #ifdef LIBXML_DEBUG_ENABLED
181 XMLNodeList children;
183 xmlKeepBlanksDefault(0);
184 doc = xmlNewDoc(xml_version);
185 xmlSetDocCompressMode(doc, _compression);
186 writenode(doc, _root, doc->children, 1);
187 xmlDebugDumpDocument (out, doc);
193 XMLTree::write_buffer() const
195 static string retval;
199 XMLNodeList children;
201 xmlKeepBlanksDefault(0);
202 doc = xmlNewDoc(xml_version);
203 xmlSetDocCompressMode(doc, _compression);
204 writenode(doc, _root, doc->children, 1);
205 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
215 XMLNode::XMLNode(const string& n)
221 XMLNode::XMLNode(const string& n, const string& c)
228 XMLNode::XMLNode(const XMLNode& from)
239 XMLNode::clear_lists ()
241 XMLNodeIterator curchild;
242 XMLPropertyIterator curprop;
244 _selected_children.clear ();
247 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
253 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
261 XMLNode::operator= (const XMLNode& from)
265 XMLPropertyList props;
266 XMLPropertyIterator curprop;
268 XMLNodeIterator curnode;
273 set_content(from.content());
275 props = from.properties();
276 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
277 add_property((*curprop)->name().c_str(), (*curprop)->value());
280 nodes = from.children();
281 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
282 add_child_copy(**curnode);
290 XMLNode::set_content(const string& c)
304 XMLNode::child (const char* name) const
306 /* returns first child matching name */
308 XMLNodeConstIterator cur;
314 for (cur = _children.begin(); cur != _children.end(); ++cur) {
315 if ((*cur)->name() == name) {
324 XMLNode::children(const string& n) const
326 /* returns all children matching name */
328 XMLNodeConstIterator cur;
334 _selected_children.clear();
336 for (cur = _children.begin(); cur != _children.end(); ++cur) {
337 if ((*cur)->name() == n) {
338 _selected_children.insert(_selected_children.end(), *cur);
342 return _selected_children;
346 XMLNode::add_child(const char* n)
348 return add_child_copy(XMLNode (n));
352 XMLNode::add_child_nocopy(XMLNode& n)
354 _children.insert(_children.end(), &n);
358 XMLNode::add_child_copy(const XMLNode& n)
360 XMLNode *copy = new XMLNode(n);
361 _children.insert(_children.end(), copy);
365 boost::shared_ptr<XMLSharedNodeList>
366 XMLTree::find(const string xpath, XMLNode* node) const
368 xmlXPathContext* ctxt;
372 doc = xmlNewDoc(xml_version);
373 writenode(doc, node, doc->children, 1);
374 ctxt = xmlXPathNewContext(doc);
376 ctxt = xmlXPathNewContext(_doc);
379 boost::shared_ptr<XMLSharedNodeList> result =
380 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
382 xmlXPathFreeContext(ctxt);
391 XMLNode::attribute_value()
393 XMLNodeList children = this->children();
394 assert(!_is_content);
395 assert(children.size() == 1);
396 XMLNode* child = *(children.begin());
397 assert(child->is_content());
398 return child->content();
402 XMLNode::add_content(const string& c)
404 return add_child_copy(XMLNode (string(), c));
408 XMLNode::property(const char* n)
411 map<string,XMLProperty*>::iterator iter;
413 if ((iter = _propmap.find(ns)) != _propmap.end()) {
421 XMLNode::property(const string& ns)
423 map<string,XMLProperty*>::iterator iter;
425 if ((iter = _propmap.find(ns)) != _propmap.end()) {
433 XMLNode::add_property(const char* n, const string& v)
436 map<string,XMLProperty*>::iterator iter;
438 if ((iter = _propmap.find(ns)) != _propmap.end()) {
439 iter->second->set_value (v);
443 XMLProperty* tmp = new XMLProperty(ns, v);
449 _propmap[tmp->name()] = tmp;
450 _proplist.insert(_proplist.end(), tmp);
456 XMLNode::add_property(const char* n, const char* v)
459 return add_property(n, vs);
463 XMLNode::add_property(const char* name, const long value)
466 snprintf(str, sizeof(str), "%ld", value);
467 return add_property(name, str);
471 XMLNode::remove_property(const string& n)
473 if (_propmap.find(n) != _propmap.end()) {
474 XMLProperty* p = _propmap[n];
475 _proplist.remove (p);
481 /** Remove any property with the given name from this node and its children */
483 XMLNode::remove_property_recursively(const string& n)
486 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
487 (*i)->remove_property_recursively (n);
492 XMLNode::remove_nodes(const string& n)
494 XMLNodeIterator i = _children.begin();
497 while (i != _children.end()) {
500 if ((*i)->name() == n) {
508 XMLNode::remove_nodes_and_delete(const string& n)
510 XMLNodeIterator i = _children.begin();
513 while (i != _children.end()) {
516 if ((*i)->name() == n) {
525 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
527 XMLNodeIterator i = _children.begin();
531 while (i != _children.end()) {
535 prop = (*i)->property(propname);
536 if (prop && prop->value() == val) {
545 XMLProperty::XMLProperty(const string& n, const string& v)
549 // Normalize property name (replace '_' with '-' as old session are inconsistent)
550 for (size_t i = 0; i < _name.length(); ++i) {
551 if (_name[i] == '_') {
557 XMLProperty::~XMLProperty()
562 readnode(xmlNodePtr node)
564 string name, content;
570 name = (const char*)node->name;
573 tmp = new XMLNode(name);
575 for (attr = node->properties; attr; attr = attr->next) {
577 if (attr->children) {
578 content = (char*)attr->children->content;
580 tmp->add_property((const char*)attr->name, content);
584 tmp->set_content((char*)node->content);
586 tmp->set_content(string());
589 for (child = node->children; child; child = child->next) {
590 tmp->add_child_nocopy (*readnode(child));
597 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
599 XMLPropertyList props;
600 XMLPropertyIterator curprop;
601 XMLNodeList children;
602 XMLNodeIterator curchild;
606 node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
608 node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
611 if (n->is_content()) {
612 node->type = XML_TEXT_NODE;
613 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
616 props = n->properties();
617 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
618 xmlSetProp(node, (const xmlChar*) (*curprop)->name().c_str(), (const xmlChar*) (*curprop)->value().c_str());
621 children = n->children();
622 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
623 writenode(doc, *curchild, node);
627 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
629 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
632 xmlXPathFreeContext(ctxt);
633 xmlFreeDoc(ctxt->doc);
635 throw XMLException("Invalid XPath: " + xpath);
638 if (result->type != XPATH_NODESET) {
639 xmlXPathFreeObject(result);
640 xmlXPathFreeContext(ctxt);
641 xmlFreeDoc(ctxt->doc);
643 throw XMLException("Only nodeset result types are supported.");
646 xmlNodeSet* nodeset = result->nodesetval;
647 XMLSharedNodeList* nodes = new XMLSharedNodeList();
649 for (int i = 0; i < nodeset->nodeNr; ++i) {
650 XMLNode* node = readnode(nodeset->nodeTab[i]);
651 nodes->push_back(boost::shared_ptr<XMLNode>(node));
657 xmlXPathFreeObject(result);
662 /** Dump a node, its properties and children to a stream */
664 XMLNode::dump (ostream& s, string p) const
667 s << p << " " << content() << "\n";
669 s << p << "<" << _name;
670 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
671 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
675 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
676 (*i)->dump (s, p + " ");
679 s << p << "</" << _name << ">\n";