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
|
|
|
*/
|
|
|
|
|
2010-05-08 20:46:33 -04:00
|
|
|
#include <iostream>
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/xml++.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <libxml/debugXML.h>
|
|
|
|
#include <libxml/xpath.h>
|
|
|
|
#include <libxml/xpathInternals.h>
|
|
|
|
|
|
|
|
#define XML_VERSION "1.0"
|
|
|
|
|
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
|
|
|
{
|
2012-03-20 14:01:07 -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;
|
|
|
|
}
|
|
|
|
|
2009-10-02 07:16:15 -04:00
|
|
|
xmlParserCtxtPtr ctxt = NULL; /* the parser context */
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
/* parse the file, activating the DTD validation option */
|
2009-02-22 15:52:34 -05:00
|
|
|
if (validate) {
|
2008-06-02 17:41:35 -04:00
|
|
|
/* create a parser context */
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if (ctxt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
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 {
|
2012-03-20 14:01:07 -04:00
|
|
|
_doc = xmlParseFile(_filename.c_str());
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2012-03-20 14:01:07 -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) {
|
2009-02-22 15:52:34 -05:00
|
|
|
if (validate) {
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlFreeParserCtxt(ctxt);
|
|
|
|
}
|
|
|
|
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 */
|
2009-02-22 15:52:34 -05:00
|
|
|
if (validate) {
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlFreeParserCtxt(ctxt);
|
|
|
|
}
|
2012-03-20 14:01:07 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
bool
|
|
|
|
XMLTree::read_buffer(const string& buffer)
|
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
|
|
|
|
|
|
|
doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
|
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));
|
|
|
|
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);
|
2009-02-22 15:52:34 -05:00
|
|
|
doc = xmlNewDoc((xmlChar*) XML_VERSION);
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
|
|
|
result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
|
|
|
|
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
|
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault(0);
|
2009-02-22 15:52:34 -05:00
|
|
|
doc = xmlNewDoc((xmlChar*) XML_VERSION);
|
2008-06-02 17:41:35 -04:00
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
|
|
|
xmlDebugDumpDocument (out, doc);
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
}
|
|
|
|
|
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);
|
2009-02-22 15:52:34 -05:00
|
|
|
doc = xmlNewDoc((xmlChar*) 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::XMLNode(const XMLNode& from)
|
|
|
|
{
|
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 ();
|
|
|
|
_propmap.clear ();
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
|
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
doc = xmlNewDoc((xmlChar*) XML_VERSION);
|
|
|
|
writenode(doc, node, doc->children, 1);
|
|
|
|
ctxt = xmlXPathNewContext(doc);
|
|
|
|
} else {
|
|
|
|
ctxt = xmlXPathNewContext(_doc);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
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();
|
|
|
|
assert(!_is_content);
|
|
|
|
assert(children.size() == 1);
|
|
|
|
XMLNode* child = *(children.begin());
|
|
|
|
assert(child->is_content());
|
|
|
|
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
|
|
|
{
|
|
|
|
return add_child_copy(XMLNode (string(), c));
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty*
|
|
|
|
XMLNode::property(const char* n)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
string ns(n);
|
|
|
|
map<string,XMLProperty*>::iterator iter;
|
|
|
|
|
|
|
|
if ((iter = _propmap.find(ns)) != _propmap.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty*
|
|
|
|
XMLNode::property(const string& ns)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
map<string,XMLProperty*>::iterator iter;
|
|
|
|
|
|
|
|
if ((iter = _propmap.find(ns)) != _propmap.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
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
|
|
|
XMLProperty*
|
|
|
|
XMLNode::add_property(const char* n, const string& v)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
string ns(n);
|
2011-03-12 15:23:56 -05:00
|
|
|
map<string,XMLProperty*>::iterator iter;
|
|
|
|
|
|
|
|
if ((iter = _propmap.find(ns)) != _propmap.end()) {
|
|
|
|
iter->second->set_value (v);
|
|
|
|
return iter->second;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty* tmp = new XMLProperty(ns, v);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
if (!tmp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_propmap[tmp->name()] = tmp;
|
|
|
|
_proplist.insert(_proplist.end(), tmp);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty*
|
|
|
|
XMLNode::add_property(const char* n, const char* v)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
string vs(v);
|
|
|
|
return add_property(n, vs);
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty*
|
|
|
|
XMLNode::add_property(const char* name, const long value)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2011-03-12 15:29:40 -05:00
|
|
|
char str[64];
|
|
|
|
snprintf(str, sizeof(str), "%ld", value);
|
2008-06-02 17:41:35 -04:00
|
|
|
return add_property(name, str);
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
void
|
|
|
|
XMLNode::remove_property(const string& n)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
if (_propmap.find(n) != _propmap.end()) {
|
2009-10-20 14:44:44 -04:00
|
|
|
XMLProperty* p = _propmap[n];
|
|
|
|
_proplist.remove (p);
|
|
|
|
delete p;
|
2008-06-02 17:41:35 -04:00
|
|
|
_propmap.erase(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
XMLNodeIterator tmp;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
while (i != _children.end()) {
|
|
|
|
tmp = i;
|
|
|
|
++tmp;
|
|
|
|
if ((*i)->name() == n) {
|
|
|
|
_children.erase (i);
|
|
|
|
}
|
|
|
|
i = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
XMLNodeIterator tmp;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
while (i != _children.end()) {
|
|
|
|
tmp = i;
|
|
|
|
++tmp;
|
|
|
|
if ((*i)->name() == n) {
|
|
|
|
delete *i;
|
|
|
|
_children.erase (i);
|
|
|
|
}
|
|
|
|
i = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
XMLNodeIterator tmp;
|
|
|
|
XMLProperty* prop;
|
|
|
|
|
|
|
|
while (i != _children.end()) {
|
|
|
|
tmp = i;
|
|
|
|
++tmp;
|
|
|
|
|
|
|
|
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;
|
|
|
|
_children.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:52:34 -05:00
|
|
|
XMLProperty::XMLProperty(const string& n, const string& v)
|
|
|
|
: _name(n)
|
|
|
|
, _value(v)
|
|
|
|
{
|
2008-10-05 19:14:48 -04:00
|
|
|
// Normalize property name (replace '_' with '-' as old session are inconsistent)
|
|
|
|
for (size_t i = 0; i < _name.length(); ++i) {
|
|
|
|
if (_name[i] == '_') {
|
|
|
|
_name[i] = '-';
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2009-02-22 15:52:34 -05:00
|
|
|
name = (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
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
tmp->add_property((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
|
|
|
{
|
|
|
|
XMLPropertyList props;
|
|
|
|
XMLPropertyIterator curprop;
|
|
|
|
XMLNodeList children;
|
|
|
|
XMLNodeIterator curchild;
|
|
|
|
xmlNodePtr node;
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (root) {
|
2009-02-22 15:52:34 -05:00
|
|
|
node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0);
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
2009-02-22 15:52:34 -05:00
|
|
|
node = xmlNewChild(p, 0, (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
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
props = n->properties();
|
|
|
|
for (curprop = props.begin(); curprop != props.end(); ++curprop) {
|
2009-02-22 15:52:34 -05:00
|
|
|
xmlSetProp(node, (xmlChar*) (*curprop)->name().c_str(), (xmlChar*) (*curprop)->value().c_str());
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2009-02-22 15:52:34 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
children = n->children();
|
|
|
|
for (curchild = children.begin(); curchild != children.end(); ++curchild) {
|
|
|
|
writenode(doc, *curchild, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
xmlXPathFreeContext(ctxt);
|
|
|
|
xmlFreeDoc(ctxt->doc);
|
|
|
|
|
|
|
|
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);
|
|
|
|
xmlXPathFreeContext(ctxt);
|
|
|
|
xmlFreeDoc(ctxt->doc);
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
|
|
|
|
(*i)->dump (s, p + " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
s << p << "</" << _name << ">\n";
|
2010-05-08 20:46:33 -04:00
|
|
|
}
|
|
|
|
}
|