source: rrlib_xml/tXMLDocument.cpp @ 40:6c6d24f78cb0

Last change on this file since 40:6c6d24f78cb0 was 40:6c6d24f78cb0, checked in by Tobias Föhst <foehst@…>, 11 years ago

Reformatted with astyle 2.02.1

File size: 6.2 KB
Line 
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    tXMLDocument.cpp
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-06-24
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/xml2_wrapper/tXMLDocument.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35#include <iostream>
36
37//----------------------------------------------------------------------
38// Internal includes with ""
39//----------------------------------------------------------------------
40#include "rrlib/xml2_wrapper/tXML2WrapperException.h"
41#include "rrlib/xml2_wrapper/tCleanupHandler.h"
42
43//----------------------------------------------------------------------
44// Debugging
45//----------------------------------------------------------------------
46#include <cassert>
47
48//----------------------------------------------------------------------
49// Namespace usage
50//----------------------------------------------------------------------
51
52//----------------------------------------------------------------------
53// Namespace declaration
54//----------------------------------------------------------------------
55namespace rrlib
56{
57namespace xml2
58{
59
60//----------------------------------------------------------------------
61// Forward declarations / typedefs / enums
62//----------------------------------------------------------------------
63
64//----------------------------------------------------------------------
65// Const values
66//----------------------------------------------------------------------
67
68//----------------------------------------------------------------------
69// Implementation
70//----------------------------------------------------------------------
71
72//----------------------------------------------------------------------
73// tXMLDocument constructors
74//----------------------------------------------------------------------
75tXMLDocument::tXMLDocument()
76  : document(xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0"))),
77    root_node(0)
78{
79  assert(this->document);
80  tCleanupHandler::Instance();
81}
82
83tXMLDocument::tXMLDocument(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<tXMLNode *>(xmlDocGetRootElement(this->document)))
86{
87  assert(this->document);
88  if (!this->document)
89  {
90    throw tXML2WrapperException("Could not parse XML file `" + file_name + "'!");
91  }
92  tCleanupHandler::Instance();
93}
94
95tXMLDocument::tXMLDocument(const void *buffer, size_t size, bool validate)
96  : document(xmlReadMemory(reinterpret_cast<const char *>(buffer), size, "noname.xml", 0, validate ? XML_PARSE_DTDVALID : 0)),
97    root_node(reinterpret_cast<tXMLNode *>(xmlDocGetRootElement(this->document)))
98{
99  if (!this->document)
100  {
101    throw tXML2WrapperException("Could not parse XML from memory buffer `" + std::string(reinterpret_cast<const char *>(buffer)) + "'!");
102  }
103  tCleanupHandler::Instance();
104}
105
106tXMLDocument::tXMLDocument(tXMLDocument && other)
107  : document(0),
108    root_node(0)
109{
110  std::swap(document, other.document);
111  std::swap(root_node, other.root_node);
112}
113
114//----------------------------------------------------------------------
115// tXMLDocument destructor
116//----------------------------------------------------------------------
117tXMLDocument::~tXMLDocument()
118{
119  if (this->document)
120  {
121    xmlFreeDoc(this->document);
122  }
123}
124
125//----------------------------------------------------------------------
126// tXMLDocument operator =
127//----------------------------------------------------------------------
128tXMLDocument &tXMLDocument::operator = (const tXMLDocument & other)
129{
130  if (this == &other)
131  {
132    return *this;
133  }
134  xmlFreeDoc(this->document);
135  this->document = xmlCopyDoc(other.document, true);
136  this->root_node = reinterpret_cast<tXMLNode *>(xmlDocGetRootElement(this->document));
137  return *this;
138}
139
140//----------------------------------------------------------------------
141// tXMLDocument RootNode
142//----------------------------------------------------------------------
143tXMLNode &tXMLDocument::RootNode()
144{
145  if (!this->root_node)
146  {
147    throw tXML2WrapperException("No root node defined for this document!");
148  }
149  return *this->root_node;
150}
151
152//----------------------------------------------------------------------
153// tXMLDocument AddRootNode
154//----------------------------------------------------------------------
155tXMLNode &tXMLDocument::AddRootNode(const std::string &name)
156{
157  if (this->root_node)
158  {
159    throw tXML2WrapperException("Root node already exists with name `" + name + "'!");
160  }
161  this->root_node = reinterpret_cast<tXMLNode *>(xmlNewNode(0, reinterpret_cast<const xmlChar *>(name.c_str())));
162  xmlDocSetRootElement(this->document, this->root_node);
163  return *this->root_node;
164}
165
166//----------------------------------------------------------------------
167// tXMLDocument WriteToFile
168//----------------------------------------------------------------------
169void tXMLDocument::WriteToFile(const std::string &file_name, int compression) const
170{
171  if (compression)
172  {
173    xmlSetDocCompressMode(this->document, compression);
174  }
175  xmlSaveFormatFileEnc(file_name.c_str(), this->document, "UTF-8", 1);
176}
177
178//----------------------------------------------------------------------
179// End of namespace declaration
180//----------------------------------------------------------------------
181}
182}
Note: See TracBrowser for help on using the repository browser.