Skip to content

Commit

Permalink
Migrate parser.cc to TinyXML2
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Jul 13, 2020
1 parent 29210cf commit 6dd6d07
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 299 deletions.
8 changes: 5 additions & 3 deletions src/SDFExtension.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace sdf
// Inline bracket to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
using XMLDocumentPtr= std::shared_ptr<tinyxml2::XMLDocument>;
using XMLElementPtr = std::shared_ptr<tinyxml2::XMLElement>;

/// \internal
/// \brief A class for holding sdf extension elements in urdf
Expand All @@ -57,7 +59,7 @@ namespace sdf
public: std::string material;

/// \brief blobs of xml to be copied into the visual sdf element
public: std::vector<std::shared_ptr<tinyxml2::XMLElement> > visual_blobs;
public: std::vector<XMLDocumentPtr> visual_blobs;

/// \brief blobs of xml to be copied into the collision sdf element
/// An example might be:
Expand All @@ -84,7 +86,7 @@ namespace sdf
/// </gazebo>
/// where all the contents of `<collision>` element is copied into the
/// resulting collision sdf.
public: std::vector<std::shared_ptr<tinyxml2::XMLElement> > collision_blobs;
public: std::vector<XMLDocumentPtr> collision_blobs;

// body, default off
public: bool setStaticFlag;
Expand Down Expand Up @@ -118,7 +120,7 @@ namespace sdf
public: bool implicitSpringDamper;

// blobs into body or robot
public: std::vector<std::shared_ptr<tinyxml2::XMLElement> > blobs;
public: std::vector<XMLDocumentPtr> blobs;

friend class URDF2SDF;
};
Expand Down
20 changes: 20 additions & 0 deletions src/XmlUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ tinyxml2::XMLNode* DeepClone(tinyxml2::XMLDocument *_doc, const tinyxml2::XMLNod
return copy;
}

/////////////////////////////////////////////////
std::string &TrimStringLeft(std::string &_s)
{
_s.erase(_s.begin(),find_if_not(_s.begin(),_s.end(),[](int c){return isspace(c);}));
return _s;
}

/////////////////////////////////////////////////
std::string &TrimStringRight(std::string &_s)
{
_s.erase(find_if_not(_s.rbegin(),_s.rend(),[](int c){return isspace(c);}).base(), _s.end());
return _s;
}

/////////////////////////////////////////////////
std::string TrimString(const std::string &_s)
{
std::string t = _s;
return TrimStringLeft(TrimStringRight(t));
}
}
}

5 changes: 5 additions & 0 deletions src/XmlUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace sdf
/// \param[in] _src The node to deep copy
/// \returns The newly copied node
tinyxml2::XMLNode* DeepClone(tinyxml2::XMLDocument *_doc, const tinyxml2::XMLNode *_src);

/// \brief Trim whitespace from a string
/// \param[in] _s String to trim
/// \returns String _s with whitespace trimmed from both ends
std::string TrimString(const std::string &_s);
}
}
#endif
Expand Down
57 changes: 28 additions & 29 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,13 @@ template <typename TPtr>
static inline bool _initFile(const std::string &_filename, TPtr _sdf)
{
tinyxml2::XMLDocument xmlDoc;
if (xmlDoc.LoadFile(_filename))
if (xmlDoc.LoadFile(_filename.c_str()))
{
return initDoc(&xmlDoc, _sdf);
}
else
{
sdferr << "Unable to load file[" << _filename << "]\n";
sdferr << "Unable to load file[" << _filename << "]: " << xmlDoc.ErrorStr() << "\n";
return false;
}

return false;
return initDoc(&xmlDoc, _sdf);
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -134,10 +131,9 @@ bool initFile(const std::string &_filename, ElementPtr _sdf)
bool initString(const std::string &_xmlString, SDFPtr _sdf)
{
tinyxml2::XMLDocument xmlDoc;
xmlDoc.Parse(_xmlString.c_str());
if (xmlDoc.Error())
if (xmlDoc.Parse(_xmlString.c_str()))
{
sdferr << "Failed to parse string as XML: " << xmlDoc.ErrorDesc() << '\n';
sdferr << "Failed to parse string as XML: " << xmlDoc.ErrorStr() << '\n';
return false;
}

Expand Down Expand Up @@ -413,10 +409,11 @@ bool readFileInternal(const std::string &_filename, SDFPtr _sdf,
return false;
}

if (!xmlDoc.LoadFile(filename))
auto error_code = xmlDoc.LoadFile(filename.c_str());
if (error_code)
{
sdferr << "Error parsing XML in file [" << filename << "]: "
<< xmlDoc.ErrorDesc() << '\n';
<< xmlDoc.ErrorStr() << '\n';
return false;
}

Expand Down Expand Up @@ -478,7 +475,7 @@ bool readStringInternal(const std::string &_xmlString, SDFPtr _sdf,
xmlDoc.Parse(_xmlString.c_str());
if (xmlDoc.Error())
{
sdferr << "Error parsing XML from string: " << xmlDoc.ErrorDesc() << '\n';
sdferr << "Error parsing XML from string: " << xmlDoc.ErrorStr() << '\n';
return false;
}
if (readDoc(&xmlDoc, _sdf, "data-string", _convert, _errors))
Expand All @@ -488,7 +485,9 @@ bool readStringInternal(const std::string &_xmlString, SDFPtr _sdf,
else
{
URDF2SDF u2g;
tinyxml2::XMLDocument doc = u2g.InitModelString(_xmlString);
tinyxml2::XMLDocument doc;
u2g.InitModelString(&doc, _xmlString);

if (sdf::readDoc(&doc, _sdf, "urdf string", _convert, _errors))
{
sdfdbg << "Parsing from urdf.\n";
Expand Down Expand Up @@ -524,7 +523,7 @@ bool readString(const std::string &_xmlString, ElementPtr _sdf, Errors &_errors)
xmlDoc.Parse(_xmlString.c_str());
if (xmlDoc.Error())
{
sdferr << "Error parsing XML from string: " << xmlDoc.ErrorDesc() << '\n';
sdferr << "Error parsing XML from string: " << xmlDoc.ErrorStr() << '\n';
return false;
}
if (readDoc(&xmlDoc, _sdf, "data-string", true, _errors))
Expand Down Expand Up @@ -587,7 +586,7 @@ bool readDoc(tinyxml2::XMLDocument *_xmlDoc, SDFPtr _sdf,
}

// parse new sdf xml
tinyxml2::XMLElement *elemXml = _xmlDoc->FirstChildElement(_sdf->Root()->GetName());
tinyxml2::XMLElement *elemXml = _xmlDoc->FirstChildElement(_sdf->Root()->GetName().c_str());
if (!readXml(elemXml, _sdf->Root(), _errors))
{
_errors.push_back({ErrorCode::ELEMENT_INVALID,
Expand Down Expand Up @@ -650,9 +649,9 @@ bool readDoc(tinyxml2::XMLDocument *_xmlDoc, ElementPtr _sdf,

tinyxml2::XMLElement *elemXml = sdfNode;
if (sdfNode->Value() != _sdf->GetName() &&
sdfNode->FirstChildElement(_sdf->GetName()))
sdfNode->FirstChildElement(_sdf->GetName().c_str()))
{
elemXml = sdfNode->FirstChildElement(_sdf->GetName());
elemXml = sdfNode->FirstChildElement(_sdf->GetName().c_str());
}

// parse new sdf xml
Expand Down Expand Up @@ -770,11 +769,11 @@ std::string getModelFilePath(const std::string &_modelDirPath)
}

tinyxml2::XMLDocument configFileDoc;
if (!configFileDoc.LoadFile(configFilePath))
if (!configFileDoc.LoadFile(configFilePath.c_str()))
{
sdferr << "Error parsing XML in file ["
<< configFilePath << "]: "
<< configFileDoc.ErrorDesc() << '\n';
<< configFileDoc.ErrorStr() << '\n';
return std::string();
}

Expand Down Expand Up @@ -838,7 +837,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf, Errors &_errors)
_sdf->Copy(refSDF);
}

tinyxml2::XMLAttribute *attribute = _xml->FirstAttribute();
const tinyxml2::XMLAttribute *attribute = _xml->FirstAttribute();

unsigned int i = 0;

Expand All @@ -851,7 +850,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf, Errors &_errors)
{
_sdf->AddAttribute(attribute->Name(), "string", "", 1, "");
_sdf->GetAttribute(attribute->Name())->SetFromString(
attribute->ValueStr());
attribute->Value());
attribute = attribute->Next();
continue;
}
Expand All @@ -862,7 +861,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf, Errors &_errors)
if (p->GetKey() == attribute->Name())
{
// Set the value of the SDF attribute
if (!p->SetFromString(attribute->ValueStr()))
if (!p->SetFromString(attribute->Value()))
{
_errors.push_back({ErrorCode::ATTRIBUTE_INVALID,
"Unable to read attribute[" + p->GetKey() + "]"});
Expand Down Expand Up @@ -1170,7 +1169,7 @@ void copyChildren(ElementPtr _sdf, tinyxml2::XMLElement *_xml, const bool _onlyU
for (elemXml = _xml->FirstChildElement(); elemXml;
elemXml = elemXml->NextSiblingElement())
{
std::string elem_name = elemXml->ValueStr();
std::string elem_name = elemXml->Name();

if (_sdf->HasElementDescription(elem_name))
{
Expand All @@ -1179,11 +1178,11 @@ void copyChildren(ElementPtr _sdf, tinyxml2::XMLElement *_xml, const bool _onlyU
sdf::ElementPtr element = _sdf->AddElement(elem_name);

// FIXME: copy attributes
for (tinyxml2::XMLAttribute *attribute = elemXml->FirstAttribute();
for (const tinyxml2::XMLAttribute *attribute = elemXml->FirstAttribute();
attribute; attribute = attribute->Next())
{
element->GetAttribute(attribute->Name())->SetFromString(
attribute->ValueStr());
attribute->Value());
}

// copy value
Expand All @@ -1205,12 +1204,12 @@ void copyChildren(ElementPtr _sdf, tinyxml2::XMLElement *_xml, const bool _onlyU
element->AddValue("string", elemXml->GetText(), "1");
}

for (tinyxml2::XMLAttribute *attribute = elemXml->FirstAttribute();
for (const tinyxml2::XMLAttribute *attribute = elemXml->FirstAttribute();
attribute; attribute = attribute->Next())
{
element->AddAttribute(attribute->Name(), "string", "", 1, "");
element->GetAttribute(attribute->Name())->SetFromString(
attribute->ValueStr());
attribute->Value());
}

copyChildren(element, elemXml, _onlyUnknown);
Expand Down Expand Up @@ -1373,7 +1372,7 @@ bool convertFile(const std::string &_filename, const std::string &_version,
}

tinyxml2::XMLDocument xmlDoc;
if (xmlDoc.LoadFile(filename))
if (!xmlDoc.LoadFile(filename.c_str()))
{
// read initial sdf version
std::string originalVersion;
Expand Down
Loading

0 comments on commit 6dd6d07

Please sign in to comment.