fix typos in d442190b
[ardour.git] / libs / pbd / xml++.cc
index 58a0c4e7479b4f17a6942f187a8219b334755dad..963531c5f5cca1c7d1880afcc18d36f5e4b28824 100644 (file)
@@ -11,7 +11,7 @@
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
 
-#define XML_VERSION "1.0"
+xmlChar* xml_version = xmlCharStrdup("1.0");
 
 using namespace std;
 
@@ -22,6 +22,7 @@ static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
 XMLTree::XMLTree()
        : _filename()
        , _root(0)
+       , _doc (0)
        , _compression(0)
 {
 }
@@ -29,6 +30,7 @@ XMLTree::XMLTree()
 XMLTree::XMLTree(const string& fn, bool validate)
        : _filename(fn)
        , _root(0)
+       , _doc (0)
        , _compression(0)
 {
        read_internal(validate);
@@ -37,13 +39,19 @@ XMLTree::XMLTree(const string& fn, bool validate)
 XMLTree::XMLTree(const XMLTree* from)
        : _filename(from->filename())
        , _root(new XMLNode(*from->root()))
+       , _doc (xmlCopyDoc (from->_doc, 1))
        , _compression(from->compression())
 {
+
 }
 
 XMLTree::~XMLTree()
 {
        delete _root;
+
+       if (_doc) {
+               xmlFreeDoc (_doc);
+       }
 }
 
 int
@@ -69,44 +77,41 @@ XMLTree::read_internal(bool validate)
        delete _root;
        _root = 0;
 
-       xmlParserCtxtPtr ctxt = NULL; /* the parser context */
-       xmlDocPtr doc; /* the resulting document tree */
+       if (_doc) {
+               xmlFreeDoc (_doc);
+               _doc = 0;
+       }
+
+       /* create a parser context */
+       xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
+       if (ctxt == NULL) {
+               return false;
+       }
 
        xmlKeepBlanksDefault(0);
        /* parse the file, activating the DTD validation option */
        if (validate) {
-               /* create a parser context */
-               ctxt = xmlNewParserCtxt();
-               if (ctxt == NULL) {
-                       return false;
-               }
-               doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
+               _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
        } else {
-               doc = xmlParseFile(_filename.c_str());
+               _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_HUGE);
        }
 
        /* check if parsing suceeded */
-       if (doc == NULL) {
-               if (validate) {
-                       xmlFreeParserCtxt(ctxt);
-               }
+       if (_doc == NULL) {
+               xmlFreeParserCtxt(ctxt);
                return false;
        } else {
                /* check if validation suceeded */
                if (validate && ctxt->valid == 0) {
                        xmlFreeParserCtxt(ctxt);
-                       xmlFreeDoc(doc);
                        throw XMLException("Failed to validate document " + _filename);
                }
        }
 
-       _root = readnode(xmlDocGetRootElement(doc));
+       _root = readnode(xmlDocGetRootElement(_doc));
 
        /* free up the parser context */
-       if (validate) {
-               xmlFreeParserCtxt(ctxt);
-       }
-       xmlFreeDoc(doc);
+       xmlFreeParserCtxt(ctxt);
 
        return true;
 }
@@ -121,7 +126,7 @@ XMLTree::read_buffer(const string& buffer)
        delete _root;
        _root = 0;
 
-       doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
+       doc = xmlParseMemory(const_cast<char*>(buffer.c_str()), buffer.length());
        if (!doc) {
                return false;
        }
@@ -141,10 +146,24 @@ XMLTree::write() const
        int result;
 
        xmlKeepBlanksDefault(0);
-       doc = xmlNewDoc((xmlChar*) XML_VERSION);
+       doc = xmlNewDoc(xml_version);
        xmlSetDocCompressMode(doc, _compression);
        writenode(doc, _root, doc->children, 1);
        result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
+#ifndef NDEBUG
+       if (result == -1) {
+               xmlErrorPtr xerr = xmlGetLastError ();
+               if (!xerr) {
+                       std::cerr << "unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
+               } else {
+                       std::cerr << "xmlSaveFormatFileEnc: error"
+                               << " domain: " << xerr->domain
+                               << " code: " << xerr->code
+                               << " msg: " << xerr->message
+                               << std::endl;
+               }
+       }
+#endif
        xmlFreeDoc(doc);
 
        if (result == -1) {
@@ -157,15 +176,17 @@ XMLTree::write() const
 void
 XMLTree::debug(FILE* out) const
 {
+#ifdef LIBXML_DEBUG_ENABLED
        xmlDocPtr doc;
        XMLNodeList children;
 
        xmlKeepBlanksDefault(0);
-       doc = xmlNewDoc((xmlChar*) XML_VERSION);
+       doc = xmlNewDoc(xml_version);
        xmlSetDocCompressMode(doc, _compression);
        writenode(doc, _root, doc->children, 1);
        xmlDebugDumpDocument (out, doc);
        xmlFreeDoc(doc);
+#endif
 }
 
 const string&
@@ -178,7 +199,7 @@ XMLTree::write_buffer() const
        XMLNodeList children;
 
        xmlKeepBlanksDefault(0);
-       doc = xmlNewDoc((xmlChar*) XML_VERSION);
+       doc = xmlNewDoc(xml_version);
        xmlSetDocCompressMode(doc, _compression);
        writenode(doc, _root, doc->children, 1);
        xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
@@ -206,37 +227,63 @@ XMLNode::XMLNode(const string& n, const string& c)
 
 XMLNode::XMLNode(const XMLNode& from)
 {
-       XMLPropertyList props;
-       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);
-       }
+       *this = from;
 }
 
 XMLNode::~XMLNode()
+{
+       clear_lists ();
+}
+
+void
+XMLNode::clear_lists ()
 {
        XMLNodeIterator curchild;
        XMLPropertyIterator curprop;
 
+       _selected_children.clear ();
+       _propmap.clear ();
+
        for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
                delete *curchild;
        }
 
+       _children.clear ();
+
        for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
                delete *curprop;
        }
+
+       _proplist.clear ();
+}
+
+XMLNode&
+XMLNode::operator= (const XMLNode& from)
+{
+       if (&from != this) {
+
+               XMLPropertyList props;
+               XMLPropertyIterator curprop;
+               XMLNodeList nodes;
+               XMLNodeIterator curnode;
+
+               clear_lists ();
+
+               _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);
+               }
+       }
+
+       return *this;
 }
 
 const string&
@@ -316,17 +363,26 @@ XMLNode::add_child_copy(const XMLNode& n)
 }
 
 boost::shared_ptr<XMLSharedNodeList>
-XMLNode::find(const string xpath) const
+XMLTree::find(const string xpath, XMLNode* node) const
 {
-       xmlDocPtr doc = xmlNewDoc((xmlChar*) XML_VERSION);
-       writenode(doc, (XMLNode*)this, doc->children, 1);
-       xmlXPathContext* ctxt = xmlXPathNewContext(doc);
+       xmlXPathContext* ctxt;
+       xmlDocPtr doc = 0;
+
+       if (node) {
+               doc = xmlNewDoc(xml_version);
+               writenode(doc, node, doc->children, 1);
+               ctxt = xmlXPathNewContext(doc);
+       } else {
+               ctxt = xmlXPathNewContext(_doc);
+       }
 
        boost::shared_ptr<XMLSharedNodeList> result =
-           boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
+               boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
 
        xmlXPathFreeContext(ctxt);
-       xmlFreeDoc(doc);
+       if (doc) {
+               xmlFreeDoc (doc);
+       }
 
        return result;
 }
@@ -378,7 +434,7 @@ XMLNode::add_property(const char* n, const string& v)
 {
        string ns(n);
         map<string,XMLProperty*>::iterator iter;
-       
+
         if ((iter = _propmap.find(ns)) != _propmap.end()) {
                 iter->second->set_value (v);
                 return iter->second;
@@ -511,7 +567,7 @@ readnode(xmlNodePtr node)
        xmlAttrPtr attr;
 
        if (node->name) {
-               name = (char*)node->name;
+               name = (const char*)node->name;
        }
 
        tmp = new XMLNode(name);
@@ -521,7 +577,7 @@ readnode(xmlNodePtr node)
                if (attr->children) {
                        content = (char*)attr->children->content;
                }
-               tmp->add_property((char*)attr->name, content);
+               tmp->add_property((const char*)attr->name, content);
        }
 
        if (node->content) {
@@ -547,9 +603,9 @@ writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
        xmlNodePtr node;
 
        if (root) {
-               node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0);
+               node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
        } else {
-               node = xmlNewChild(p, 0, (xmlChar*) n->name().c_str(), 0);
+               node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
        }
 
        if (n->is_content()) {
@@ -559,7 +615,7 @@ writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
 
        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, (const xmlChar*) (*curprop)->name().c_str(), (const xmlChar*) (*curprop)->value().c_str());
        }
 
        children = n->children();
@@ -607,13 +663,19 @@ static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
 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 + "  ");
+       if (_is_content) {
+               s << p << "  " << content() << "\n";
+       } else {
+               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 + "  ");
+               }
+
+               s << p << "</" << _name << ">\n";
        }
 }