Added proper support for writing out Xml graph

This commit is contained in:
Robert Osfield 2010-04-30 19:41:56 +00:00
parent 7ef776ec5f
commit 400aa8ebcd
2 changed files with 69 additions and 39 deletions

View File

@ -66,7 +66,26 @@ class OSGDB_EXPORT XmlNode : public osg::Referenced
public:
class OSGDB_EXPORT Input
class OSGDB_EXPORT ControlMap
{
public:
ControlMap();
typedef std::map< std::string, int > ControlToCharacterMap;
typedef std::map< int, std::string> CharacterToControlMap;
void addControlToCharacter(const std::string& control, int c);
ControlToCharacterMap _controlToCharacterMap;
CharacterToControlMap _characterToControlMap;
private:
void setUpControlMappings();
};
class OSGDB_EXPORT Input : public ControlMap
{
public:
@ -110,19 +129,8 @@ class OSGDB_EXPORT XmlNode : public osg::Referenced
bool match(const std::string& str) { return (_currentPos<_buffer.size()) ? _buffer.compare(_currentPos, str.size(), str)==0 : false; }
typedef std::map< std::string, int > ControlToCharacterMap;
typedef std::map< int, std::string> CharacterToControlMap;
void addControlToCharacter(const std::string& control, int c);
ControlToCharacterMap _controlToCharacterMap;
CharacterToControlMap _characterToControlMap;
private:
void setUpControlMappings();
size_type _currentPos;
std::ifstream _fin;
@ -131,14 +139,15 @@ class OSGDB_EXPORT XmlNode : public osg::Referenced
};
bool read(Input& input);
bool write(std::ostream& fout, const std::string& indent = "") const;
bool writeString(std::ostream& fout, const std::string& str) const;
bool write(const ControlMap& controlMap, std::ostream& fout, const std::string& indent = "") const;
bool writeString(const ControlMap& controlMap, 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;
bool writeChildren(const ControlMap& controlMap, std::ostream& fout, const std::string& indent) const;
bool writeProperties(const ControlMap& controlMap, std::ostream& fout) const;
};
}

View File

@ -77,24 +77,18 @@ XmlNode* osgDB::readXmlStream(std::istream& fin)
return root.release();
}
XmlNode::Input::Input():
_currentPos(0)
XmlNode::ControlMap::ControlMap()
{
setUpControlMappings();
}
XmlNode::Input::Input(const Input&):
_currentPos(0)
void XmlNode::ControlMap::addControlToCharacter(const std::string& control, int c)
{
setUpControlMappings();
_controlToCharacterMap[control] = c;
_characterToControlMap[c] = control;
}
XmlNode::Input::~Input()
{
}
void XmlNode::Input::setUpControlMappings()
void XmlNode::ControlMap::setUpControlMappings()
{
addControlToCharacter("&amp;",'&');
addControlToCharacter("&lt;",'<');
@ -103,12 +97,20 @@ void XmlNode::Input::setUpControlMappings()
addControlToCharacter("&apos;",'\'');
}
void XmlNode::Input::addControlToCharacter(const std::string& control, int c)
XmlNode::Input::Input():
_currentPos(0)
{
_controlToCharacterMap[control] = c;
_characterToControlMap[c] = control;
}
XmlNode::Input::Input(const Input&):
ControlMap(),
_currentPos(0)
{
}
XmlNode::Input::~Input()
{
}
void XmlNode::Input::open(const std::string& filename)
{
_fin.open(filename.c_str());
@ -362,31 +364,42 @@ bool XmlNode::read(Input& input)
}
bool XmlNode::write(std::ostream& fout, const std::string& indent) const
{
ControlMap controlMap;
return write(controlMap, fout, indent);
}
bool XmlNode::write(const ControlMap& controlMap, std::ostream& fout, const std::string& indent) const
{
switch(type)
{
case(UNASSIGNED):
OSG_NOTICE<<"UNASSIGNED"<<std::endl;
return false;
case(ATOM):
{
fout<<indent<<"<"<<name;
writeProperties(fout);
writeProperties(controlMap, fout);
fout<<" />"<<std::endl;
return true;
}
case(ROOT):
{
writeChildren(fout, indent);
writeChildren(controlMap, fout, indent);
return true;
}
case(NODE):
fout<<indent<<"<"<<name;
writeProperties(controlMap,fout);
fout<<">"; writeString(controlMap, fout, contents); fout<<"</"<<name<<">"<<std::endl;
return true;
case(GROUP):
{
fout<<indent<<"<"<<name;
writeProperties(fout);
writeProperties(controlMap,fout);
fout<<">"<<std::endl;
writeChildren(fout, indent + " ");
writeChildren(controlMap, fout, indent + " ");
fout<<indent<<"</"<<name<<">"<<std::endl;
return true;
@ -405,13 +418,21 @@ bool XmlNode::write(std::ostream& fout, const std::string& indent) const
return false;
}
bool XmlNode::writeString(std::ostream& fout, const std::string& str) const
bool XmlNode::writeString(const ControlMap& controlMap, std::ostream& fout, const std::string& str) const
{
fout<<str;
for(std::string::const_iterator itr = str.begin();
itr != str.end();
++itr)
{
int c = *itr;
ControlMap::CharacterToControlMap::const_iterator citr = controlMap._characterToControlMap.find(c);
if (citr != controlMap._characterToControlMap.end()) fout << citr->second;
else fout.put(c);
}
return true;
}
bool XmlNode::writeChildren(std::ostream& fout, const std::string& indent) const
bool XmlNode::writeChildren(const ControlMap& controlMap, std::ostream& fout, const std::string& indent) const
{
for(Children::const_iterator citr = children.begin();
citr != children.end();
@ -424,14 +445,14 @@ bool XmlNode::writeChildren(std::ostream& fout, const std::string& indent) const
return true;
}
bool XmlNode::writeProperties(std::ostream& fout) const
bool XmlNode::writeProperties(const ControlMap& controlMap, std::ostream& fout) const
{
for(Properties::const_iterator oitr = properties.begin();
oitr != properties.end();
++oitr)
{
fout<<" "<<oitr->first<<"=\"";
if (!writeString(fout,oitr->second))
if (!writeString(controlMap,fout,oitr->second))
return false;
fout<<"\"";
}