From f53c300e9e3073dab5b656c7642b4fcc5e0247fb Mon Sep 17 00:00:00 2001 From: Ken Renard Date: Tue, 24 Aug 2010 08:35:56 -0400 Subject: [PATCH] fixes for "transparent root" and "compare" --- core/XdmfCore.i | 16 ++++++ core/XdmfCoreReader.cpp | 12 ++++- core/XdmfCoreReader.hpp | 8 +++ core/XdmfItem.cpp | 7 +++ core/XdmfItem.hpp | 7 +++ core/XdmfWriter.cpp | 109 ++++++++++++++++++++++++---------------- core/XdmfWriter.hpp | 14 ++++++ 7 files changed, 129 insertions(+), 44 deletions(-) diff --git a/core/XdmfCore.i b/core/XdmfCore.i index 2b89c88f..e4ab6820 100644 --- a/core/XdmfCore.i +++ b/core/XdmfCore.i @@ -64,6 +64,22 @@ swig -v -c++ -python -o XdmfCorePython.cpp XdmfCore.i %} #endif /* SWIGJAVA */ +#ifdef SWIGPYTHON +%extend XdmfItem { + bool __eq__(boost::shared_ptr item) { + return(self->IsEqual(item)); + } +}; +#endif + +#ifdef SWIGJAVA +%extend XdmfItem { + bool equals(boost::shared_ptr item) { + return(self->IsEqual(item)); + } +}; +#endif + %include XdmfItem.hpp %include XdmfItemProperty.hpp %include XdmfVisitor.hpp diff --git a/core/XdmfCoreReader.cpp b/core/XdmfCoreReader.cpp index c28180df..86fab485 100644 --- a/core/XdmfCoreReader.cpp +++ b/core/XdmfCoreReader.cpp @@ -171,13 +171,21 @@ XdmfCoreReader::~XdmfCoreReader() delete mImpl; } -boost::shared_ptr XdmfCoreReader::read(const std::string & filePath) const +std::vector > XdmfCoreReader::readItems(const std::string & filePath) const { mImpl->openFile(filePath); const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument); const std::vector > toReturn = mImpl->read(currNode->children); mImpl->closeFile(); - return toReturn[0]; + return toReturn; +} + +boost::shared_ptr XdmfCoreReader::read(const std::string & filePath) const +{ + const std::vector > toReturn = readItems(filePath); + if (toReturn.size() == 0) + return(boost::shared_ptr()); + return(toReturn[0]); } std::vector > XdmfCoreReader::read(const std::string & filePath, const std::string & xPath) const diff --git a/core/XdmfCoreReader.hpp b/core/XdmfCoreReader.hpp index 4d22e6e4..95c7f3ea 100644 --- a/core/XdmfCoreReader.hpp +++ b/core/XdmfCoreReader.hpp @@ -30,6 +30,14 @@ public: */ virtual boost::shared_ptr read(const std::string & filePath) const; + /** + * Read an Xdmf file from disk into memory. + * + * @param filePath the path of the Xdmf file to read in from disk. + * @return a vector of XdmfItems at the root of the Xdmf tree. + */ + virtual std::vector > readItems(const std::string & filePath) const; + /** * Read part of an Xdmf file from disk into memory. * diff --git a/core/XdmfItem.cpp b/core/XdmfItem.cpp index 373bb3d8..34552caa 100644 --- a/core/XdmfItem.cpp +++ b/core/XdmfItem.cpp @@ -22,6 +22,13 @@ void XdmfItem::populateItem(const std::map &, std::vec } } +bool XdmfItem::IsEqual(boost::shared_ptr item) +{ + if (item == NULL) return false; + if (this == item.get()) return(true); + return(false); +} + void XdmfItem::traverse(const boost::shared_ptr visitor) { for(std::vector >::const_iterator iter = mInformations.begin(); iter != mInformations.end(); ++iter) diff --git a/core/XdmfItem.hpp b/core/XdmfItem.hpp index 31140763..c75382cc 100644 --- a/core/XdmfItem.hpp +++ b/core/XdmfItem.hpp @@ -147,6 +147,13 @@ public: */ virtual void traverse(const boost::shared_ptr visitor); + /** + * Test for equality of underlying item. + * + * @param an XdmfItem to test for equality + */ + bool IsEqual(boost::shared_ptr item); + protected: XdmfItem(); diff --git a/core/XdmfWriter.cpp b/core/XdmfWriter.cpp index 48f975f2..abb32926 100644 --- a/core/XdmfWriter.cpp +++ b/core/XdmfWriter.cpp @@ -27,7 +27,10 @@ public: mXMLDocument(NULL), mXMLFilePath(XdmfSystemUtils::getRealPath(xmlFilePath)), mXPathCount(0), - mXPathString("") + mXPathString(""), + mDocumentTitle("Xdmf"), + mVersionString("2.0"), + mDepth(0) { }; @@ -46,9 +49,9 @@ public: void openFile() { mXMLDocument = xmlNewDoc((xmlChar*)"1.0"); - mXMLCurrentNode = xmlNewNode(NULL, (xmlChar*)"Xdmf"); + mXMLCurrentNode = xmlNewNode(NULL, (xmlChar*)mDocumentTitle.c_str()); xmlNewProp(mXMLCurrentNode, (xmlChar*)"xmlns:xi", (xmlChar*)"http://www.w3.org/2001/XInclude"); - xmlNewProp(mXMLCurrentNode, (xmlChar*)"Version", (xmlChar*)"2.0"); + xmlNewProp(mXMLCurrentNode, (xmlChar*)"Version", (xmlChar*)mVersionString.c_str()); xmlDocSetRootElement(mXMLDocument, mXMLCurrentNode); } @@ -63,6 +66,9 @@ public: std::map mXPath; unsigned int mXPathCount; std::string mXPathString; + std::string mDocumentTitle; + std::string mVersionString; + int mDepth; }; boost::shared_ptr XdmfWriter::New(const std::string & xmlFilePath) @@ -148,6 +154,16 @@ void XdmfWriter::setWriteXPaths(const bool writeXPaths) mImpl->mWriteXPaths = writeXPaths; } +void XdmfWriter::setDocumentTitle(const std::string title) +{ + mImpl->mDocumentTitle = title; +} + +void XdmfWriter::setVersionString(const std::string version) +{ + mImpl->mVersionString = version; +} + void XdmfWriter::visit(XdmfArray & array, const boost::shared_ptr visitor) { bool isSubclassed = array.getItemTag().compare(XdmfArray::ItemTag) != 0; @@ -217,65 +233,74 @@ void XdmfWriter::visit(XdmfArray & array, const boost::shared_ptr visitor) { - if(mImpl->mXPathString.compare("") == 0) - { - mImpl->openFile(); + if (mImpl->mDepth == 0) { + mImpl->openFile(); } + mImpl->mDepth++; - if(mImpl->mWriteXPaths) + std::string tag = item.getItemTag(); + if (tag.length() == 0) { - mImpl->mXPathCount++; + item.traverse(visitor); + } + else + { + if(mImpl->mWriteXPaths) + { + mImpl->mXPathCount++; - std::string parentXPathString = mImpl->mXPathString; + std::string parentXPathString = mImpl->mXPathString; - std::stringstream newXPathString; - newXPathString << mImpl->mXPathString << "/" << mImpl->mXPathCount; - mImpl->mXPathString = newXPathString.str(); + std::stringstream newXPathString; + newXPathString << mImpl->mXPathString << "/" << mImpl->mXPathCount; + mImpl->mXPathString = newXPathString.str(); - std::map::const_iterator iter = mImpl->mXPath.find(&item); - if(iter != mImpl->mXPath.end()) - { - // Inserted before --- just xpath location of previously written node - mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar*)"xi:include", NULL); - xmlNewProp(mImpl->mXMLCurrentNode, (xmlChar*)"xpointer", (xmlChar*)iter->second.c_str()); - mImpl->mLastXPathed = true; + std::map::const_iterator iter = mImpl->mXPath.find(&item); + if(iter != mImpl->mXPath.end()) + { + // Inserted before --- just xpath location of previously written node + mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar*)"xi:include", NULL); + xmlNewProp(mImpl->mXMLCurrentNode, (xmlChar*)"xpointer", (xmlChar*)iter->second.c_str()); + mImpl->mLastXPathed = true; + } + else + { + // Not inserted before --- need to write all data and traverse. + mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar *)tag.c_str(), NULL); + std::stringstream xPathProp; + xPathProp << "element(/1" << mImpl->mXPathString << ")"; + mImpl->mXPath[&item] = xPathProp.str(); + const std::map itemProperties = item.getItemProperties(); + for(std::map::const_iterator iter = itemProperties.begin(); iter != itemProperties.end(); ++iter) + { + xmlNewProp(mImpl->mXMLCurrentNode, (xmlChar*)iter->first.c_str(), (xmlChar*)iter->second.c_str()); + } + unsigned int parentCount = mImpl->mXPathCount; + mImpl->mXPathCount = 0; + item.traverse(visitor); + mImpl->mXPathCount = parentCount; + mImpl->mLastXPathed = false; + } + + mImpl->mXPathString = parentXPathString; } else { // Not inserted before --- need to write all data and traverse. - mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar*)item.getItemTag().c_str(), NULL); - std::stringstream xPathProp; - xPathProp << "element(/1" << mImpl->mXPathString << ")"; - mImpl->mXPath[&item] = xPathProp.str(); + mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar*)tag.c_str(), NULL); const std::map itemProperties = item.getItemProperties(); for(std::map::const_iterator iter = itemProperties.begin(); iter != itemProperties.end(); ++iter) { xmlNewProp(mImpl->mXMLCurrentNode, (xmlChar*)iter->first.c_str(), (xmlChar*)iter->second.c_str()); } - unsigned int parentCount = mImpl->mXPathCount; - mImpl->mXPathCount = 0; item.traverse(visitor); - mImpl->mXPathCount = parentCount; - mImpl->mLastXPathed = false; } - mImpl->mXPathString = parentXPathString; + mImpl->mXMLCurrentNode = mImpl->mXMLCurrentNode->parent; } - else - { - // Not inserted before --- need to write all data and traverse. - mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar*)item.getItemTag().c_str(), NULL); - const std::map itemProperties = item.getItemProperties(); - for(std::map::const_iterator iter = itemProperties.begin(); iter != itemProperties.end(); ++iter) - { - xmlNewProp(mImpl->mXMLCurrentNode, (xmlChar*)iter->first.c_str(), (xmlChar*)iter->second.c_str()); - } - item.traverse(visitor); - } - - mImpl->mXMLCurrentNode = mImpl->mXMLCurrentNode->parent; - if(mImpl->mXPathString.compare("") == 0) + mImpl->mDepth--; + if(mImpl->mDepth <= 0) { mImpl->closeFile(); } diff --git a/core/XdmfWriter.hpp b/core/XdmfWriter.hpp index 581d17b7..8397fc13 100644 --- a/core/XdmfWriter.hpp +++ b/core/XdmfWriter.hpp @@ -108,6 +108,20 @@ public: */ void setMode(const Mode mode); + /** + * Set XML document title + * + * @param title, title to use for this XML document + */ + void setDocumentTitle(const std::string title); + + /** + * Set version String + * + * @param version, string to use as version attribute in document title + */ + void setVersionString(const std::string version); + /** * Set whether to write xpaths for this writer. * -- GitLab