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
32namespace armarx
33{
34
35 class RapidXmlWriterNode
36 {
37 friend class RapidXmlWriter;
38
39 private:
42
43 RapidXmlWriterNode(rapidxml::xml_document<>* document, rapidxml::node_type node_type)
44 {
45 this->document = document;
46 node = document->allocate_node(node_type);
47 }
48
50 rapidxml::node_type node_type,
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:
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
std::string str(const T &t)
RapidXmlWriterNode & append_bool_attribute(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool value)
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)
void saveToFile(const std::string &path, bool indent)
RapidXmlWriterNode createRootNode(const std::string &name)
Ch * allocate_string(const Ch *source=0, std::size_t size=0)
Allocates a char array of given size from the pool, and optionally copies a given string to it.
Definition rapidxml.hpp:535
xml_node< Ch > * allocate_node(node_type type, const Ch *name=nullptr, const Ch *value=nullptr, std::size_t name_size=0, std::size_t value_size=0)
Allocates a new node from the pool, and optionally assigns name and value to it.
Definition rapidxml.hpp:446
This class represents root of the DOM hierarchy.
Class representing a node of XML document.
void append_node(xml_node< Ch > *child)
Appends a new child node.
This file offers overloads of toIce() and fromIce() functions for STL container types.
OutIt print(OutIt out, const xml_node< Ch > &node, int flags=0)
Prints XML to given output iterator.
node_type
Enumeration listing all node types produced by the parser.
Definition rapidxml.hpp:149
@ node_data
A data node. Name is empty. Value contains data text.
Definition rapidxml.hpp:152
@ node_element
An element node. Name contains element name. Value contains text of first data node.
Definition rapidxml.hpp:151
@ node_declaration
A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalon...
Definition rapidxml.hpp:155
const int print_no_indenting
Printer flag instructing the printer to suppress indenting of XML. See print() function.
This file contains rapidxml parser and DOM implementation.
This file contains rapidxml printer implementation.