X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fpbd%2Fxml%2B%2B.cc;h=1b006fd63d4f7677fff92fc5532c4fcb82a53982;hb=84bb14efdc07b5c6a18f02aa61d46bbf76c4a077;hp=ec1ca5d2b9e0ba5c4605106c99a067d4f4592e8c;hpb=911c5717bcf8da0f59048d1fd3af0da09a21cbd7;p=ardour.git diff --git a/libs/pbd/xml++.cc b/libs/pbd/xml++.cc index ec1ca5d2b9..1b006fd63d 100644 --- a/libs/pbd/xml++.cc +++ b/libs/pbd/xml++.cc @@ -2,49 +2,51 @@ * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and * are covered by the GNU Lesser General Public License, which should be * included with libxml++ as the file COPYING. + * Modified for Ardour and released under the same terms. */ -#include +#include +#include "pbd/xml++.h" #include #include #include #define XML_VERSION "1.0" -static XMLNode *readnode(xmlNodePtr); -static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int); -static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath); +using namespace std; -XMLTree::XMLTree() - : _filename(), - _root(0), - _compression(0) -{ +static XMLNode* readnode(xmlNodePtr); +static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int); +static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath); + +XMLTree::XMLTree() + : _filename() + , _root(0) + , _compression(0) +{ } -XMLTree::XMLTree(const string &fn, bool validate) - : _filename(fn), - _root(0), - _compression(0) -{ - read_internal(validate); +XMLTree::XMLTree(const string& fn, bool validate) + : _filename(fn) + , _root(0) + , _compression(0) +{ + read_internal(validate); } -XMLTree::XMLTree(const XMLTree * from) +XMLTree::XMLTree(const XMLTree* from) + : _filename(from->filename()) + , _root(new XMLNode(*from->root())) + , _compression(from->compression()) { - _filename = from->filename(); - _root = new XMLNode(*from->root()); - _compression = from->compression(); } XMLTree::~XMLTree() { - if (_root) { - delete _root; - } + delete _root; } -int +int XMLTree::set_compression(int c) { if (c > 9) { @@ -52,28 +54,27 @@ XMLTree::set_compression(int c) } else if (c < 0) { c = 0; } - + _compression = c; - + return _compression; } -bool +bool XMLTree::read_internal(bool validate) { //shouldnt be used anywhere ATM, remove if so! assert(!validate); - if (_root) { - delete _root; - _root = 0; - } - - xmlParserCtxtPtr ctxt; /* the parser context */ + + delete _root; + _root = 0; + + xmlParserCtxtPtr ctxt = NULL; /* the parser context */ xmlDocPtr doc; /* the resulting document tree */ - + xmlKeepBlanksDefault(0); /* parse the file, activating the DTD validation option */ - if(validate) { + if (validate) { /* create a parser context */ ctxt = xmlNewParserCtxt(); if (ctxt == NULL) { @@ -83,77 +84,73 @@ XMLTree::read_internal(bool validate) } else { doc = xmlParseFile(_filename.c_str()); } - + /* check if parsing suceeded */ if (doc == NULL) { - if(validate) { + if (validate) { xmlFreeParserCtxt(ctxt); } return false; } else { - /* check if validation suceeded */ + /* check if validation suceeded */ if (validate && ctxt->valid == 0) { xmlFreeParserCtxt(ctxt); xmlFreeDoc(doc); - xmlCleanupParser(); throw XMLException("Failed to validate document " + _filename); } } - + _root = readnode(xmlDocGetRootElement(doc)); - + /* free up the parser context */ - if(validate) { + if (validate) { xmlFreeParserCtxt(ctxt); } xmlFreeDoc(doc); - xmlCleanupParser(); - + return true; } -bool -XMLTree::read_buffer(const string & buffer) +bool +XMLTree::read_buffer(const string& buffer) { xmlDocPtr doc; - + _filename = ""; - - if (_root) { - delete _root; - _root = 0; - } - - doc = xmlParseMemory((char *) buffer.c_str(), buffer.length()); + + delete _root; + _root = 0; + + doc = xmlParseMemory((char*)buffer.c_str(), buffer.length()); if (!doc) { return false; } - + _root = readnode(xmlDocGetRootElement(doc)); xmlFreeDoc(doc); - + return true; } -bool -XMLTree::write(void) const +bool +XMLTree::write() const { xmlDocPtr doc; XMLNodeList children; int result; - + xmlKeepBlanksDefault(0); - doc = xmlNewDoc((xmlChar *) XML_VERSION); + doc = xmlNewDoc((xmlChar*) XML_VERSION); xmlSetDocCompressMode(doc, _compression); writenode(doc, _root, doc->children, 1); result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1); xmlFreeDoc(doc); - + if (result == -1) { return false; } - + return true; } @@ -164,43 +161,46 @@ XMLTree::debug(FILE* out) const XMLNodeList children; xmlKeepBlanksDefault(0); - doc = xmlNewDoc((xmlChar *) XML_VERSION); + doc = xmlNewDoc((xmlChar*) XML_VERSION); xmlSetDocCompressMode(doc, _compression); writenode(doc, _root, doc->children, 1); xmlDebugDumpDocument (out, doc); xmlFreeDoc(doc); } -const string & -XMLTree::write_buffer(void) const +const string& +XMLTree::write_buffer() const { static string retval; - char *ptr; + char* ptr; int len; xmlDocPtr doc; XMLNodeList children; - + xmlKeepBlanksDefault(0); - doc = xmlNewDoc((xmlChar *) XML_VERSION); + doc = xmlNewDoc((xmlChar*) XML_VERSION); xmlSetDocCompressMode(doc, _compression); writenode(doc, _root, doc->children, 1); xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len); xmlFreeDoc(doc); - + retval = ptr; - + free(ptr); - + return retval; } -XMLNode::XMLNode(const string & n) - : _name(n), _is_content(false), _content(string()) +XMLNode::XMLNode(const string& n) + : _name(n) + , _is_content(false) { } -XMLNode::XMLNode(const string & n, const string & c) - :_name(n), _is_content(true), _content(c) +XMLNode::XMLNode(const string& n, const string& c) + : _name(n) + , _is_content(true) + , _content(c) { } @@ -210,15 +210,15 @@ XMLNode::XMLNode(const XMLNode& from) XMLPropertyIterator curprop; XMLNodeList nodes; XMLNodeIterator curnode; - + _name = from.name(); set_content(from.content()); - + props = from.properties(); for (curprop = props.begin(); curprop != props.end(); ++curprop) { add_property((*curprop)->name().c_str(), (*curprop)->value()); } - + nodes = from.children(); for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) { add_child_copy(**curnode); @@ -229,110 +229,109 @@ XMLNode::~XMLNode() { XMLNodeIterator curchild; XMLPropertyIterator curprop; - + for (curchild = _children.begin(); curchild != _children.end(); ++curchild) { delete *curchild; } - + for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) { delete *curprop; } } -const string & -XMLNode::set_content(const string & c) +const string& +XMLNode::set_content(const string& c) { if (c.empty()) { _is_content = false; } else { _is_content = true; } - + _content = c; - + return _content; } XMLNode* -XMLNode::child (const char *name) const +XMLNode::child (const char* name) const { /* returns first child matching name */ XMLNodeConstIterator cur; - + if (name == 0) { return 0; } - + for (cur = _children.begin(); cur != _children.end(); ++cur) { if ((*cur)->name() == name) { return *cur; } } - + return 0; } -const XMLNodeList & +const XMLNodeList& XMLNode::children(const string& n) const { /* returns all children matching name */ - static XMLNodeList retval; XMLNodeConstIterator cur; - + if (n.empty()) { return _children; } - - retval.erase(retval.begin(), retval.end()); - + + _selected_children.clear(); + for (cur = _children.begin(); cur != _children.end(); ++cur) { if ((*cur)->name() == n) { - retval.insert(retval.end(), *cur); + _selected_children.insert(_selected_children.end(), *cur); } } - - return retval; + + return _selected_children; } -XMLNode * -XMLNode::add_child(const char * n) +XMLNode* +XMLNode::add_child(const char* n) { return add_child_copy(XMLNode (n)); } void -XMLNode::add_child_nocopy (XMLNode& n) +XMLNode::add_child_nocopy(XMLNode& n) { _children.insert(_children.end(), &n); } -XMLNode * +XMLNode* XMLNode::add_child_copy(const XMLNode& n) { - XMLNode *copy = new XMLNode (n); + XMLNode *copy = new XMLNode(n); _children.insert(_children.end(), copy); return copy; } -boost::shared_ptr +boost::shared_ptr XMLNode::find(const string xpath) const { - xmlDocPtr doc = xmlNewDoc((xmlChar *) XML_VERSION); - writenode(doc, (XMLNode *) this, doc->children, 1); + xmlDocPtr doc = xmlNewDoc((xmlChar*) XML_VERSION); + writenode(doc, (XMLNode*)this, doc->children, 1); xmlXPathContext* ctxt = xmlXPathNewContext(doc); - - boost::shared_ptr result = - boost::shared_ptr(find_impl(ctxt, xpath)); - + + boost::shared_ptr result = + boost::shared_ptr(find_impl(ctxt, xpath)); + xmlXPathFreeContext(ctxt); xmlFreeDoc(doc); - + return result; } -std::string +std::string XMLNode::attribute_value() { XMLNodeList children = this->children(); @@ -343,14 +342,14 @@ XMLNode::attribute_value() return child->content(); } -XMLNode * -XMLNode::add_content(const string & c) +XMLNode* +XMLNode::add_content(const string& c) { return add_child_copy(XMLNode (string(), c)); } -XMLProperty * -XMLNode::property(const char * n) +XMLProperty* +XMLNode::property(const char* n) { string ns(n); map::iterator iter; @@ -362,27 +361,30 @@ XMLNode::property(const char * n) return 0; } -XMLProperty * -XMLNode::property(const string & ns) +XMLProperty* +XMLNode::property(const string& ns) { map::iterator iter; if ((iter = _propmap.find(ns)) != _propmap.end()) { return iter->second; } - + return 0; } -XMLProperty * -XMLNode::add_property(const char * n, const string & v) +XMLProperty* +XMLNode::add_property(const char* n, const string& v) { string ns(n); - if(_propmap.find(ns) != _propmap.end()){ - remove_property(ns); + map::iterator iter; + + if ((iter = _propmap.find(ns)) != _propmap.end()) { + iter->second->set_value (v); + return iter->second; } - XMLProperty *tmp = new XMLProperty(ns, v); + XMLProperty* tmp = new XMLProperty(ns, v); if (!tmp) { return 0; @@ -394,36 +396,38 @@ XMLNode::add_property(const char * n, const string & v) return tmp; } -XMLProperty * -XMLNode::add_property(const char * n, const char * v) +XMLProperty* +XMLNode::add_property(const char* n, const char* v) { string vs(v); return add_property(n, vs); } -XMLProperty * -XMLNode::add_property(const char *name, const long value) +XMLProperty* +XMLNode::add_property(const char* name, const long value) { - static char str[1024]; - snprintf(str, 1024, "%ld", value); + char str[64]; + snprintf(str, sizeof(str), "%ld", value); return add_property(name, str); } -void -XMLNode::remove_property(const string & n) +void +XMLNode::remove_property(const string& n) { if (_propmap.find(n) != _propmap.end()) { - _proplist.remove(_propmap[n]); + XMLProperty* p = _propmap[n]; + _proplist.remove (p); + delete p; _propmap.erase(n); } } -void -XMLNode::remove_nodes(const string & n) +void +XMLNode::remove_nodes(const string& n) { XMLNodeIterator i = _children.begin(); XMLNodeIterator tmp; - + while (i != _children.end()) { tmp = i; ++tmp; @@ -434,12 +438,12 @@ XMLNode::remove_nodes(const string & n) } } -void -XMLNode::remove_nodes_and_delete(const string & n) +void +XMLNode::remove_nodes_and_delete(const string& n) { XMLNodeIterator i = _children.begin(); XMLNodeIterator tmp; - + while (i != _children.end()) { tmp = i; ++tmp; @@ -452,7 +456,7 @@ XMLNode::remove_nodes_and_delete(const string & n) } void -XMLNode::remove_nodes_and_delete(const string& propname, const string& val) +XMLNode::remove_nodes_and_delete(const string& propname, const string& val) { XMLNodeIterator i = _children.begin(); XMLNodeIterator tmp; @@ -463,7 +467,7 @@ XMLNode::remove_nodes_and_delete(const string& propname, const string& val) ++tmp; prop = (*i)->property(propname); - if(prop && prop->value() == val) { + if (prop && prop->value() == val) { delete *i; _children.erase(i); } @@ -472,96 +476,100 @@ XMLNode::remove_nodes_and_delete(const string& propname, const string& val) } } -XMLProperty::XMLProperty(const string &n, const string &v) - : _name(n), - _value(v) -{ +XMLProperty::XMLProperty(const string& n, const string& v) + : _name(n) + , _value(v) +{ + // Normalize property name (replace '_' with '-' as old session are inconsistent) + for (size_t i = 0; i < _name.length(); ++i) { + if (_name[i] == '_') { + _name[i] = '-'; + } + } } XMLProperty::~XMLProperty() { } -static XMLNode * +static XMLNode* readnode(xmlNodePtr node) { string name, content; xmlNodePtr child; - XMLNode *tmp; + XMLNode* tmp; xmlAttrPtr attr; - + if (node->name) { - name = (char *) node->name; + name = (char*)node->name; } - + tmp = new XMLNode(name); - + for (attr = node->properties; attr; attr = attr->next) { content = ""; if (attr->children) { - content = (char *) attr->children->content; + content = (char*)attr->children->content; } - tmp->add_property((char *) attr->name, content); + tmp->add_property((char*)attr->name, content); } - + if (node->content) { - tmp->set_content((char *) node->content); + tmp->set_content((char*)node->content); } else { tmp->set_content(string()); } - + for (child = node->children; child; child = child->next) { tmp->add_child_nocopy (*readnode(child)); } - + return tmp; } -static void -writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0) +static void +writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0) { XMLPropertyList props; XMLPropertyIterator curprop; XMLNodeList children; XMLNodeIterator curchild; xmlNodePtr node; - + if (root) { - node = doc->children = xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0); + node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0); } else { - node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0); + node = xmlNewChild(p, 0, (xmlChar*) n->name().c_str(), 0); } - + if (n->is_content()) { node->type = XML_TEXT_NODE; - xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(), n->content().length()); + xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length()); } - + props = n->properties(); for (curprop = props.begin(); curprop != props.end(); ++curprop) { - xmlSetProp(node, (xmlChar *) (*curprop)->name().c_str(), (xmlChar *) (*curprop)->value().c_str()); + xmlSetProp(node, (xmlChar*) (*curprop)->name().c_str(), (xmlChar*) (*curprop)->value().c_str()); } - + children = n->children(); for (curchild = children.begin(); curchild != children.end(); ++curchild) { writenode(doc, *curchild, node); } } -static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath) +static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath) { xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt); - if(!result) - { + if (!result) { xmlXPathFreeContext(ctxt); xmlFreeDoc(ctxt->doc); throw XMLException("Invalid XPath: " + xpath); } - if(result->type != XPATH_NODESET) - { + if (result->type != XPATH_NODESET) { xmlXPathFreeObject(result); xmlXPathFreeContext(ctxt); xmlFreeDoc(ctxt->doc); @@ -571,15 +579,12 @@ static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath) xmlNodeSet* nodeset = result->nodesetval; XMLSharedNodeList* nodes = new XMLSharedNodeList(); - if( nodeset ) - { + if (nodeset) { for (int i = 0; i < nodeset->nodeNr; ++i) { XMLNode* node = readnode(nodeset->nodeTab[i]); nodes->push_back(boost::shared_ptr(node)); } - } - else - { + } else { // return empty set } @@ -588,3 +593,17 @@ static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath) return nodes; } +/** Dump a node, its properties and children to a stream */ +void +XMLNode::dump (ostream& s, string p) const +{ + s << p << _name << " "; + for (XMLPropertyList::const_iterator i = _proplist.begin(); i != _proplist.end(); ++i) { + s << (*i)->name() << "=" << (*i)->value() << " "; + } + s << "\n"; + + for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) { + (*i)->dump (s, p + " "); + } +}