17 #ifndef GZ_MSGS_CONVERT_FUELMETADATA_HH_
18 #define GZ_MSGS_CONVERT_FUELMETADATA_HH_
21 #include "gz/msgs/fuel_metadata.pb.h"
28 #include <gz/math/SemanticVersion.hh>
32 inline namespace GZ_MSGS_VERSION_NAMESPACE {
36 msgs::FuelMetadata &_meta)
38 gz::msgs::FuelMetadata meta;
43 [](
int c) {return !std::isspace(c);}));
47 [](
int c) {return !std::isspace(c);}).base(), _s.end());
53 tinyxml2::XMLDocument modelConfigDoc;
54 if (modelConfigDoc.Parse(_modelConfigStr.
c_str()) !=
55 tinyxml2::XML_SUCCESS)
57 std::cerr <<
"Unable to parse model config XML string.\n";
62 tinyxml2::XMLElement *topElement = modelConfigDoc.FirstChildElement(
67 topElement = modelConfigDoc.FirstChildElement(
"world");
70 std::cerr <<
"Model config string does not contain a "
71 <<
"<model> or <world> element\n";
78 tinyxml2::XMLElement *elem = topElement->FirstChildElement(
"name");
79 if (!elem || !elem->GetText())
81 std::cerr <<
"Model config string does not contain a <name> element\n";
84 meta.set_name(trimmed(elem->GetText()));
87 elem = topElement->FirstChildElement(
"version");
88 if (elem && elem->GetText())
90 auto version =
std::stoi(trimmed(elem->GetText()));
91 meta.set_version(version);
95 elem = topElement->FirstChildElement(
"description");
96 if (elem && elem->GetText())
97 meta.set_description(trimmed(elem->GetText()));
100 elem = topElement->FirstChildElement(
"depend");
103 auto modelElem = elem->FirstChildElement(
"model");
106 auto uriElem = modelElem->FirstChildElement(
"uri");
109 auto dependency = meta.add_dependencies();
110 dependency->set_uri(uriElem->GetText());
113 elem = elem->NextSiblingElement(
"depend");
117 elem = topElement->FirstChildElement(
"author");
120 gz::msgs::FuelMetadata::Contact *author = meta.add_authors();
122 if (elem->FirstChildElement(
"name") &&
123 elem->FirstChildElement(
"name")->GetText())
125 author->set_name(trimmed(elem->FirstChildElement(
"name")->GetText()));
127 if (elem->FirstChildElement(
"email") &&
128 elem->FirstChildElement(
"email")->GetText())
131 trimmed(elem->FirstChildElement(
"email")->GetText()));
134 elem = elem->NextSiblingElement(
"author");
138 elem = topElement->FirstChildElement(
"sdf");
142 if (elem->GetText() && elem->Attribute(
"version"))
148 gz::msgs::Version *verMsg;
152 meta.mutable_model()->mutable_file_format()->set_name(
"sdf");
154 meta.mutable_model()->mutable_file_format()->mutable_version();
155 meta.mutable_model()->set_file(trimmed(elem->GetText()));
159 meta.mutable_world()->mutable_file_format()->set_name(
"sdf");
161 meta.mutable_world()->mutable_file_format()->mutable_version();
162 meta.mutable_world()->set_file(trimmed(elem->GetText()));
165 verMsg->set_major(ver.
Major());
166 verMsg->set_minor(ver.
Minor());
167 verMsg->set_patch(ver.
Patch());
169 verMsg->set_build(ver.
Build());
173 elem = elem->NextSiblingElement(
"sdf");
175 if (meta.model().file().empty() && meta.world().file().empty())
177 std::cerr <<
"Model config string does not contain an <sdf> element\n";
181 _meta.CopyFrom(meta);
192 if (_meta.has_model())
194 if (_meta.model().file_format().name() !=
"sdf")
196 std::cerr <<
"Model _metadata does not contain an SDF file.\n";
200 out <<
"<?xml version='1.0'?>\n"
203 << _meta.model().file_format().version().major()
204 <<
"." << _meta.model().file_format().version().minor() <<
"'>"
205 << _meta.model().file() <<
"</sdf>\n";
209 if (_meta.world().file_format().name() !=
"sdf")
211 std::cerr <<
"World _metadata does not contain an SDF file.\n";
215 out <<
"<?xml version='1.0'?>\n"
218 << _meta.world().file_format().version().major()
219 <<
"." << _meta.world().file_format().version().minor() <<
"'>"
220 << _meta.world().file() <<
"</sdf>\n";
223 out <<
" <name>" << _meta.name() <<
"</name>\n"
224 <<
" <version>" << _meta.version() <<
"</version>\n"
225 <<
" <description>" << _meta.description() <<
"</description>\n";
228 for (
int i = 0; i < _meta.authors_size(); ++i)
231 <<
" <name>" << _meta.authors(i).name() <<
"</name>\n"
232 <<
" <email>" << _meta.authors(i).email() <<
"</email>\n"
237 for (
int i = 0; i < _meta.dependencies_size(); ++i)
241 <<
" <uri>" << _meta.dependencies(i).uri() <<
"</uri>\n"
247 if (_meta.has_model())
248 out <<
" </model>\n";
250 out <<
" </world>\n";
252 _modelConfigStr = out.
str();