From Jean-Sebastien Guay, "OK, so here are new changes.

1. The node type will be set to ATOM on read of <tag prop="..." ... /> 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 <tag ... /> 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."
This commit is contained in:
Robert Osfield 2010-01-11 17:36:03 +00:00
parent 8bef9c57a3
commit cbd9a1370e
2 changed files with 45 additions and 67 deletions

View File

@ -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;
};
}

View File

@ -307,6 +307,7 @@ bool XmlNode::read(Input& input)
{
++input;
osg::notify(osg::INFO)<<"tag is closed correctly"<<std::endl;
childNode->type = ATOM;
}
else
osg::notify(osg::NOTICE)<<"Error: tag is not closed correctly"<<std::endl;
@ -360,7 +361,7 @@ bool XmlNode::read(Input& input)
return false;
}
bool XmlNode::write(std::ostream& fout) const
bool XmlNode::write(std::ostream& fout, const std::string& indent) const
{
switch(type)
{
@ -368,91 +369,36 @@ bool XmlNode::write(std::ostream& fout) const
return false;
case(ATOM):
{
fout<<"<"<<name;
for(Properties::const_iterator oitr = properties.begin();
oitr != properties.end();
++oitr)
{
fout<<oitr->first<<"\"";
writeString(fout,oitr->second);
fout<<"\""<<std::endl;
}
return true;
fout<<indent<<"<"<<name;
writeProperties(fout);
fout<<" />"<<std::endl;
return true;
}
case(ROOT):
{
for(Children::const_iterator citr = children.begin();
citr != children.end();
++citr)
{
(*citr)->write(fout);
}
writeChildren(fout, indent);
return true;
}
case(NODE):
{
fout<<"<"<<name;
for(Properties::const_iterator oitr = properties.begin();
oitr != properties.end();
++oitr)
{
fout<<" "<<oitr->first<<"=\"";
writeString(fout,oitr->second);
fout<<"\"";
}
if (children.empty() && contents.empty())
{
fout<<" />"<<std::endl;
}
else
{
fout<<">";
for(Children::const_iterator citr = children.begin();
citr != children.end();
++citr)
{
(*citr)->write(fout);
}
if (!contents.empty()) writeString(fout,contents);
fout<<"</"<<name<<">"<<std::endl;
}
return true;
}
case(GROUP):
{
fout<<"<"<<name;
for(Properties::const_iterator oitr = properties.begin();
oitr != properties.end();
++oitr)
{
fout<<" "<<oitr->first<<"=\"";
writeString(fout,oitr->second);
fout<<"\"";
}
fout<<indent<<"<"<<name;
writeProperties(fout);
fout<<">"<<std::endl;
for(Children::const_iterator citr = children.begin();
citr != children.end();
++citr)
{
(*citr)->write(fout);
}
writeChildren(fout, indent + " ");
fout<<"</"<<name<<">"<<std::endl;
fout<<indent<<"</"<<name<<">"<<std::endl;
return true;
}
case(COMMENT):
{
fout<<"<!--"<<contents<<"-->"<<std::endl;
fout<<indent<<"<!--"<<contents<<"-->"<<std::endl;
return true;
}
case(INFORMATION):
{
fout<<"<?"<<contents<<"?>"<<std::endl;
fout<<indent<<"<?"<<contents<<"?>"<<std::endl;
return true;
}
}
@ -464,3 +410,31 @@ bool XmlNode::writeString(std::ostream& fout, const std::string& str) const
fout<<str;
return true;
}
bool XmlNode::writeChildren(std::ostream& fout, const std::string& indent) const
{
for(Children::const_iterator citr = children.begin();
citr != children.end();
++citr)
{
if (!(*citr)->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<<" "<<oitr->first<<"=\"";
if (!writeString(fout,oitr->second))
return false;
fout<<"\"";
}
return true;
}