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 
28 #include <string>
29 #include <fstream>
30 
31 namespace armarx
32 {
33 
35  {
36  friend class RapidXmlWriter;
37 
38  private:
39  rapidxml::xml_document<>* document;
41 
43  {
44  this->document = document;
45  node = document->allocate_node(node_type);
46  }
48  {
49  this->document = document;
50  node = document->allocate_node(node_type, cloneString(name));
51  }
52 
53  public:
54  RapidXmlWriterNode& append_attribute(const std::string& name, const std::string& value)
55  {
56  node->append_attribute(document->allocate_attribute(cloneString(name), cloneString(value)));
57  return *this;
58  }
59  RapidXmlWriterNode& append_bool_attribute(const std::string& name, const std::string& trueValue, const std::string& falseValue, bool value)
60  {
61  append_attribute(name, value ? trueValue : falseValue);
62  return *this;
63  }
64  RapidXmlWriterNode& append_optional_bool_attribute(const std::string& name, const std::string& trueValue, const std::string& falseValue, bool value, bool defaultValue)
65  {
66  if (value != defaultValue)
67  {
68  append_attribute(name, value ? trueValue : falseValue);
69  }
70  return *this;
71  }
72 
73  RapidXmlWriterNode append_node(const std::string& name)
74  {
75  RapidXmlWriterNode node(document, rapidxml::node_element, name);
76  this->node->append_node(node.node);
77  return node;
78  }
80  {
81  this->node->append_node(document->allocate_node(rapidxml::node_data, nullptr, cloneString(value)));
82  return *this;
83  }
84 
85  RapidXmlWriterNode& append_string_node(const std::string& name, const std::string& value)
86  {
87  this->node->append_node(document->allocate_node(rapidxml::node_element, cloneString(name), cloneString(value)));
88  return *this;
89  }
90 
91  private:
92  const char* cloneString(const std::string& str)
93  {
94  return document->allocate_string(str.c_str());
95  }
96 
97  };
98 
100  {
101  private:
102  rapidxml::xml_document<> document;
103 
104  public:
106  {
107  RapidXmlWriterNode declaration(&document, rapidxml::node_declaration);
108  declaration.append_attribute("version", "1.0");
109  declaration.append_attribute("encoding", "utf-8");
110  document.append_node(declaration.node);
111  }
112 
113  RapidXmlWriterNode createRootNode(const std::string& name)
114  {
115  RapidXmlWriterNode rootNode(&document, rapidxml::node_element, name);
116  document.append_node(rootNode.node);
117  return rootNode;
118  }
119 
120  std::string print(bool indent)
121  {
122  std::string s;
123  rapidxml::print(std::back_inserter(s), document, indent ? 0 : rapidxml::print_no_indenting);
124  return s;
125  }
126  void saveToFile(const std::string& path, bool indent)
127  {
128  std::ofstream file;
129  file.open(path.c_str());
130  file << print(indent);
131  file.close();
132  }
133 
134 
135  };
136 }
137 
armarx::RapidXmlWriter::RapidXmlWriter
RapidXmlWriter()
Definition: RapidXmlWriter.h:105
armarx::RapidXmlWriterNode::append_data_node
RapidXmlWriterNode & append_data_node(const std::string &value)
Definition: RapidXmlWriter.h:79
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::RapidXmlWriter
Definition: RapidXmlWriter.h:99
rapidxml::print
OutIt print(OutIt out, const xml_node< Ch > &node, int flags=0)
Prints XML to given output iterator.
Definition: rapidxml_print.hpp:507
rapidxml::node_data
@ node_data
A data node. Name is empty. Value contains data text.
Definition: rapidxml.hpp:148
rapidxml::node_element
@ node_element
An element node. Name contains element name. Value contains text of first data node.
Definition: rapidxml.hpp:147
rapidxml::node_declaration
@ node_declaration
A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalon...
Definition: rapidxml.hpp:151
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:59
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
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
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:54
rapidxml::xml_document
This class represents root of the DOM hierarchy.
Definition: rapidxml.hpp:140
rapidxml::xml_node
Class representing a node of XML document.
Definition: rapidxml.hpp:138
rapidxml::node_type
node_type
Enumeration listing all node types produced by the parser.
Definition: rapidxml.hpp:144
rapidxml::xml_node::append_node
void append_node(xml_node< Ch > *child)
Appends a new child node.
Definition: rapidxml.hpp:1241
armarx::RapidXmlWriter::saveToFile
void saveToFile(const std::string &path, bool indent)
Definition: RapidXmlWriter.h:126
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::RapidXmlWriterNode::append_node
RapidXmlWriterNode append_node(const std::string &name)
Definition: RapidXmlWriter.h:73
rapidxml::xml_node::append_attribute
void append_attribute(xml_attribute< Ch > *attribute)
Appends a new attribute to the node.
Definition: rapidxml.hpp:1388
rapidxml.hpp
armarx::RapidXmlWriterNode
Definition: RapidXmlWriter.h:34
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:28
rapidxml_print.hpp