Clean up xml++.h and xml++.cc in Ardour style.

No functional changes.
(We've diverged far enough for it to not matter, and are about to diverge even more, so might as well).


git-svn-id: svn://localhost/ardour2/branches/3.0@4649 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
David Robillard 2009-02-22 20:52:34 +00:00
parent 5b04ddf424
commit c9e5903e73
2 changed files with 245 additions and 252 deletions

View file

@ -2,6 +2,7 @@
* libxml++ and this file are copyright (C) 2000 by Ari Johnson, and * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
* are covered by the GNU Lesser General Public License, which should be * are covered by the GNU Lesser General Public License, which should be
* included with libxml++ as the file COPYING. * included with libxml++ as the file COPYING.
* Modified for Ardour and released under the same terms.
*/ */
#include <string> #include <string>
@ -25,134 +26,127 @@ class XMLTree;
class XMLNode; class XMLNode;
class XMLProperty; class XMLProperty;
typedef list<XMLNode *> XMLNodeList; typedef list<XMLNode *> XMLNodeList;
typedef list<boost::shared_ptr<XMLNode> > XMLSharedNodeList; typedef list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
typedef XMLNodeList::iterator XMLNodeIterator; typedef XMLNodeList::iterator XMLNodeIterator;
typedef XMLNodeList::const_iterator XMLNodeConstIterator; typedef XMLNodeList::const_iterator XMLNodeConstIterator;
typedef list<XMLProperty*> XMLPropertyList; typedef list<XMLProperty*> XMLPropertyList;
typedef XMLPropertyList::iterator XMLPropertyIterator; typedef XMLPropertyList::iterator XMLPropertyIterator;
typedef XMLPropertyList::const_iterator XMLPropertyConstIterator; typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
typedef map<string, XMLProperty*> XMLPropertyMap; typedef map<string, XMLProperty*> XMLPropertyMap;
class XMLTree { class XMLTree {
private:
string _filename;
XMLNode *_root;
int _compression;
bool read_internal(bool validate);
public: public:
XMLTree(); XMLTree();
XMLTree(const string &fn, bool validate = false); XMLTree(const string& fn, bool validate = false);
XMLTree(const XMLTree *); XMLTree(const XMLTree*);
~XMLTree(); ~XMLTree();
XMLNode *root() const { return _root; }; XMLNode* root() const { return _root; }
XMLNode *set_root(XMLNode *n) { return _root = n; }; XMLNode* set_root(XMLNode* n) { return _root = n; }
const string & filename() const { return _filename; }; const string& filename() const { return _filename; }
const string & set_filename(const string &fn) { return _filename = fn; }; const string& set_filename(const string& fn) { return _filename = fn; }
int compression() const { return _compression; }; int compression() const { return _compression; }
int set_compression(int); int set_compression(int);
bool read() { return read_internal(false); }; bool read() { return read_internal(false); }
bool read(const string &fn) { set_filename(fn); return read_internal(false); }; bool read(const string& fn) { set_filename(fn); return read_internal(false); }
bool read_and_validate() { return read_internal(true); }; bool read_and_validate() { return read_internal(true); }
bool read_and_validate(const string &fn) { set_filename(fn); return read_internal(true); }; bool read_and_validate(const string& fn) { set_filename(fn); return read_internal(true); }
bool read_buffer(const string &); bool read_buffer(const string&);
bool write() const; bool write() const;
bool write(const string &fn) { set_filename(fn); return write(); }; bool write(const string& fn) { set_filename(fn); return write(); }
void debug (FILE*) const; void debug (FILE*) const;
const string & write_buffer() const; const string& write_buffer() const;
private:
bool read_internal(bool validate);
string _filename;
XMLNode* _root;
int _compression;
}; };
class XMLNode { class XMLNode {
private:
string _name;
bool _is_content;
string _content;
XMLNodeList _children;
XMLPropertyList _proplist;
XMLPropertyMap _propmap;
mutable XMLNodeList _selected_children;
public: public:
XMLNode(const string& name); XMLNode(const string& name);
XMLNode(const string& name, const string& content); XMLNode(const string& name, const string& content);
XMLNode(const XMLNode& other); XMLNode(const XMLNode& other);
~XMLNode(); ~XMLNode();
const string name() const { return _name; }; const string& name() const { return _name; }
bool is_content() const { return _is_content; }; bool is_content() const { return _is_content; }
const string & content() const { return _content; }; const string& content() const { return _content; }
const string & set_content (const string &); const string& set_content(const string&);
XMLNode *add_content(const string & = string()); XMLNode* add_content(const string& s = string());
const XMLNodeList & children (const string& str = string()) const; const XMLNodeList& children(const string& str = string()) const;
XMLNode *add_child (const char *); XMLNode* child(const char*) const;
XMLNode *add_child_copy (const XMLNode&); XMLNode* add_child(const char *);
XMLNode *child (const char*) const; XMLNode* add_child_copy(const XMLNode&);
void add_child_nocopy (XMLNode&); void add_child_nocopy(XMLNode&);
boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath) const;
std::string attribute_value();
const XMLPropertyList & properties() const { return _proplist; };
XMLProperty *property(const char * );
XMLProperty *property(const std::string&);
const XMLProperty *property(const char * n) const
{ return ((XMLNode *) this)->property(n); };
const XMLProperty *property(const std::string& ns) const
{ return ((XMLNode *) this)->property(ns); };
XMLProperty *add_property(const char *name, const string& value);
XMLProperty *add_property(const char *name, const char *value = "");
XMLProperty *add_property(const char *name, const long value);
void remove_property(const string &); boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath) const;
std::string attribute_value();
/** Remove all nodes with the name passed to remove_nodes */ const XMLPropertyList& properties() const { return _proplist; }
void remove_nodes(const string &); XMLProperty* property(const char*);
/** Remove and delete all nodes with the name passed to remove_nodes */ XMLProperty* property(const string&);
void remove_nodes_and_delete(const string &); const XMLProperty* property(const char* n) const { return ((XMLNode*)this)->property(n); }
/** Remove and delete all nodes with property prop matching val */ const XMLProperty* property(const string& n) const { return ((XMLNode*)this)->property(n); }
void remove_nodes_and_delete(const string& propname, const string& val);
XMLProperty* add_property(const char* name, const string& value);
XMLProperty* add_property(const char* name, const char* value = "");
XMLProperty* add_property(const char* name, const long value);
void remove_property(const string&);
/** Remove all nodes with the name passed to remove_nodes */
void remove_nodes(const string&);
/** Remove and delete all nodes with the name passed to remove_nodes */
void remove_nodes_and_delete(const string&);
/** Remove and delete all nodes with property prop matching val */
void remove_nodes_and_delete(const string& propname, const string& val);
private:
string _name;
bool _is_content;
string _content;
XMLNodeList _children;
XMLPropertyList _proplist;
XMLPropertyMap _propmap;
mutable XMLNodeList _selected_children;
}; };
class XMLProperty { class XMLProperty {
private:
string _name;
string _value;
public: public:
XMLProperty(const string &n, const string &v = string()); XMLProperty(const string& n, const string& v = string());
~XMLProperty(); ~XMLProperty();
const string & name() const { return _name; }; const string& name() const { return _name; }
const string & value() const { return _value; }; const string& value() const { return _value; }
const string & set_value(const string &v) { return _value = v; }; const string& set_value(const string& v) { return _value = v; }
private:
string _name;
string _value;
}; };
class XMLException: public std::exception class XMLException: public std::exception {
{
public: public:
explicit XMLException(const string message) explicit XMLException(const string msg) : _message(msg) {}
: message_(message) virtual ~XMLException() throw() {}
{
}
virtual ~XMLException() throw() {};
virtual const char* what() const throw() { return message_.c_str(); } virtual const char* what() const throw() { return _message.c_str(); }
virtual void Raise() const { throw *this; }
virtual exception * Clone() const { return new exception(*this); }
private: private:
string message_; string _message;
}; };
#endif /* __XML_H */ #endif /* __XML_H */

View file

@ -2,6 +2,7 @@
* libxml++ and this file are copyright (C) 2000 by Ari Johnson, and * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
* are covered by the GNU Lesser General Public License, which should be * are covered by the GNU Lesser General Public License, which should be
* included with libxml++ as the file COPYING. * included with libxml++ as the file COPYING.
* Modified for Ardour and released under the same terms.
*/ */
#include <pbd/xml++.h> #include <pbd/xml++.h>
@ -11,30 +12,30 @@
#define XML_VERSION "1.0" #define XML_VERSION "1.0"
static XMLNode *readnode(xmlNodePtr); static XMLNode* readnode(xmlNodePtr);
static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int); static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int);
static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath); static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
XMLTree::XMLTree() XMLTree::XMLTree()
: _filename(), : _filename()
_root(0), , _root(0)
_compression(0) , _compression(0)
{ {
} }
XMLTree::XMLTree(const string &fn, bool validate) XMLTree::XMLTree(const string& fn, bool validate)
: _filename(fn), : _filename(fn)
_root(0), , _root(0)
_compression(0) , _compression(0)
{ {
read_internal(validate); 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() XMLTree::~XMLTree()
@ -42,7 +43,7 @@ XMLTree::~XMLTree()
delete _root; delete _root;
} }
int int
XMLTree::set_compression(int c) XMLTree::set_compression(int c)
{ {
if (c > 9) { if (c > 9) {
@ -50,13 +51,13 @@ XMLTree::set_compression(int c)
} else if (c < 0) { } else if (c < 0) {
c = 0; c = 0;
} }
_compression = c; _compression = c;
return _compression; return _compression;
} }
bool bool
XMLTree::read_internal(bool validate) XMLTree::read_internal(bool validate)
{ {
//shouldnt be used anywhere ATM, remove if so! //shouldnt be used anywhere ATM, remove if so!
@ -64,13 +65,13 @@ XMLTree::read_internal(bool validate)
delete _root; delete _root;
_root = 0; _root = 0;
xmlParserCtxtPtr ctxt; /* the parser context */ xmlParserCtxtPtr ctxt; /* the parser context */
xmlDocPtr doc; /* the resulting document tree */ xmlDocPtr doc; /* the resulting document tree */
xmlKeepBlanksDefault(0); xmlKeepBlanksDefault(0);
/* parse the file, activating the DTD validation option */ /* parse the file, activating the DTD validation option */
if(validate) { if (validate) {
/* create a parser context */ /* create a parser context */
ctxt = xmlNewParserCtxt(); ctxt = xmlNewParserCtxt();
if (ctxt == NULL) { if (ctxt == NULL) {
@ -80,15 +81,15 @@ XMLTree::read_internal(bool validate)
} else { } else {
doc = xmlParseFile(_filename.c_str()); doc = xmlParseFile(_filename.c_str());
} }
/* check if parsing suceeded */ /* check if parsing suceeded */
if (doc == NULL) { if (doc == NULL) {
if(validate) { if (validate) {
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
} }
return false; return false;
} else { } else {
/* check if validation suceeded */ /* check if validation suceeded */
if (validate && ctxt->valid == 0) { if (validate && ctxt->valid == 0) {
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
xmlFreeDoc(doc); xmlFreeDoc(doc);
@ -96,59 +97,59 @@ XMLTree::read_internal(bool validate)
throw XMLException("Failed to validate document " + _filename); throw XMLException("Failed to validate document " + _filename);
} }
} }
_root = readnode(xmlDocGetRootElement(doc)); _root = readnode(xmlDocGetRootElement(doc));
/* free up the parser context */ /* free up the parser context */
if(validate) { if (validate) {
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
} }
xmlFreeDoc(doc); xmlFreeDoc(doc);
xmlCleanupParser(); xmlCleanupParser();
return true; return true;
} }
bool bool
XMLTree::read_buffer(const string & buffer) XMLTree::read_buffer(const string& buffer)
{ {
xmlDocPtr doc; xmlDocPtr doc;
_filename = ""; _filename = "";
delete _root; delete _root;
_root = 0; _root = 0;
doc = xmlParseMemory((char *) buffer.c_str(), buffer.length()); doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
if (!doc) { if (!doc) {
return false; return false;
} }
_root = readnode(xmlDocGetRootElement(doc)); _root = readnode(xmlDocGetRootElement(doc));
xmlFreeDoc(doc); xmlFreeDoc(doc);
return true; return true;
} }
bool bool
XMLTree::write(void) const XMLTree::write() const
{ {
xmlDocPtr doc; xmlDocPtr doc;
XMLNodeList children; XMLNodeList children;
int result; int result;
xmlKeepBlanksDefault(0); xmlKeepBlanksDefault(0);
doc = xmlNewDoc((xmlChar *) XML_VERSION); doc = xmlNewDoc((xmlChar*) XML_VERSION);
xmlSetDocCompressMode(doc, _compression); xmlSetDocCompressMode(doc, _compression);
writenode(doc, _root, doc->children, 1); writenode(doc, _root, doc->children, 1);
result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1); result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
xmlFreeDoc(doc); xmlFreeDoc(doc);
if (result == -1) { if (result == -1) {
return false; return false;
} }
return true; return true;
} }
@ -159,43 +160,46 @@ XMLTree::debug(FILE* out) const
XMLNodeList children; XMLNodeList children;
xmlKeepBlanksDefault(0); xmlKeepBlanksDefault(0);
doc = xmlNewDoc((xmlChar *) XML_VERSION); doc = xmlNewDoc((xmlChar*) XML_VERSION);
xmlSetDocCompressMode(doc, _compression); xmlSetDocCompressMode(doc, _compression);
writenode(doc, _root, doc->children, 1); writenode(doc, _root, doc->children, 1);
xmlDebugDumpDocument (out, doc); xmlDebugDumpDocument (out, doc);
xmlFreeDoc(doc); xmlFreeDoc(doc);
} }
const string & const string&
XMLTree::write_buffer(void) const XMLTree::write_buffer() const
{ {
static string retval; static string retval;
char *ptr; char* ptr;
int len; int len;
xmlDocPtr doc; xmlDocPtr doc;
XMLNodeList children; XMLNodeList children;
xmlKeepBlanksDefault(0); xmlKeepBlanksDefault(0);
doc = xmlNewDoc((xmlChar *) XML_VERSION); doc = xmlNewDoc((xmlChar*) XML_VERSION);
xmlSetDocCompressMode(doc, _compression); xmlSetDocCompressMode(doc, _compression);
writenode(doc, _root, doc->children, 1); writenode(doc, _root, doc->children, 1);
xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len); xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
xmlFreeDoc(doc); xmlFreeDoc(doc);
retval = ptr; retval = ptr;
free(ptr); free(ptr);
return retval; return retval;
} }
XMLNode::XMLNode(const string & n) XMLNode::XMLNode(const string& n)
: _name(n), _is_content(false), _content(string()) : _name(n)
, _is_content(false)
{ {
} }
XMLNode::XMLNode(const string & n, const string & c) XMLNode::XMLNode(const string& n, const string& c)
:_name(n), _is_content(true), _content(c) : _name(n)
, _is_content(true)
, _content(c)
{ {
} }
@ -205,15 +209,15 @@ XMLNode::XMLNode(const XMLNode& from)
XMLPropertyIterator curprop; XMLPropertyIterator curprop;
XMLNodeList nodes; XMLNodeList nodes;
XMLNodeIterator curnode; XMLNodeIterator curnode;
_name = from.name(); _name = from.name();
set_content(from.content()); set_content(from.content());
props = from.properties(); props = from.properties();
for (curprop = props.begin(); curprop != props.end(); ++curprop) { for (curprop = props.begin(); curprop != props.end(); ++curprop) {
add_property((*curprop)->name().c_str(), (*curprop)->value()); add_property((*curprop)->name().c_str(), (*curprop)->value());
} }
nodes = from.children(); nodes = from.children();
for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) { for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
add_child_copy(**curnode); add_child_copy(**curnode);
@ -224,109 +228,109 @@ XMLNode::~XMLNode()
{ {
XMLNodeIterator curchild; XMLNodeIterator curchild;
XMLPropertyIterator curprop; XMLPropertyIterator curprop;
for (curchild = _children.begin(); curchild != _children.end(); ++curchild) { for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
delete *curchild; delete *curchild;
} }
for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) { for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
delete *curprop; delete *curprop;
} }
} }
const string & const string&
XMLNode::set_content(const string & c) XMLNode::set_content(const string& c)
{ {
if (c.empty()) { if (c.empty()) {
_is_content = false; _is_content = false;
} else { } else {
_is_content = true; _is_content = true;
} }
_content = c; _content = c;
return _content; return _content;
} }
XMLNode* XMLNode*
XMLNode::child (const char *name) const XMLNode::child (const char* name) const
{ {
/* returns first child matching name */ /* returns first child matching name */
XMLNodeConstIterator cur; XMLNodeConstIterator cur;
if (name == 0) { if (name == 0) {
return 0; return 0;
} }
for (cur = _children.begin(); cur != _children.end(); ++cur) { for (cur = _children.begin(); cur != _children.end(); ++cur) {
if ((*cur)->name() == name) { if ((*cur)->name() == name) {
return *cur; return *cur;
} }
} }
return 0; return 0;
} }
const XMLNodeList & const XMLNodeList&
XMLNode::children(const string& n) const XMLNode::children(const string& n) const
{ {
/* returns all children matching name */ /* returns all children matching name */
XMLNodeConstIterator cur; XMLNodeConstIterator cur;
if (n.empty()) { if (n.empty()) {
return _children; return _children;
} }
_selected_children.clear(); _selected_children.clear();
for (cur = _children.begin(); cur != _children.end(); ++cur) { for (cur = _children.begin(); cur != _children.end(); ++cur) {
if ((*cur)->name() == n) { if ((*cur)->name() == n) {
_selected_children.insert(_selected_children.end(), *cur); _selected_children.insert(_selected_children.end(), *cur);
} }
} }
return _selected_children; return _selected_children;
} }
XMLNode * XMLNode*
XMLNode::add_child(const char * n) XMLNode::add_child(const char* n)
{ {
return add_child_copy(XMLNode (n)); return add_child_copy(XMLNode (n));
} }
void void
XMLNode::add_child_nocopy (XMLNode& n) XMLNode::add_child_nocopy(XMLNode& n)
{ {
_children.insert(_children.end(), &n); _children.insert(_children.end(), &n);
} }
XMLNode * XMLNode*
XMLNode::add_child_copy(const XMLNode& n) XMLNode::add_child_copy(const XMLNode& n)
{ {
XMLNode *copy = new XMLNode (n); XMLNode *copy = new XMLNode(n);
_children.insert(_children.end(), copy); _children.insert(_children.end(), copy);
return copy; return copy;
} }
boost::shared_ptr<XMLSharedNodeList> boost::shared_ptr<XMLSharedNodeList>
XMLNode::find(const string xpath) const XMLNode::find(const string xpath) const
{ {
xmlDocPtr doc = xmlNewDoc((xmlChar *) XML_VERSION); xmlDocPtr doc = xmlNewDoc((xmlChar*) XML_VERSION);
writenode(doc, (XMLNode *) this, doc->children, 1); writenode(doc, (XMLNode*)this, doc->children, 1);
xmlXPathContext* ctxt = xmlXPathNewContext(doc); xmlXPathContext* ctxt = xmlXPathNewContext(doc);
boost::shared_ptr<XMLSharedNodeList> result = boost::shared_ptr<XMLSharedNodeList> result =
boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath)); boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
xmlFreeDoc(doc); xmlFreeDoc(doc);
return result; return result;
} }
std::string std::string
XMLNode::attribute_value() XMLNode::attribute_value()
{ {
XMLNodeList children = this->children(); XMLNodeList children = this->children();
@ -337,14 +341,14 @@ XMLNode::attribute_value()
return child->content(); return child->content();
} }
XMLNode * XMLNode*
XMLNode::add_content(const string & c) XMLNode::add_content(const string& c)
{ {
return add_child_copy(XMLNode (string(), c)); return add_child_copy(XMLNode (string(), c));
} }
XMLProperty * XMLProperty*
XMLNode::property(const char * n) XMLNode::property(const char* n)
{ {
string ns(n); string ns(n);
map<string,XMLProperty*>::iterator iter; map<string,XMLProperty*>::iterator iter;
@ -356,27 +360,27 @@ XMLNode::property(const char * n)
return 0; return 0;
} }
XMLProperty * XMLProperty*
XMLNode::property(const string & ns) XMLNode::property(const string& ns)
{ {
map<string,XMLProperty*>::iterator iter; map<string,XMLProperty*>::iterator iter;
if ((iter = _propmap.find(ns)) != _propmap.end()) { if ((iter = _propmap.find(ns)) != _propmap.end()) {
return iter->second; return iter->second;
} }
return 0; return 0;
} }
XMLProperty * XMLProperty*
XMLNode::add_property(const char * n, const string & v) XMLNode::add_property(const char* n, const string& v)
{ {
string ns(n); string ns(n);
if(_propmap.find(ns) != _propmap.end()){ if (_propmap.find(ns) != _propmap.end()) {
remove_property(ns); remove_property(ns);
} }
XMLProperty *tmp = new XMLProperty(ns, v); XMLProperty* tmp = new XMLProperty(ns, v);
if (!tmp) { if (!tmp) {
return 0; return 0;
@ -388,23 +392,23 @@ XMLNode::add_property(const char * n, const string & v)
return tmp; return tmp;
} }
XMLProperty * XMLProperty*
XMLNode::add_property(const char * n, const char * v) XMLNode::add_property(const char* n, const char* v)
{ {
string vs(v); string vs(v);
return add_property(n, vs); return add_property(n, vs);
} }
XMLProperty * XMLProperty*
XMLNode::add_property(const char *name, const long value) XMLNode::add_property(const char* name, const long value)
{ {
static char str[1024]; static char str[1024];
snprintf(str, 1024, "%ld", value); snprintf(str, 1024, "%ld", value);
return add_property(name, str); return add_property(name, str);
} }
void void
XMLNode::remove_property(const string & n) XMLNode::remove_property(const string& n)
{ {
if (_propmap.find(n) != _propmap.end()) { if (_propmap.find(n) != _propmap.end()) {
_proplist.remove(_propmap[n]); _proplist.remove(_propmap[n]);
@ -412,12 +416,12 @@ XMLNode::remove_property(const string & n)
} }
} }
void void
XMLNode::remove_nodes(const string & n) XMLNode::remove_nodes(const string& n)
{ {
XMLNodeIterator i = _children.begin(); XMLNodeIterator i = _children.begin();
XMLNodeIterator tmp; XMLNodeIterator tmp;
while (i != _children.end()) { while (i != _children.end()) {
tmp = i; tmp = i;
++tmp; ++tmp;
@ -428,12 +432,12 @@ XMLNode::remove_nodes(const string & n)
} }
} }
void void
XMLNode::remove_nodes_and_delete(const string & n) XMLNode::remove_nodes_and_delete(const string& n)
{ {
XMLNodeIterator i = _children.begin(); XMLNodeIterator i = _children.begin();
XMLNodeIterator tmp; XMLNodeIterator tmp;
while (i != _children.end()) { while (i != _children.end()) {
tmp = i; tmp = i;
++tmp; ++tmp;
@ -446,7 +450,7 @@ XMLNode::remove_nodes_and_delete(const string & n)
} }
void 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 i = _children.begin();
XMLNodeIterator tmp; XMLNodeIterator tmp;
@ -457,7 +461,7 @@ XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
++tmp; ++tmp;
prop = (*i)->property(propname); prop = (*i)->property(propname);
if(prop && prop->value() == val) { if (prop && prop->value() == val) {
delete *i; delete *i;
_children.erase(i); _children.erase(i);
} }
@ -466,10 +470,10 @@ XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
} }
} }
XMLProperty::XMLProperty(const string &n, const string &v) XMLProperty::XMLProperty(const string& n, const string& v)
: _name(n), : _name(n)
_value(v) , _value(v)
{ {
// Normalize property name (replace '_' with '-' as old session are inconsistent) // Normalize property name (replace '_' with '-' as old session are inconsistent)
for (size_t i = 0; i < _name.length(); ++i) { for (size_t i = 0; i < _name.length(); ++i) {
if (_name[i] == '_') { if (_name[i] == '_') {
@ -482,86 +486,84 @@ XMLProperty::~XMLProperty()
{ {
} }
static XMLNode * static XMLNode*
readnode(xmlNodePtr node) readnode(xmlNodePtr node)
{ {
string name, content; string name, content;
xmlNodePtr child; xmlNodePtr child;
XMLNode *tmp; XMLNode* tmp;
xmlAttrPtr attr; xmlAttrPtr attr;
if (node->name) { if (node->name) {
name = (char *) node->name; name = (char*)node->name;
} }
tmp = new XMLNode(name); tmp = new XMLNode(name);
for (attr = node->properties; attr; attr = attr->next) { for (attr = node->properties; attr; attr = attr->next) {
content = ""; content = "";
if (attr->children) { 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) { if (node->content) {
tmp->set_content((char *) node->content); tmp->set_content((char*)node->content);
} else { } else {
tmp->set_content(string()); tmp->set_content(string());
} }
for (child = node->children; child; child = child->next) { for (child = node->children; child; child = child->next) {
tmp->add_child_nocopy (*readnode(child)); tmp->add_child_nocopy (*readnode(child));
} }
return tmp; return tmp;
} }
static void static void
writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0) writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
{ {
XMLPropertyList props; XMLPropertyList props;
XMLPropertyIterator curprop; XMLPropertyIterator curprop;
XMLNodeList children; XMLNodeList children;
XMLNodeIterator curchild; XMLNodeIterator curchild;
xmlNodePtr node; xmlNodePtr node;
if (root) { 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 { } 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()) { if (n->is_content()) {
node->type = XML_TEXT_NODE; 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(); props = n->properties();
for (curprop = props.begin(); curprop != props.end(); ++curprop) { 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(); children = n->children();
for (curchild = children.begin(); curchild != children.end(); ++curchild) { for (curchild = children.begin(); curchild != children.end(); ++curchild) {
writenode(doc, *curchild, node); 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); xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
if(!result) if (!result) {
{
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
xmlFreeDoc(ctxt->doc); xmlFreeDoc(ctxt->doc);
throw XMLException("Invalid XPath: " + xpath); throw XMLException("Invalid XPath: " + xpath);
} }
if(result->type != XPATH_NODESET) if (result->type != XPATH_NODESET) {
{
xmlXPathFreeObject(result); xmlXPathFreeObject(result);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
xmlFreeDoc(ctxt->doc); xmlFreeDoc(ctxt->doc);
@ -571,15 +573,12 @@ static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath)
xmlNodeSet* nodeset = result->nodesetval; xmlNodeSet* nodeset = result->nodesetval;
XMLSharedNodeList* nodes = new XMLSharedNodeList(); XMLSharedNodeList* nodes = new XMLSharedNodeList();
if( nodeset ) if (nodeset) {
{
for (int i = 0; i < nodeset->nodeNr; ++i) { for (int i = 0; i < nodeset->nodeNr; ++i) {
XMLNode* node = readnode(nodeset->nodeTab[i]); XMLNode* node = readnode(nodeset->nodeTab[i]);
nodes->push_back(boost::shared_ptr<XMLNode>(node)); nodes->push_back(boost::shared_ptr<XMLNode>(node));
} }
} } else {
else
{
// return empty set // return empty set
} }