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>
10 static XMLNode *readnode(xmlNodePtr);
11 static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int);
21 XMLTree::XMLTree(const string &fn)
30 XMLTree::XMLTree(const XMLTree * from)
32 _filename = from->filename();
33 _root = new XMLNode(*from->root());
34 _compression = from->compression();
40 if (_initialized && _root)
45 XMLTree::set_compression(int c)
68 xmlKeepBlanksDefault(0);
70 doc = xmlParseFile(_filename.c_str());
76 _root = readnode(xmlDocGetRootElement(doc));
84 XMLTree::read_buffer(const string & buffer)
95 doc = xmlParseMemory((char *) buffer.c_str(), buffer.length());
101 _root = readnode(xmlDocGetRootElement(doc));
109 XMLTree::write(void) const
112 XMLNodeList children;
115 xmlKeepBlanksDefault(0);
116 doc = xmlNewDoc((xmlChar *) "1.0");
117 xmlSetDocCompressMode(doc, _compression);
118 writenode(doc, _root, doc->children, 1);
119 result = xmlSaveFormatFile(_filename.c_str(), doc, 1);
129 XMLTree::debug(FILE* out) const
132 XMLNodeList children;
134 xmlKeepBlanksDefault(0);
135 doc = xmlNewDoc((xmlChar *) "1.0");
136 xmlSetDocCompressMode(doc, _compression);
137 writenode(doc, _root, doc->children, 1);
138 xmlDebugDumpDocument (out, doc);
143 XMLTree::write_buffer(void) const
145 static string retval;
149 XMLNodeList children;
151 xmlKeepBlanksDefault(0);
152 doc = xmlNewDoc((xmlChar *) "1.0");
153 xmlSetDocCompressMode(doc, _compression);
154 writenode(doc, _root, doc->children, 1);
155 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
165 XMLNode::XMLNode(const string & n)
166 : _name(n), _is_content(false), _content(string())
170 _initialized = false;
175 XMLNode::XMLNode(const string & n, const string & c)
176 :_name(string()), _is_content(true), _content(c)
181 XMLNode::XMLNode(const XMLNode& from)
182 : _initialized(false)
184 XMLPropertyList props;
185 XMLPropertyIterator curprop;
187 XMLNodeIterator curnode;
190 set_content(from.content());
192 props = from.properties();
193 for (curprop = props.begin(); curprop != props.end(); curprop++)
194 add_property((*curprop)->name().c_str(), (*curprop)->value());
196 nodes = from.children();
197 for (curnode = nodes.begin(); curnode != nodes.end(); curnode++)
198 add_child_copy(**curnode);
203 XMLNodeIterator curchild;
204 XMLPropertyIterator curprop;
206 for (curchild = _children.begin(); curchild != _children.end();
210 for (curprop = _proplist.begin(); curprop != _proplist.end();
216 XMLNode::set_content(const string & c)
229 XMLNode::children(const string & n) const
231 static XMLNodeList retval;
232 XMLNodeConstIterator cur;
237 retval.erase(retval.begin(), retval.end());
239 for (cur = _children.begin(); cur != _children.end(); cur++)
240 if ((*cur)->name() == n)
241 retval.insert(retval.end(), *cur);
247 XMLNode::add_child(const char * n)
249 return add_child_copy(XMLNode (n));
253 XMLNode::add_child_nocopy (XMLNode& n)
255 _children.insert(_children.end(), &n);
259 XMLNode::add_child_copy(const XMLNode& n)
261 XMLNode *copy = new XMLNode (n);
262 _children.insert(_children.end(), copy);
267 XMLNode::add_content(const string & c)
269 return add_child_copy(XMLNode (string(), c));
273 XMLNode::property(const char * n)
276 if (_propmap.find(ns) == _propmap.end())
282 XMLNode::add_property(const char * n, const string & v)
285 if(_propmap.find(ns) != _propmap.end()){
289 XMLProperty *tmp = new XMLProperty(ns, v);
294 _propmap[tmp->name()] = tmp;
295 _proplist.insert(_proplist.end(), tmp);
301 XMLNode::add_property(const char * n, const char * v)
304 return add_property(n, vs);
308 XMLNode::remove_property(const string & n)
310 if (_propmap.find(n) != _propmap.end()) {
311 _proplist.remove(_propmap[n]);
317 XMLNode::remove_nodes(const string & n)
319 XMLNodeIterator i = _children.begin();
322 while (i != _children.end()) {
325 if ((*i)->name() == n) {
333 XMLNode::remove_nodes_and_delete(const string & n)
335 XMLNodeIterator i = _children.begin();
338 while (i != _children.end()) {
341 if ((*i)->name() == n) {
349 XMLProperty::XMLProperty(const string &n, const string &v)
355 XMLProperty::~XMLProperty()
360 readnode(xmlNodePtr node)
362 string name, content;
368 name = (char *) node->name;
370 tmp = new XMLNode(name);
372 for (attr = node->properties; attr; attr = attr->next) {
375 content = (char *) attr->children->content;
376 tmp->add_property((char *) attr->name, content);
380 tmp->set_content((char *) node->content);
382 tmp->set_content(string());
384 for (child = node->children; child; child = child->next)
385 tmp->add_child_nocopy (*readnode(child));
391 writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root =
394 XMLPropertyList props;
395 XMLPropertyIterator curprop;
396 XMLNodeList children;
397 XMLNodeIterator curchild;
401 node = doc->children =
402 xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0);
405 node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0);
407 if (n->is_content()) {
408 node->type = XML_TEXT_NODE;
409 xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(),
410 n->content().length());
413 props = n->properties();
414 for (curprop = props.begin(); curprop != props.end(); curprop++)
415 xmlSetProp(node, (xmlChar *) (*curprop)->name().c_str(),
416 (xmlChar *) (*curprop)->value().c_str());
418 children = n->children();
419 for (curchild = children.begin(); curchild != children.end();
421 writenode(doc, *curchild, node);