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 
35 using namespace armarx;
36 
37 GroupXmlWriter::GroupXmlWriter()
38 {
39 }
40 
41 void
42 GroupXmlWriter::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 
94 void
95 GroupXmlWriter::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 
109 void
110 GroupXmlWriter::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 
127 void
128 GroupXmlWriter::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 
139 void
140 GroupXmlWriter::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 }
armarx::RapidXmlWriterNode::append_data_node
RapidXmlWriterNode & append_data_node(const std::string &value)
Definition: RapidXmlWriter.h:99
armarx::RapidXmlWriter
Definition: RapidXmlWriter.h:122
armarx::StateTreeNodePtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr
Definition: StatechartGroupDefs.h:31
armarx::GroupXmlWriter::WriteXml
static void WriteXml(StatechartGroupPtr group, QString path, bool indent=true)
Definition: GroupXmlWriter.cpp:42
FileIOException.h
armarx::StatechartGroupGenerator::writeFileContentsIfChanged
static bool writeFileContentsIfChanged(const std::string &path, const std::string &contents)
Definition: StateGroupGenerator.cpp:483
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::RapidXmlWriterNode::append_optional_bool_attribute
RapidXmlWriterNode & append_optional_bool_attribute(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool value, bool defaultValue)
Definition: RapidXmlWriter.h:77
armarx::RapidXmlWriter::print
std::string print(bool indent)
Definition: RapidXmlWriter.h:145
armarx::exceptions::local::FileOpenException
Definition: FileIOException.h:59
armarx::GroupXmlWriter::WriteFileContents
static void WriteFileContents(QString path, QString contents)
Definition: GroupXmlWriter.cpp:95
armarx::StateTreeNode::State
@ State
Definition: StateTreeNode.h:49
armarx::RapidXmlWriterNode::append_attribute
RapidXmlWriterNode & append_attribute(const std::string &name, const std::string &value)
Definition: RapidXmlWriter.h:59
GroupXmlWriter.h
armarx::StatechartGroupPtr
std::shared_ptr< StatechartGroup > StatechartGroupPtr
Definition: StatechartGroupDefs.h:34
StateGroupGenerator.h
armarx::RapidXmlWriter::createRootNode
RapidXmlWriterNode createRootNode(const std::string &name)
Definition: RapidXmlWriter.h:137
armarx::RapidXmlWriterNode::append_string_node
RapidXmlWriterNode & append_string_node(const std::string &name, const std::string &value)
Definition: RapidXmlWriter.h:107
armarx::StateTreeNode::Folder
@ Folder
Definition: StateTreeNode.h:48
armarx::RapidXmlWriterNode::append_node
RapidXmlWriterNode append_node(const std::string &name)
Definition: RapidXmlWriter.h:91
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
armarx::RapidXmlWriterNode
Definition: RapidXmlWriter.h:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27