RapidXmlWriter.h
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 #pragma once
25 
26 #include <fstream>
27 #include <string>
28 
31 
32 namespace armarx
33 {
34 
36  {
37  friend class RapidXmlWriter;
38 
39  private:
40  rapidxml::xml_document<>* document;
42 
44  {
45  this->document = document;
46  node = document->allocate_node(node_type);
47  }
48 
51  const std::string& name)
52  {
53  this->document = document;
54  node = document->allocate_node(node_type, cloneString(name));
55  }
56 
57  public:
58  RapidXmlWriterNode&
59  append_attribute(const std::string& name, const std::string& value)
60  {
61  node->append_attribute(
62  document->allocate_attribute(cloneString(name), cloneString(value)));
63  return *this;
64  }
65 
67  append_bool_attribute(const std::string& name,
68  const std::string& trueValue,
69  const std::string& falseValue,
70  bool value)
71  {
72  append_attribute(name, value ? trueValue : falseValue);
73  return *this;
74  }
75 
77  append_optional_bool_attribute(const std::string& name,
78  const std::string& trueValue,
79  const std::string& falseValue,
80  bool value,
81  bool defaultValue)
82  {
83  if (value != defaultValue)
84  {
85  append_attribute(name, value ? trueValue : falseValue);
86  }
87  return *this;
88  }
89 
91  append_node(const std::string& name)
92  {
93  RapidXmlWriterNode node(document, rapidxml::node_element, name);
94  this->node->append_node(node.node);
95  return node;
96  }
97 
99  append_data_node(const std::string& value)
100  {
101  this->node->append_node(
102  document->allocate_node(rapidxml::node_data, nullptr, cloneString(value)));
103  return *this;
104  }
105 
107  append_string_node(const std::string& name, const std::string& value)
108  {
109  this->node->append_node(document->allocate_node(
110  rapidxml::node_element, cloneString(name), cloneString(value)));
111  return *this;
112  }
113 
114  private:
115  const char*
116  cloneString(const std::string& str)
117  {
118  return document->allocate_string(str.c_str());
119  }
120  };
121 
123  {
124  private:
125  rapidxml::xml_document<> document;
126 
127  public:
129  {
130  RapidXmlWriterNode declaration(&document, rapidxml::node_declaration);
131  declaration.append_attribute("version", "1.0");
132  declaration.append_attribute("encoding", "utf-8");
133  document.append_node(declaration.node);
134  }
135 
137  createRootNode(const std::string& name)
138  {
139  RapidXmlWriterNode rootNode(&document, rapidxml::node_element, name);
140  document.append_node(rootNode.node);
141  return rootNode;
142  }
143 
144  std::string
145  print(bool indent)
146  {
147  std::string s;
149  std::back_inserter(s), document, indent ? 0 : rapidxml::print_no_indenting);
150  return s;
151  }
152 
153  void
154  saveToFile(const std::string& path, bool indent)
155  {
156  std::ofstream file;
157  file.open(path.c_str());
158  file << print(indent);
159  file.close();
160  }
161  };
162 } // namespace armarx
armarx::RapidXmlWriter::RapidXmlWriter
RapidXmlWriter()
Definition: RapidXmlWriter.h:128
armarx::RapidXmlWriterNode::append_data_node
RapidXmlWriterNode & append_data_node(const std::string &value)
Definition: RapidXmlWriter.h:99
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::RapidXmlWriter
Definition: RapidXmlWriter.h:122
rapidxml::print
OutIt print(OutIt out, const xml_node< Ch > &node, int flags=0)
Prints XML to given output iterator.
Definition: rapidxml_print.hpp:537
rapidxml::node_data
@ node_data
A data node. Name is empty. Value contains data text.
Definition: rapidxml.hpp:152
rapidxml::node_element
@ node_element
An element node. Name contains element name. Value contains text of first data node.
Definition: rapidxml.hpp:151
rapidxml::node_declaration
@ node_declaration
A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalon...
Definition: rapidxml.hpp:155
armarx::RapidXmlWriterNode::append_bool_attribute
RapidXmlWriterNode & append_bool_attribute(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool value)
Definition: RapidXmlWriter.h:67
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
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
rapidxml::print_no_indenting
const int print_no_indenting
Printer flag instructing the printer to suppress indenting of XML. See print() function.
Definition: rapidxml_print.hpp:24
armarx::RapidXmlWriterNode::append_attribute
RapidXmlWriterNode & append_attribute(const std::string &name, const std::string &value)
Definition: RapidXmlWriter.h:59
rapidxml::xml_document
This class represents root of the DOM hierarchy.
Definition: rapidxml.hpp:144
rapidxml::xml_node
Class representing a node of XML document.
Definition: rapidxml.hpp:140
rapidxml::node_type
node_type
Enumeration listing all node types produced by the parser.
Definition: rapidxml.hpp:148
rapidxml::xml_node::append_node
void append_node(xml_node< Ch > *child)
Appends a new child node.
Definition: rapidxml.hpp:1332
armarx::RapidXmlWriter::saveToFile
void saveToFile(const std::string &path, bool indent)
Definition: RapidXmlWriter.h:154
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::RapidXmlWriterNode::append_node
RapidXmlWriterNode append_node(const std::string &name)
Definition: RapidXmlWriter.h:91
rapidxml::xml_node::append_attribute
void append_attribute(xml_attribute< Ch > *attribute)
Appends a new attribute to the node.
Definition: rapidxml.hpp:1486
rapidxml.hpp
armarx::RapidXmlWriterNode
Definition: RapidXmlWriter.h:35
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
rapidxml_print.hpp