26#define SYNTAX_ERROR(line) yError() << "Syntax error while loading" << curr_filename << "at line" << line << "."
27#define SYNTAX_WARNING(line) yWarning() << "Invalid syntax while loading" << curr_filename << "at line" << line << "."
32#define TINYXML_UNSIGNED_INT_BUG 0
59 bool PerformInclusions(TiXmlNode* pParent,
const std::string& parent_fileName,
const std::string& current_path);
60 void ReplaceAllStrings(std::string& str,
const std::string& from,
const std::string& to);
87 yDebug() <<
"Reading file" << fileName.c_str();
88 auto* doc =
new TiXmlDocument(fileName.c_str());
89 if (!doc->LoadFile()) {
94 if (!doc->RootElement()) {
100 for (TiXmlNode* childNode = doc->FirstChild(); childNode != 0; childNode = childNode->NextSibling()) {
101 if (childNode->Type() == TiXmlNode::TINYXML_UNKNOWN) {
102 if (dtd.parse(childNode->ToUnknown(), curr_filename)) {
109 SYNTAX_WARNING(doc->Row()) <<
"No DTD found. Assuming version yarprobotinterfaceV1.0";
119 if (dtd.majorVersion != 1 || dtd.minorVersion != 0) {
120 SYNTAX_WARNING(doc->Row()) <<
"Only yarprobotinterface DTD version 1.0 is supported";
124 std::string current_path;
125 current_path = fileName.substr(0, fileName.find_last_of(
"\\/"));
126 std::string current_filename;
127 std::string log_filename;
128 current_filename = fileName.substr(fileName.find_last_of(
"\\/") + 1);
129 log_filename = current_filename.substr(0, current_filename.find(
".xml"));
130 log_filename +=
"_preprocessor_log.xml";
132 PerformInclusions(doc->RootElement(), current_filename, current_path);
134 std::string full_log_withpath = current_path + std::string(
"\\") + log_filename;
135 std::replace(full_log_withpath.begin(), full_log_withpath.end(),
'\\',
'/');
136 yDebug() <<
"Preprocessor complete in: " << end_time - start_time <<
"s";
137 if (verbose_output) {
138 yDebug() <<
"Preprocessor output stored in: " << full_log_withpath;
139 doc->SaveFile(full_log_withpath);
151 curr_filename =
" XML runtime string ";
152 auto* doc =
new TiXmlDocument();
153 if (!doc->Parse(xmlString.c_str())) {
158 if (!doc->RootElement()) {
172 for (TiXmlElement* childElem = pParent->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
175 if (childElem->FirstAttribute())
176 a = childElem->FirstAttribute()->Value();
177 yDebug() <<
"Parsing" << childElem->Value() << a;
179 std::string elemString = childElem->ValueStr();
180 if (elemString ==
"file") {
181 yFatal() <<
"'file' attribute is forbidden in yarprobotinterface DTD format 3.0. Error found in " << parent_fileName;
185 if (elemString ==
"xi:include") {
186 std::string href_filename;
187 std::string included_filename;
188 std::string included_path;
189 if (childElem->QueryStringAttribute(
"href", &href_filename) == TIXML_SUCCESS) {
190 included_path = std::string(current_path).append(
"\\").append(href_filename.substr(0, href_filename.find_last_of(
"\\/")));
191 included_filename = href_filename.substr(href_filename.find_last_of(
"\\/") + 1);
192 std::string full_path_file = std::string(included_path).append(
"\\").append(included_filename);
193 TiXmlDocument included_file;
195 std::replace(full_path_file.begin(), full_path_file.end(),
'\\',
'/');
196 if (included_file.LoadFile(full_path_file)) {
197 PerformInclusions(included_file.RootElement(), included_filename, included_path);
199 included_file.RootElement()->RemoveAttribute(
"xmlns:xi");
200 if (pParent->ReplaceChild(childElem, *included_file.FirstChildElement())) {
205 yFatal() <<
"Failed to include: " << included_filename <<
" in: " << parent_fileName;
210 yError() << included_file.ErrorDesc() <<
" file" << full_path_file <<
"included by " << parent_fileName <<
"at line" << childElem->Row();
211 yFatal() <<
"In file:" << included_filename <<
" included by: " << parent_fileName <<
" at line: " << childElem->Row();
216 yFatal() <<
"Syntax error in: " << parent_fileName <<
" while searching for href attribute";
220 PerformInclusions(childElem, parent_fileName, current_path);
230 if (robotElem->ValueStr() !=
"robot") {
231 SYNTAX_ERROR(robotElem->Row()) <<
"Root element should be \"robot\". Found" << robotElem->ValueStr();
235 if (robotElem->QueryStringAttribute(
"name", &result.
robot.
name()) != TIXML_SUCCESS) {
236 SYNTAX_ERROR(robotElem->Row()) << R
"("robot" element should contain the "name" attribute)";
240#if TINYXML_UNSIGNED_INT_BUG
241 if (robotElem->QueryUnsignedAttribute(
"build", &result.
robot.
build()) != TIXML_SUCCESS) {
243 SYNTAX_WARNING(robotElem->Row()) <<
"\"robot\" element should contain the \"build\" attribute [unsigned int]. Assuming 0";
247 if (robotElem->QueryIntAttribute(
"build", &tmp) != TIXML_SUCCESS || tmp < 0) {
249 SYNTAX_WARNING(robotElem->Row()) << R
"("robot" element should contain the "build" attribute [unsigned int]. Assuming 0)";
252 result.robot.build() = static_cast<unsigned>(tmp);
256 if (!config.check(
"portprefix"))
258 if (robotElem->QueryStringAttribute(
"portprefix", &result.
robot.
portprefix()) != TIXML_SUCCESS) {
259 SYNTAX_WARNING(robotElem->Row()) << R
"("robot" element should contain the "portprefix" attribute. Using "name" attribute)";
268 bool reverse =
false;
269 if (robotElem->QueryBoolAttribute(
"reverse-shutdown-action-order", &reverse) == TIXML_WRONG_TYPE) {
270 SYNTAX_ERROR(robotElem->Row()) << R
"(The "reverse-shutdown-action-order" attribute in the "robot" element should be a bool.)";
277 for (TiXmlElement* childElem = robotElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
278 std::string elemString = childElem->ValueStr();
279 if (elemString ==
"device" || elemString ==
"devices") {
280 for (
const auto& childDevice : readDevices(childElem, result)) {
284 for (
const auto& childParam : readParams(childElem, result)) {
297 const std::string& valueStr = devicesElem->ValueStr();
299 if (valueStr ==
"device") {
302 deviceList.push_back(readDeviceTag(devicesElem, result));
305 if (valueStr ==
"devices") {
307 return readDevicesTag(devicesElem, result);
310 SYNTAX_ERROR(devicesElem->Row()) << R
"(Expected "device" or "devices". Found)" << valueStr;
318 const std::string& valueStr = deviceElem->ValueStr();
320 if (valueStr !=
"device") {
321 SYNTAX_ERROR(deviceElem->Row()) <<
"Expected \"device\". Found" << valueStr;
328 if (deviceElem->QueryStringAttribute(
"name", &device.
name()) != TIXML_SUCCESS) {
329 SYNTAX_ERROR(deviceElem->Row()) << R
"("device" element should contain the "name" attribute)";
336 if (deviceElem->QueryStringAttribute(
"type", &device.
type()) != TIXML_SUCCESS) {
337 SYNTAX_ERROR(deviceElem->Row()) << R
"("device" element should contain the "type" attribute)";
344 for (TiXmlElement* childElem = deviceElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
345 if (childElem->ValueStr() ==
"action" || childElem->ValueStr() ==
"actions") {
346 for (
const auto& childAction : readActions(childElem, result)) {
347 device.
actions().push_back(childAction);
350 for (
const auto& childParam : readParams(childElem, result)) {
351 device.
params().push_back(childParam);
368 for (TiXmlElement* childElem = devicesElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
371 if (childElem->ValueStr() ==
"param" || childElem->ValueStr() ==
"group" || childElem->ValueStr() ==
"paramlist" || childElem->ValueStr() ==
"params") {
372 for (
const auto& childParam : readParams(childElem, result)) {
373 params.push_back(childParam);
376 for (
const auto& childDevice : readDevices(childElem, result)) {
377 devices.push_back(childDevice);
383 for (
auto& device : devices) {
384 device.params().insert(device.params().begin(), params.begin(), params.end());
393 const std::string& valueStr = paramsElem->ValueStr();
395 if (valueStr ==
"param") {
397 params.push_back(readParamTag(paramsElem, result));
400 if (valueStr ==
"group") {
402 params.push_back(readGroupTag(paramsElem, result));
405 if (valueStr ==
"paramlist") {
406 return readParamListTag(paramsElem, result);
408 if (valueStr ==
"subdevice") {
409 return readSubDeviceTag(paramsElem, result);
411 if (valueStr ==
"params") {
412 return readParamsTag(paramsElem, result);
415 SYNTAX_ERROR(paramsElem->Row()) << R
"(Expected "param", "group", "paramlist", "subdevice", or "params". Found)" << valueStr;
424 size_t start_pos = 0;
425 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
426 str.replace(start_pos, from.length(), to);
427 start_pos += to.length();
435 if (paramElem->ValueStr() !=
"param") {
436 SYNTAX_ERROR(paramElem->Row()) <<
"Expected \"param\". Found" << paramElem->ValueStr();
443 if (paramElem->QueryStringAttribute(
"name", ¶m.
name()) != TIXML_SUCCESS) {
444 SYNTAX_ERROR(paramElem->Row()) << R
"("param" element should contain the "name" attribute)";
451 const char* valueText = paramElem->GetText();
453 SYNTAX_ERROR(paramElem->Row()) << R
"("param" element should have a value [ "name" = )" << param.name() << "]";
459 std::string extern_name;
460 if (paramElem->QueryStringAttribute(
"extern-name", &extern_name) == TIXML_SUCCESS && config.check(extern_name)) {
462 if (config.find(extern_name).isList())
464 param.
value() =
"(" + config.find(extern_name).asList()->toString() +
")";
468 param.
value() = config.find(extern_name).toString();
471 param.
value() = valueText;
475 std::string paramValueBefore = param.
value();
476 std::string paramValueAfter = paramValueBefore;
477 std::string portprefix = config.find(
"portprefix").toString();
478 ReplaceAllStrings(paramValueAfter,
"${portprefix}", portprefix);
479 param.
value() = paramValueAfter;
490 if (groupElem->ValueStr() !=
"group") {
491 SYNTAX_ERROR(groupElem->Row()) <<
"Expected \"group\". Found" << groupElem->ValueStr();
498 if (groupElem->QueryStringAttribute(
"name", &group.
name()) != TIXML_SUCCESS) {
499 SYNTAX_ERROR(groupElem->Row()) << R
"("group" element should contain the "name" attribute)";
507 for (TiXmlElement* childElem = groupElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
508 for (
const auto& childParam : readParams(childElem, result)) {
509 params.push_back(childParam);
512 if (params.empty()) {
513 SYNTAX_ERROR(groupElem->Row()) <<
"\"group\" cannot be empty";
518 std::string groupString;
519 for (
auto& param : params) {
520 if (!groupString.empty()) {
523 groupString +=
"(" + param.name() +
" " + param.value() +
")";
526 group.
value() = groupString;
534 if (paramListElem->ValueStr() !=
"paramlist") {
535 SYNTAX_ERROR(paramListElem->Row()) <<
"Expected \"paramlist\". Found" << paramListElem->ValueStr();
543 if (paramListElem->QueryStringAttribute(
"name", &mainparam.
name()) != TIXML_SUCCESS) {
544 SYNTAX_ERROR(paramListElem->Row()) << R
"("paramlist" element should contain the "name" attribute)";
549 params.push_back(mainparam);
553 for (TiXmlElement* childElem = paramListElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
554 if (childElem->ValueStr() !=
"elem") {
555 SYNTAX_ERROR(childElem->Row()) <<
"Expected \"elem\". Found" << childElem->ValueStr();
562 if (childElem->QueryStringAttribute(
"name", &childParam.
name()) != TIXML_SUCCESS) {
563 SYNTAX_ERROR(childElem->Row()) << R
"("elem" element should contain the "name" attribute)";
568 const char* valueText = childElem->GetText();
570 SYNTAX_ERROR(childElem->Row()) << R
"("elem" element should have a value [ "name" = )" << childParam.name() << "]";
574 childParam.
value() = valueText;
576 params.push_back(childParam);
579 if (params.empty()) {
580 SYNTAX_ERROR(paramListElem->Row()) <<
"\"paramlist\" cannot be empty";
586 for (
auto it = params.begin() + 1; it != params.end(); ++it) {
588 params.at(0).
value() += (params.at(0).value().empty() ?
"(" :
" ") + param.
name();
590 params.at(0).value() +=
")";
599 if (subDeviceElem->ValueStr() !=
"subdevice") {
600 SYNTAX_ERROR(subDeviceElem->Row()) <<
"Expected \"subdevice\". Found" << subDeviceElem->ValueStr();
611 subDeviceParam.
name() =
"subdevice";
617 if (subDeviceElem->QueryStringAttribute(
"type", &subDeviceParam.
value()) != TIXML_SUCCESS) {
618 SYNTAX_ERROR(subDeviceElem->Row()) << R
"("subdevice" element should contain the "type" attribute)";
624 params.push_back(subDeviceParam);
628 for (TiXmlElement* childElem = subDeviceElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
629 for (
const auto& childParam : readParams(childElem, result)) {
644 for (TiXmlElement* childElem = paramsElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
645 for (
const auto & childParam : readParams(childElem, result)) {
646 params.push_back(childParam);
656 const std::string& valueStr = actionsElem->ValueStr();
658 if (valueStr !=
"action" && valueStr !=
"actions") {
659 SYNTAX_ERROR(actionsElem->Row()) << R
"(Expected "action" or "actions". Found)" << valueStr;
662 if (valueStr ==
"action") {
664 actionList.push_back(readActionTag(actionsElem, result));
668 return readActionsTag(actionsElem, result);
674 if (actionElem->ValueStr() !=
"action") {
675 SYNTAX_ERROR(actionElem->Row()) <<
"Expected \"action\". Found" << actionElem->ValueStr();
683 SYNTAX_ERROR(actionElem->Row()) << R
"("action" element should contain the "phase" attribute [startup|interrupt{1,2,3}|shutdown])";
690 SYNTAX_ERROR(actionElem->Row()) << R
"("action" element should contain the "type" attribute [configure|calibrate|attach|abort|detach|park|custom])";
697#if TINYXML_UNSIGNED_INT_BUG
698 if (actionElem->QueryUnsignedAttribute(
"level", &action.
level()) != TIXML_SUCCESS) {
699 SYNTAX_ERROR(actionElem->Row()) <<
"\"action\" element should contain the \"level\" attribute [unsigned int]";
703 if (actionElem->QueryIntAttribute(
"level", &tmp) != TIXML_SUCCESS || tmp < 0) {
704 SYNTAX_ERROR(actionElem->Row()) << R
"("action" element should contain the "level" attribute [unsigned int])";
708 action.
level() =
static_cast<unsigned>(tmp);
711 for (TiXmlElement* childElem = actionElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
712 for (
const auto& childParam : readParams(childElem, result)) {
713 action.
params().push_back(childParam);
726 std::string robotName;
727 if (actionsElem->QueryStringAttribute(
"robot", &robotName) != TIXML_SUCCESS) {
728 SYNTAX_WARNING(actionsElem->Row()) << R
"("actions" element should contain the "robot" attribute)";
732 SYNTAX_WARNING(actionsElem->Row()) <<
"Trying to import a file for the wrong robot. Found" << robotName <<
"instead of" << result.
robot.
name();
736#if TINYXML_UNSIGNED_INT_BUG
737 if (actionsElem->QueryUnsignedAttribute(
"build", &build()) != TIXML_SUCCESS) {
739 SYNTAX_WARNING(actionsElem->Row()) <<
"\"actions\" element should contain the \"build\" attribute [unsigned int]. Assuming 0";
743 if (actionsElem->QueryIntAttribute(
"build", &tmp) != TIXML_SUCCESS || tmp < 0) {
745 SYNTAX_WARNING(actionsElem->Row()) << R
"("actions" element should contain the "build" attribute [unsigned int]. Assuming 0)";
748 build = static_cast<unsigned>(tmp);
752 SYNTAX_WARNING(actionsElem->Row()) <<
"Import a file for a different robot build. Found" << build <<
"instead of" << result.
robot.
build();
756 for (TiXmlElement* childElem = actionsElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
757 for (
const auto & childAction : readActions(childElem, result)) {
758 actions.push_back(childAction);
#define SYNTAX_ERROR(line)
#define SYNTAX_WARNING(line)
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void clear()
Remove all associations.
A base class for nested structures that can be searched.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
std::string & portprefix()
void setReverseShutdownActionOrder(bool reverseShutdownActionOrder)
Result of the parsing of yarp::robotinterface::XMLReader.
bool parsingIsSuccessful
True if the parsing was successful, false otherwise.
Robot robot
If parsingIsSuccessful is true, contains a valid robot instance.
static XMLReaderResult ParsingFailed()
unsigned int majorVersion
yarp::os::Property config
void ReplaceAllStrings(std::string &str, const std::string &from, const std::string &to)
yarp::robotinterface::Device readDeviceTag(TiXmlElement *deviceElem, yarp::robotinterface::XMLReaderResult &result)
unsigned int minorVersion
yarp::robotinterface::Param readParamTag(TiXmlElement *paramElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::XMLReaderResult readRobotFromString(const std::string &xmlString)
yarp::robotinterface::ActionList readActionsTag(TiXmlElement *actionsElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::ParamList readSubDeviceTag(TiXmlElement *subDeviceElem, yarp::robotinterface::XMLReaderResult &result)
Private(XMLReaderFileV3 *parent)
yarp::robotinterface::DeviceList readDevices(TiXmlElement *devicesElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::ParamList readParamListTag(TiXmlElement *paramListElem, yarp::robotinterface::XMLReaderResult &result)
bool PerformInclusions(TiXmlNode *pParent, const std::string &parent_fileName, const std::string ¤t_path)
yarp::robotinterface::XMLReaderResult readRobotTag(TiXmlElement *robotElem)
yarp::robotinterface::Param readGroupTag(TiXmlElement *groupElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::ParamList readParams(TiXmlElement *paramsElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::DeviceList readDevicesTag(TiXmlElement *devicesElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::ActionList readActions(TiXmlElement *actionsElem, yarp::robotinterface::XMLReaderResult &result)
XMLReaderFileV3 *const parent
yarp::robotinterface::Action readActionTag(TiXmlElement *actionElem, yarp::robotinterface::XMLReaderResult &result)
std::string curr_filename
yarp::robotinterface::ParamList readParamsTag(TiXmlElement *paramsElem, yarp::robotinterface::XMLReaderResult &result)
yarp::robotinterface::XMLReaderResult readRobotFromFile(const std::string &fileName)
yarp::robotinterface::XMLReaderResult getRobotFromString(const std::string &xmlString, const yarp::os::Searchable &config, bool verbose=false) override
yarp::robotinterface::XMLReaderResult getRobotFromFile(const std::string &filename, const yarp::os::Searchable &config, bool verbose=false) override
~XMLReaderFileV3() override
double now()
Return the current time in seconds, relative to an arbitrary starting point.
yarp::robotinterface::Param Param
yarp::robotinterface::Device Device
yarp::robotinterface::Action Action
std::string DocTypeToString(RobotInterfaceDTD::DocType doctype)
std::vector< robotinterface::Action > ActionList
std::vector< robotinterface::Param > ParamList
std::vector< robotinterface::Device > DeviceList