GroupXmlWriter.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 "GroupXmlWriter.h"
25
26#include <stdexcept>
27
28#include <QFile>
29#include <QTextStream>
30
33
34
35using namespace armarx;
36
37GroupXmlWriter::GroupXmlWriter()
38{
39}
40
41void
42GroupXmlWriter::WriteXml(StatechartGroupPtr group, QString path, bool indent)
43{
44 RapidXmlWriter builder;
45 RapidXmlWriterNode root = builder.createRootNode("StatechartGroup");
46 root.append_attribute("name", group->getName().toUtf8().data());
47 root.append_attribute("package", group->getPackageName().toUtf8().data());
49 "generateContext", "true", "false", group->contextGenerationEnabled(), false);
50
51 if (!group->getDescription().isEmpty())
52 {
53 root.append_string_node("Description", group->getDescription().toUtf8().data());
54 }
55
56 RapidXmlWriterNode proxies = root.append_node("Proxies");
57
58 for (QString proxy : group->getProxies())
59 {
60 proxies.append_node("Proxy").append_attribute("value", proxy.toUtf8().data());
61 }
62
63 auto c = group->getConfigurations();
64 if (!c.empty())
65 {
66 RapidXmlWriterNode configurations = root.append_node("Configurations");
67 for (auto it = c.begin(); it != c.end(); it++)
68 {
69 if (!it->isEmpty())
70 {
71 configurations.append_node("Configuration")
72 .append_attribute("profileName", it.key().toUtf8().data())
73 .append_data_node(it->toUtf8().data());
74 }
75 }
76 }
77
78 StateTreeNodePtr rootNode = group->getRootNode();
79
80 for (int i = 0; i < rootNode->getChildren().count(); i++)
81 {
82 WriteNode(rootNode->getChildren().at(i), root);
83 }
84
85
86 //builder.saveToFile(path.toUtf8().data(), true);
87 std::string contents = builder.print(true);
88 if (StatechartGroupGenerator::writeFileContentsIfChanged(path.toUtf8().data(), contents))
89 {
90 ARMARX_INFO_S << "Writing new group definition to " << path.toUtf8().data();
91 }
92}
93
94void
95GroupXmlWriter::WriteFileContents(QString path, QString contents)
96{
97 QFile f(path);
98
99 if (!f.open(QFile::WriteOnly | QFile::Text))
100 {
101 throw armarx::exceptions::local::FileOpenException(path.toUtf8().data());
102 }
103
104 QTextStream out(&f);
105 out << contents;
106 out.flush();
107}
108
109void
110GroupXmlWriter::WriteNode(const StateTreeNodePtr& node, RapidXmlWriterNode parentXmlNode)
111{
112 switch (node->getNodeType())
113 {
115 WriteFolderNode(node, parentXmlNode);
116 return;
117
119 WriteStateNode(node, parentXmlNode);
120 return;
121
122 default:
123 throw std::runtime_error("Unknown node type");
124 }
125}
126
127void
128GroupXmlWriter::WriteFolderNode(const StateTreeNodePtr& node, RapidXmlWriterNode parentXmlNode)
129{
130 RapidXmlWriterNode xmlNode = parentXmlNode.append_node("Folder");
131 xmlNode.append_attribute("basename", node->getBasename().toUtf8().data());
132
133 for (int i = 0; i < node->getChildren().count(); i++)
134 {
135 WriteNode(node->getChildren().at(i), xmlNode);
136 }
137}
138
139void
140GroupXmlWriter::WriteStateNode(const StateTreeNodePtr& node, RapidXmlWriterNode parentXmlNode)
141{
142 RapidXmlWriterNode xmlNode = parentXmlNode.append_node("State");
143 xmlNode.append_attribute("filename", node->getBasename().toUtf8().data());
145 "visibility", "public", "private", node->isPublic(), false);
146}
constexpr T c
static void WriteFileContents(QString path, QString contents)
static void WriteXml(StatechartGroupPtr group, QString path, bool indent=true)
RapidXmlWriterNode & append_data_node(const std::string &value)
RapidXmlWriterNode append_node(const std::string &name)
RapidXmlWriterNode & append_string_node(const std::string &name, const std::string &value)
RapidXmlWriterNode & append_optional_bool_attribute(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool value, bool defaultValue)
RapidXmlWriterNode & append_attribute(const std::string &name, const std::string &value)
std::string print(bool indent)
RapidXmlWriterNode createRootNode(const std::string &name)
static bool writeFileContentsIfChanged(const std::string &path, const std::string &contents)
#define ARMARX_INFO_S
Definition Logging.h:202
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< StatechartGroup > StatechartGroupPtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr