From cbd9a1370e1a4eb25383068b4816d64f13730d02 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 11 Jan 2010 17:36:03 +0000 Subject: [PATCH] From Jean-Sebastien Guay, "OK, so here are new changes. 1. The node type will be set to ATOM on read of type tags. 2. GROUP and NODE are now written using the same code (and not just duplicated code). Also NODE will not be written as an ATOM if it has no children or contents, so you need to set the type to ATOM if you want the style. 3. You had put the write of "/>" for ATOM after the "return true", so it had no effect... Moved to before the return. 4. ATOM did not write its properties correctly, fixed. 5. As an added bonus, I made the write() method indent the output so it's more readable. It brings a small public interface change but the indent argument has a default value so client code doesn't need to change (if there even is any). 6. Another added bonus, I've simplified the write() method a bit by factoring out the write for children and properties into protected methods." --- include/osgDB/XmlParser | 6 ++- src/osgDB/XmlParser.cpp | 106 +++++++++++++++------------------------- 2 files changed, 45 insertions(+), 67 deletions(-) diff --git a/include/osgDB/XmlParser b/include/osgDB/XmlParser index 6631b7517..fc6b5bf82 100644 --- a/include/osgDB/XmlParser +++ b/include/osgDB/XmlParser @@ -132,9 +132,13 @@ class OSGDB_EXPORT XmlNode : public osg::Referenced bool read(Input& input); - bool write(std::ostream& fout) const; + bool write(std::ostream& fout, const std::string& indent = "") const; bool writeString(std::ostream& fout, const std::string& str) const; + protected: + + bool writeChildren(std::ostream& fout, const std::string& indent) const; + bool writeProperties(std::ostream& fout) const; }; } diff --git a/src/osgDB/XmlParser.cpp b/src/osgDB/XmlParser.cpp index 71729c03e..ebad4413f 100644 --- a/src/osgDB/XmlParser.cpp +++ b/src/osgDB/XmlParser.cpp @@ -307,6 +307,7 @@ bool XmlNode::read(Input& input) { ++input; osg::notify(osg::INFO)<<"tag is closed correctly"<type = ATOM; } else osg::notify(osg::NOTICE)<<"Error: tag is not closed correctly"<first<<"\""; - writeString(fout,oitr->second); - fout<<"\""<"<write(fout); - } + writeChildren(fout, indent); return true; } case(NODE): - { - fout<<"<"<first<<"=\""; - writeString(fout,oitr->second); - fout<<"\""; - } - - if (children.empty() && contents.empty()) - { - fout<<" />"<"; - for(Children::const_iterator citr = children.begin(); - citr != children.end(); - ++citr) - { - (*citr)->write(fout); - } - - if (!contents.empty()) writeString(fout,contents); - - fout<<""<first<<"=\""; - writeString(fout,oitr->second); - fout<<"\""; - } + fout<"<write(fout); - } + writeChildren(fout, indent + " "); - fout<<""<"<"<"<"<"<write(fout, indent)) + return false; + } + + return true; +} + +bool XmlNode::writeProperties(std::ostream& fout) const +{ + for(Properties::const_iterator oitr = properties.begin(); + oitr != properties.end(); + ++oitr) + { + fout<<" "<first<<"=\""; + if (!writeString(fout,oitr->second)) + return false; + fout<<"\""; + } + + return true; +}