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);
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);
255 if (robotElem->QueryStringAttribute(
"portprefix", &result.
robot.
portprefix()) != TIXML_SUCCESS) {
256 SYNTAX_WARNING(robotElem->Row()) << R
"("robot" element should contain the "portprefix" attribute. Using "name" attribute)";
262 for (TiXmlElement* childElem = robotElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
263 std::string elemString = childElem->ValueStr();
264 if (elemString ==
"device" || elemString ==
"devices") {
265 for (
const auto& childDevice : readDevices(childElem, result)) {
269 for (
const auto& childParam : readParams(childElem, result)) {
282 const std::string& valueStr = devicesElem->ValueStr();
284 if (valueStr ==
"device") {
287 deviceList.push_back(readDeviceTag(devicesElem, result));
290 if (valueStr ==
"devices") {
292 return readDevicesTag(devicesElem, result);
295 SYNTAX_ERROR(devicesElem->Row()) << R
"(Expected "device" or "devices". Found)" << valueStr;
303 const std::string& valueStr = deviceElem->ValueStr();
305 if (valueStr !=
"device") {
306 SYNTAX_ERROR(deviceElem->Row()) <<
"Expected \"device\". Found" << valueStr;
313 if (deviceElem->QueryStringAttribute(
"name", &device.
name()) != TIXML_SUCCESS) {
314 SYNTAX_ERROR(deviceElem->Row()) << R
"("device" element should contain the "name" attribute)";
321 if (deviceElem->QueryStringAttribute(
"type", &device.
type()) != TIXML_SUCCESS) {
322 SYNTAX_ERROR(deviceElem->Row()) << R
"("device" element should contain the "type" attribute)";
329 for (TiXmlElement* childElem = deviceElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
330 if (childElem->ValueStr() ==
"action" || childElem->ValueStr() ==
"actions") {
331 for (
const auto& childAction : readActions(childElem, result)) {
332 device.
actions().push_back(childAction);
335 for (
const auto& childParam : readParams(childElem, result)) {
336 device.
params().push_back(childParam);
351 for (TiXmlElement* childElem = devicesElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
352 for (
const auto& childDevice : readDevices(childElem, result)) {
353 devices.push_back(childDevice);
363 const std::string& valueStr = paramsElem->ValueStr();
365 if (valueStr ==
"param") {
367 params.push_back(readParamTag(paramsElem, result));
370 if (valueStr ==
"group") {
372 params.push_back(readGroupTag(paramsElem, result));
375 if (valueStr ==
"paramlist") {
376 return readParamListTag(paramsElem, result);
378 if (valueStr ==
"subdevice") {
379 return readSubDeviceTag(paramsElem, result);
381 if (valueStr ==
"params") {
382 return readParamsTag(paramsElem, result);
385 SYNTAX_ERROR(paramsElem->Row()) << R
"(Expected "param", "group", "paramlist", "subdevice", or "params". Found)" << valueStr;
394 if (paramElem->ValueStr() !=
"param") {
395 SYNTAX_ERROR(paramElem->Row()) <<
"Expected \"param\". Found" << paramElem->ValueStr();
402 if (paramElem->QueryStringAttribute(
"name", ¶m.
name()) != TIXML_SUCCESS) {
403 SYNTAX_ERROR(paramElem->Row()) << R
"("param" element should contain the "name" attribute)";
410 const char* valueText = paramElem->GetText();
412 SYNTAX_ERROR(paramElem->Row()) << R
"("param" element should have a value [ "name" = )" << param.name() << "]";
417 std::string extern_name;
418 if (paramElem->QueryStringAttribute(
"extern-name", &extern_name) == TIXML_SUCCESS && config && config->check(extern_name)) {
420 param.
value() = config->find(extern_name).toString();
422 param.
value() = valueText;
432 if (groupElem->ValueStr() !=
"group") {
433 SYNTAX_ERROR(groupElem->Row()) <<
"Expected \"group\". Found" << groupElem->ValueStr();
440 if (groupElem->QueryStringAttribute(
"name", &group.
name()) != TIXML_SUCCESS) {
441 SYNTAX_ERROR(groupElem->Row()) << R
"("group" element should contain the "name" attribute)";
449 for (TiXmlElement* childElem = groupElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
450 for (
const auto& childParam : readParams(childElem, result)) {
451 params.push_back(childParam);
454 if (params.empty()) {
455 SYNTAX_ERROR(groupElem->Row()) <<
"\"group\" cannot be empty";
460 std::string groupString;
461 for (
auto& param : params) {
462 if (!groupString.empty()) {
465 groupString +=
"(" + param.name() +
" " + param.value() +
")";
468 group.
value() = groupString;
476 if (paramListElem->ValueStr() !=
"paramlist") {
477 SYNTAX_ERROR(paramListElem->Row()) <<
"Expected \"paramlist\". Found" << paramListElem->ValueStr();
485 if (paramListElem->QueryStringAttribute(
"name", &mainparam.
name()) != TIXML_SUCCESS) {
486 SYNTAX_ERROR(paramListElem->Row()) << R
"("paramlist" element should contain the "name" attribute)";
491 params.push_back(mainparam);
495 for (TiXmlElement* childElem = paramListElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
496 if (childElem->ValueStr() !=
"elem") {
497 SYNTAX_ERROR(childElem->Row()) <<
"Expected \"elem\". Found" << childElem->ValueStr();
504 if (childElem->QueryStringAttribute(
"name", &childParam.
name()) != TIXML_SUCCESS) {
505 SYNTAX_ERROR(childElem->Row()) << R
"("elem" element should contain the "name" attribute)";
510 const char* valueText = childElem->GetText();
512 SYNTAX_ERROR(childElem->Row()) << R
"("elem" element should have a value [ "name" = )" << childParam.name() << "]";
516 childParam.
value() = valueText;
518 params.push_back(childParam);
521 if (params.empty()) {
522 SYNTAX_ERROR(paramListElem->Row()) <<
"\"paramlist\" cannot be empty";
528 for (
auto it = params.begin() + 1; it != params.end(); ++it) {
530 params.at(0).
value() += (params.at(0).value().empty() ?
"(" :
" ") + param.
name();
532 params.at(0).value() +=
")";
541 if (subDeviceElem->ValueStr() !=
"subdevice") {
542 SYNTAX_ERROR(subDeviceElem->Row()) <<
"Expected \"subdevice\". Found" << subDeviceElem->ValueStr();
553 subDeviceParam.
name() =
"subdevice";
559 if (subDeviceElem->QueryStringAttribute(
"type", &subDeviceParam.
value()) != TIXML_SUCCESS) {
560 SYNTAX_ERROR(subDeviceElem->Row()) << R
"("subdevice" element should contain the "type" attribute)";
566 params.push_back(subDeviceParam);
570 for (TiXmlElement* childElem = subDeviceElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
571 for (
const auto& childParam : readParams(childElem, result)) {
586 for (TiXmlElement* childElem = paramsElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
587 for (
const auto & childParam : readParams(childElem, result)) {
588 params.push_back(childParam);
598 const std::string& valueStr = actionsElem->ValueStr();
600 if (valueStr !=
"action" && valueStr !=
"actions") {
601 SYNTAX_ERROR(actionsElem->Row()) << R
"(Expected "action" or "actions". Found)" << valueStr;
604 if (valueStr ==
"action") {
606 actionList.push_back(readActionTag(actionsElem, result));
610 return readActionsTag(actionsElem, result);
616 if (actionElem->ValueStr() !=
"action") {
617 SYNTAX_ERROR(actionElem->Row()) <<
"Expected \"action\". Found" << actionElem->ValueStr();
625 SYNTAX_ERROR(actionElem->Row()) << R
"("action" element should contain the "phase" attribute [startup|interrupt{1,2,3}|shutdown])";
632 SYNTAX_ERROR(actionElem->Row()) << R
"("action" element should contain the "type" attribute [configure|calibrate|attach|abort|detach|park|custom])";
639 #if TINYXML_UNSIGNED_INT_BUG
640 if (actionElem->QueryUnsignedAttribute(
"level", &action.
level()) != TIXML_SUCCESS) {
641 SYNTAX_ERROR(actionElem->Row()) <<
"\"action\" element should contain the \"level\" attribute [unsigned int]";
645 if (actionElem->QueryIntAttribute(
"level", &tmp) != TIXML_SUCCESS || tmp < 0) {
646 SYNTAX_ERROR(actionElem->Row()) << R
"("action" element should contain the "level" attribute [unsigned int])";
650 action.
level() =
static_cast<unsigned>(tmp);
653 for (TiXmlElement* childElem = actionElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
654 for (
const auto& childParam : readParams(childElem, result)) {
655 action.
params().push_back(childParam);
668 std::string robotName;
669 if (actionsElem->QueryStringAttribute(
"robot", &robotName) != TIXML_SUCCESS) {
670 SYNTAX_WARNING(actionsElem->Row()) << R
"("actions" element should contain the "robot" attribute)";
674 SYNTAX_WARNING(actionsElem->Row()) <<
"Trying to import a file for the wrong robot. Found" << robotName <<
"instead of" << result.
robot.
name();
678 #if TINYXML_UNSIGNED_INT_BUG
679 if (actionsElem->QueryUnsignedAttribute(
"build", &build()) != TIXML_SUCCESS) {
681 SYNTAX_WARNING(actionsElem->Row()) <<
"\"actions\" element should contain the \"build\" attribute [unsigned int]. Assuming 0";
685 if (actionsElem->QueryIntAttribute(
"build", &tmp) != TIXML_SUCCESS || tmp < 0) {
687 SYNTAX_WARNING(actionsElem->Row()) << R
"("actions" element should contain the "build" attribute [unsigned int]. Assuming 0)";
690 build = static_cast<unsigned>(tmp);
694 SYNTAX_WARNING(actionsElem->Row()) <<
"Import a file for a different robot build. Found" << build <<
"instead of" << result.
robot.
build();
698 for (TiXmlElement* childElem = actionsElem->FirstChildElement(); childElem !=
nullptr; childElem = childElem->NextSiblingElement()) {
699 for (
const auto & childAction : readActions(childElem, result)) {
700 actions.push_back(childAction);
#define SYNTAX_ERROR(line)
#define SYNTAX_WARNING(line)
A base class for nested structures that can be searched.
std::string & portprefix()
Result of the parsing of 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::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)
const yarp::os::Searchable * config
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