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.
8 #include <libxml/debugXML.h>
9 #include <libxml/xpath.h>
10 #include <libxml/xpathInternals.h>
12 #define XML_VERSION "1.0"
14 static XMLNode *readnode(xmlNodePtr);
15 static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int);
16 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath);
25 XMLTree::XMLTree(const string &fn, bool validate)
30 read_internal(validate);
33 XMLTree::XMLTree(const XMLTree * from)
35 _filename = from->filename();
36 _root = new XMLNode(*from->root());
37 _compression = from->compression();
48 XMLTree::set_compression(int c)
62 XMLTree::read_internal(bool validate)
64 //shouldnt be used anywhere ATM, remove if so!
71 xmlParserCtxtPtr ctxt; /* the parser context */
72 xmlDocPtr doc; /* the resulting document tree */
74 xmlKeepBlanksDefault(0);
75 /* parse the file, activating the DTD validation option */
77 /* create a parser context */
78 ctxt = xmlNewParserCtxt();
82 doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
84 doc = xmlParseFile(_filename.c_str());
87 /* check if parsing suceeded */
90 xmlFreeParserCtxt(ctxt);
94 /* check if validation suceeded */
95 if (validate && ctxt->valid == 0) {
96 xmlFreeParserCtxt(ctxt);
99 throw XMLException("Failed to validate document " + _filename);
103 _root = readnode(xmlDocGetRootElement(doc));
105 /* free up the parser context */
107 xmlFreeParserCtxt(ctxt);
116 XMLTree::read_buffer(const string & buffer)
127 doc = xmlParseMemory((char *) buffer.c_str(), buffer.length());
132 _root = readnode(xmlDocGetRootElement(doc));
140 XMLTree::write(void) const
143 XMLNodeList children;
146 xmlKeepBlanksDefault(0);
147 doc = xmlNewDoc((xmlChar *) XML_VERSION);
148 xmlSetDocCompressMode(doc, _compression);
149 writenode(doc, _root, doc->children, 1);
150 result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
161 XMLTree::debug(FILE* out) const
164 XMLNodeList children;
166 xmlKeepBlanksDefault(0);
167 doc = xmlNewDoc((xmlChar *) XML_VERSION);
168 xmlSetDocCompressMode(doc, _compression);
169 writenode(doc, _root, doc->children, 1);
170 xmlDebugDumpDocument (out, doc);
175 XMLTree::write_buffer(void) const
177 static string retval;
181 XMLNodeList children;
183 xmlKeepBlanksDefault(0);
184 doc = xmlNewDoc((xmlChar *) XML_VERSION);
185 xmlSetDocCompressMode(doc, _compression);
186 writenode(doc, _root, doc->children, 1);
187 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
197 XMLNode::XMLNode(const string & n)
198 : _name(n), _is_content(false), _content(string())
202 XMLNode::XMLNode(const string & n, const string & c)
203 :_name(n), _is_content(true), _content(c)
207 XMLNode::XMLNode(const XMLNode& from)
209 XMLPropertyList props;
210 XMLPropertyIterator curprop;
212 XMLNodeIterator curnode;
215 set_content(from.content());
217 props = from.properties();
218 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
219 add_property((*curprop)->name().c_str(), (*curprop)->value());
222 nodes = from.children();
223 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
224 add_child_copy(**curnode);
230 XMLNodeIterator curchild;
231 XMLPropertyIterator curprop;
233 for (curchild = _children.begin(); curchild != _children.end(); ++curchild) {
237 for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) {
243 XMLNode::set_content(const string & c)
257 XMLNode::child (const char *name) const
259 /* returns first child matching name */
261 XMLNodeConstIterator cur;
267 for (cur = _children.begin(); cur != _children.end(); ++cur) {
268 if ((*cur)->name() == name) {
277 XMLNode::children(const string& n) const
279 /* returns all children matching name */
281 XMLNodeConstIterator cur;
287 _selected_children.clear();
289 for (cur = _children.begin(); cur != _children.end(); ++cur) {
290 if ((*cur)->name() == n) {
291 _selected_children.insert(_selected_children.end(), *cur);
295 return _selected_children;
299 XMLNode::add_child(const char * n)
301 return add_child_copy(XMLNode (n));
305 XMLNode::add_child_nocopy (XMLNode& n)
307 _children.insert(_children.end(), &n);
311 XMLNode::add_child_copy(const XMLNode& n)
313 XMLNode *copy = new XMLNode (n);
314 _children.insert(_children.end(), copy);
318 boost::shared_ptr<XMLSharedNodeList>
319 XMLNode::find(const string xpath) const
321 xmlDocPtr doc = xmlNewDoc((xmlChar *) XML_VERSION);
322 writenode(doc, (XMLNode *) this, doc->children, 1);
323 xmlXPathContext* ctxt = xmlXPathNewContext(doc);
325 boost::shared_ptr<XMLSharedNodeList> result =
326 boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
328 xmlXPathFreeContext(ctxt);
335 XMLNode::attribute_value()
337 XMLNodeList children = this->children();
338 assert(!_is_content);
339 assert(children.size() == 1);
340 XMLNode* child = *(children.begin());
341 assert(child->is_content());
342 return child->content();
346 XMLNode::add_content(const string & c)
348 return add_child_copy(XMLNode (string(), c));
352 XMLNode::property(const char * n)
355 map<string,XMLProperty*>::iterator iter;
357 if ((iter = _propmap.find(ns)) != _propmap.end()) {
365 XMLNode::property(const string & ns)
367 map<string,XMLProperty*>::iterator iter;
369 if ((iter = _propmap.find(ns)) != _propmap.end()) {
377 XMLNode::add_property(const char * n, const string & v)
380 if(_propmap.find(ns) != _propmap.end()){
384 XMLProperty *tmp = new XMLProperty(ns, v);
390 _propmap[tmp->name()] = tmp;
391 _proplist.insert(_proplist.end(), tmp);
397 XMLNode::add_property(const char * n, const char * v)
400 return add_property(n, vs);
404 XMLNode::add_property(const char *name, const long value)
406 static char str[1024];
407 snprintf(str, 1024, "%ld", value);
408 return add_property(name, str);
412 XMLNode::remove_property(const string & n)
414 if (_propmap.find(n) != _propmap.end()) {
415 _proplist.remove(_propmap[n]);
421 XMLNode::remove_nodes(const string & n)
423 XMLNodeIterator i = _children.begin();
426 while (i != _children.end()) {
429 if ((*i)->name() == n) {
437 XMLNode::remove_nodes_and_delete(const string & n)
439 XMLNodeIterator i = _children.begin();
442 while (i != _children.end()) {
445 if ((*i)->name() == n) {
454 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
456 XMLNodeIterator i = _children.begin();
460 while (i != _children.end()) {
464 prop = (*i)->property(propname);
465 if(prop && prop->value() == val) {
474 XMLProperty::XMLProperty(const string &n, const string &v)
478 // Normalize property name (replace '_' with '-' as old session are inconsistent)
479 for (size_t i = 0; i < _name.length(); ++i) {
480 if (_name[i] == '_') {
486 XMLProperty::~XMLProperty()
491 readnode(xmlNodePtr node)
493 string name, content;
499 name = (char *) node->name;
502 tmp = new XMLNode(name);
504 for (attr = node->properties; attr; attr = attr->next) {
506 if (attr->children) {
507 content = (char *) attr->children->content;
509 tmp->add_property((char *) attr->name, content);
513 tmp->set_content((char *) node->content);
515 tmp->set_content(string());
518 for (child = node->children; child; child = child->next) {
519 tmp->add_child_nocopy (*readnode(child));
526 writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0)
528 XMLPropertyList props;
529 XMLPropertyIterator curprop;
530 XMLNodeList children;
531 XMLNodeIterator curchild;
535 node = doc->children = xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0);
537 node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0);
540 if (n->is_content()) {
541 node->type = XML_TEXT_NODE;
542 xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(), n->content().length());
545 props = n->properties();
546 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
547 xmlSetProp(node, (xmlChar *) (*curprop)->name().c_str(), (xmlChar *) (*curprop)->value().c_str());
550 children = n->children();
551 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
552 writenode(doc, *curchild, node);
556 static XMLSharedNodeList* find_impl(xmlXPathContext* ctxt, const string xpath)
558 xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt);
562 xmlXPathFreeContext(ctxt);
563 xmlFreeDoc(ctxt->doc);
565 throw XMLException("Invalid XPath: " + xpath);
568 if(result->type != XPATH_NODESET)
570 xmlXPathFreeObject(result);
571 xmlXPathFreeContext(ctxt);
572 xmlFreeDoc(ctxt->doc);
574 throw XMLException("Only nodeset result types are supported.");
577 xmlNodeSet* nodeset = result->nodesetval;
578 XMLSharedNodeList* nodes = new XMLSharedNodeList();
581 for (int i = 0; i < nodeset->nodeNr; ++i) {
582 XMLNode* node = readnode(nodeset->nodeTab[i]);
583 nodes->push_back(boost::shared_ptr<XMLNode>(node));
591 xmlXPathFreeObject(result);