2 * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3 * are covered by the GNU Lesser General Public License, which should be
4 * included with libxml++ as the file COPYING.
5 * Modified for Ardour and released under the same terms.
9 #include <libxml/debugXML.h>
10 #include <libxml/xpath.h>
11 #include <libxml/xpathInternals.h>
13 #define XML_VERSION "1.0"
15 static XMLNode* readnode(xmlNodePtr);
16 static void writenode(xmlDocPtr, XMLNode*, xmlNodePtr, int);
17 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath);
26 XMLTree::XMLTree(const string& fn, bool validate)
31 read_internal(validate);
34 XMLTree::XMLTree(const XMLTree* from)
35 : _filename(from->filename())
36 , _root(new XMLNode(*from->root()))
37 , _compression(from->compression())
47 XMLTree::set_compression(int c)
61 XMLTree::read_internal(bool validate)
63 //shouldnt be used anywhere ATM, remove if so!
69 xmlParserCtxtPtr ctxt; /* the parser context */
70 xmlDocPtr doc; /* the resulting document tree */
72 xmlKeepBlanksDefault(0);
73 /* parse the file, activating the DTD validation option */
75 /* create a parser context */
76 ctxt = xmlNewParserCtxt();
80 doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
82 doc = xmlParseFile(_filename.c_str());
85 /* check if parsing suceeded */
88 xmlFreeParserCtxt(ctxt);
92 /* check if validation suceeded */
93 if (validate && ctxt->valid == 0) {
94 xmlFreeParserCtxt(ctxt);
97 throw XMLException("Failed to validate document " + _filename);
101 _root = readnode(xmlDocGetRootElement(doc));
103 /* free up the parser context */
105 xmlFreeParserCtxt(ctxt);
114 XMLTree::read_buffer(const string& buffer)
123 doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
128 _root = readnode(xmlDocGetRootElement(doc));
136 XMLTree::write() const
139 XMLNodeList children;
142 xmlKeepBlanksDefault(0);
143 doc = xmlNewDoc((xmlChar*) XML_VERSION);
144 xmlSetDocCompressMode(doc, _compression);
145 writenode(doc, _root, doc->children, 1);
146 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
157 XMLTree::debug(FILE* out) const
160 XMLNodeList children;
162 xmlKeepBlanksDefault(0);
163 doc = xmlNewDoc((xmlChar*) XML_VERSION);
164 xmlSetDocCompressMode(doc, _compression);
165 writenode(doc, _root, doc->children, 1);
166 xmlDebugDumpDocument (out, doc);
171 XMLTree::write_buffer() const
173 static string retval;
177 XMLNodeList children;
179 xmlKeepBlanksDefault(0);
180 doc = xmlNewDoc((xmlChar*) XML_VERSION);
181 xmlSetDocCompressMode(doc, _compression);
182 writenode(doc, _root, doc->children, 1);
183 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
193 XMLNode::XMLNode(const string& n)
199 XMLNode::XMLNode(const string& n, const string& c)
206 XMLNode::XMLNode(const XMLNode& from)
208 XMLPropertyList props;
209 XMLPropertyIterator curprop;
211 XMLNodeIterator curnode;
214 set_content(from.content());
216 props = from.properties();
217 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
218 add_property((*curprop)->name().c_str(), (*curprop)->value());
221 nodes = from.children();
222 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
223 add_child_copy(**curnode);
229 XMLNodeIterator curchild;
230 XMLPropertyIterator curprop;
232 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
236 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
242 XMLNode::set_content(const string& c)
256 XMLNode::child (const char* name) const
258 /* returns first child matching name */
260 XMLNodeConstIterator cur;
266 for (cur = _children.begin(); cur != _children.end(); ++cur) {
267 if ((*cur)->name() == name) {
276 XMLNode::children(const string& n) const
278 /* returns all children matching name */
280 XMLNodeConstIterator cur;
286 _selected_children.clear();
288 for (cur = _children.begin(); cur != _children.end(); ++cur) {
289 if ((*cur)->name() == n) {
290 _selected_children.insert(_selected_children.end(), *cur);
294 return _selected_children;
298 XMLNode::add_child(const char* n)
300 return add_child_copy(XMLNode (n));
304 XMLNode::add_child_nocopy(XMLNode& n)
306 _children.insert(_children.end(), &n);
310 XMLNode::add_child_copy(const XMLNode& n)
312 XMLNode *copy = new XMLNode(n);
313 _children.insert(_children.end(), copy);
317 boost::shared_ptr<XMLSharedNodeList>
318 XMLNode::find(const string xpath) const
320 xmlDocPtr doc = xmlNewDoc((xmlChar*) XML_VERSION);
321 writenode(doc, (XMLNode*)this, doc->children, 1);
322 xmlXPathContext* ctxt = xmlXPathNewContext(doc);
324 boost::shared_ptr<XMLSharedNodeList> result =
325 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
327 xmlXPathFreeContext(ctxt);
334 XMLNode::attribute_value()
336 XMLNodeList children = this->children();
337 assert(!_is_content);
338 assert(children.size() == 1);
339 XMLNode* child = *(children.begin());
340 assert(child->is_content());
341 return child->content();
345 XMLNode::add_content(const string& c)
347 return add_child_copy(XMLNode (string(), c));
351 XMLNode::property(const char* n)
354 map<string,XMLProperty*>::iterator iter;
356 if ((iter = _propmap.find(ns)) != _propmap.end()) {
364 XMLNode::property(const string& ns)
366 map<string,XMLProperty*>::iterator iter;
368 if ((iter = _propmap.find(ns)) != _propmap.end()) {
376 XMLNode::add_property(const char* n, const string& v)
379 if (_propmap.find(ns) != _propmap.end()) {
383 XMLProperty* tmp = new XMLProperty(ns, v);
389 _propmap[tmp->name()] = tmp;
390 _proplist.insert(_proplist.end(), tmp);
396 XMLNode::add_property(const char* n, const char* v)
399 return add_property(n, vs);
403 XMLNode::add_property(const char* name, const long value)
405 static char str[1024];
406 snprintf(str, 1024, "%ld", value);
407 return add_property(name, str);
411 XMLNode::remove_property(const string& n)
413 if (_propmap.find(n) != _propmap.end()) {
414 _proplist.remove(_propmap[n]);
420 XMLNode::remove_nodes(const string& n)
422 XMLNodeIterator i = _children.begin();
425 while (i != _children.end()) {
428 if ((*i)->name() == n) {
436 XMLNode::remove_nodes_and_delete(const string& n)
438 XMLNodeIterator i = _children.begin();
441 while (i != _children.end()) {
444 if ((*i)->name() == n) {
453 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
455 XMLNodeIterator i = _children.begin();
459 while (i != _children.end()) {
463 prop = (*i)->property(propname);
464 if (prop && prop->value() == val) {
473 XMLProperty::XMLProperty(const string& n, const string& v)
477 // Normalize property name (replace '_' with '-' as old session are inconsistent)
478 for (size_t i = 0; i < _name.length(); ++i) {
479 if (_name[i] == '_') {
485 XMLProperty::~XMLProperty()
490 readnode(xmlNodePtr node)
492 string name, content;
498 name = (char*)node->name;
501 tmp = new XMLNode(name);
503 for (attr = node->properties; attr; attr = attr->next) {
505 if (attr->children) {
506 content = (char*)attr->children->content;
508 tmp->add_property((char*)attr->name, content);
512 tmp->set_content((char*)node->content);
514 tmp->set_content(string());
517 for (child = node->children; child; child = child->next) {
518 tmp->add_child_nocopy (*readnode(child));
525 writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
527 XMLPropertyList props;
528 XMLPropertyIterator curprop;
529 XMLNodeList children;
530 XMLNodeIterator curchild;
534 node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0);
536 node = xmlNewChild(p, 0, (xmlChar*) n->name().c_str(), 0);
539 if (n->is_content()) {
540 node->type = XML_TEXT_NODE;
541 xmlNodeSetContentLen(node, (const xmlChar*)n->content().c_str(), n->content().length());
544 props = n->properties();
545 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
546 xmlSetProp(node, (xmlChar*) (*curprop)->name().c_str(), (xmlChar*) (*curprop)->value().c_str());
549 children = n->children();
550 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
551 writenode(doc, *curchild, node);
555 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string& xpath)
557 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
560 xmlXPathFreeContext(ctxt);
561 xmlFreeDoc(ctxt->doc);
563 throw XMLException("Invalid XPath: " + xpath);
566 if (result->type != XPATH_NODESET) {
567 xmlXPathFreeObject(result);
568 xmlXPathFreeContext(ctxt);
569 xmlFreeDoc(ctxt->doc);
571 throw XMLException("Only nodeset result types are supported.");
574 xmlNodeSet* nodeset = result->nodesetval;
575 XMLSharedNodeList* nodes = new XMLSharedNodeList();
577 for (int i = 0; i < nodeset->nodeNr; ++i) {
578 XMLNode* node = readnode(nodeset->nodeTab[i]);
579 nodes->push_back(boost::shared_ptr<XMLNode>(node));
585 xmlXPathFreeObject(result);