YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
XMLReaderFileV3.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
14
15#include <yarp/os/LogStream.h>
16#include <yarp/os/Network.h>
17#include <yarp/os/Property.h>
18
19#include <algorithm>
20#include <iterator>
21#include <sstream>
22#include <string>
23#include <tinyxml.h>
24#include <vector>
25#include <set>
26
27#define SYNTAX_ERROR(line) yError() << "Syntax error while loading" << curr_filename << "at line" << line << "."
28#define SYNTAX_WARNING(line) yWarning() << "Invalid syntax while loading" << curr_filename << "at line" << line << "."
29
30// BUG in TinyXML, see
31// https://sourceforge.net/tracker/?func=detail&aid=3567726&group_id=13559&atid=113559
32// When this bug is fixed upstream we can enable this
33#define TINYXML_UNSIGNED_INT_BUG 0
34
36{
37public:
39 virtual ~Private();
40
44
48
54
58
59 bool CheckIfIncludeSectionIsEnabled(const std::string& href_enable_tags, const std::string& href_disable_tags);
60 bool PerformInclusions(TiXmlNode* pParent, const std::string& parent_fileName, const std::string& current_path);
61 void ReplaceAllStrings(std::string& str, const std::string& from, const std::string& to);
63
64#ifdef USE_DTD
66#endif
67
70 std::string curr_filename;
71 unsigned int minorVersion;
72 unsigned int majorVersion;
75 std::set<std::string> m_enable_set_found_in_all_xml;
76 std::set<std::string> m_disable_set_found_in_all_xml;
77};
78
79
81 parent(p),
82 minorVersion(0),
83 majorVersion(0)
84{
85 verbose_output = false;
86}
87
89
91{
92 yDebug() << "Reading file" << fileName.c_str();
93 auto* doc = new TiXmlDocument(fileName.c_str());
94 if (!doc->LoadFile()) {
95 SYNTAX_ERROR(doc->ErrorRow()) << doc->ErrorDesc();
97 }
98
99 if (!doc->RootElement()) {
100 SYNTAX_ERROR(doc->Row()) << "No root element.";
102 }
103
104#ifdef USE_DTD
105 for (TiXmlNode* childNode = doc->FirstChild(); childNode != 0; childNode = childNode->NextSibling()) {
106 if (childNode->Type() == TiXmlNode::TINYXML_UNKNOWN) {
107 if (dtd.parse(childNode->ToUnknown(), curr_filename)) {
108 break;
109 }
110 }
111 }
112
113 if (!dtd.valid()) {
114 SYNTAX_WARNING(doc->Row()) << "No DTD found. Assuming version yarprobotinterfaceV1.0";
115 dtd.setDefault();
117 }
118
119 if (dtd.type != RobotInterfaceDTD::DocTypeRobot) {
120 SYNTAX_WARNING(doc->Row()) << "Expected document of type" << DocTypeToString(RobotInterfaceDTD::DocTypeRobot)
121 << ". Found" << DocTypeToString(dtd.type);
122 }
123
124 if (dtd.majorVersion != 1 || dtd.minorVersion != 0) {
125 SYNTAX_WARNING(doc->Row()) << "Only yarprobotinterface DTD version 1.0 is supported";
126 }
127#endif
128
129 std::string current_path;
130 current_path = fileName.substr(0, fileName.find_last_of("\\/"));
131 std::string current_filename;
132 std::string log_filename;
133 current_filename = fileName.substr(fileName.find_last_of("\\/") + 1);
134 log_filename = current_filename.substr(0, current_filename.find(".xml"));
135 log_filename += "_preprocessor_log.xml";
136
137 std::string enable_tags_string;
138 yarp::os::Bottle* be = config.find("enable_tags").asList();
139 if (be) { enable_tags_string = be->toString();}
140
141 std::string disable_tags_string;
142 yarp::os::Bottle* bd = config.find("disable_tags").asList();
143 if (bd) { disable_tags_string = bd->toString();}
144
145 {
146 char* all_string = strdup(enable_tags_string.c_str());
147 if (all_string==nullptr) { yError() << "Internal error"; return yarp::robotinterface::XMLReaderResult::ParsingFailed(); }
148 char* token = strtok(all_string, " ");
149 while (token) {
150 m_enabled_options_from_command_line.insert(token);
151 token = strtok(NULL, " ");
152 }
153 free (all_string);
154 }
155 {
156 char* all_string = strdup(disable_tags_string.c_str());
157 if (all_string == nullptr) { yError() << "Internal error"; return yarp::robotinterface::XMLReaderResult::ParsingFailed(); }
158 char* token = strtok(all_string, " ");
159 while (token) {
160 m_disabled_options_from_command_line.insert(token);
161 token = strtok(NULL, " ");
162 }
163 free(all_string);
164 }
165 yInfo() << "Yarprobotinterface was started using the following enable_tags:"<< enable_tags_string;
166 yInfo() << "Yarprobotinterface was started using the following disable_tags:" << disable_tags_string;
167
168 double start_time = yarp::os::Time::now();
169 PerformInclusions(doc->RootElement(), current_filename, current_path);
170 double end_time = yarp::os::Time::now();
171
172 std::string all_enable_att_string = "List of all enable attributes found in the include tags: ";
173 for (auto it = m_enable_set_found_in_all_xml.begin(); it != m_enable_set_found_in_all_xml.end(); it++)
174 {
175 all_enable_att_string += (" " + *it);
176 }
177 yDebug() << all_enable_att_string;
178
179 std::string all_disable_att_string = "List of all disable attributes found in the include tags: ";
180 for (auto it = m_disable_set_found_in_all_xml.begin(); it != m_disable_set_found_in_all_xml.end(); it++)
181 {
182 all_disable_att_string += (" " + *it);
183 }
184 yDebug() << all_disable_att_string;
185
186 std::string full_log_withpath = current_path + std::string("\\") + log_filename;
187 std::replace(full_log_withpath.begin(), full_log_withpath.end(), '\\', '/');
188 yDebug() << "Preprocessor complete in: " << end_time - start_time << "s";
189 if (verbose_output)
190 {
191 yDebug() << "Preprocessor output stored in: " << full_log_withpath;
192 doc->SaveFile(full_log_withpath);
193 }
194
195 yarp::robotinterface::XMLReaderResult result = readRobotTag(doc->RootElement());
196 delete doc;
197
198 // yDebug() << robot;
199
200 return result;
201}
202
204{
205 curr_filename = " XML runtime string ";
206 auto* doc = new TiXmlDocument();
207 if (!doc->Parse(xmlString.c_str())) {
208 SYNTAX_ERROR(doc->ErrorRow()) << doc->ErrorDesc();
210 }
211
212 if (!doc->RootElement()) {
213 SYNTAX_ERROR(doc->Row()) << "No root element.";
215 }
216
217 yarp::robotinterface::XMLReaderResult result = readRobotTag(doc->RootElement());
218 delete doc;
219
220 return result;
221}
222
223bool yarp::robotinterface::impl::XMLReaderFileV3::Private::CheckIfIncludeSectionIsEnabled(const std::string& hrefxml_enable_tags_s, const std::string& hrefxml_disable_tags_s)
224{
225 yarp::os::Bottle hrefxml_enable_tags_b;
226 hrefxml_enable_tags_b.fromString(hrefxml_enable_tags_s);
227 yarp::os::Bottle hrefxml_disable_tags_b;
228 hrefxml_disable_tags_b.fromString(hrefxml_disable_tags_s);
229 //yDebug() << "included enable tag size:" << b_included_enable_options.size() << " contents:" << b_included_enable_options.toString();
230 //yDebug() << "included disable tag size:" << b_included_disable_options.size() << " contents:" << b_included_disable_options.toString();
231
232 //if no `enabled_by` attribute are found in the xi::include line, then the include is enabled by default.
233 bool enabled = true;
234 size_t hrefxml_enable_tags_b_size = hrefxml_enable_tags_b.size();
235 if (hrefxml_enable_tags_b_size != 0)
236 {
237 //.otherwise, if a `enabled_by` attribute is found, then the include line is not enabled by default and it
238 // is enabled only if yarprobotinterface has been executed with the specific option --enable_tags
239 enabled = false;
240 for (size_t i = 0; i < hrefxml_enable_tags_b_size; i++)
241 {
242 std::string s = hrefxml_enable_tags_b.get(i).asString();
243 m_enable_set_found_in_all_xml.insert(s);
244 if (m_enabled_options_from_command_line.find(s) != m_enabled_options_from_command_line.end() ||
245 m_enabled_options_from_command_line.find("enable_all") != m_enabled_options_from_command_line.end())
246 {
247 enabled = true;
248 }
249 }
250 }
251 // if a `disabled_by` attribute is found, then the include line (either enabled by default or by an `enable_by` tag ) can
252 // be disabled if yarprobotinterface has been executed with the specific option --disable_tags
253 size_t hrefxml_disable_tags_b_size = hrefxml_disable_tags_b.size();
254 for (size_t i = 0; i < hrefxml_disable_tags_b_size; i++)
255 {
256 std::string s = hrefxml_disable_tags_b.get(i).asString();
257 m_disable_set_found_in_all_xml.insert(s);
258 if (m_disabled_options_from_command_line.find(s) != m_disabled_options_from_command_line.end())
259 {
260 enabled = false;
261 }
262 }
263
264 return enabled;
265}
266
267bool yarp::robotinterface::impl::XMLReaderFileV3::Private::PerformInclusions(TiXmlNode* pParent, const std::string& parent_fileName, const std::string& current_path)
268{
269loop_start: //goto label
270 for (TiXmlElement* childElem = pParent->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
271#ifdef DEBUG_PARSER
272 std::string a;
273 if (childElem->FirstAttribute())
274 a = childElem->FirstAttribute()->Value();
275 yDebug() << "Parsing" << childElem->Value() << a;
276#endif
277 std::string elemString = childElem->ValueStr();
278 if (elemString == "file") {
279 yFatal() << "'file' attribute is forbidden in yarprobotinterface DTD format 3.0. Error found in " << parent_fileName;
280 return false;
281 }
282
283 if (elemString == "xi:include")
284 {
285 std::string href_filename;
286 if (childElem->QueryStringAttribute("href", &href_filename) == TIXML_SUCCESS)
287 {
288 std::string href_enable_tags, href_disable_tags;
289 childElem->QueryStringAttribute("enabled_by", &href_enable_tags);
290 childElem->QueryStringAttribute("disabled_by", &href_disable_tags);
291 if (CheckIfIncludeSectionIsEnabled(href_enable_tags, href_disable_tags))
292 {
293 std::string included_path = std::string(current_path).append("\\").append(href_filename.substr(0, href_filename.find_last_of("\\/")));
294 std::string included_filename = href_filename.substr(href_filename.find_last_of("\\/") + 1);
295 std::string full_path_file = std::string(included_path).append("\\").append(included_filename);
296 TiXmlDocument included_file;
297
298 std::replace(full_path_file.begin(), full_path_file.end(), '\\', '/');
299 if (included_file.LoadFile(full_path_file))
300 {
301 PerformInclusions(included_file.RootElement(), included_filename, included_path);
302 //included_file.RootElement()->SetAttribute("xml:base", href_filename); //not yet implemented
303 included_file.RootElement()->RemoveAttribute("xmlns:xi");
304 if (pParent->ReplaceChild(childElem, *included_file.FirstChildElement()))
305 {
306 //the replace operation invalidates the iterator, hence we need to restart the parsing of this level
307 goto loop_start;
308 }
309 else
310 {
311 //fatal error
312 yFatal() << "Failed to include: " << included_filename << " in: " << parent_fileName;
313 return false;
314 }
315 }
316 else
317 {
318 //fatal error
319 yError() << included_file.ErrorDesc() << " file" << full_path_file << "included by " << parent_fileName << "at line" << childElem->Row();
320 yFatal() << "In file:" << included_filename << " included by: " << parent_fileName << " at line: " << childElem->Row();
321 return false;
322 }
323 }
324 else
325 {
326 yDebug() << "Skipping include section `" << href_filename << "` because is not enabled";
327 if (pParent->RemoveChild(childElem))
328 {
329 //the remove operation invalidates the iterator, hence we need to restart the parsing of this level
330 goto loop_start;
331 }
332 }
333 }
334 else
335 {
336 //fatal error
337 yFatal() << "Syntax error in: " << parent_fileName << " while searching for href attribute";
338 return false;
339 }
340 }
341 PerformInclusions(childElem, parent_fileName, current_path);
342 }
343 return true;
344}
345
347{
349 result.parsingIsSuccessful = true;
350
351 if (robotElem->ValueStr() != "robot") {
352 SYNTAX_ERROR(robotElem->Row()) << "Root element should be \"robot\". Found" << robotElem->ValueStr();
354 }
355
356 if (robotElem->QueryStringAttribute("name", &result.robot.name()) != TIXML_SUCCESS) {
357 SYNTAX_ERROR(robotElem->Row()) << R"("robot" element should contain the "name" attribute)";
359 }
360 //add the robot name to the cinfiguration
361 config.put("robotname",result.robot.name());
362
363#if TINYXML_UNSIGNED_INT_BUG
364 if (robotElem->QueryUnsignedAttribute("build", &result.robot.build()) != TIXML_SUCCESS) {
365 // No build attribute. Assuming build="0"
366 SYNTAX_WARNING(robotElem->Row()) << "\"robot\" element should contain the \"build\" attribute [unsigned int]. Assuming 0";
367 }
368#else
369 int tmp;
370 if (robotElem->QueryIntAttribute("build", &tmp) != TIXML_SUCCESS || tmp < 0) {
371 // No build attribute. Assuming build="0"
372 SYNTAX_WARNING(robotElem->Row()) << R"("robot" element should contain the "build" attribute [unsigned int]. Assuming 0)";
373 tmp = 0;
374 }
375 result.robot.build() = static_cast<unsigned>(tmp);
376#endif
377
378 // If portprefix is already present in config we use that one
379 if (!config.check("portprefix"))
380 {
381 if (robotElem->QueryStringAttribute("portprefix", &result.robot.portprefix()) != TIXML_SUCCESS) {
382 SYNTAX_WARNING(robotElem->Row()) << R"("robot" element should contain the "portprefix" attribute. Using "name" attribute)";
383 result.robot.portprefix() = result.robot.name();
384 }
385 config.put("portprefix",result.robot.portprefix());
386 } else {
387 result.robot.portprefix() = config.find("portprefix").asString();
388 }
389
390 // FIXME DTD >= 4 Make this the default behaviour
391 bool reverse = false;
392 if (robotElem->QueryBoolAttribute("reverse-shutdown-action-order", &reverse) == TIXML_WRONG_TYPE) {
393 SYNTAX_ERROR(robotElem->Row()) << R"(The "reverse-shutdown-action-order" attribute in the "robot" element should be a bool.)";
395 }
396 result.robot.setReverseShutdownActionOrder(reverse);
397
398 // yDebug() << "Found robot [" << robot.name() << "] build [" << robot.build() << "] portprefix [" << robot.portprefix() << "]";
399
400 for (TiXmlElement* childElem = robotElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
401 std::string elemString = childElem->ValueStr();
402 if (elemString == "device" || elemString == "devices") {
403 for (const auto& childDevice : readDevices(childElem, result)) {
404 result.robot.devices().push_back(childDevice);
405 }
406 } else {
407 for (const auto& childParam : readParams(childElem, result)) {
408 result.robot.params().push_back(childParam);
409 }
410 }
411 }
412
413 return result;
414}
415
416
419{
420 const std::string& valueStr = devicesElem->ValueStr();
421
422 if (valueStr == "device") {
423 // yDebug() << valueStr;
425 deviceList.push_back(readDeviceTag(devicesElem, result));
426 return deviceList;
427 }
428 if (valueStr == "devices") {
429 // "devices"
430 return readDevicesTag(devicesElem, result);
431 }
432
433 SYNTAX_ERROR(devicesElem->Row()) << R"(Expected "device" or "devices". Found)" << valueStr;
434 result.parsingIsSuccessful = false;
436}
437
440{
441 const std::string& valueStr = deviceElem->ValueStr();
442
443 if (valueStr != "device") {
444 SYNTAX_ERROR(deviceElem->Row()) << "Expected \"device\". Found" << valueStr;
445 result.parsingIsSuccessful = false;
447 }
448
450
451 if (deviceElem->QueryStringAttribute("name", &device.name()) != TIXML_SUCCESS) {
452 SYNTAX_ERROR(deviceElem->Row()) << R"("device" element should contain the "name" attribute)";
453 result.parsingIsSuccessful = false;
454 return device;
455 }
456
457 // yDebug() << "Found device [" << device.name() << "]";
458
459 if (deviceElem->QueryStringAttribute("type", &device.type()) != TIXML_SUCCESS) {
460 SYNTAX_ERROR(deviceElem->Row()) << R"("device" element should contain the "type" attribute)";
461 result.parsingIsSuccessful = false;
462 return device;
463 }
464
465 device.params().push_back(yarp::robotinterface::Param("robotName", result.robot.portprefix()));
466
467 for (TiXmlElement* childElem = deviceElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
468 if (childElem->ValueStr() == "action" || childElem->ValueStr() == "actions") {
469 for (const auto& childAction : readActions(childElem, result)) {
470 device.actions().push_back(childAction);
471 }
472 } else {
473 for (const auto& childParam : readParams(childElem, result)) {
474 device.params().push_back(childParam);
475 }
476 }
477 }
478
479 // yDebug() << device;
480 return device;
481}
482
485{
486 //const std::string &valueStr = devicesElem->ValueStr();
487
490
491 for (TiXmlElement* childElem = devicesElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
492 if (childElem->ValueStr() == "param" || childElem->ValueStr() == "group" || childElem->ValueStr() == "paramlist" || childElem->ValueStr() == "params") {
493 for (const auto& childParam : readParams(childElem, result)) {
494 params.push_back(childParam);
495 }
496 } else {
497 for (const auto& childDevice : readDevices(childElem, result)) {
498 devices.push_back(childDevice);
499 }
500 }
501 }
502
503
504 for (auto& device : devices) {
505 device.params().insert(device.params().begin(), params.begin(), params.end());
506 }
507
508 return devices;
509}
510
513{
514 const std::string& valueStr = paramsElem->ValueStr();
515
516 if (valueStr == "param") {
518 params.push_back(readParamTag(paramsElem, result));
519 return params;
520 }
521 if (valueStr == "group") {
523 params.push_back(readGroupTag(paramsElem, result));
524 return params;
525 }
526 if (valueStr == "paramlist") {
527 return readParamListTag(paramsElem, result);
528 }
529 if (valueStr == "params") {
530 return readParamsTag(paramsElem, result);
531 }
532
533 SYNTAX_ERROR(paramsElem->Row()) << R"(Expected "param", "group", "paramlist", or "params". Found)" << valueStr;
534 result.parsingIsSuccessful = false;
536}
537
538void yarp::robotinterface::impl::XMLReaderFileV3::Private::ReplaceAllStrings(std::string& str, const std::string& from, const std::string& to)
539{
540 if(from.empty())
541 return;
542 size_t start_pos = 0;
543 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
544 str.replace(start_pos, from.length(), to);
545 start_pos += to.length();
546 }
547}
548
549
552{
553 if (paramElem->ValueStr() != "param") {
554 SYNTAX_ERROR(paramElem->Row()) << "Expected \"param\". Found" << paramElem->ValueStr();
555 result.parsingIsSuccessful = false;
557 }
558
560
561 if (paramElem->QueryStringAttribute("name", &param.name()) != TIXML_SUCCESS) {
562 SYNTAX_ERROR(paramElem->Row()) << R"("param" element should contain the "name" attribute)";
563 result.parsingIsSuccessful = false;
565 }
566
567 // yDebug() << "Found param [" << param.name() << "]";
568
569 const char* valueText = paramElem->GetText();
570 if (!valueText) {
571 SYNTAX_ERROR(paramElem->Row()) << R"("param" element should have a value [ "name" = )" << param.name() << "]";
572 result.parsingIsSuccessful = false;
574 }
575
576 // First process extern-name
577 std::string extern_name;
578 if (paramElem->QueryStringAttribute("extern-name", &extern_name) == TIXML_SUCCESS && config.check(extern_name)) {
579 // FIXME Check DTD >= 3.1
580 if (config.find(extern_name).isList())
581 {
582 param.value() = "(" + config.find(extern_name).asList()->toString() + ")";
583 }
584 else
585 {
586 param.value() = config.find(extern_name).toString();
587 }
588 } else {
589 param.value() = valueText;
590 }
591
592 std::string debug_string = config.toString();
593
594
595 // Process ${portprefix}
596 {
597 std::string paramValueBefore = param.value();
598 std::string paramValueAfter = paramValueBefore;
599 std::string portprefix = config.find("portprefix").toString();
600 ReplaceAllStrings(paramValueAfter, "${portprefix}", portprefix);
601 param.value() = paramValueAfter;
602 }
603
604 // Process ${robotname}
605 {
606 std::string paramValueBefore = param.value();
607 std::string paramValueAfter = paramValueBefore;
608 std::string robotname = config.find("robotname").toString();
609 ReplaceAllStrings(paramValueAfter, "${robotname}", robotname);
610 param.value() = paramValueAfter;
611 }
612
613 // Process ${portprefixnoslash}
614 {
615 std::string paramValueBefore = param.value();
616 std::string paramValueAfter = paramValueBefore;
617 std::string portprefix = config.find("portprefix").toString();
618 std::string portprefixnoslash = portprefix;
619 if (!portprefixnoslash.empty() && portprefixnoslash[0] == '/') { portprefixnoslash.erase(0, 1); }
620 ReplaceAllStrings(paramValueAfter, "${portprefixnoslash}", portprefixnoslash);
621 param.value() = paramValueAfter;
622 }
623
624
625 // yDebug() << param;
626 return param;
627}
628
631{
632 if (groupElem->ValueStr() != "group") {
633 SYNTAX_ERROR(groupElem->Row()) << "Expected \"group\". Found" << groupElem->ValueStr();
634 result.parsingIsSuccessful = false;
636 }
637
638 yarp::robotinterface::Param group(true);
639
640 if (groupElem->QueryStringAttribute("name", &group.name()) != TIXML_SUCCESS) {
641 SYNTAX_ERROR(groupElem->Row()) << R"("group" element should contain the "name" attribute)";
642 result.parsingIsSuccessful = false;
644 }
645
646 // yDebug() << "Found group [" << group.name() << "]";
647
649 for (TiXmlElement* childElem = groupElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
650 for (const auto& childParam : readParams(childElem, result)) {
651 params.push_back(childParam);
652 }
653 }
654 if (params.empty()) {
655 SYNTAX_ERROR(groupElem->Row()) << "\"group\" cannot be empty";
656 result.parsingIsSuccessful = false;
658 }
659
660 std::string groupString;
661 for (auto& param : params) {
662 if (!groupString.empty()) {
663 groupString += " ";
664 }
665 groupString += "(" + param.name() + " " + param.value() + ")";
666 }
667
668 group.value() = groupString;
669
670 return group;
671}
672
675{
676 if (paramListElem->ValueStr() != "paramlist") {
677 SYNTAX_ERROR(paramListElem->Row()) << "Expected \"paramlist\". Found" << paramListElem->ValueStr();
678 result.parsingIsSuccessful = false;
680 }
681
684
685 if (paramListElem->QueryStringAttribute("name", &mainparam.name()) != TIXML_SUCCESS) {
686 SYNTAX_ERROR(paramListElem->Row()) << R"("paramlist" element should contain the "name" attribute)";
687 result.parsingIsSuccessful = false;
689 }
690
691 params.push_back(mainparam);
692
693 // yDebug() << "Found paramlist [" << params.at(0).name() << "]";
694
695 for (TiXmlElement* childElem = paramListElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
696 if (childElem->ValueStr() != "elem") {
697 SYNTAX_ERROR(childElem->Row()) << "Expected \"elem\". Found" << childElem->ValueStr();
698 result.parsingIsSuccessful = false;
700 }
701
703
704 if (childElem->QueryStringAttribute("name", &childParam.name()) != TIXML_SUCCESS) {
705 SYNTAX_ERROR(childElem->Row()) << R"("elem" element should contain the "name" attribute)";
706 result.parsingIsSuccessful = false;
708 }
709
710 const char* valueText = childElem->GetText();
711 if (!valueText) {
712 SYNTAX_ERROR(childElem->Row()) << R"("elem" element should have a value [ "name" = )" << childParam.name() << "]";
713 result.parsingIsSuccessful = false;
715 }
716 childParam.value() = valueText;
717
718 params.push_back(childParam);
719 }
720
721 if (params.empty()) {
722 SYNTAX_ERROR(paramListElem->Row()) << "\"paramlist\" cannot be empty";
723 result.parsingIsSuccessful = false;
725 }
726
727 // +1 skips the first element, that is the main param
728 for (auto it = params.begin() + 1; it != params.end(); ++it) {
729 yarp::robotinterface::Param& param = *it;
730 params.at(0).value() += (params.at(0).value().empty() ? "(" : " ") + param.name();
731 }
732 params.at(0).value() += ")";
733
734 // yDebug() << params;
735 return params;
736}
737
740{
741 //const std::string &valueStr = paramsElem->ValueStr();
742
744 for (TiXmlElement* childElem = paramsElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
745 for (const auto & childParam : readParams(childElem, result)) {
746 params.push_back(childParam);
747 }
748 }
749
750 return params;
751}
752
755{
756 const std::string& valueStr = actionsElem->ValueStr();
757
758 if (valueStr != "action" && valueStr != "actions") {
759 SYNTAX_ERROR(actionsElem->Row()) << R"(Expected "action" or "actions". Found)" << valueStr;
760 }
761
762 if (valueStr == "action") {
764 actionList.push_back(readActionTag(actionsElem, result));
765 return actionList;
766 }
767 // "actions"
768 return readActionsTag(actionsElem, result);
769}
770
773{
774 if (actionElem->ValueStr() != "action") {
775 SYNTAX_ERROR(actionElem->Row()) << "Expected \"action\". Found" << actionElem->ValueStr();
776 result.parsingIsSuccessful = false;
778 }
779
781
782 if (actionElem->QueryValueAttribute<yarp::robotinterface::ActionPhase>("phase", &action.phase()) != TIXML_SUCCESS || action.phase() == yarp::robotinterface::ActionPhaseUnknown) {
783 SYNTAX_ERROR(actionElem->Row()) << R"("action" element should contain the "phase" attribute [startup|interrupt{1,2,3}|shutdown])";
784 result.parsingIsSuccessful = false;
786 }
787
788
789 if (actionElem->QueryValueAttribute<yarp::robotinterface::ActionType>("type", &action.type()) != TIXML_SUCCESS || action.type() == yarp::robotinterface::ActionTypeUnknown) {
790 SYNTAX_ERROR(actionElem->Row()) << R"("action" element should contain the "type" attribute [configure|calibrate|attach|abort|detach|park|custom])";
791 result.parsingIsSuccessful = false;
793 }
794
795 // yDebug() << "Found action [ ]";
796
797#if TINYXML_UNSIGNED_INT_BUG
798 if (actionElem->QueryUnsignedAttribute("level", &action.level()) != TIXML_SUCCESS) {
799 SYNTAX_ERROR(actionElem->Row()) << "\"action\" element should contain the \"level\" attribute [unsigned int]";
800 }
801#else
802 int tmp;
803 if (actionElem->QueryIntAttribute("level", &tmp) != TIXML_SUCCESS || tmp < 0) {
804 SYNTAX_ERROR(actionElem->Row()) << R"("action" element should contain the "level" attribute [unsigned int])";
805 result.parsingIsSuccessful = false;
807 }
808 action.level() = static_cast<unsigned>(tmp);
809#endif
810
811 for (TiXmlElement* childElem = actionElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
812 for (const auto& childParam : readParams(childElem, result)) {
813 action.params().push_back(childParam);
814 }
815 }
816
817 // yDebug() << action;
818 return action;
819}
820
823{
824 //const std::string &valueStr = actionsElem->ValueStr();
825
826 std::string robotName;
827 if (actionsElem->QueryStringAttribute("robot", &robotName) != TIXML_SUCCESS) {
828 SYNTAX_WARNING(actionsElem->Row()) << R"("actions" element should contain the "robot" attribute)";
829 }
830
831 if (robotName != result.robot.name()) {
832 SYNTAX_WARNING(actionsElem->Row()) << "Trying to import a file for the wrong robot. Found" << robotName << "instead of" << result.robot.name();
833 }
834
835 unsigned int build;
836#if TINYXML_UNSIGNED_INT_BUG
837 if (actionsElem->QueryUnsignedAttribute("build", &build()) != TIXML_SUCCESS) {
838 // No build attribute. Assuming build="0"
839 SYNTAX_WARNING(actionsElem->Row()) << "\"actions\" element should contain the \"build\" attribute [unsigned int]. Assuming 0";
840 }
841#else
842 int tmp;
843 if (actionsElem->QueryIntAttribute("build", &tmp) != TIXML_SUCCESS || tmp < 0) {
844 // No build attribute. Assuming build="0"
845 SYNTAX_WARNING(actionsElem->Row()) << R"("actions" element should contain the "build" attribute [unsigned int]. Assuming 0)";
846 tmp = 0;
847 }
848 build = static_cast<unsigned>(tmp);
849#endif
850
851 if (build != result.robot.build()) {
852 SYNTAX_WARNING(actionsElem->Row()) << "Import a file for a different robot build. Found" << build << "instead of" << result.robot.build();
853 }
854
856 for (TiXmlElement* childElem = actionsElem->FirstChildElement(); childElem != nullptr; childElem = childElem->NextSiblingElement()) {
857 for (const auto & childAction : readActions(childElem, result)) {
858 actions.push_back(childAction);
859 }
860 }
861
862 return actions;
863}
864
865
867 const yarp::os::Searchable& config,
868 bool verb)
869{
870 mPriv->config.fromString(config.toString());
871 mPriv->verbose_output = verb;
872 auto ret = mPriv->readRobotFromFile(filename);
873 mPriv->config.clear();
874 return ret;
875}
876
878 const yarp::os::Searchable& config,
879 bool verb)
880{
881 mPriv->config.fromString(config.toString());
882 mPriv->verbose_output = verb;
883 auto ret = mPriv->readRobotFromString(xmlString);
884 mPriv->config.clear();
885 return ret;
886}
887
888
893
#define yInfo(...)
Definition Log.h:319
#define yError(...)
Definition Log.h:361
#define yDebug(...)
Definition Log.h:275
#define yFatal(...)
Definition Log.h:382
#define SYNTAX_ERROR(line)
#define SYNTAX_WARNING(line)
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
void fromString(const std::string &text)
Initializes bottle from a string.
Definition Bottle.cpp:210
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:257
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:217
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Bottle.cpp:293
A class for storing options and configuration information.
Definition Property.h:33
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.
Definition Searchable.h:31
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:252
virtual std::string asString() const
Get string value.
Definition Value.cpp:246
unsigned int & level()
Definition Action.cpp:105
ActionPhase & phase()
Definition Action.cpp:95
std::string & name()
Definition Param.cpp:83
std::string & value()
Definition Param.cpp:88
std::string & portprefix()
Definition Robot.cpp:637
DeviceList & devices()
Definition Robot.cpp:684
void setReverseShutdownActionOrder(bool reverseShutdownActionOrder)
Definition Robot.cpp:674
std::string & name()
Definition Robot.cpp:627
unsigned int & build()
Definition Robot.cpp:632
Result of the parsing of yarp::robotinterface::XMLReader.
Definition XMLReader.h:26
bool parsingIsSuccessful
True if the parsing was successful, false otherwise.
Definition XMLReader.h:38
Robot robot
If parsingIsSuccessful is true, contains a valid robot instance.
Definition XMLReader.h:43
static XMLReaderResult ParsingFailed()
Definition XMLReader.h:28
void ReplaceAllStrings(std::string &str, const std::string &from, const std::string &to)
yarp::robotinterface::Device readDeviceTag(TiXmlElement *deviceElem, yarp::robotinterface::XMLReaderResult &result)
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::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 &current_path)
yarp::robotinterface::XMLReaderResult readRobotTag(TiXmlElement *robotElem)
bool CheckIfIncludeSectionIsEnabled(const std::string &href_enable_tags, const std::string &href_disable_tags)
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)
yarp::robotinterface::Action readActionTag(TiXmlElement *actionElem, yarp::robotinterface::XMLReaderResult &result)
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
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
std::string DocTypeToString(RobotInterfaceDTD::DocType doctype)
std::vector< robotinterface::Action > ActionList
Definition Types.h:31
std::vector< robotinterface::Param > ParamList
Definition Types.h:30
std::vector< robotinterface::Device > DeviceList
Definition Types.h:32