21 #ifndef _LibXmlDomDocumentHandler_hxx_
22 #define _LibXmlDomDocumentHandler_hxx_
30 #include <libxml++/libxml++.h>
34 class LibXmlDomReadingContext;
35 class LibXmlDomWritingContext;
47 return _currentReadContext;
51 _currentReadContext = context;
55 return _currentWriteContext;
59 _currentWriteContext = context;
64 xmlpp::Document * _document;
65 xmlpp::Element * _selection;
66 xmlpp::DomParser * _parser;
76 releaseIfAnyDocument();
81 _selection = _document->get_root_node();
86 _selection = recursiveSelection(_selection, path, 0);
88 _selection = absoluteSelection(path);
97 releaseIfAnyDocument();
99 xmlpp::Document * domDoc =
new xmlpp::Document;
100 domDoc->create_root_node(rootName);
103 void read(std::istream & stream)
106 xmlpp::Document * domDoc;
107 domDoc = reader.
read(stream);
115 writer.
write(os,_document);
121 writer.
write(os,_selection);
124 void releaseIfAnyDocument()
126 if (!_document)
return;
135 xmlpp::Element * absoluteSelection(
const std::string & path)
137 xmlpp::Element * root = _document->get_root_node();
138 unsigned int nextSlash = std::string(path).find(
'/',1);
139 std::string rootStep = std::string(path).substr(1,nextSlash-1);
140 std::string rootName = root->get_name();
141 if (rootStep==
"")
return root;
143 if (rootName== rootStep)
144 return recursiveSelection(root, path, nextSlash+1);
146 throw XmlStorageErr(
"Wrong root name, expected '"+rootStep+
"' but found '"+rootName+
"'");
148 xmlpp::Element * recursiveSelection(xmlpp::Element * current,
const std::string & path,
unsigned int pos)
150 if (pos >= path.length())
return current;
151 unsigned int slashPosition = path.find(
'/', pos);
152 unsigned int stepSize =
153 slashPosition == std::string::npos ?
154 std::string::npos : slashPosition-pos;
155 std::string step = path.substr(pos, stepSize);
156 xmlpp::Element::NodeList children = current->get_children();
158 xmlpp::Element::NodeList::iterator child = children.begin();
159 child!=children.end();
162 xmlpp::Element * element =
dynamic_cast<xmlpp::Element*
>(*child);
163 if (!element)
continue;
164 std::string nodeName= element->get_name();
165 if (nodeName != step)
continue;
166 if (slashPosition==std::string::npos)
return element;
167 return recursiveSelection(element, path, slashPosition+1);
169 std::string msg =
"Wrong path step '" + step +
"'";
170 throw XmlStorageErr(msg);
181 #endif//_LibXmlDomDocumentHandler_hxx_