2008-06-02 17:41:35 -04:00
|
|
|
/* xml++.cc
|
|
|
|
* 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.
|
2009-02-22 15:52:34 -05:00
|
|
|
* Modified for Ardour and released under the same terms.
|
2008-06-02 17:41:35 -04:00
|
|
|
*/
|
|
|
|
|
2020-04-23 19:53:59 -04:00
|
|
|
#include <string.h>
|
2012-06-23 08:54:11 -04:00
|
|
|
#include <iostream>
|
2016-05-04 23:09:37 -04:00
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/xml++.h"
|
2016-05-04 23:09:37 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <libxml/debugXML.h>
|
|
|
|
#include <libxml/xpath.h>
|
|
|
|
#include <libxml/xpathInternals.h>
|
|
|
|
|
2013-03-24 09:55:56 -04:00
|
|
|
xmlChar* xml_version = xmlCharStrdup("1.0");
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-05-12 13:03:42 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
static XMLNode* readnode(xmlNodePtr);
|
|
|
|
static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int);
|
|
|
|
static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLTree::XMLTree()
|
|
|
|
: _filename()
|
|
|
|
, _root(0)
|
2012-03-20 14:01:07 -04:00
|
|
|
, _doc (0)
|
2009-02-22 15:52:34 -05:00
|
|
|
, _compression(0)
|
|
|
|
{
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLTree::XMLTree(const string& fn, bool validate)
|
|
|
|
: _filename(fn)
|
|
|
|
, _root(0)
|
2012-03-20 14:01:07 -04:00
|
|
|
, _doc (0)
|
2009-02-22 15:52:34 -05:00
|
|
|
, _compression(0)
|
|
|
|
{
|
|
|
|
read_internal(validate);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLTree::XMLTree(const XMLTree* from)
|
|
|
|
: _filename(from->filename())
|
|
|
|
, _root(new XMLNode(*from->root()))
|
2012-03-20 14:01:07 -04:00
|
|
|
, _doc (xmlCopyDoc (from->_doc, 1))
|
2009-02-22 15:52:34 -05:00
|
|
|
, _compression(from->compression())
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLTree::~XMLTree()
|
|
|
|
{
|
2008-12-18 14:31:00 -05:00
|
|
|
delete _root;
|
2012-03-20 14:01:07 -04:00
|
|
|
|
|
|
|
if (_doc) {
|
|
|
|
xmlFreeDoc (_doc);
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
int
|
2008-06-02 17:41:35 -04:00
|
|
|
XMLTree::set_compression(int c)
|
|
|
|
{
|
|
|
|
if (c > 9) {
|
|
|
|
c = 9;
|
|
|
|
} else if (c < 0) {
|
|
|
|
c = 0;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_compression = c;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return _compression;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
bool
|
2008-06-02 17:41:35 -04:00
|
|
|
XMLTree::read_internal(bool validate)
|
|
|
|
{
|
|
|
|
//shouldnt be used anywhere ATM, remove if so!
|
|
|
|
assert(!validate);
|
2008-12-18 14:31:00 -05:00
|
|
|
|
|
|
|
delete _root;
|
|
|
|
_root = 0;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2012-03-20 14:01:07 -04:00
|
|
|
if (_doc) {
|
|
|
|
xmlFreeDoc (_doc);
|
|
|
|
_doc = 0;
|
|
|
|
}
|
|
|
|
|
2020-01-05 19:49:35 -05:00
|
|
|
/* Calling this prevents libxml2 from treating whitespace as active
|
|
|
|
nodes. It needs to be called before we create a parser context.
|
|
|
|
*/
|
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
|
2015-10-07 08:36:32 -04:00
|
|
|
/* create a parser context */
|
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if (ctxt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* parse the file, activating the DTD validation option */
|
2009-02-22 15:52:34 -05:00
|
|
|
if (validate) {
|
2012-03-20 14:01:07 -04:00
|
|
|
_doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
2015-10-07 08:36:32 -04:00
|
|
|
_doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_HUGE);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* check if parsing suceeded */
|
2012-03-20 14:01:07 -04:00
|
|
|
if (_doc == NULL) {
|
2015-10-07 08:36:32 -04:00
|
|
|
xmlFreeParserCtxt(ctxt);
|
2008-06-02 17:41:35 -04:00
|
|
|
return false;
|
|
|
|
} else {
|
2009-02-22 15:52:34 -05:00
|
|
|
/* check if validation suceeded */
|
2008-06-02 17:41:35 -04:00
|
|
|
if (validate && ctxt->valid == 0) {
|
|
|
|
xmlFreeParserCtxt(ctxt);
|
|
|
|
throw XMLException("Failed to validate document " + _filename);
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2012-03-20 14:01:07 -04:00
|
|
|
_root = readnode(xmlDocGetRootElement(_doc));
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* free up the parser context */
|
2015-10-07 08:36:32 -04:00
|
|
|
xmlFreeParserCtxt(ctxt);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
bool
|
2020-04-22 20:25:38 -04:00
|
|
|
XMLTree::read_buffer (char const* buffer, bool to_tree_doc)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_filename = "";
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-12-18 14:31:00 -05:00
|
|
|
delete _root;
|
|
|
|
_root = 0;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2020-04-22 20:27:04 -04:00
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
|
2020-04-23 19:53:59 -04:00
|
|
|
doc = xmlParseMemory (buffer, ::strlen(buffer));
|
2008-06-02 17:41:35 -04:00
|
|
|
if (!doc) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_root = readnode(xmlDocGetRootElement(doc));
|
2016-10-29 11:03:34 -04:00
|
|
|
if (to_tree_doc) {
|
|
|
|
if (_doc) {
|
|
|
|
xmlFreeDoc (_doc);
|
|
|
|
}
|
|
|
|
_doc = doc;
|
|
|
|
} else {
|
|
|
|
xmlFreeDoc (doc);
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
bool
|
|
|
|
XMLTree::write() const
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
|
|
|
int result;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlKeepBlanksDefault(0);
|
2013-03-24 09:55:56 -04:00
|
|
|
doc = xmlNewDoc(xml_version);
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
2015-07-25 06:09:32 -04:00
|
|
|
result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
|
2014-07-05 14:53:22 -04:00
|
|
|
#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
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlFreeDoc(doc);
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (result == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLTree::debug(FILE* out) const
|
|
|
|
{
|
2013-07-17 03:08:46 -04:00
|
|
|
#ifdef LIBXML_DEBUG_ENABLED
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault(0);
|
2013-03-24 09:55:56 -04:00
|
|
|
doc = xmlNewDoc(xml_version);
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
|
|
|
xmlDebugDumpDocument (out, doc);
|
|
|
|
xmlFreeDoc(doc);
|
2013-07-17 03:08:46 -04:00
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
const string&
|
|
|
|
XMLTree::write_buffer() const
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
static string retval;
|
2009-02-22 15:52:34 -05:00
|
|
|
char* ptr;
|
2008-06-02 17:41:35 -04:00
|
|
|
int len;
|
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlKeepBlanksDefault(0);
|
2013-03-24 09:55:56 -04:00
|
|
|
doc = xmlNewDoc(xml_version);
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
|
|
|
xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
|
|
|
|
xmlFreeDoc(doc);
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
retval = ptr;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
free(ptr);
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-09-23 09:01:18 -04:00
|
|
|
static const int PROPERTY_RESERVE_COUNT = 16;
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode::XMLNode(const string& n)
|
|
|
|
: _name(n)
|
|
|
|
, _is_content(false)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2016-09-23 09:01:18 -04:00
|
|
|
_proplist.reserve (PROPERTY_RESERVE_COUNT);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode::XMLNode(const string& n, const string& c)
|
|
|
|
: _name(n)
|
|
|
|
, _is_content(true)
|
|
|
|
, _content(c)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2016-09-23 09:01:18 -04:00
|
|
|
_proplist.reserve (PROPERTY_RESERVE_COUNT);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::XMLNode(const XMLNode& from)
|
|
|
|
{
|
2016-09-23 09:01:18 -04:00
|
|
|
_proplist.reserve (PROPERTY_RESERVE_COUNT);
|
2012-03-06 21:07:35 -05:00
|
|
|
*this = from;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::~XMLNode()
|
2012-03-06 21:07:35 -05:00
|
|
|
{
|
|
|
|
clear_lists ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLNode::clear_lists ()
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
XMLNodeIterator curchild;
|
|
|
|
XMLPropertyIterator curprop;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2012-03-06 21:07:35 -05:00
|
|
|
_selected_children.clear ();
|
|
|
|
|
2019-04-13 13:19:29 -04:00
|
|
|
for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
|
2008-06-02 17:41:35 -04:00
|
|
|
delete *curchild;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2012-03-06 21:07:35 -05:00
|
|
|
_children.clear ();
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
|
|
|
|
delete *curprop;
|
|
|
|
}
|
2012-03-06 21:07:35 -05:00
|
|
|
|
|
|
|
_proplist.clear ();
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
XMLNode&
|
2012-03-06 21:07:35 -05:00
|
|
|
XMLNode::operator= (const XMLNode& from)
|
|
|
|
{
|
2016-09-22 08:38:12 -04:00
|
|
|
if (&from == this) {
|
|
|
|
return *this;
|
|
|
|
}
|
2012-03-06 21:07:35 -05:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
clear_lists ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
_name = from.name ();
|
|
|
|
set_content (from.content ());
|
2012-03-06 21:07:35 -05:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
const XMLPropertyList& props = from.properties ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
for (XMLPropertyConstIterator prop_iter = props.begin (); prop_iter != props.end (); ++prop_iter) {
|
2016-08-30 04:59:05 -04:00
|
|
|
set_property ((*prop_iter)->name ().c_str (), (*prop_iter)->value ());
|
2016-09-22 08:38:12 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
const XMLNodeList& nodes = from.children ();
|
|
|
|
for (XMLNodeConstIterator child_iter = nodes.begin (); child_iter != nodes.end (); ++child_iter) {
|
|
|
|
add_child_copy (**child_iter);
|
2012-03-06 21:07:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2016-09-25 22:16:01 -04:00
|
|
|
bool
|
|
|
|
XMLNode::operator== (const XMLNode& other) const
|
|
|
|
{
|
|
|
|
if (is_content () != other.is_content ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_content ()) {
|
|
|
|
if (content () != other.content ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (name () != other.name ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLPropertyList const& other_properties = other.properties ();
|
|
|
|
|
|
|
|
if (_proplist.size () != other_properties.size ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLPropertyConstIterator our_prop_iter = _proplist.begin();
|
|
|
|
XMLPropertyConstIterator other_prop_iter = other_properties.begin();
|
|
|
|
|
|
|
|
while (our_prop_iter != _proplist.end ()) {
|
|
|
|
XMLProperty const* our_prop = *our_prop_iter;
|
|
|
|
XMLProperty const* other_prop = *other_prop_iter;
|
|
|
|
if (our_prop->name () != other_prop->name () || our_prop->value () != other_prop->value ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++our_prop_iter;
|
|
|
|
++other_prop_iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNodeList const& other_children = other.children();
|
|
|
|
|
|
|
|
if (_children.size() != other_children.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNodeConstIterator our_child_iter = _children.begin ();
|
|
|
|
XMLNodeConstIterator other_child_iter = other_children.begin ();
|
|
|
|
|
|
|
|
while (our_child_iter != _children.end()) {
|
|
|
|
XMLNode const* our_child = *our_child_iter;
|
|
|
|
XMLNode const* other_child = *other_child_iter;
|
|
|
|
|
|
|
|
if (*our_child != *other_child) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++our_child_iter;
|
|
|
|
++other_child_iter;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XMLNode::operator!= (const XMLNode& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
const string&
|
|
|
|
XMLNode::set_content(const string& c)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
if (c.empty()) {
|
|
|
|
_is_content = false;
|
|
|
|
} else {
|
|
|
|
_is_content = true;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
_content = c;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return _content;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode*
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode::child (const char* name) const
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
/* returns first child matching name */
|
|
|
|
|
|
|
|
XMLNodeConstIterator cur;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (name == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
for (cur = _children.begin(); cur != _children.end(); ++cur) {
|
|
|
|
if ((*cur)->name() == name) {
|
|
|
|
return *cur;
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
const XMLNodeList&
|
2008-06-02 17:41:35 -04:00
|
|
|
XMLNode::children(const string& n) const
|
|
|
|
{
|
|
|
|
/* returns all children matching name */
|
|
|
|
|
|
|
|
XMLNodeConstIterator cur;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (n.empty()) {
|
|
|
|
return _children;
|
|
|
|
}
|
2008-09-10 17:27:39 -04:00
|
|
|
|
|
|
|
_selected_children.clear();
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
for (cur = _children.begin(); cur != _children.end(); ++cur) {
|
|
|
|
if ((*cur)->name() == n) {
|
2008-09-10 17:27:39 -04:00
|
|
|
_selected_children.insert(_selected_children.end(), *cur);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-09-10 17:27:39 -04:00
|
|
|
return _selected_children;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode*
|
|
|
|
XMLNode::add_child(const char* n)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
return add_child_copy(XMLNode (n));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode::add_child_nocopy(XMLNode& n)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
_children.insert(_children.end(), &n);
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode*
|
2008-06-02 17:41:35 -04:00
|
|
|
XMLNode::add_child_copy(const XMLNode& n)
|
|
|
|
{
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode *copy = new XMLNode(n);
|
2008-06-02 17:41:35 -04:00
|
|
|
_children.insert(_children.end(), copy);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
boost::shared_ptr<XMLSharedNodeList>
|
2012-03-20 14:01:07 -04:00
|
|
|
XMLTree::find(const string xpath, XMLNode* node) const
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2012-03-20 14:01:07 -04:00
|
|
|
xmlXPathContext* ctxt;
|
|
|
|
xmlDocPtr doc = 0;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2012-03-20 14:01:07 -04:00
|
|
|
if (node) {
|
2013-03-24 09:55:56 -04:00
|
|
|
doc = xmlNewDoc(xml_version);
|
2012-03-20 14:01:07 -04:00
|
|
|
writenode(doc, node, doc->children, 1);
|
|
|
|
ctxt = xmlXPathNewContext(doc);
|
|
|
|
} else {
|
|
|
|
ctxt = xmlXPathNewContext(_doc);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
boost::shared_ptr<XMLSharedNodeList> result =
|
2012-03-20 14:01:07 -04:00
|
|
|
boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlXPathFreeContext(ctxt);
|
2012-03-20 14:01:07 -04:00
|
|
|
if (doc) {
|
|
|
|
xmlFreeDoc (doc);
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
std::string
|
2008-06-02 17:41:35 -04:00
|
|
|
XMLNode::attribute_value()
|
|
|
|
{
|
|
|
|
XMLNodeList children = this->children();
|
2017-08-30 10:32:21 -04:00
|
|
|
if (_is_content)
|
|
|
|
throw XMLException("XMLNode: attribute_value failed (is_content) for requested node: " + name());
|
|
|
|
|
|
|
|
if (children.size() != 1)
|
|
|
|
throw XMLException("XMLNode: attribute_value failed (children.size != 1) for requested node: " + name());
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
XMLNode* child = *(children.begin());
|
2017-08-30 10:32:21 -04:00
|
|
|
if (!child->is_content())
|
|
|
|
throw XMLException("XMLNode: attribute_value failed (!child->is_content()) for requested node: " + name());
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return child->content();
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode*
|
|
|
|
XMLNode::add_content(const string& c)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2019-11-01 10:39:44 -04:00
|
|
|
if (c.empty ()) {
|
|
|
|
/* this would add a "</>" child, leading to invalid XML.
|
|
|
|
* Also in XML, empty string content is equivalent to no content.
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
return add_child_copy(XMLNode (string(), c));
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:09:37 -04:00
|
|
|
XMLProperty const *
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLNode::property(const char* name) const
|
2016-05-04 23:09:37 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyConstIterator iter = _proplist.begin();
|
2016-05-04 23:09:37 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name) {
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
2016-05-04 23:09:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty const *
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLNode::property(const string& name) const
|
2016-05-04 23:09:37 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyConstIterator iter = _proplist.begin();
|
2016-05-04 23:09:37 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name) {
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
2016-05-04 23:09:37 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty *
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLNode::property(const char* name)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyIterator iter = _proplist.begin();
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name) {
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:09:37 -04:00
|
|
|
XMLProperty *
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLNode::property(const string& name)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyIterator iter = _proplist.begin();
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name) {
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-02 20:07:40 -04:00
|
|
|
bool
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLNode::has_property_with_value (const string& name, const string& value) const
|
2016-05-02 20:07:40 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyConstIterator iter = _proplist.begin();
|
|
|
|
|
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name && (*iter)->value() == value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++iter;
|
2016-05-02 20:07:40 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-30 04:59:05 -04:00
|
|
|
bool
|
|
|
|
XMLNode::set_property(const char* name, const string& value)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyIterator iter = _proplist.begin();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name) {
|
|
|
|
(*iter)->set_value (value);
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLProperty* new_property = new XMLProperty(name, value);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
if (!new_property) {
|
2008-06-02 17:41:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
_proplist.insert(_proplist.end(), new_property);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2016-09-23 08:56:36 -04:00
|
|
|
return new_property;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:08:32 -04:00
|
|
|
bool
|
|
|
|
XMLNode::get_property(const char* name, std::string& value) const
|
|
|
|
{
|
|
|
|
XMLProperty const* const prop = property (name);
|
|
|
|
if (!prop)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
value = prop->value ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
void
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLNode::remove_property(const string& name)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2016-09-23 08:56:36 -04:00
|
|
|
XMLPropertyIterator iter = _proplist.begin();
|
|
|
|
|
|
|
|
while (iter != _proplist.end()) {
|
|
|
|
if ((*iter)->name() == name) {
|
|
|
|
XMLProperty* property = *iter;
|
|
|
|
_proplist.erase (iter);
|
|
|
|
delete property;
|
|
|
|
break;
|
2016-04-08 15:03:23 -04:00
|
|
|
}
|
2016-09-23 08:56:36 -04:00
|
|
|
++iter;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:03:59 -05:00
|
|
|
/** Remove any property with the given name from this node and its children */
|
|
|
|
void
|
|
|
|
XMLNode::remove_property_recursively(const string& n)
|
|
|
|
{
|
|
|
|
remove_property (n);
|
|
|
|
for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
|
|
|
|
(*i)->remove_property_recursively (n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
void
|
|
|
|
XMLNode::remove_nodes(const string& n)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
XMLNodeIterator i = _children.begin();
|
|
|
|
while (i != _children.end()) {
|
|
|
|
if ((*i)->name() == n) {
|
2016-04-08 15:03:23 -04:00
|
|
|
i = _children.erase (i);
|
|
|
|
} else {
|
|
|
|
++i;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
void
|
|
|
|
XMLNode::remove_nodes_and_delete(const string& n)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
XMLNodeIterator i = _children.begin();
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
while (i != _children.end()) {
|
|
|
|
if ((*i)->name() == n) {
|
|
|
|
delete *i;
|
2016-04-08 15:03:23 -04:00
|
|
|
i = _children.erase (i);
|
|
|
|
} else {
|
|
|
|
++i;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
XMLNodeIterator i = _children.begin();
|
2016-05-04 23:09:37 -04:00
|
|
|
XMLProperty const * prop;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
while (i != _children.end()) {
|
|
|
|
prop = (*i)->property(propname);
|
2009-02-22 15:52:34 -05:00
|
|
|
if (prop && prop->value() == val) {
|
2008-06-02 17:41:35 -04:00
|
|
|
delete *i;
|
2016-04-08 15:03:23 -04:00
|
|
|
i = _children.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:46:22 -04:00
|
|
|
void
|
2019-04-13 13:19:29 -04:00
|
|
|
XMLNode::remove_node_and_delete(const string& n, const string& propname, const string& val)
|
2017-05-28 16:46:22 -04:00
|
|
|
{
|
|
|
|
for (XMLNodeIterator i = _children.begin(); i != _children.end(); ++i) {
|
|
|
|
if ((*i)->name() == n) {
|
|
|
|
XMLProperty const * prop = (*i)->property (propname);
|
|
|
|
if (prop && prop->value() == val) {
|
|
|
|
delete *i;
|
2017-05-29 17:45:16 -04:00
|
|
|
_children.erase (i);
|
2017-05-28 16:46:22 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty::XMLProperty(const string& n, const string& v)
|
|
|
|
: _name(n)
|
|
|
|
, _value(v)
|
|
|
|
{
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty::~XMLProperty()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
static XMLNode*
|
2008-06-02 17:41:35 -04:00
|
|
|
readnode(xmlNodePtr node)
|
|
|
|
{
|
|
|
|
string name, content;
|
|
|
|
xmlNodePtr child;
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLNode* tmp;
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlAttrPtr attr;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (node->name) {
|
2013-03-24 09:55:56 -04:00
|
|
|
name = (const char*)node->name;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
tmp = new XMLNode(name);
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
for (attr = node->properties; attr; attr = attr->next) {
|
|
|
|
content = "";
|
|
|
|
if (attr->children) {
|
2009-02-22 15:52:34 -05:00
|
|
|
content = (char*)attr->children->content;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2016-08-30 04:59:05 -04:00
|
|
|
tmp->set_property((const char*)attr->name, content);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (node->content) {
|
2009-02-22 15:52:34 -05:00
|
|
|
tmp->set_content((char*)node->content);
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
|
|
|
tmp->set_content(string());
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
for (child = node->children; child; child = child->next) {
|
|
|
|
tmp->add_child_nocopy (*readnode(child));
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
static void
|
|
|
|
writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
xmlNodePtr node;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (root) {
|
2013-03-24 09:55:56 -04:00
|
|
|
node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
2013-03-24 09:55:56 -04:00
|
|
|
node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (n->is_content()) {
|
|
|
|
node->type = XML_TEXT_NODE;
|
2009-02-22 15:52:34 -05:00
|
|
|
xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
const XMLPropertyList& props = n->properties();
|
|
|
|
|
|
|
|
for (XMLPropertyConstIterator prop_iter = props.begin (); prop_iter != props.end ();
|
|
|
|
++prop_iter) {
|
|
|
|
xmlSetProp (node, (const xmlChar*)(*prop_iter)->name ().c_str (),
|
|
|
|
(const xmlChar*)(*prop_iter)->value ().c_str ());
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2016-09-22 08:38:12 -04:00
|
|
|
const XMLNodeList& children = n->children ();
|
|
|
|
for (XMLNodeConstIterator child_iter = children.begin (); child_iter != children.end ();
|
|
|
|
++child_iter) {
|
|
|
|
writenode (doc, *child_iter, node);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
if (!result) {
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlFreeDoc(ctxt->doc);
|
2020-03-11 10:49:42 -04:00
|
|
|
xmlXPathFreeContext(ctxt);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
throw XMLException("Invalid XPath: " + xpath);
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
if (result->type != XPATH_NODESET) {
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlXPathFreeObject(result);
|
|
|
|
xmlFreeDoc(ctxt->doc);
|
2020-03-11 10:49:42 -04:00
|
|
|
xmlXPathFreeContext(ctxt);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
throw XMLException("Only nodeset result types are supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodeSet* nodeset = result->nodesetval;
|
|
|
|
XMLSharedNodeList* nodes = new XMLSharedNodeList();
|
2009-02-22 15:52:34 -05:00
|
|
|
if (nodeset) {
|
2008-06-02 17:41:35 -04:00
|
|
|
for (int i = 0; i < nodeset->nodeNr; ++i) {
|
|
|
|
XMLNode* node = readnode(nodeset->nodeTab[i]);
|
|
|
|
nodes->push_back(boost::shared_ptr<XMLNode>(node));
|
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
} else {
|
2008-06-02 17:41:35 -04:00
|
|
|
// return empty set
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlXPathFreeObject(result);
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2010-05-08 20:46:33 -04:00
|
|
|
/** Dump a node, its properties and children to a stream */
|
|
|
|
void
|
2010-09-03 11:24:21 -04:00
|
|
|
XMLNode::dump (ostream& s, string p) const
|
2010-05-08 20:46:33 -04:00
|
|
|
{
|
2012-06-16 13:20:10 -04:00
|
|
|
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";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2012-06-16 13:20:10 -04:00
|
|
|
for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
|
|
|
|
(*i)->dump (s, p + " ");
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2012-06-16 13:20:10 -04:00
|
|
|
s << p << "</" << _name << ">\n";
|
2010-05-08 20:46:33 -04:00
|
|
|
}
|
|
|
|
}
|