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
41using namespace armarx;
42using namespace rapidxml;
43
44GuiStatechartGroupXmlReader::GuiStatechartGroupXmlReader()
45{
46}
47
49GuiStatechartGroupXmlReader::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 displayName += QString(" [%1]").arg(packageName);
71
72 QList<QString> proxies;
73
74 for (RapidXmlReaderNode proxyNode : xmlGroupNode.nodes("Proxies", "Proxy"))
75 {
76 proxies.append(QString::fromUtf8(proxyNode.attribute_value("value").c_str()));
77 }
78
79 QMap<QString, QString> statechartGroupConfigurations;
80 for (RapidXmlReaderNode configNode : xmlGroupNode.nodes("Configurations", "Configuration"))
81 {
82 auto profileName = QString::fromUtf8(configNode.attribute_value("profileName").c_str());
83 auto configuration = QString::fromUtf8(configNode.value().c_str());
84 statechartGroupConfigurations[profileName] = configuration;
85 }
86
87 bool generateContext =
88 xmlGroupNode.attribute_as_optional_bool("generateContext", "true", "false", false);
89
90 StatechartGroupPtr group(new StatechartGroup(groupDefinitionFile,
91 QString::fromUtf8(groupPath.parent_path().c_str()),
92 name,
93 description,
94 packageName,
95 proxies,
96 generateContext,
97 statechartGroupConfigurations,
98 writable));
99 StateTreeNodePtr rootNode(
100 new StateTreeNode(displayName, "", StateTreeNode::Group, virtualRoot, group, false));
101 ReadChildren(xmlGroupNode, rootNode, group);
102 group->setRootNode(rootNode);
103 ARMARX_LOG_S << "Reading done: " << groupDefinitionFile.toStdString() << flush;
104 return group;
105}
106
107QString
109{
110 QFile f(path);
111
112 if (!f.open(QFile::ReadOnly | QFile::Text))
113 {
114 throw armarx::exceptions::local::FileOpenException(path.toUtf8().data());
115 }
116
117 QTextStream in(&f);
118 return in.readAll();
119}
120
121void
122GuiStatechartGroupXmlReader::ReadChildren(RapidXmlReaderNode xmlNode,
123 StateTreeNodePtr parentNode,
124 StatechartGroupPtr group)
125{
126 for (RapidXmlReaderNode xmlFolderNode : xmlNode.nodes("Folder"))
127 {
128 QString basename = QString::fromUtf8(xmlFolderNode.attribute_value("basename").c_str());
129 StateTreeNodePtr folderNode(
130 new StateTreeNode(basename, basename, StateTreeNode::Folder, parentNode, group, false));
131 parentNode->appendChild(folderNode);
132 ReadChildren(xmlFolderNode, folderNode, group);
133 }
134
135 for (RapidXmlReaderNode xmlStateNode : xmlNode.nodes("State"))
136 {
137 QString filename = QString::fromUtf8(xmlStateNode.attribute_value("filename").c_str());
138 bool stateIsPublic =
139 xmlStateNode.attribute_as_optional_bool("visibility", "public", "private", false);
140 StateTreeNodePtr stateNode(new StateTreeNode(
141 filename, filename, StateTreeNode::State, parentNode, group, stateIsPublic));
142 parentNode->appendChild(stateNode);
143 }
144}
static QString ReadFileContents(QString path)
static StatechartGroupPtr ReadXml(QString groupDefinitionFile, StateTreeNodePtr virtualRoot)
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
std::string first_node_value_or_default(const char *name, const std::string &defaultValue) const
std::string attribute_value(const char *attrName) const
bool attribute_as_optional_bool(const char *name, const std::string &trueValue, const std::string &falseValue, bool defaultValue) const
static RapidXmlReaderPtr FromFile(const std::string &path)
#define ARMARX_LOG_S
This macro creates a new temporary instance which can then be used to log data using the << operator.
Definition Logging.h:145
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
std::shared_ptr< StatechartGroup > StatechartGroupPtr
const LogSender::manipulator flush
Definition LogSender.h:251
std::shared_ptr< StateTreeNode > StateTreeNodePtr