YARP
Yet Another Robot Platform
xmlappsaver.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 #include <yarp/manager/utility.h>
11 #include <dirent.h>
12 
13 #include <algorithm>
14 #include <cctype>
15 #include <string>
16 #include <fstream>
17 
18 #include <tinyxml.h>
19 
20 
21 using namespace std;
22 using namespace yarp::manager;
23 
24 
25 XmlAppSaver::XmlAppSaver(const char* szFileName)
26 {
27  if(szFileName) strFileName = szFileName;
28 }
29 
30 bool XmlAppSaver::save(Application* application)
31 {
32  if(!strFileName.size())
33  return serialXml(application, application->getXmlFile());
34 
35  return serialXml(application, strFileName.c_str());
36 }
37 
38 XmlAppSaver::~XmlAppSaver() = default;
39 
40 bool XmlAppSaver::serialXml(Application* app, const char* szFile)
41 {
42 
43  // updating application xml file name
44  app->setXmlFile(szFile);
45 
46  ErrorLogger* logger = ErrorLogger::Instance();
47 
48  TiXmlDocument doc; //(szFile);
49  auto* root = new TiXmlElement("application");
50  doc.LinkEndChild( root );
51  auto* appName = new TiXmlElement("name"); //are all these NEW ok?
52  appName->LinkEndChild(new TiXmlText(app->getName()));
53  root->LinkEndChild(appName);
54 
55 
56  if (strcmp(app->getDescription(), "") != 0)
57  {
58  auto* desc= new TiXmlElement( "description");
59  desc->LinkEndChild(new TiXmlText( app->getDescription()));
60  root->LinkEndChild(desc);
61  }
62 
63  if(strcmp (app->getVersion(), "") != 0)
64  {
65  auto* vers= new TiXmlElement( "version");
66  vers->LinkEndChild(new TiXmlText( app->getVersion()));
67  root->LinkEndChild(vers);
68 
69  }
70 
71  /*
72  * TODO: setting prefix of the main application is inactivated.
73  * Check this should be supported in future or not!
74  */
75  /*
76  if(strcmp (app->getPrefix(), ""))
77  {
78  TiXmlElement * prefix= new TiXmlElement( "prefix");
79  prefix->LinkEndChild(new TiXmlText( app->getPrefix()));
80  root->LinkEndChild(prefix);
81 
82  }
83  */
84 
85  if(app->authorCount()>0)
86  {
87  auto* auths=new TiXmlElement("authors");
88  for (int i=0; i<app->authorCount(); i++)
89  {
90  //app->getAuthorAt(i);
91  auto* auth=new TiXmlElement("author");
92  auth->SetAttribute("email", app->getAuthorAt(i).getEmail());
93  auth->LinkEndChild(new TiXmlText(app->getAuthorAt(i).getName()));
94  auths->LinkEndChild(auth);
95  }
96  root->LinkEndChild(auths);
97  }
98 
99  // iterate over modules
100  {
101  int nModules=app->imoduleCount();
102  for (int modCt=0; modCt<nModules; ++modCt)
103  {
104  auto* newMod = new TiXmlElement("module");
105  root->LinkEndChild(newMod); //add module element
106  ModuleInterface curMod=app->getImoduleAt(modCt);
107 
108  auto* name = new TiXmlElement("name");
109  name->LinkEndChild(new TiXmlText(curMod.getName()));
110  newMod->LinkEndChild(name);
111 
112  auto* parameters=new TiXmlElement("parameters");
113  parameters->LinkEndChild(new TiXmlText(curMod.getParam()));
114  newMod->LinkEndChild(parameters);
115 
116  auto* node = new TiXmlElement("node");
117  node->LinkEndChild(new TiXmlText(curMod.getHost())); //is host the same as node?
118  newMod->LinkEndChild(node);
119 
120  auto* prefix=new TiXmlElement("prefix");
121  prefix->LinkEndChild(new TiXmlText(curMod.getPrefix()));
122  newMod->LinkEndChild(prefix);
123 
124  if(strcmp(curMod.getStdio(), "") != 0)
125  {
126  auto* stdio=new TiXmlElement("stdio");
127  stdio->LinkEndChild(new TiXmlText(curMod.getStdio()));
128  newMod->LinkEndChild(stdio);
129  }
130 
131  if(strcmp(curMod.getWorkDir(), "") != 0)
132  {
133  auto* workdir=new TiXmlElement("workdir");
134  workdir->LinkEndChild(new TiXmlText(curMod.getWorkDir()));
135  newMod->LinkEndChild(workdir);
136  }
137 
138  if(strcmp(curMod.getBroker(), "") != 0)
139  {
140  auto* broker=new TiXmlElement("deployer");
141  broker->LinkEndChild(new TiXmlText(curMod.getBroker()));
142  newMod->LinkEndChild(broker);
143  }
144  /*
145  if(curMod.getRank()>0) //TODO check here how is rank handled
146  {
147  TiXmlElement *rank=new TiXmlElement("rank");
148  char str[256];
149  sprintf(str,"%d", curMod.getRank());
150  rank->LinkEndChild(new TiXmlText(str));
151  newMod->LinkEndChild(rank);
152  }
153  */
154 
155  GraphicModel model = curMod.getModelBase();
156  OSTRINGSTREAM txt;
157  if(model.points.size()>0)
158  {
159  txt<<"(Pos (x "<<model.points[0].x<<") "<<"(y "<<model.points[0].y<<"))";
160  auto* geometry=new TiXmlElement("geometry");
161  geometry->LinkEndChild(new TiXmlText(txt.str().c_str()));
162  newMod->LinkEndChild(geometry);
163  }
164  }
165 
166  }
167 
168  // iterate over embedded applications
169  {
170  int nApps=app->iapplicationCount();
171  for (int appCt=0; appCt<nApps; ++appCt)
172  {
173  auto* newApp = new TiXmlElement("application");
174  root->LinkEndChild(newApp); //add application element
175  ApplicationInterface curApp=app->getIapplicationAt(appCt);
176 
177  auto* name = new TiXmlElement("name");
178  name->LinkEndChild(new TiXmlText(curApp.getName()));
179  newApp->LinkEndChild(name);
180 
181 
182  auto* prefix=new TiXmlElement("prefix");
183  prefix->LinkEndChild(new TiXmlText(curApp.getPrefix()));
184  newApp->LinkEndChild(prefix);
185 
186  GraphicModel model = curApp.getModelBase();
187  OSTRINGSTREAM txt;
188  if(model.points.size()>0)
189  {
190  txt<<"(Pos (x "<<model.points[0].x<<") "<<"(y "<<model.points[0].y<<"))";
191  auto* geometry=new TiXmlElement("geometry");
192  geometry->LinkEndChild(new TiXmlText(txt.str().c_str()));
193  newApp->LinkEndChild(geometry);
194  }
195 
196  }
197  }
198 
199  // iterate over connections
200  {
201  int nConns=app->connectionCount();
202  for (int connCt=0; connCt<nConns; ++connCt)
203  {
204  auto* newConn=new TiXmlElement("connection");
205  Connection curConn=app->getConnectionAt(connCt);
206 
207  if(strlen(curConn.getId()))
208  newConn->SetAttribute("id", curConn.getId());
209 
210  if(curConn.isPersistent())
211  newConn->SetAttribute("persist", "true");
212 
213  auto* from = new TiXmlElement("from");
214  if (curConn.isExternalFrom())
215  from->SetAttribute("external", "true");
216  from->LinkEndChild(new TiXmlText(curConn.from()));
217  newConn->LinkEndChild(from);
218 
219  auto* to = new TiXmlElement("to");
220  if (curConn.isExternalTo())
221  to->SetAttribute("external", "true");
222  to->LinkEndChild(new TiXmlText(curConn.to()));
223  newConn->LinkEndChild(to);
224 
225  auto* protocol = new TiXmlElement("protocol");
226  protocol->LinkEndChild(new TiXmlText(curConn.carrier()));
227  newConn->LinkEndChild(protocol);
228 
229  GraphicModel model = curConn.getModelBase();
230  OSTRINGSTREAM txt;
231  if(model.points.size()>0)
232  {
233  txt<<"(Pos ";
234  for(auto& point : model.points)
235  txt<<"((x "<<point.x<<") "<<"(y "<<point.y<<")) ";
236  txt<<" )";
237  auto* geometry=new TiXmlElement("geometry");
238  geometry->LinkEndChild(new TiXmlText(txt.str().c_str()));
239  newConn->LinkEndChild(geometry);
240  }
241 
242  root->LinkEndChild(newConn);
243  }
244 
245  }
246 
247  // iterate over arbitrators
248  for(int i=0; i<app->arbitratorCount(); i++)
249  {
250  Arbitrator& arb = app->getArbitratorAt(i);
251  auto* newArb = new TiXmlElement("arbitrator");
252 
253  auto* port = new TiXmlElement("port");
254  port->LinkEndChild(new TiXmlText(arb.getPort()));
255  newArb->LinkEndChild(port);
256 
257  std::map<string, string> &rules = arb.getRuleMap();
258  for(auto& it : rules)
259  {
260  auto* rule = new TiXmlElement("rule");
261  rule->SetAttribute("connection", it.first.c_str());
262  rule->LinkEndChild(new TiXmlText(it.second.c_str()));
263  newArb->LinkEndChild(rule);
264  }
265 
266  GraphicModel model = arb.getModelBase();
267  OSTRINGSTREAM txt;
268  if(model.points.size()>0)
269  {
270  txt<<"(Pos ";
271  for(auto& point : model.points)
272  txt<<"((x "<<point.x<<") "<<"(y "<<point.y<<")) ";
273  txt<<" )";
274  auto* geometry=new TiXmlElement("geometry");
275  geometry->LinkEndChild(new TiXmlText(txt.str().c_str()));
276  newArb->LinkEndChild(geometry);
277  }
278  root->LinkEndChild(newArb);
279  }
280 
281 
282  bool ok=doc.SaveFile(app->getXmlFile());
283  if (!ok)
284  {
285 
286  OSTRINGSTREAM err;
287  err<<"tinyXml error for file " << app->getXmlFile();
288  if (doc.Error())
289  err <<" at line " << doc.ErrorRow() << ", column " << doc.ErrorCol() << ": " << doc.ErrorDesc();
290  logger->addError(err);
291  err <<"\n";
292  return false;
293  }
294  else return true;
295 
296 }
yarp::manager::Application
Class Application.
Definition: application.h:292
yarp::manager::Connection::from
const char * from()
Definition: application.h:79
yarp::manager::Connection::getModelBase
GraphicModel & getModelBase()
Definition: application.h:121
yarp::manager::Connection::isExternalTo
bool isExternalTo()
Definition: application.h:97
yarp::manager::ModuleInterface::getModelBase
GraphicModel & getModelBase()
Definition: application.h:221
yarp::manager::Application::getName
const char * getName()
Definition: application.h:306
yarp::manager::Connection
Class Connection.
Definition: application.h:60
yarp::manager::Arbitrator
Class port Arbitrator.
Definition: arbitrator.h:28
yarp::manager::Application::getAuthorAt
Author & getAuthorAt(int index)
Definition: application.h:314
yarp::manager::Application::getImoduleAt
ModuleInterface & getImoduleAt(int index)
Definition: application.h:318
yarp::manager::OSTRINGSTREAM
std::stringstream OSTRINGSTREAM
Definition: utility.h:52
yarp::manager
Definition: application.h:24
yarp::manager::ErrorLogger
Singleton class ErrorLogger.
Definition: utility.h:60
yarp::manager::Application::getConnectionAt
Connection & getConnectionAt(int index)
Definition: application.h:344
utility.h
yarp::manager::Application::setXmlFile
void setXmlFile(const char *szFilename)
Definition: application.h:340
yarp::manager::ModuleInterface
Class ModuleInterface.
Definition: application.h:164
yarp::manager::GraphicModel::points
std::vector< GyPoint > points
Definition: node.h:33
yarp::manager::ApplicationInterface::getModelBase
GraphicModel & getModelBase()
Definition: application.h:273
yarp::manager::Application::getArbitratorAt
Arbitrator & getArbitratorAt(int index)
Definition: application.h:350
yarp::manager::Application::arbitratorCount
int arbitratorCount()
Definition: application.h:349
yarp::manager::Application::getIapplicationAt
ApplicationInterface & getIapplicationAt(int index)
Definition: application.h:324
yarp::manager::Connection::to
const char * to()
Definition: application.h:80
yarp::manager::ModuleInterface::getHost
const char * getHost()
Definition: application.h:189
yarp::manager::Connection::carrier
const char * carrier()
Definition: application.h:81
yarp::manager::Arbitrator::getPort
const char * getPort()
Definition: arbitrator.h:36
yarp::manager::Application::imoduleCount
int imoduleCount()
Definition: application.h:317
yarp::manager::ModuleInterface::getWorkDir
const char * getWorkDir()
Definition: application.h:192
yarp::manager::Application::iapplicationCount
int iapplicationCount()
Definition: application.h:323
yarp::manager::ApplicationInterface
Class ApplicationInterface.
Definition: application.h:256
yarp::manager::Application::getDescription
const char * getDescription()
Definition: application.h:309
yarp::manager::Connection::isPersistent
bool isPersistent()
Definition: application.h:98
yarp::manager::ApplicationInterface::getName
const char * getName()
Definition: application.h:264
yarp::manager::ErrorLogger::addError
void addError(const char *szError)
Definition: utility.cpp:121
yarp::manager::ModuleInterface::getPrefix
const char * getPrefix()
Definition: application.h:195
yarp::manager::Connection::getId
const char * getId()
Definition: application.h:102
yarp::manager::ModuleInterface::getStdio
const char * getStdio()
Definition: application.h:193
yarp::manager::Author::getName
const char * getName()
Definition: module.h:35
yarp::manager::ApplicationInterface::getPrefix
const char * getPrefix()
Definition: application.h:265
yarp::manager::Connection::isExternalFrom
bool isExternalFrom()
Definition: application.h:96
yarp::manager::ModuleInterface::getParam
const char * getParam()
Definition: application.h:190
yarp::manager::Application::connectionCount
int connectionCount()
Definition: application.h:343
yarp::manager::GraphicModel
Definition: node.h:28
yarp::manager::Arbitrator::getRuleMap
std::map< std::string, std::string > & getRuleMap()
Definition: arbitrator.h:46
yarp::manager::Author::getEmail
const char * getEmail()
Definition: module.h:36
yarp::manager::ModuleInterface::getBroker
const char * getBroker()
Definition: application.h:194
yarp::manager::Application::getVersion
const char * getVersion()
Definition: application.h:308
yarp::manager::ModuleInterface::getName
const char * getName()
Definition: application.h:188
yarp::manager::Arbitrator::getModelBase
GraphicModel & getModelBase()
Definition: arbitrator.h:55
xmlappsaver.h
yarp::manager::Application::getXmlFile
const char * getXmlFile()
Definition: application.h:341
yarp::manager::Application::authorCount
int authorCount()
Definition: application.h:313