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