1 | // |
---|
2 | // You received this file as part of RRLib |
---|
3 | // Robotics Research Library |
---|
4 | // |
---|
5 | // Copyright (C) Finroc GbR (finroc.org) |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or |
---|
8 | // modify it under the terms of the GNU General Public License |
---|
9 | // as published by the Free Software Foundation; either version 2 |
---|
10 | // of the License, or (at your option) any later version. |
---|
11 | // |
---|
12 | // This program is distributed in the hope that it will be useful, |
---|
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | // GNU General Public License for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License |
---|
18 | // along with this program; if not, write to the Free Software |
---|
19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | // |
---|
21 | //---------------------------------------------------------------------- |
---|
22 | /*!\file rrlib/xml/tDocument.cpp |
---|
23 | * |
---|
24 | * \author Tobias Foehst |
---|
25 | * |
---|
26 | * \date 2010-06-24 |
---|
27 | * |
---|
28 | */ |
---|
29 | //---------------------------------------------------------------------- |
---|
30 | #include "rrlib/xml/tDocument.h" |
---|
31 | |
---|
32 | //---------------------------------------------------------------------- |
---|
33 | // External includes (system with <>, local with "") |
---|
34 | //---------------------------------------------------------------------- |
---|
35 | #include <iostream> |
---|
36 | |
---|
37 | //---------------------------------------------------------------------- |
---|
38 | // Internal includes with "" |
---|
39 | //---------------------------------------------------------------------- |
---|
40 | #include "rrlib/xml/tException.h" |
---|
41 | #include "rrlib/xml/tCleanupHandler.h" |
---|
42 | |
---|
43 | //---------------------------------------------------------------------- |
---|
44 | // Debugging |
---|
45 | //---------------------------------------------------------------------- |
---|
46 | #include <cassert> |
---|
47 | |
---|
48 | //---------------------------------------------------------------------- |
---|
49 | // Namespace usage |
---|
50 | //---------------------------------------------------------------------- |
---|
51 | |
---|
52 | //---------------------------------------------------------------------- |
---|
53 | // Namespace declaration |
---|
54 | //---------------------------------------------------------------------- |
---|
55 | namespace rrlib |
---|
56 | { |
---|
57 | namespace xml |
---|
58 | { |
---|
59 | |
---|
60 | //---------------------------------------------------------------------- |
---|
61 | // Forward declarations / typedefs / enums |
---|
62 | //---------------------------------------------------------------------- |
---|
63 | |
---|
64 | //---------------------------------------------------------------------- |
---|
65 | // Const values |
---|
66 | //---------------------------------------------------------------------- |
---|
67 | |
---|
68 | //---------------------------------------------------------------------- |
---|
69 | // Implementation |
---|
70 | //---------------------------------------------------------------------- |
---|
71 | |
---|
72 | //---------------------------------------------------------------------- |
---|
73 | // tDocument constructors |
---|
74 | //---------------------------------------------------------------------- |
---|
75 | tDocument::tDocument() |
---|
76 | : document(xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0"))), |
---|
77 | root_node(0) |
---|
78 | { |
---|
79 | assert(this->document); |
---|
80 | tCleanupHandler::Instance(); |
---|
81 | } |
---|
82 | |
---|
83 | tDocument::tDocument(const std::string &file_name, bool validate) |
---|
84 | : document(xmlReadFile(file_name.c_str(), 0, validate ? XML_PARSE_DTDVALID : 0)), |
---|
85 | root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) |
---|
86 | { |
---|
87 | assert(this->document); |
---|
88 | this->CheckIfDocumentIsValid("Could not parse XML file `" + file_name + "'!"); |
---|
89 | tCleanupHandler::Instance(); |
---|
90 | } |
---|
91 | |
---|
92 | tDocument::tDocument(const std::string &file_name, const std::string &encoding, bool validate) |
---|
93 | : document(xmlReadFile(file_name.c_str(), encoding.c_str(), validate ? XML_PARSE_DTDVALID : 0)), |
---|
94 | root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) |
---|
95 | { |
---|
96 | assert(this->document); |
---|
97 | this->CheckIfDocumentIsValid("Could not parse XML file `" + file_name + "'!"); |
---|
98 | tCleanupHandler::Instance(); |
---|
99 | } |
---|
100 | |
---|
101 | tDocument::tDocument(const void *buffer, size_t size, bool validate) |
---|
102 | : document(xmlReadMemory(reinterpret_cast<const char *>(buffer), size, "noname.xml", 0, validate ? XML_PARSE_DTDVALID : 0)), |
---|
103 | root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) |
---|
104 | { |
---|
105 | this->CheckIfDocumentIsValid("Could not parse XML from memory buffer `" + std::string(reinterpret_cast<const char *>(buffer)) + "'!"); |
---|
106 | tCleanupHandler::Instance(); |
---|
107 | } |
---|
108 | |
---|
109 | tDocument::tDocument(const void *buffer, size_t size, const std::string &encoding, bool validate) |
---|
110 | : document(xmlReadMemory(reinterpret_cast<const char *>(buffer), size, "noname.xml", encoding.c_str(), validate ? XML_PARSE_DTDVALID : 0)), |
---|
111 | root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) |
---|
112 | { |
---|
113 | this->CheckIfDocumentIsValid("Could not parse XML from memory buffer `" + std::string(reinterpret_cast<const char *>(buffer)) + "'!"); |
---|
114 | tCleanupHandler::Instance(); |
---|
115 | } |
---|
116 | |
---|
117 | tDocument::tDocument(tDocument && other) |
---|
118 | : document(0), |
---|
119 | root_node(0) |
---|
120 | { |
---|
121 | std::swap(document, other.document); |
---|
122 | std::swap(root_node, other.root_node); |
---|
123 | } |
---|
124 | |
---|
125 | //---------------------------------------------------------------------- |
---|
126 | // tDocument destructor |
---|
127 | //---------------------------------------------------------------------- |
---|
128 | tDocument::~tDocument() |
---|
129 | { |
---|
130 | if (this->document) |
---|
131 | { |
---|
132 | xmlFreeDoc(this->document); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | //---------------------------------------------------------------------- |
---|
137 | // tDocument operator = |
---|
138 | //---------------------------------------------------------------------- |
---|
139 | tDocument &tDocument::operator = (const tDocument & other) |
---|
140 | { |
---|
141 | if (this == &other) |
---|
142 | { |
---|
143 | return *this; |
---|
144 | } |
---|
145 | xmlFreeDoc(this->document); |
---|
146 | this->document = xmlCopyDoc(other.document, true); |
---|
147 | this->root_node = reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document)); |
---|
148 | return *this; |
---|
149 | } |
---|
150 | |
---|
151 | //---------------------------------------------------------------------- |
---|
152 | // tDocument RootNode |
---|
153 | //---------------------------------------------------------------------- |
---|
154 | tNode &tDocument::RootNode() |
---|
155 | { |
---|
156 | if (!this->root_node) |
---|
157 | { |
---|
158 | throw tException("No root node defined for this document!"); |
---|
159 | } |
---|
160 | return *this->root_node; |
---|
161 | } |
---|
162 | |
---|
163 | //---------------------------------------------------------------------- |
---|
164 | // tDocument AddRootNode |
---|
165 | //---------------------------------------------------------------------- |
---|
166 | tNode &tDocument::AddRootNode(const std::string &name) |
---|
167 | { |
---|
168 | if (this->root_node) |
---|
169 | { |
---|
170 | throw tException("Root node already exists with name `" + name + "'!"); |
---|
171 | } |
---|
172 | this->root_node = reinterpret_cast<tNode *>(xmlNewNode(0, reinterpret_cast<const xmlChar *>(name.c_str()))); |
---|
173 | xmlDocSetRootElement(this->document, this->root_node); |
---|
174 | return *this->root_node; |
---|
175 | } |
---|
176 | |
---|
177 | //---------------------------------------------------------------------- |
---|
178 | // tDocument WriteToFile |
---|
179 | //---------------------------------------------------------------------- |
---|
180 | void tDocument::WriteToFile(const std::string &file_name, int compression) const |
---|
181 | { |
---|
182 | if (compression) |
---|
183 | { |
---|
184 | xmlSetDocCompressMode(this->document, compression); |
---|
185 | } |
---|
186 | xmlSaveFormatFileEnc(file_name.c_str(), this->document, "UTF-8", 1); |
---|
187 | } |
---|
188 | |
---|
189 | //---------------------------------------------------------------------- |
---|
190 | // tDocument CheckIfDocumentIsValid |
---|
191 | //---------------------------------------------------------------------- |
---|
192 | void tDocument::CheckIfDocumentIsValid(const std::string &exception_message) |
---|
193 | { |
---|
194 | if (!this->document) |
---|
195 | { |
---|
196 | throw tException(exception_message); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | //---------------------------------------------------------------------- |
---|
201 | // End of namespace declaration |
---|
202 | //---------------------------------------------------------------------- |
---|
203 | } |
---|
204 | } |
---|