summaryrefslogtreecommitdiff
path: root/libs/pbd/xml++.cc
diff options
context:
space:
mode:
authorHans Baier <hansfbaier@googlemail.com>2008-05-25 23:35:23 +0000
committerHans Baier <hansfbaier@googlemail.com>2008-05-25 23:35:23 +0000
commit911c5717bcf8da0f59048d1fd3af0da09a21cbd7 (patch)
tree864ede432712a932b9b101206c63556df57e34df /libs/pbd/xml++.cc
parent5c60257b4aa9c31c25c6afea95208e4fffc8465b (diff)
* splitted midi++/event.h in header and implementation
* added to_string(), to_xml() and from_xml() to MIDI::Event * added partial support for midnam-Patchfiles (http://www.sonosphere.com/dtds/MIDINameDocument.dtd): midnam_patch.h/.cc * added validation support to xml++.cc/.h * added XMLNode::add_property(const char *name, const long value) * added test to pbd/tests/xpath.cc git-svn-id: svn://localhost/ardour2/branches/3.0@3412 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd/xml++.cc')
-rw-r--r--libs/pbd/xml++.cc91
1 files changed, 65 insertions, 26 deletions
diff --git a/libs/pbd/xml++.cc b/libs/pbd/xml++.cc
index 522b1dc3bc..ec1ca5d2b9 100644
--- a/libs/pbd/xml++.cc
+++ b/libs/pbd/xml++.cc
@@ -22,12 +22,12 @@ XMLTree::XMLTree()
{
}
-XMLTree::XMLTree(const string &fn)
+XMLTree::XMLTree(const string &fn, bool validate)
: _filename(fn),
_root(0),
_compression(0)
{
- read();
+ read_internal(validate);
}
XMLTree::XMLTree(const XMLTree * from)
@@ -48,9 +48,9 @@ int
XMLTree::set_compression(int c)
{
if (c > 9) {
- c = 9;
+ c = 9;
} else if (c < 0) {
- c = 0;
+ c = 0;
}
_compression = c;
@@ -59,24 +59,55 @@ XMLTree::set_compression(int c)
}
bool
-XMLTree::read(void)
+XMLTree::read_internal(bool validate)
{
- xmlDocPtr doc;
-
+ //shouldnt be used anywhere ATM, remove if so!
+ assert(!validate);
if (_root) {
delete _root;
_root = 0;
}
+ xmlParserCtxtPtr ctxt; /* the parser context */
+ xmlDocPtr doc; /* the resulting document tree */
+
xmlKeepBlanksDefault(0);
+ /* parse the file, activating the DTD validation option */
+ if(validate) {
+ /* create a parser context */
+ ctxt = xmlNewParserCtxt();
+ if (ctxt == NULL) {
+ return false;
+ }
+ doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
+ } else {
+ doc = xmlParseFile(_filename.c_str());
+ }
- doc = xmlParseFile(_filename.c_str());
- if (!doc) {
- return false;
+ /* check if parsing suceeded */
+ if (doc == NULL) {
+ if(validate) {
+ xmlFreeParserCtxt(ctxt);
+ }
+ return false;
+ } else {
+ /* check if validation suceeded */
+ if (validate && ctxt->valid == 0) {
+ xmlFreeParserCtxt(ctxt);
+ xmlFreeDoc(doc);
+ xmlCleanupParser();
+ throw XMLException("Failed to validate document " + _filename);
+ }
}
_root = readnode(xmlDocGetRootElement(doc));
+
+ /* free up the parser context */
+ if(validate) {
+ xmlFreeParserCtxt(ctxt);
+ }
xmlFreeDoc(doc);
+ xmlCleanupParser();
return true;
}
@@ -129,15 +160,15 @@ XMLTree::write(void) const
void
XMLTree::debug(FILE* out) const
{
- xmlDocPtr doc;
- XMLNodeList children;
+ xmlDocPtr doc;
+ XMLNodeList children;
- xmlKeepBlanksDefault(0);
- doc = xmlNewDoc((xmlChar *) XML_VERSION);
- xmlSetDocCompressMode(doc, _compression);
- writenode(doc, _root, doc->children, 1);
- xmlDebugDumpDocument (out, doc);
- xmlFreeDoc(doc);
+ xmlKeepBlanksDefault(0);
+ doc = xmlNewDoc((xmlChar *) XML_VERSION);
+ xmlSetDocCompressMode(doc, _compression);
+ writenode(doc, _root, doc->children, 1);
+ xmlDebugDumpDocument (out, doc);
+ xmlFreeDoc(doc);
}
const string &
@@ -202,7 +233,7 @@ XMLNode::~XMLNode()
for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
delete *curchild;
}
-
+
for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
delete *curprop;
}
@@ -216,7 +247,7 @@ XMLNode::set_content(const string & c)
} else {
_is_content = true;
}
-
+
_content = c;
return _content;
@@ -232,13 +263,13 @@ XMLNode::child (const char *name) const
if (name == 0) {
return 0;
}
-
+
for (cur = _children.begin(); cur != _children.end(); ++cur) {
if ((*cur)->name() == name) {
return *cur;
}
}
-
+
return 0;
}
@@ -253,7 +284,7 @@ XMLNode::children(const string& n) const
if (n.empty()) {
return _children;
}
-
+
retval.erase(retval.begin(), retval.end());
for (cur = _children.begin(); cur != _children.end(); ++cur) {
@@ -261,7 +292,7 @@ XMLNode::children(const string& n) const
retval.insert(retval.end(), *cur);
}
}
-
+
return retval;
}
@@ -370,6 +401,14 @@ XMLNode::add_property(const char * n, const char * v)
return add_property(n, vs);
}
+XMLProperty *
+XMLNode::add_property(const char *name, const long value)
+{
+ static char str[1024];
+ snprintf(str, 1024, "%ld", value);
+ return add_property(name, str);
+}
+
void
XMLNode::remove_property(const string & n)
{
@@ -492,7 +531,7 @@ writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0)
} else {
node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0);
}
-
+
if (n->is_content()) {
node->type = XML_TEXT_NODE;
xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(), n->content().length());
@@ -502,7 +541,7 @@ writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0)
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) {
writenode(doc, *curchild, node);