Commit 326d3866 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: First draft of xpointer respected reading.

parent 60362c29
// Kenneth Leiter
// Xdmf Smart Pointer Test
#include <libxml/xpointer.h>
#include <libxml/xmlreader.h>
#include "XdmfCoreItemFactory.hpp"
#include "XdmfCoreReader.hpp"
......@@ -22,7 +23,7 @@ public:
{
};
std::vector<boost::shared_ptr<XdmfItem> > read(xmlNodePtr currNode) const
std::vector<boost::shared_ptr<XdmfItem> > read(xmlNodePtr currNode)
{
std::vector<boost::shared_ptr<XdmfItem> > myItems;
......@@ -30,32 +31,77 @@ public:
{
if(currNode->type == XML_ELEMENT_NODE)
{
std::map<std::string, std::string> itemProperties;
std::cout << currNode->name << std::endl;
if(currNode->children != NULL)
if(xmlStrcmp(currNode->name, (xmlChar*)"include") == 0)
{
itemProperties["Content"] = (const char *)currNode->children->content;
xmlAttrPtr currAttribute = currNode->properties;
xmlChar * xpointer;
while(currAttribute != NULL)
{
if(xmlStrcmp(currAttribute->name, (xmlChar*)"xpointer") == 0)
{
xpointer = currAttribute->children->content;
break;
}
}
xmlXPathObjectPtr xPathObject = xmlXPtrEval(xpointer, mXPathContext);
std::cout << "HERE" << std::endl;
std::map<xmlNodePtr, boost::shared_ptr<XdmfItem> >::const_iterator iter = mXPathMap.find(xPathObject->nodesetval->nodeTab[0]);
if(iter != mXPathMap.end())
{
myItems.push_back(iter->second);
}
//this->read();
//for(unsigned int i=0; i<xPathObject->nodesetval->nodeNr; ++i)
//{
//std::cout << xPathObject->nodesetval->nodeTab[i]->type << std::endl;
//std::cout << xPathObject->nodesetval->nodeTab[i]->name << std::endl;
//this->read(xPathObject->nodesetval->nodeTab[i]);
//currNode = NULL;
//currNode = xPathObject->nodesetval->nodeTab[0];
//}
//xmlXPathFreeObject(xPathObject);
}
else
{
itemProperties["Content"] = "";
std::map<xmlNodePtr, boost::shared_ptr<XdmfItem> >::const_iterator iter = mXPathMap.find(currNode);
if(iter != mXPathMap.end())
{
myItems.push_back(iter->second);
}
else
{
std::map<std::string, std::string> itemProperties;
if(currNode->children != NULL)
{
itemProperties["Content"] = (const char *)currNode->children->content;
}
else
{
itemProperties["Content"] = "";
}
xmlAttrPtr currAttribute = currNode->properties;
while(currAttribute != NULL)
{
itemProperties[(const char *)currAttribute->name] = (const char *)currAttribute->children->content;
currAttribute = currAttribute->next;
}
std::vector<boost::shared_ptr<XdmfItem> > childItems = this->read(currNode->children);
boost::shared_ptr<XdmfItem> newItem = mItemFactory->createItem((const char *)currNode->name, itemProperties);
newItem->populateItem(itemProperties, childItems);
myItems.push_back(newItem);
mXPathMap[currNode] = newItem;
}
}
xmlAttrPtr currAttribute = currNode->properties;
while(currAttribute != NULL)
{
itemProperties[(const char *)currAttribute->name] = (const char *)currAttribute->children->content;
currAttribute = currAttribute->next;
}
std::vector<boost::shared_ptr<XdmfItem> > childItems = this->read(currNode->children);
boost::shared_ptr<XdmfItem> newItem = mItemFactory->createItem((const char *)currNode->name, itemProperties);
newItem->populateItem(itemProperties, childItems);
myItems.push_back(newItem);
}
currNode = currNode->next;
}
return myItems;
}
xmlXPathContextPtr mXPathContext;
std::map<xmlNodePtr, boost::shared_ptr<XdmfItem> > mXPathMap;
private:
const boost::shared_ptr<XdmfCoreItemFactory> mItemFactory;
......@@ -79,6 +125,7 @@ boost::shared_ptr<XdmfItem> XdmfCoreReader::read(const std::string & filePath) c
xmlNodePtr currNode;
document = xmlReadFile(filePath.c_str(), NULL, 0);
mImpl->mXPathContext = xmlXPtrNewContext(document, NULL, NULL);
if(document == NULL)
{
assert(false);
......@@ -87,6 +134,8 @@ boost::shared_ptr<XdmfItem> XdmfCoreReader::read(const std::string & filePath) c
std::vector<boost::shared_ptr<XdmfItem> > toReturn = mImpl->read(currNode->children);
mImpl->mXPathMap.clear();
xmlXPathFreeContext(mImpl->mXPathContext);
xmlFreeDoc(document);
xmlCleanupParser();
......
......@@ -47,7 +47,7 @@ private:
XdmfCoreReader(const XdmfCoreReader & reader); // Not implemented.
void operator=(const XdmfCoreReader & reader); // Not implemented.
const XdmfCoreReaderImpl * const mImpl;
XdmfCoreReaderImpl * const mImpl;
};
#endif /* XDMFCOREREADER_HPP_ */
......@@ -2,7 +2,6 @@
// Xdmf Smart Pointer Test
#include <libxml/tree.h>
#include <map>
#include <sstream>
#include "XdmfArray.hpp"
#include "XdmfItem.hpp"
......
#include <fstream>
#include <sstream>
#include "XdmfDomain.hpp"
#include "XdmfReader.hpp"
#include "XdmfWriter.hpp"
#include "XdmfTestDataGenerator.hpp"
......@@ -31,5 +32,11 @@ int main(int argc, char* argv[])
assert(fileContents.find("xpointer=\"element(/1/1/1/2)\"") != std::string::npos);
assert(fileContents.find("xpointer=\"element(/1/1/1/3)\"") != std::string::npos);
// Make sure when we read it in we get the same structure as when we wrote it out (multiple items holding the same shared pointers)
boost::shared_ptr<XdmfReader> reader = XdmfReader::New();
boost::shared_ptr<XdmfDomain> domain2 = boost::shared_dynamic_cast<XdmfDomain>(reader->read("xpath.xmf"));
boost::shared_ptr<XdmfWriter> writer2 = XdmfWriter::New("xpath2.xmf");
domain2->accept(writer2);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment