2006-07-05 15:47:25 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pbd/xml++.h>
|
|
|
|
#include <libxml/debugXML.h>
|
|
|
|
|
|
|
|
static XMLNode *readnode(xmlNodePtr);
|
|
|
|
static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int);
|
|
|
|
|
|
|
|
XMLTree::XMLTree()
|
|
|
|
: _filename(),
|
2006-07-27 21:08:57 -04:00
|
|
|
_root(0),
|
|
|
|
_compression(0)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLTree::XMLTree(const string &fn)
|
|
|
|
: _filename(fn),
|
|
|
|
_root(0),
|
2006-07-27 21:08:57 -04:00
|
|
|
_compression(0)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
|
|
|
read();
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLTree::XMLTree(const XMLTree * from)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
_filename = from->filename();
|
|
|
|
_root = new XMLNode(*from->root());
|
|
|
|
_compression = from->compression();
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLTree::~XMLTree()
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
if (_root) {
|
2006-07-05 15:47:25 -04:00
|
|
|
delete _root;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
XMLTree::set_compression(int c)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
if (c > 9) {
|
|
|
|
c = 9;
|
|
|
|
} else if (c < 0) {
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_compression = c;
|
|
|
|
|
|
|
|
return _compression;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XMLTree::read(void)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
xmlDocPtr doc;
|
|
|
|
|
|
|
|
if (_root) {
|
2006-07-05 15:47:25 -04:00
|
|
|
delete _root;
|
|
|
|
_root = 0;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
|
|
|
|
doc = xmlParseFile(_filename.c_str());
|
|
|
|
if (!doc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_root = readnode(xmlDocGetRootElement(doc));
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
|
|
|
|
return true;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XMLTree::read_buffer(const string & buffer)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
xmlDocPtr doc;
|
|
|
|
|
|
|
|
_filename = "";
|
|
|
|
|
|
|
|
if (_root) {
|
2006-07-05 15:47:25 -04:00
|
|
|
delete _root;
|
|
|
|
_root = 0;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
doc = xmlParseMemory((char *) buffer.c_str(), buffer.length());
|
|
|
|
if (!doc) {
|
2006-07-05 15:47:25 -04:00
|
|
|
return false;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_root = readnode(xmlDocGetRootElement(doc));
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
|
|
|
|
return true;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XMLTree::write(void) const
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
doc = xmlNewDoc((xmlChar *) "1.0");
|
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
2006-11-19 11:45:16 -05:00
|
|
|
result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
|
2006-07-27 21:08:57 -04:00
|
|
|
xmlFreeDoc(doc);
|
|
|
|
|
|
|
|
if (result == -1) {
|
2006-07-05 15:47:25 -04:00
|
|
|
return false;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLTree::debug(FILE* out) const
|
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
doc = xmlNewDoc((xmlChar *) "1.0");
|
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
|
|
|
xmlDebugDumpDocument (out, doc);
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const string &
|
|
|
|
XMLTree::write_buffer(void) const
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
static string retval;
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
xmlDocPtr doc;
|
|
|
|
XMLNodeList children;
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
doc = xmlNewDoc((xmlChar *) "1.0");
|
|
|
|
xmlSetDocCompressMode(doc, _compression);
|
|
|
|
writenode(doc, _root, doc->children, 1);
|
|
|
|
xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
|
|
|
|
retval = ptr;
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
|
|
|
|
return retval;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::XMLNode(const string & n)
|
|
|
|
: _name(n), _is_content(false), _content(string())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::XMLNode(const string & n, const string & c)
|
2006-07-27 21:08:57 -04:00
|
|
|
:_name(n), _is_content(true), _content(c)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::XMLNode(const XMLNode& from)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
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) {
|
2006-07-05 15:47:25 -04:00
|
|
|
add_child_copy(**curnode);
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode::~XMLNode()
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
XMLNodeIterator curchild;
|
|
|
|
XMLPropertyIterator curprop;
|
|
|
|
|
|
|
|
for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
|
2006-07-05 15:47:25 -04:00
|
|
|
delete *curchild;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
|
2006-07-05 15:47:25 -04:00
|
|
|
delete *curprop;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const string &
|
|
|
|
XMLNode::set_content(const string & c)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
if (c.empty()) {
|
2006-07-05 15:47:25 -04:00
|
|
|
_is_content = false;
|
2006-07-27 21:08:57 -04:00
|
|
|
} else {
|
2006-07-05 15:47:25 -04:00
|
|
|
_is_content = true;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_content = c;
|
|
|
|
|
|
|
|
return _content;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
2006-11-19 11:45:16 -05:00
|
|
|
XMLNode*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-07-05 15:47:25 -04:00
|
|
|
const XMLNodeList &
|
2006-11-19 11:45:16 -05:00
|
|
|
XMLNode::children(const string& n) const
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-11-19 11:45:16 -05:00
|
|
|
/* returns all children matching name */
|
|
|
|
|
2006-07-27 21:08:57 -04:00
|
|
|
static XMLNodeList retval;
|
|
|
|
XMLNodeConstIterator cur;
|
|
|
|
|
2006-11-19 11:45:16 -05:00
|
|
|
if (n.empty()) {
|
2006-07-05 15:47:25 -04:00
|
|
|
return _children;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
retval.erase(retval.begin(), retval.end());
|
|
|
|
|
|
|
|
for (cur = _children.begin(); cur != _children.end(); ++cur) {
|
|
|
|
if ((*cur)->name() == n) {
|
|
|
|
retval.insert(retval.end(), *cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode *
|
|
|
|
XMLNode::add_child(const char * n)
|
|
|
|
{
|
|
|
|
return add_child_copy(XMLNode (n));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLNode::add_child_nocopy (XMLNode& n)
|
|
|
|
{
|
|
|
|
_children.insert(_children.end(), &n);
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode *
|
|
|
|
XMLNode::add_child_copy(const XMLNode& n)
|
|
|
|
{
|
|
|
|
XMLNode *copy = new XMLNode (n);
|
|
|
|
_children.insert(_children.end(), copy);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode *
|
|
|
|
XMLNode::add_content(const string & c)
|
|
|
|
{
|
|
|
|
return add_child_copy(XMLNode (string(), c));
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty *
|
|
|
|
XMLNode::property(const char * n)
|
|
|
|
{
|
|
|
|
string ns(n);
|
2007-01-11 14:50:49 -05:00
|
|
|
map<string,XMLProperty*>::iterator iter;
|
|
|
|
|
|
|
|
if ((iter = _propmap.find(ns)) != _propmap.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty *
|
|
|
|
XMLNode::property(const string & ns)
|
|
|
|
{
|
|
|
|
map<string,XMLProperty*>::iterator iter;
|
|
|
|
|
|
|
|
if ((iter = _propmap.find(ns)) != _propmap.end()) {
|
|
|
|
return iter->second;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
2007-01-11 14:50:49 -05:00
|
|
|
return 0;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty *
|
|
|
|
XMLNode::add_property(const char * n, const string & v)
|
|
|
|
{
|
|
|
|
string ns(n);
|
|
|
|
if(_propmap.find(ns) != _propmap.end()){
|
|
|
|
remove_property(ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty *tmp = new XMLProperty(ns, v);
|
|
|
|
|
2006-07-27 21:08:57 -04:00
|
|
|
if (!tmp) {
|
2006-07-05 15:47:25 -04:00
|
|
|
return 0;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
|
|
|
|
_propmap[tmp->name()] = tmp;
|
|
|
|
_proplist.insert(_proplist.end(), tmp);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty *
|
|
|
|
XMLNode::add_property(const char * n, const char * v)
|
|
|
|
{
|
|
|
|
string vs(v);
|
|
|
|
return add_property(n, vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLNode::remove_property(const string & n)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
if (_propmap.find(n) != _propmap.end()) {
|
2006-07-05 15:47:25 -04:00
|
|
|
_proplist.remove(_propmap[n]);
|
|
|
|
_propmap.erase(n);
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLNode::remove_nodes(const string & n)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
XMLNodeIterator i = _children.begin();
|
|
|
|
XMLNodeIterator tmp;
|
|
|
|
|
|
|
|
while (i != _children.end()) {
|
|
|
|
tmp = i;
|
|
|
|
++tmp;
|
|
|
|
if ((*i)->name() == n) {
|
|
|
|
_children.erase (i);
|
|
|
|
}
|
|
|
|
i = tmp;
|
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XMLNode::remove_nodes_and_delete(const string & n)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
XMLNodeIterator i = _children.begin();
|
|
|
|
XMLNodeIterator tmp;
|
|
|
|
|
|
|
|
while (i != _children.end()) {
|
|
|
|
tmp = i;
|
|
|
|
++tmp;
|
|
|
|
if ((*i)->name() == n) {
|
|
|
|
delete *i;
|
|
|
|
_children.erase (i);
|
|
|
|
}
|
|
|
|
i = tmp;
|
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty::XMLProperty(const string &n, const string &v)
|
|
|
|
: _name(n),
|
|
|
|
_value(v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty::~XMLProperty()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static XMLNode *
|
|
|
|
readnode(xmlNodePtr node)
|
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
string name, content;
|
|
|
|
xmlNodePtr child;
|
|
|
|
XMLNode *tmp;
|
|
|
|
xmlAttrPtr attr;
|
|
|
|
|
|
|
|
if (node->name) {
|
2006-07-05 15:47:25 -04:00
|
|
|
name = (char *) node->name;
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp = new XMLNode(name);
|
|
|
|
|
|
|
|
for (attr = node->properties; attr; attr = attr->next) {
|
2006-07-05 15:47:25 -04:00
|
|
|
content = "";
|
2006-07-27 21:08:57 -04:00
|
|
|
if (attr->children) {
|
|
|
|
content = (char *) attr->children->content;
|
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
tmp->add_property((char *) attr->name, content);
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node->content) {
|
2006-07-05 15:47:25 -04:00
|
|
|
tmp->set_content((char *) node->content);
|
2006-07-27 21:08:57 -04:00
|
|
|
} else {
|
2006-07-05 15:47:25 -04:00
|
|
|
tmp->set_content(string());
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (child = node->children; child; child = child->next) {
|
|
|
|
tmp->add_child_nocopy (*readnode(child));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-07-27 21:08:57 -04:00
|
|
|
writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-07-27 21:08:57 -04:00
|
|
|
XMLPropertyList props;
|
|
|
|
XMLPropertyIterator curprop;
|
|
|
|
XMLNodeList children;
|
|
|
|
XMLNodeIterator curchild;
|
|
|
|
xmlNodePtr node;
|
|
|
|
|
|
|
|
if (root) {
|
|
|
|
node = doc->children = xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0);
|
|
|
|
} else {
|
2006-07-05 15:47:25 -04:00
|
|
|
node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0);
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n->is_content()) {
|
2006-07-05 15:47:25 -04:00
|
|
|
node->type = XML_TEXT_NODE;
|
2006-07-27 21:08:57 -04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
children = n->children();
|
|
|
|
for (curchild = children.begin(); curchild != children.end(); ++curchild) {
|
2006-07-05 15:47:25 -04:00
|
|
|
writenode(doc, *curchild, node);
|
2006-07-27 21:08:57 -04:00
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|