21 #ifndef _XercesDomDocumentHandler_hxx_
22 #define _XercesDomDocumentHandler_hxx_
32 #include <xercesc/parsers/XercesDOMParser.hpp>
33 #include <xercesc/dom/DOMImplementation.hpp>
34 #include <xercesc/dom/DOMDocument.hpp>
35 #include <xercesc/dom/DOMElement.hpp>
39 class XercesDomReadingContext;
40 class XercesDomWritingContext;
53 return _currentReadContext;
57 _currentReadContext = context;
61 return _currentWriteContext;
65 _currentWriteContext = context;
70 xercesc::DOMDocument * _document;
71 xercesc::DOMElement * _selection;
72 xercesc::XercesDOMParser * _parser;
82 releaseIfAnyDocument();
87 _selection = _document->getDocumentElement();
92 _selection = recursiveSelection(_selection, path, 0);
94 _selection = absoluteSelection(path);
105 releaseIfAnyDocument();
106 xercesc::DOMImplementation * imp =
107 xercesc::DOMImplementation::getImplementation();
108 xercesc::DOMDocument * domDoc = imp->createDocument(0,
U(rootName),0);
111 void read(std::istream & stream)
114 xercesc::DOMDocument * domDoc;
115 domDoc = reader.
read(stream);
123 writer.
write(os,_document);
129 writer.
write(os,_selection);
132 void releaseIfAnyDocument()
134 if (!_document)
return;
138 _document->release();
142 xercesc::DOMElement * absoluteSelection(
const std::string & path)
144 xercesc::DOMElement * root = (xercesc::DOMElement*) _document->getDocumentElement();
145 unsigned int nextSlash = std::string(path).find(
'/',1);
146 std::string rootStep = std::string(path).substr(1,nextSlash-1);
147 std::string rootName =
L(root->getNodeName());
148 if (rootStep==
"")
return root;
150 if (rootName== rootStep)
151 return recursiveSelection(root, path, nextSlash+1);
153 throw XmlStorageErr(
"Wrong root name, expected '"+rootStep+
"' but found '"+rootName+
"'");
155 xercesc::DOMElement * recursiveSelection(xercesc::DOMElement * current,
const std::string & path,
unsigned int pos)
157 if (pos >= path.length())
return current;
158 unsigned int slashPosition = path.find(
'/', pos);
159 unsigned int stepSize =
160 slashPosition == std::string::npos ?
161 std::string::npos : slashPosition-pos;
162 std::string step = path.substr(pos, stepSize);
164 xercesc::DOMNode * child = current->getFirstChild();
166 child = child->getNextSibling())
168 if (child->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
continue;
169 std::string nodeName(
L(child->getNodeName()));
170 if (nodeName != step)
continue;
171 if (slashPosition==std::string::npos)
return (xercesc::DOMElement *) child;
172 return recursiveSelection((xercesc::DOMElement *) child, path, slashPosition+1);
174 std::string msg =
"Wrong path step '" + step +
"'";
175 throw XmlStorageErr(msg);
188 #endif//_XercesDomDocumentHandler_hxx_