zenilib: zenilib/jni/external/zenilib/zeni/XML.cpp Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
XML.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2 * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3 *
4 * zenilib is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * zenilib is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <zeni.h>
19
20 #include <iostream>
21
22 #if defined(_DEBUG) && defined(_WINDOWS)
23 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
24 #define new DEBUG_NEW
25 #endif
26
27 namespace Zeni {
28
29 XML_Element_c::XML_Element_c(const TiXmlHandle &handle_)
30 : m_handle(handle_)
31 {
32 }
33
34 XML_Element_c XML_Element_c::operator[](const String &field) const {
35 if(!m_handle.ToNode()) {
36 std::cerr << "Bad XML_Element_c attempted to access field '" << field << "'\n";
37 throw XML_Element_Ungood();
38 }
39
40 TiXmlNode * node = child_element(field);
41 return XML_Element_c(node ? node->ToElement() : 0);
42 }
43
44 XML_Element_c XML_Element_c::first() const {
45 if(!m_handle.ToNode()) {
46 std::cerr << "Bad XML_Element_c attempted to access first field\n";
47 throw XML_Element_Ungood();
48 }
49
50 TiXmlNode * node = first_child_element();
51 return XML_Element_c(node ? node->ToElement() : 0);
52 }
53
54 XML_Element_c XML_Element_c::next() const {
55 if(!m_handle.ToNode()) {
56 std::cerr << "Bad XML_Element_c attempted to access next field\n";
57 throw XML_Element_Ungood();
58 }
59
60 TiXmlNode * node = m_handle.ToNode();
61 return XML_Element_c(node ? node->NextSiblingElement() : 0);
62 }
63
64 bool XML_Element_c::good() const {
65 return m_handle.ToElement() != 0;
66 }
67
68 String XML_Element_c::value() const {
69 if(!good()) {
70 std::cerr << "Bad XML_Element_c attempted to access value\n";
71 throw XML_Element_Ungood();
72 }
73
74 TiXmlElement * element = m_handle.ToElement();
75 if(!element) {
76 std::cerr << "XML_Element_c invalid\n";
77 throw XML_Element_Ungood();
78 }
79
80 return element->Value();
81 }
82
83 bool XML_Element_c::to_bool() const {
84 return to_int() != 0;
85 }
86
87 int XML_Element_c::to_int() const {
88 return atoi(to_string().c_str());
89 }
90
91 float XML_Element_c::to_float() const {
92 return float(atof(to_string().c_str()));
93 }
94
95 double XML_Element_c::to_double() const {
96 return atof(to_string().c_str());
97 }
98
99 String XML_Element_c::to_string() const {
100 if(!good()) {
101 std::cerr << "Bad XML_Element_c attempted to_string()\n";
102 throw XML_Element_Ungood();
103 }
104
105 TiXmlElement *element = m_handle.ToElement();
106 if(!element) {
107 std::cerr << "XML_Element_c invalid\n";
108 throw XML_Element_Ungood();
109 }
110
111 const char * text = element->GetText();
112 if(!text) {
113 std::cerr << "XML_Element_c attempted to_string() on non-leaf node\n";
114 throw XML_Element_Nonleaf();
115 }
116
117 return text;
118 }
119
120 TiXmlNode * XML_Element_c::child(const String &field) const {
121 TiXmlNode * node = m_handle.ToNode();
122 return node ? node->FirstChild(field.c_str()) : 0;
123 }
124
125 TiXmlNode * XML_Element_c::first_child() const {
126 TiXmlNode * node = m_handle.ToNode();
127 return node ? node->FirstChild() : 0;
128 }
129
130 TiXmlNode * XML_Element_c::child_element(const String &field) const {
131 TiXmlNode * node = m_handle.ToNode();
132 return node ? node->FirstChildElement(field.c_str()) : 0;
133 }
134
135 TiXmlNode * XML_Element_c::first_child_element() const {
136 TiXmlNode * node = m_handle.ToNode();
137 return node ? node->FirstChildElement() : 0;
138 }
139
140 XML_Element::XML_Element(const TiXmlHandle &handle_)
141 : XML_Element_c(handle_)
142 {
143 }
144
145 XML_Element XML_Element::operator[](const String &field) {
146 if(!m_handle.ToNode()) {
147 std::cerr << "Bad XML_Element_c attempted to access field '" << field << "'\n";
148 throw XML_Element_Ungood();
149 }
150
151 TiXmlNode * node = child_element(field);
152 return XML_Element(node ? node->ToElement() : 0);
153 }
154
155 XML_Element XML_Element::first() {
156 if(!m_handle.ToNode()) {
157 std::cerr << "Bad XML_Element_c attempted to access first field\n";
158 throw XML_Element_Ungood();
159 }
160
161 TiXmlNode * node = first_child_element();
162 return XML_Element(node ? node->ToElement() : 0);
163 }
164
165 XML_Element XML_Element::next() {
166 if(!m_handle.ToNode()) {
167 std::cerr << "Bad XML_Element_c attempted to access next field\n";
168 throw XML_Element_Ungood();
169 }
170
171 TiXmlNode * node = m_handle.ToNode();
172 return XML_Element(node ? node->NextSiblingElement() : 0);
173 }
174
175 void XML_Element::create_child(const String &field) {
176 if(!m_handle.ToNode()) {
177 std::cerr << "Bad XML_Element_c attempted to create_child(...)\n";
178 throw XML_Element_Ungood();
179 }
180
181 TiXmlElement * element = new TiXmlElement(field.c_str());
182 if(!m_handle.ToNode()->LinkEndChild(element)) {
183 std::cerr << "XML_Element_c failed to create a child\n";
184 throw XML_Create_Child_Failure();
185 }
186 }
187
188 void XML_Element::remove_child(const XML_Element &child) {
189 if(!good() || !child.good()) {
190 std::cerr << "Bad XML_Element_c attempted to remove_child(...)\n";
191 throw XML_Element_Ungood();
192 }
193
194 if(!m_handle.ToNode()->RemoveChild(child.m_handle.ToNode())) {
195 std::cerr << "XML_Element_c failed to remove a child\n";
196 throw XML_Remove_Child_Failure();
197 }
198 }
199
200 void XML_Element::set_bool(const bool &b) {
201 set_string(itoa(b));
202 }
203
204 void XML_Element::set_int(const int &i) {
205 set_string(itoa(i));
206 }
207
208 void XML_Element::set_float(const float &f) {
209 set_string(ftoa(f));
210 }
211
212 void XML_Element::set_double(const double &d) {
213 set_string(dtoa(d));
214 }
215
216 void XML_Element::set_string(const String &s) {
217 if(!good()) {
218 std::cerr << "Bad XML_Element_c attempted set_string(...)\n";
219 throw XML_Element_Ungood();
220 }
221
222 TiXmlNode * const node = first_child();
223
224 if(!node)
225 m_handle.ToNode()->LinkEndChild(new TiXmlText(s.c_str()));
226 else if(node->ToText()) {
227 TiXmlText replacement(s.c_str());
228 m_handle.ToNode()->ReplaceChild(node, replacement);
229 }
230 else {
231 std::cerr << "XML_Element_c attempted set_string(...) on non-leaf node\n";
232 throw XML_Element_Nonleaf();
233 }
234 }
235
236 XML_Document::XML_Document()
237 : m_root(0)
238 {
239 }
240
241 XML_Document::XML_Document(const String &filename)
242 : m_root(0)
243 {
244 load(filename);
245 }
246
247 XML_Document::XML_Document(const String &filename, const String &backup)
248 : m_root(0)
249 {
250 if(!try_load(filename))
251 load(backup);
252 }
253
254 XML_Document::~XML_Document() {
255 delete m_root;
256 }
257
258 bool XML_Document::good() const {
259 return m_root != 0;
260 }
261
262 void XML_Document::load_mem(const String &data) {
263 if(!try_load_mem(data)) {
264 std::cerr << "Failed to load XML_Document from memory\n"
265 << m_xml_file.ErrorDesc() << std::endl;
266 throw XML_Load_Failure();
267 }
268 }
269
270 void XML_Document::load(const String &filename) {
271 if(!try_load(filename)) {
272 std::cerr << "Failed to load XML_Document '" << filename << "'\n"
273 << m_xml_file.ErrorDesc() << std::endl;
274 throw XML_Load_Failure();
275 }
276 }
277
278 void XML_Document::save() {
279 if(!try_save()) {
280 std::cerr << "Failed to save XML_Document" << std::endl
281 << m_xml_file.ErrorDesc() << std::endl;
282 throw XML_Save_Failure();
283 }
284 }
285
286 void XML_Document::save(const String &filename) {
287 if(!try_save(filename)) {
288 std::cerr << "Failed to save XML_Document '" << filename << "'\n"
289 << m_xml_file.ErrorDesc() << std::endl;
290 throw XML_Save_Failure();
291 }
292 }
293
294 bool XML_Document::try_load_mem(const String &data) {
295 TiXmlDocument next;
296
297 if(!next.Parse(data.c_str())) {
298 ZENI_LOGE("Loading XML data failed.");
299 return false;
300 }
301 else {
302 delete m_root;
303 m_root = 0;
304
305 m_xml_file = next;
306
307 TiXmlHandle root = &m_xml_file;
308 m_root = new XML_Element(root);
309
310 return true;
311 }
312 }
313
314 bool XML_Document::try_load(const String &filename) {
315 TiXmlDocument next;
316
317 if(!next.LoadFile(filename.c_str()))
318 return false;
319 else {
320 delete m_root;
321 m_root = 0;
322
323 m_xml_file = next;
324
325 TiXmlHandle root = &m_xml_file;
326 m_root = new XML_Element(root);
327
328 return true;
329 }
330 }
331
332 bool XML_Document::try_save() {
333 if(!good()) {
334 std::cerr << "Bad XML_Document attempted to save\n";
335 throw XML_Document_Ungood();
336 }
337
338 return m_xml_file.SaveFile();
339 }
340
341 bool XML_Document::try_save(const String &filename) {
342 if(!good()) {
343 std::cerr << "Bad XML_Document attempted to save to '" << filename << "'\n";
344 throw XML_Document_Ungood();
345 }
346
347 return m_xml_file.SaveFile(filename.c_str());
348 }
349
350 XML_Element_c XML_Document::operator[](const String &field) const {
351 if(!good()) {
352 std::cerr << "Bad XML_Document attempted to access field '" << field << "'\n";
353 throw XML_Document_Ungood();
354 }
355
356 return (*m_root)[field];
357 }
358
359 XML_Element XML_Document::operator[](const String &field) {
360 if(!good()) {
361 std::cerr << "Bad XML_Document attempted to access field '" << field << "'\n";
362 throw XML_Document_Ungood();
363 }
364
365 return (*m_root)[field];
366 }
367
368 XML_Element_c XML_Document::first() const {
369 if(!good()) {
370 std::cerr << "Bad XML_Document attempted to access first field\n";
371 throw XML_Document_Ungood();
372 }
373
374 return m_root->first();
375 }
376
377 XML_Element XML_Document::first() {
378 if(!good()) {
379 std::cerr << "Bad XML_Document attempted to access first field\n";
380 throw XML_Document_Ungood();
381 }
382
383 return m_root->first();
384 }
385
386 void XML_Document::create_root(const String &field) {
387 delete m_root;
388 m_root = 0;
389
390 m_xml_file = TiXmlDocument();
391
392 m_xml_file.LinkEndChild(new TiXmlDeclaration("1.0", "", ""));
393 m_xml_file.LinkEndChild(new TiXmlElement(field.c_str()));
394
395 TiXmlHandle root = &m_xml_file;
396 m_root = new XML_Element(root);
397 }
398
399 }
Zeni::XML_Element::operator[]
XML_Element operator[](const String &field)
Get a named subelement.
Definition: XML.cpp:145
Zeni::XML_Element_c::child
TiXmlNode * child(const String &field) const
Definition: XML.cpp:120
TiXmlDocument
Definition: tinyxml.h:1386
Zeni::XML_Element_c::to_string
String to_string() const
Get the contained string.
Definition: XML.cpp:99
s
GLdouble s
Definition: glew.h:1376
TiXmlNode::LinkEndChild
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Definition: tinyxml.cpp:186
Zeni::XML_Element_c::m_handle
TiXmlHandle m_handle
Definition: XML.h:83
Zeni::XML_Element::remove_child
void remove_child(const XML_Element &child)
Remove a child node, which will be rendered invalid.
Definition: XML.cpp:188
Zeni::XML_Element::set_float
void set_float(const float &f)
Set the contained string as a floating point number.
Definition: XML.cpp:208
f
GLclampf f
Definition: glew.h:3390
Zeni::XML_Save_Failure
Definition: XML.h:155
Zeni::XML_Element_c::XML_Element_c
XML_Element_c(const TiXmlHandle &handle)
Definition: XML.cpp:29
Zeni::XML_Document::try_load
bool try_load(const String &filename)
Definition: XML.cpp:314
TiXmlHandle
Definition: tinyxml.h:1631
TiXmlDocument::SaveFile
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
Definition: tinyxml.cpp:915
Zeni::XML_Element::create_child
void create_child(const String &field)
Create a child node.
Definition: XML.cpp:175
TiXmlNode::RemoveChild
bool RemoveChild(TiXmlNode *removeThis)
Delete a child of this node.
Definition: tinyxml.cpp:331
Zeni::XML_Element::first
XML_Element first()
Get the first subelement.
Definition: XML.cpp:155
Zeni::String::c_str
const char * c_str() const
Definition: String.cpp:467
Zeni::dtoa
String dtoa(const double &number)
Definition: Serialization.cpp:174
Zeni::XML_Document::load_mem
void load_mem(const String &data)
Definition: XML.cpp:262
Zeni::XML_Document::save
void save()
Definition: XML.cpp:278
Zeni::XML_Create_Child_Failure
Definition: XML.h:171
d
return Display return Display Bool Bool int d
Definition: SDL_x11sym.h:30
Zeni::XML_Document::create_root
void create_root(const String &field)
(Re)Initializes the document with a given root field
Definition: XML.cpp:386
Zeni::XML_Element_c::to_float
float to_float() const
Get the contained string as a floating point number.
Definition: XML.cpp:91
TiXmlNode::Value
const char * Value() const
Definition: tinyxml.h:487
Zeni::XML_Element_Nonleaf
Definition: XML.h:167
TiXmlHandle::ToElement
TiXmlElement * ToElement() const
Definition: tinyxml.h:1681
Zeni::XML_Document_Ungood
Definition: XML.h:159
Zeni::XML_Element::set_string
void set_string(const String &s)
Set the contained string.
Definition: XML.cpp:216
Zeni::String
Definition: String.h:29
TiXmlNode
Definition: tinyxml.h:423
Zeni::XML_Element_c::to_bool
bool to_bool() const
Get the contained string as a boolean.
Definition: XML.cpp:83
Zeni::XML_Document::try_load_mem
bool try_load_mem(const String &data)
Definition: XML.cpp:294
TiXmlNode::ReplaceChild
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
Definition: tinyxml.cpp:292
Zeni::ftoa
String ftoa(const float &number)
Definition: Serialization.cpp:157
TiXmlDocument::Parse
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Definition: tinyxmlparser.cpp:704
Zeni::XML_Element_c::first_child_element
TiXmlNode * first_child_element() const
Definition: XML.cpp:135
Zeni::XML_Element_c::to_double
double to_double() const
Get the contained string as a double precision floating point number.
Definition: XML.cpp:95
Zeni::XML_Remove_Child_Failure
Definition: XML.h:175
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
Zeni::XML_Element_c::operator[]
XML_Element_c operator[](const String &field) const
Get a named subelement.
Definition: XML.cpp:34
Zeni::XML_Element_c::first_child
TiXmlNode * first_child() const
Definition: XML.cpp:125
Zeni::XML_Load_Failure
Definition: XML.h:151
sort_controllers.filename
string filename
Definition: sort_controllers.py:8
TiXmlDeclaration
Definition: tinyxml.h:1278
TiXmlNode::NextSiblingElement
const TiXmlElement * NextSiblingElement() const
Definition: tinyxml.cpp:478
Zeni::itoa
String itoa(const int &number)
Definition: Serialization.cpp:98
Zeni::XML_Element::set_bool
void set_bool(const bool &b)
Set the contained string as a boolean.
Definition: XML.cpp:200
Zeni::XML_Document::operator[]
XML_Element_c operator[](const String &field) const
Get a named subelement.
Definition: XML.cpp:350
Zeni::XML_Document::load
void load(const String &filename)
Definition: XML.cpp:270
Zeni::XML_Document::XML_Document
XML_Document()
Definition: XML.cpp:236
Zeni::XML_Document::good
bool good() const
Definition: XML.cpp:258
ZENI_LOGE
void ZENI_LOGE(const Zeni::String &str)
Definition: Android.h:59
TiXmlNode::ToText
virtual const TiXmlText * ToText() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:701
Zeni::XML_Document::~XML_Document
~XML_Document()
Definition: XML.cpp:254
Zeni::XML_Element
A node in an XML tree, possibly containing useful data.
Definition: XML.h:86
TiXmlNode::ToElement
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:698
Zeni::XML_Element::next
XML_Element next()
Get the next subelement.
Definition: XML.cpp:165
Zeni::XML_Document::first
XML_Element_c first() const
Get the first subelement.
Definition: XML.cpp:368
TiXmlText
Definition: tinyxml.h:1205
Zeni::XML_Element_Ungood
Definition: XML.h:163
TiXmlElement::GetText
const char * GetText() const
Definition: tinyxml.cpp:856
Zeni::XML_Element::set_double
void set_double(const double &d)
Set the contained string as a double precision floating point number.
Definition: XML.cpp:212
Zeni::XML_Element_c
Definition: XML.h:53
TiXmlElement
Definition: tinyxml.h:940
Zeni::XML_Element_c::value
String value() const
Get the value of the element (For nodes, this is the name)
Definition: XML.cpp:68
TiXmlDocument::LoadFile
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Definition: tinyxml.cpp:909
TiXmlDocument::ErrorDesc
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
Definition: tinyxml.h:1456
Zeni::XML_Element_c::child_element
TiXmlNode * child_element(const String &field) const
Definition: XML.cpp:130
b
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
Zeni::XML_Element_c::to_int
int to_int() const
Get the contained string as an integer.
Definition: XML.cpp:87
Zeni::XML_Element_c::next
XML_Element_c next() const
Get the next subelement.
Definition: XML.cpp:54
Zeni::XML_Element::set_int
void set_int(const int &i)
Set the contained string as an integer.
Definition: XML.cpp:204
zeni.h
Zeni::XML_Element::XML_Element
XML_Element(const TiXmlHandle &handle)
Definition: XML.cpp:140
i
int i
Definition: pngrutil.c:1377
Zeni::XML_Document::try_save
bool try_save()
Definition: XML.cpp:332
TiXmlNode::FirstChild
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:522
Zeni::XML_Element_c::first
XML_Element_c first() const
Get the first subelement.
Definition: XML.cpp:44
TiXmlHandle::ToNode
TiXmlNode * ToNode() const
Definition: tinyxml.h:1678
TiXmlNode::FirstChildElement
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:448
Zeni::XML_Element_c::good
bool good() const
Test to see if the Element is valid.
Definition: XML.cpp:64