GroupXmlReader.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "GroupXmlReader.h"
25 
26 #include <filesystem>
27 #include <fstream>
28 #include <stdexcept>
29 #include <streambuf>
30 #include <string>
31 
32 #include <boost/regex.hpp>
33 
34 #include <QFile>
35 #include <QFileInfo>
36 #include <QTextStream>
37 
39 
40 
41 using namespace armarx;
42 using namespace rapidxml;
43 
44 GuiStatechartGroupXmlReader::GuiStatechartGroupXmlReader()
45 {
46 }
47 
49 GuiStatechartGroupXmlReader::ReadXml(QString groupDefinitionFile, StateTreeNodePtr virtualRoot)
50 {
51  RapidXmlReaderPtr wrapper =
52  RapidXmlReader::FromFile(std::string(groupDefinitionFile.toUtf8().data()));
53  RapidXmlReaderNode xmlGroupNode = wrapper->getRoot("StatechartGroup");
54  QString name = QString::fromUtf8(xmlGroupNode.attribute_value("name").c_str());
55  QString description =
56  QString::fromUtf8(xmlGroupNode.first_node_value_or_default("Description", "").c_str());
57  QString packageName = QString::fromUtf8(xmlGroupNode.attribute_value("package").c_str());
58 
59  QString displayName = name;
60  boost::regex projectNameRegex("(\\w+)/statecharts/\\w+/?$");
61  boost::match_results<std::string::const_iterator> match;
62  std::filesystem::path groupPath(groupDefinitionFile.toUtf8().data());
63  const std::string groupPathStr(groupPath.parent_path().c_str());
65  QFileInfo(QString::fromStdString(groupPath.string())).permissions() & QFile::WriteUser
68 
69 
70 
71  displayName += QString(" [%1]").arg(packageName);
72 
73  QList<QString> proxies;
74 
75  for (RapidXmlReaderNode proxyNode : xmlGroupNode.nodes("Proxies", "Proxy"))
76  {
77  proxies.append(QString::fromUtf8(proxyNode.attribute_value("value").c_str()));
78  }
79 
80  QMap<QString, QString> statechartGroupConfigurations;
81  for (RapidXmlReaderNode configNode : xmlGroupNode.nodes("Configurations", "Configuration"))
82  {
83  auto profileName = QString::fromUtf8(configNode.attribute_value("profileName").c_str());
84  auto configuration = QString::fromUtf8(configNode.value().c_str());
85  statechartGroupConfigurations[profileName] = configuration;
86  }
87 
88  bool generateContext =
89  xmlGroupNode.attribute_as_optional_bool("generateContext", "true", "false", false);
90 
91  StatechartGroupPtr group(new StatechartGroup(groupDefinitionFile,
92  QString::fromUtf8(groupPath.parent_path().c_str()),
93  name,
94  description,
95  packageName,
96  proxies,
97  generateContext,
98  statechartGroupConfigurations,
99  writable));
100  StateTreeNodePtr rootNode(
101  new StateTreeNode(displayName, "", StateTreeNode::Group, virtualRoot, group, false));
102  ReadChildren(xmlGroupNode, rootNode, group);
103  group->setRootNode(rootNode);
104  ARMARX_LOG_S << "Reading done: " << groupDefinitionFile.toStdString() << flush;
105  return group;
106 }
107 
108 QString
110 {
111  QFile f(path);
112 
113  if (!f.open(QFile::ReadOnly | QFile::Text))
114  {
115  throw armarx::exceptions::local::FileOpenException(path.toUtf8().data());
116  }
117 
118  QTextStream in(&f);
119  return in.readAll();
120 }
121 
122 void
123 GuiStatechartGroupXmlReader::ReadChildren(RapidXmlReaderNode xmlNode,
124  StateTreeNodePtr parentNode,
125  StatechartGroupPtr group)
126 {
127  for (RapidXmlReaderNode xmlFolderNode : xmlNode.nodes("Folder"))
128  {
129  QString basename = QString::fromUtf8(xmlFolderNode.attribute_value("basename").c_str());
130  StateTreeNodePtr folderNode(
131  new StateTreeNode(basename, basename, StateTreeNode::Folder, parentNode, group, false));
132  parentNode->appendChild(folderNode);
133  ReadChildren(xmlFolderNode, folderNode, group);
134  }
135 
136  for (RapidXmlReaderNode xmlStateNode : xmlNode.nodes("State"))
137  {
138  QString filename = QString::fromUtf8(xmlStateNode.attribute_value("filename").c_str());
139  bool stateIsPublic =
140  xmlStateNode.attribute_as_optional_bool("visibility", "public", "private", false);
141  StateTreeNodePtr stateNode(new StateTreeNode(
142  filename, filename, StateTreeNode::State, parentNode, group, stateIsPublic));
143  parentNode->appendChild(stateNode);
144  }
145 }
armarx::RapidXmlReaderPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
Definition: RapidXmlReader.h:66
armarx::RapidXmlReader::FromFile
static RapidXmlReaderPtr FromFile(const std::string &path)
Definition: RapidXmlReader.h:497
armarx::StateTreeNode
Definition: StateTreeNode.h:40
armarx::GuiStatechartGroupXmlReader::ReadFileContents
static QString ReadFileContents(QString path)
Definition: GroupXmlReader.cpp:109
armarx::StateTreeNodePtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr
Definition: StatechartGroupDefs.h:31
FileIOException.h
armarx::StatechartGroup::WriteAccess
WriteAccess
Definition: StatechartGroup.h:40
armarx::StateTreeNode::Group
@ Group
Definition: StateTreeNode.h:45
armarx::RapidXmlReaderNode::attribute_as_optional_bool
bool attribute_as_optional_bool(const char *name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
Definition: RapidXmlReader.h:256
armarx::StatechartGroup::eWritable
@ eWritable
Definition: StatechartGroup.h:40
armarx::RapidXmlReaderNode::attribute_value
std::string attribute_value(const char *attrName) const
Definition: RapidXmlReader.h:198
armarx::StatechartGroup
Definition: StatechartGroup.h:37
armarx::StatechartGroup::eReadOnly
@ eReadOnly
Definition: StatechartGroup.h:40
armarx::exceptions::local::FileOpenException
Definition: FileIOException.h:58
rapidxml
Definition: rapidxml.hpp:58
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::StateTreeNode::State
@ State
Definition: StateTreeNode.h:45
filename
std::string filename
Definition: VisualizationRobot.cpp:84
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
ARMARX_LOG_S
#define ARMARX_LOG_S
Definition: Logging.h:145
armarx::StatechartGroupPtr
std::shared_ptr< StatechartGroup > StatechartGroupPtr
Definition: StatechartGroupDefs.h:34
armarx::RapidXmlReaderNode::first_node_value_or_default
std::string first_node_value_or_default(const char *name, const std::string &defaultValue) const
Definition: RapidXmlReader.h:374
armarx::RapidXmlReaderNode::nodes
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
Definition: RapidXmlReader.h:158
GroupXmlReader.h
armarx::StateTreeNode::Folder
@ Folder
Definition: StateTreeNode.h:45
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GuiStatechartGroupXmlReader::ReadXml
static StatechartGroupPtr ReadXml(QString groupDefinitionFile, StateTreeNodePtr virtualRoot)
Definition: GroupXmlReader.cpp:49