37 strAppName = szAppName;
44 if ((strPath.rfind(directorySeparator) == std::string::npos) || (strPath.rfind(directorySeparator) != strPath.size() - 1)) {
45 strPath = strPath + std::string(directorySeparator);
58 strFileName = szFileName;
81 if(!strFileName.empty())
83 fileNames.push_back(strFileName);
89 logger->
addError(
"No application path is introduced.");
95 if ((dir = opendir(strPath.c_str())) ==
nullptr)
98 err<<
"Cannot access "<<strPath;
104 while((entry = readdir(dir)))
106 std::string name = entry->d_name;
109 std::string ext = name.substr(name.size()-3,3);
111 fileNames.push_back(strPath + name);
144 if(strAppName.empty())
149 if (fileNames.empty()) {
152 std::string fname = fileNames.back();
153 fileNames.pop_back();
154 app = parsXml(fname.c_str());
160 std::vector<std::string>::iterator itr;
161 for(itr=fileNames.begin(); itr<fileNames.end(); itr++)
164 if (app && (std::string(app->
getName()) == strAppName)) {
172Application* XmlAppLoader::parsXml(
const char* szFile)
178 TiXmlDocument doc(szFile);
182 err<<
"Syntax error while loading "<<szFile<<
" at line "\
183 <<doc.ErrorRow()<<
": ";
184 err<<doc.ErrorDesc();
190 TiXmlElement *root = doc.RootElement();
194 err<<
"Syntax error while loading "<<szFile<<
" . ";
195 err<<
"No root element.";
209 auto* name = (TiXmlElement*) root->FirstChild(
"name");
210 if(!name || !name->GetText())
213 err<<
"Module from "<<szFile<<
" has no name.";
218 for(TiXmlElement* var = root->FirstChildElement(
"var"); var; var = var->NextSiblingElement())
220 if(var->Attribute(
"name") && var->GetText())
222 parser->
addVariable(var->Attribute(
"name"), var->GetText());
230 std::string strname = parser->
parseText(name->GetText());
231 for (
char& i : strname) {
241 if ((desc = (TiXmlElement*)root->FirstChild(
"description"))) {
247 if ((ver = (TiXmlElement*)root->FirstChild(
"version"))) {
263 TiXmlElement* authors;
264 if ((authors = (TiXmlElement*)root->FirstChild(
"authors"))) {
265 for(TiXmlElement* ath = authors->FirstChildElement(); ath;
266 ath = ath->NextSiblingElement())
271 if (ath->GetText()) {
274 if (ath->Attribute(
"email")) {
275 author.
setEmail(ath->Attribute(
"email"));
282 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
290 TiXmlElement* resources;
291 if ((resources = (TiXmlElement*)root->FirstChild(
"dependencies"))) {
292 for(TiXmlElement* res = resources->FirstChildElement(); res;
293 res = res->NextSiblingElement())
300 resource.setPort(parser->
parseText(res->GetText()).c_str());
307 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
317 std::vector<std::pair<const char*, setter> > modList;
318 std::pair<const char*, setter> pairNode;
328 for(TiXmlElement* mod = root->FirstChildElement(); mod; mod = mod->NextSiblingElement())
332 TiXmlElement* element;
333 if((element = (TiXmlElement*) mod->FirstChild(
"name")))
335 std::string elemText;
340 if(element->GetText())
342 elemText = parser->
parseText(element->GetText());
343 text = elemText.c_str();
348 for(
auto& i : modList)
350 if((element = (TiXmlElement*) mod->FirstChild(i.first)))
353 if(element->GetText())
355 elemText = parser->
parseText(element->GetText());
356 text = elemText.c_str();
359 (
module.*(i.second))(text);
363 if((element = (TiXmlElement*) mod->FirstChild(
"rank")))
365 if(element->GetText())
367 elemText = parser->
parseText(element->GetText());
368 text = elemText.c_str();
371 module.setRank(atoi(text));
375 element = (TiXmlElement*) mod->FirstChild(
"geometry");
376 if(element && element->GetText())
381 if(prop.check(
"Pos"))
383 pt.
x = prop.findGroup(
"Pos").find(
"x").asFloat64();
384 pt.
y = prop.findGroup(
"Pos").find(
"y").asFloat64();
385 model.
points.push_back(pt);
386 module.setModelBase(model);
391 TiXmlElement* resources;
392 if((resources = (TiXmlElement*) mod->FirstChild(
"dependencies")))
394 for(TiXmlElement* res = resources->FirstChildElement(); res;
395 res = res->NextSiblingElement())
402 resource.setPort(parser->
parseText(res->GetText()).c_str());
403 if (res->Attribute(
"timeout")) {
404 resource.setTimeout(atof(res->Attribute(
"timeout")));
406 if (res->Attribute(
"request")) {
407 resource.setRequest(res->Attribute(
"request"));
409 if (res->Attribute(
"reply")) {
410 resource.setReply(res->Attribute(
"reply"));
412 module.addResource(resource);
418 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
426 TiXmlElement* ensure;
427 if((ensure = (TiXmlElement*) mod->FirstChild(
"ensure")))
429 for(TiXmlElement* res = ensure->FirstChildElement(); res;
430 res = res->NextSiblingElement())
434 if (res->Attribute(
"when") &&
compareString(res->Attribute(
"when"),
"start")) {
435 if (parser->
parseText(res->GetText()).c_str()) {
436 module.setPostExecWait(atof(parser->parseText(res->GetText()).c_str()));
439 else if (res->Attribute(
"when") &&
compareString(res->Attribute(
"when"),
"stop")) {
440 if (parser->
parseText(res->GetText()).c_str()) {
441 module.setPostStopWait(atof(parser->parseText(res->GetText()).c_str()));
444 else if (res->Attribute(
"when") && strlen(res->Attribute(
"when"))) {
446 war <<
"Unrecognized value for 'when' property from " << szFile <<
" at line "<< res->Row() <<
".";
450 if (parser->
parseText(res->GetText()).c_str()) {
451 module.setPostExecWait(atof(parser->parseText(res->GetText()).c_str()));
458 war<<
"Unrecognized tag from "<<szFile<<
" at line "\
465 for (TiXmlElement* map = mod->FirstChildElement(); map;
466 map = map->NextSiblingElement()) {
470 TiXmlElement* second;
471 if((first=(TiXmlElement*) map->FirstChild(
"old")) &&
472 (second=(TiXmlElement*) map->FirstChild(
"new")) )
475 module.addPortmap(portmap);
485 war<<
"Module from "<<szFile<<
" at line "\
486 <<mod->Row()<<
" has not name tag.";
494 for(TiXmlElement* embApp = root->FirstChildElement(); embApp;
495 embApp = embApp->NextSiblingElement())
500 TiXmlElement* prefix;
501 if((name=(TiXmlElement*) embApp->FirstChild(
"name")))
504 if ((prefix = (TiXmlElement*)embApp->FirstChild(
"prefix"))) {
505 IApp.setPrefix(parser->
parseText(prefix->GetText()).c_str());
508 auto* element = (TiXmlElement*) embApp->FirstChild(
"geometry");
509 if(element && element->GetText())
514 if(prop.check(
"Pos"))
516 pt.
x = prop.findGroup(
"Pos").find(
"x").asFloat64();
517 pt.
y = prop.findGroup(
"Pos").find(
"y").asFloat64();
518 model.
points.push_back(pt);
519 IApp.setModelBase(model);
528 war<<
"Incomplete application tag from "<<szFile<<
" at line "\
529 <<embApp->Row()<<
". (no name)";
537 for(TiXmlElement* arb = root->FirstChildElement(); arb;
538 arb = arb->NextSiblingElement())
542 auto* port = (TiXmlElement*) arb->FirstChild(
"port");
543 if(port && port->GetText())
548 for(TiXmlElement* rule = arb->FirstChildElement(); rule;
549 rule = rule->NextSiblingElement())
553 if (rule->Attribute(
"connection")) {
554 arbitrator.addRule(rule->Attribute(
"connection"), parser->
parseText(rule->GetText()).c_str());
559 auto* geometry = (TiXmlElement*) arb->FirstChild(
"geometry");
560 if(geometry && geometry->GetText())
564 if(prop.check(
"Pos"))
567 for(
size_t i=1; i<pos.
size(); i++)
572 model.
points.push_back(pt);
574 arbitrator.setModelBase(model);
583 war<<
"Incomplete arbitrator tag from "<<szFile<<
" at line "\
591 for(TiXmlElement* cnn = root->FirstChildElement(); cnn;
592 cnn = cnn->NextSiblingElement())
596 auto* from = (TiXmlElement*) cnn->FirstChild(
"from");
597 auto* to = (TiXmlElement*) cnn->FirstChild(
"to");
600 from = (TiXmlElement*)cnn->FirstChild(
"output");
603 to = (TiXmlElement*)cnn->FirstChild(
"input");
606 TiXmlElement* protocol;
609 std::string strCarrier;
610 if ((protocol = (TiXmlElement*)cnn->FirstChild(
"protocol")) && protocol->GetText()) {
611 strCarrier = parser->
parseText(protocol->GetText());
614 parser->
parseText(to->GetText()).c_str(),
618 if(cnn->Attribute(
"qos")) {
619 connection.setQosTo(cnn->Attribute(
"qos"));
620 connection.setQosFrom(cnn->Attribute(
"qos"));
623 if(from->Attribute(
"external") &&
626 connection.setFromExternal(
true);
630 resource.setPort(parser->
parseText(from->GetText()).c_str());
634 if (from->Attribute(
"qos")) {
635 connection.setQosFrom(from->Attribute(
"qos"));
637 if(to->Attribute(
"external") &&
642 connection.setToExternal(
true);
644 resource.setPort(parser->
parseText(to->GetText()).c_str());
648 if (to->Attribute(
"qos")) {
649 connection.setQosTo(to->Attribute(
"qos"));
658 connection.setFromExternal(
true);
661 connection.setToExternal(
true);
665 if (cnn->Attribute(
"id")) {
666 connection.setId(cnn->Attribute(
"id"));
669 if (cnn->Attribute(
"persist") &&
compareString(cnn->Attribute(
"persist"),
"true")) {
670 connection.setPersistent(
true);
674 auto* geometry = (TiXmlElement*) cnn->FirstChild(
"geometry");
675 if(geometry && geometry->GetText())
679 if(prop.check(
"Pos"))
682 for(
size_t i=1; i<pos.
size(); i++)
687 model.
points.push_back(pt);
689 connection.setModelBase(model);
699 war<<
"Incomplete connection tag from "<<szFile<<
" at line "\
Class ApplicationInterface.
void setName(const char *szName)
Arbitrator & addArbitrator(Arbitrator &arb)
bool addIapplication(ApplicationInterface &iapp)
void addAuthor(Author &author)
void setXmlFile(const char *szFilename)
void setVersion(const char *szVersion)
void setDescription(const char *szDesc)
Connection & addConnection(Connection &cnn)
bool addResource(ResYarpPort &res)
ResYarpPort & getResourceAt(int index)
bool addImodule(ModuleInterface &imod)
void setName(const char *name)
void setEmail(const char *email)
Singleton class ErrorLogger.
void addError(const char *szError)
void addWarning(const char *szWarning)
static ErrorLogger * Instance()
Singleton class ErrorLogger.
std::vector< GyPoint > points
void setDisplay(const char *szDisplay)
void setHost(const char *szHost)
void setWorkDir(const char *szWDir)
void setParam(const char *szParam)
void setStdio(const char *szStdio)
void setPrefix(const char *szPrefix)
void setEnvironment(const char *szEnv)
void setBroker(const char *szBroker)
bool addVariable(const std::string &key, const std::string &value)
std::string parseText(const char *element)
Application * getNextApplication() override
XmlAppLoader(const char *szFileName)
load only one application indicated by its xml file name
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
A class for storing options and configuration information.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
static constexpr value_type preferred_separator
bool compareString(const char *szFirst, const char *szSecond)
std::stringstream OSTRINGSTREAM