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);
168 XMLTree::debug(FILE* out) const
170 #ifdef LIBXML_DEBUG_ENABLED
172 XMLNodeList children;
174 xmlKeepBlanksDefault(0);
175 doc = xmlNewDoc(xml_version);
176 xmlSetDocCompressMode(doc, _compression);
177 writenode(doc, _root, doc->children, 1);
178 xmlDebugDumpDocument (out, doc);
184 XMLTree::write_buffer() const
186 static string retval;
190 XMLNodeList children;
192 xmlKeepBlanksDefault(0);
193 doc = xmlNewDoc(xml_version);
194 xmlSetDocCompressMode(doc, _compression);
195 writenode(doc, _root, doc->children, 1);
196 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
206 XMLNode::XMLNode(const string& n)
212 XMLNode::XMLNode(const string& n, const string& c)
219 XMLNode::XMLNode(const XMLNode& from)
230 XMLNode::clear_lists ()
232 XMLNodeIterator curchild;
233 XMLPropertyIterator curprop;
235 _selected_children.clear ();
238 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
244 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
252 XMLNode::operator= (const XMLNode& from)
256 XMLPropertyList props;
257 XMLPropertyIterator curprop;
259 XMLNodeIterator curnode;
264 set_content(from.content());
266 props = from.properties();
267 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
268 add_property((*curprop)->name().c_str(), (*curprop)->value());
271 nodes = from.children();
272 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
273 add_child_copy(**curnode);
281 XMLNode::set_content(const string& c)
295 XMLNode::child (const char* name) const
297 /* returns first child matching name */
299 XMLNodeConstIterator cur;
305 for (cur = _children.begin(); cur != _children.end(); ++cur) {
306 if ((*cur)->name() == name) {
315 XMLNode::children(const string& n) const
317 /* returns all children matching name */
319 XMLNodeConstIterator cur;
325 _selected_children.clear();
327 for (cur = _children.begin(); cur != _children.end(); ++cur) {
328 if ((*cur)->name() == n) {
329 _selected_children.insert(_selected_children.end(), *cur);
333 return _selected_children;
337 XMLNode::add_child(const char* n)
339 return add_child_copy(XMLNode (n));
343 XMLNode::add_child_nocopy(XMLNode& n)
345 _children.insert(_children.end(), &n);
349 XMLNode::add_child_copy(const XMLNode& n)
351 XMLNode *copy = new XMLNode(n);
352 _children.insert(_children.end(), copy);
356 boost::shared_ptr<XMLSharedNodeList>
357 XMLTree::find(const string xpath, XMLNode* node) const
359 xmlXPathContext* ctxt;
363 doc = xmlNewDoc(xml_version);
364 writenode(doc, node, doc->children, 1);
365 ctxt = xmlXPathNewContext(doc);
367 ctxt = xmlXPathNewContext(_doc);
370 boost::shared_ptr<XMLSharedNodeList> result =
371 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
373 xmlXPathFreeContext(ctxt);
382 XMLNode::attribute_value()
384 XMLNodeList children = this->children();
385 assert(!_is_content);
386 assert(children.size() == 1);
387 XMLNode* child = *(children.begin());
388 assert(child->is_content());
389 return child->content();
393 XMLNode::add_content(const string& c)
395 return add_child_copy(XMLNode (string(), c));
399 XMLNode::property(const char* n)
402 map<string,XMLProperty*>::iterator iter;
404 if ((iter = _propmap.find(ns)) != _propmap.end()) {
412 XMLNode::property(const string& ns)
414 map<string,XMLProperty*>::iterator iter;
416 if ((iter = _propmap.find(ns)) != _propmap.end()) {
424 XMLNode::add_property(const char* n, const string& v)
427 map<string,XMLProperty*>::iterator iter;
429 if ((iter = _propmap.find(ns)) != _propmap.end()) {
430 iter->second->set_value (v);
434 XMLProperty* tmp = new XMLProperty(ns, v);
440 _propmap[tmp->name()] = tmp;
441 _proplist.insert(_proplist.end(), tmp);
447 XMLNode::add_property(const char* n, const char* v)
450 return add_property(n, vs);
454 XMLNode::add_property(const char* name, const long value)
457 snprintf(str, sizeof(str), "%ld", value);
458 return add_property(name, str);
462 XMLNode::remove_property(const string& n)
464 if (_propmap.find(n) != _propmap.end()) {
465 XMLProperty* p = _propmap[n];
466 _proplist.remove (p);
472 /** Remove any property with the given name from this node and its children */
474 XMLNode::remove_property_recursively(const string& n)
477 for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
478 (*i)->remove_property_recursively (n);
483 XMLNode::remove_nodes(const string& n)
485 XMLNodeIterator i = _children.begin();
488 while (i != _children.end()) {
491 if ((*i)->name() == n) {
499 XMLNode::remove_nodes_and_delete(const string& n)
501 XMLNodeIterator i = _children.begin();
504 while (i != _children.end()) {
507 if ((*i)->name() == n) {
516 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
518 XMLNodeIterator i = _children.begin();
522 while (i != _children.end()) {
526 prop = (*i)->property(propname);
527 if (prop && prop->value() == val) {
536 XMLProperty::XMLProperty(const string& n, const string& v)
540 // Normalize property name (replace '_' with '-' as old session are inconsistent)
541 for (size_t i = 0; i < _name.length(); ++i) {
542 if (_name[i] == '_') {
548 XMLProperty::~XMLProperty()
553 readnode(xmlNodePtr node)
555 string name, content;
561 name = (const char*)node->name;
564 tmp = new XMLNode(name);
566 for (attr = node->properties; attr; attr = attr->next) {
568 if (attr->children) {
569 content = (char*)attr->children->content;
571 tmp->add_property((const char*)attr->name, content);
575 tmp->set_content((char*)node->content);
577 tmp->set_content(string());
580 for (child = node->children; child; child = child->next) {
581 tmp->add_child_nocopy (*readnode(child));
588 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
590 XMLPropertyList props;
591 XMLPropertyIterator curprop;
592 XMLNodeList children;
593 XMLNodeIterator curchild;
597 node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
599 node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
602 if (n->is_content()) {
603 node->type = XML_TEXT_NODE;
604 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
607 props = n->properties();
608 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
609 xmlSetProp(node, (const xmlChar*) (*curprop)->name().c_str(), (const xmlChar*) (*curprop)->value().c_str());
612 children = n->children();
613 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
614 writenode(doc, *curchild, node);
618 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
620 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
623 xmlXPathFreeContext(ctxt);
624 xmlFreeDoc(ctxt->doc);
626 throw XMLException("Invalid XPath: " + xpath);
629 if (result->type != XPATH_NODESET) {
630 xmlXPathFreeObject(result);
631 xmlXPathFreeContext(ctxt);
632 xmlFreeDoc(ctxt->doc);
634 throw XMLException("Only nodeset result types are supported.");
637 xmlNodeSet* nodeset = result->nodesetval;
638 XMLSharedNodeList* nodes = new XMLSharedNodeList();
640 for (int i = 0; i < nodeset->nodeNr; ++i) {
641 XMLNode* node = readnode(nodeset->nodeTab[i]);
642 nodes->push_back(boost::shared_ptr<XMLNode>(node));
648 xmlXPathFreeObject(result);
653 /** Dump a node, its properties and children to a stream */
655 XMLNode::dump (ostream& s, string p) const
658 s << p << " " << content() << "\n";
660 s << p << "<" << _name;
661 for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) {
662 s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
666 for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
667 (*i)->dump (s, p + " ");
670 s << p << "</" << _name << ">\n";