Robotics Library  0.7.0
Document.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_XML_DOCUMENT_H
28 #define RL_XML_DOCUMENT_H
29 
30 #include <memory>
31 #include <string>
32 #include <boost/shared_array.hpp>
33 #include <libxml/parser.h>
34 #include <libxml/xinclude.h>
35 
36 #include "Exception.h"
37 #include "Node.h"
38 
39 namespace rl
40 {
44  namespace xml
45  {
46  class Document
47  {
48  public:
49  explicit Document(const ::std::string& version = "1.0") :
50  doc(::xmlNewDoc(reinterpret_cast<const ::xmlChar*>(version.c_str())), ::xmlFreeDoc)
51  {
52  }
53 
54  explicit Document(::xmlDocPtr doc) :
55  doc(doc, ::xmlFreeDoc)
56  {
57  }
58 
59  Document(Document&& document) :
60  doc(::std::move(document.doc))
61  {
62  }
63 
65  {
66  }
67 
68  ::std::string dumpFormatMemory(const bool& format) const
69  {
70  ::xmlChar* mem;
71  int size;
72 
73  ::xmlDocDumpFormatMemory(this->doc.get(), &mem, &size, format ? 1 : 0);
74  ::boost::shared_array< ::xmlChar> memory(mem, ::xmlFree);
75 
76  return nullptr != memory.get() ? reinterpret_cast<const char*>(memory.get()) : ::std::string();
77  }
78 
79  ::std::string dumpMemory() const
80  {
81  ::xmlChar* mem;
82  int size;
83 
84  ::xmlDocDumpMemory(this->doc.get(), &mem, &size);
85  ::boost::shared_array< ::xmlChar> memory(mem, ::xmlFree);
86 
87  return nullptr != memory.get() ? reinterpret_cast<const char*>(memory.get()) : ::std::string();
88  }
89 
90  ::xmlDocPtr get() const
91  {
92  return this->doc.get();
93  }
94 
95  int getCompression() const
96  {
97  return this->doc->compression;
98  }
99 
100  ::std::string getEncoding() const
101  {
102  return nullptr != this->doc->encoding ? reinterpret_cast<const char*>(this->doc->encoding) : ::std::string();
103  }
104 
105  ::std::string getName() const
106  {
107  return nullptr != this->doc->name ? this->doc->name : ::std::string();
108  }
109 
110  int getProperties() const
111  {
112  return this->doc->properties;
113  }
114 
116  {
117  return Node(::xmlDocGetRootElement(this->doc.get()));
118  }
119 
120  ::std::string getVersion() const
121  {
122  return nullptr != this->doc->version ? reinterpret_cast<const char*>(this->doc->version) : ::std::string();
123  }
124 
125  bool isStandalone() const
126  {
127  return 1 == this->doc->standalone ? true : false;
128  }
129 
131  {
132  this->doc = std::move(other.doc);
133  return *this;
134  }
135 
136  ::xmlDoc& operator*() const
137  {
138  return *this->doc;
139  }
140 
141  ::xmlDocPtr release()
142  {
143  return this->doc.release();
144  }
145 
146  void save(const ::std::string& filename, const bool& format = true) const
147  {
148  ::xmlSaveFormatFile(filename.c_str(), this->doc.get(), format ? 1 : 0);
149  }
150 
151  void save(const ::std::string& filename, const ::std::string& encoding, const bool& format = true) const
152  {
153  ::xmlSaveFormatFileEnc(filename.c_str(), this->doc.get(), encoding.c_str(), format ? 1 : 0);
154  }
155 
156  void setRootElement(const Node& node)
157  {
158  ::xmlDocSetRootElement(this->doc.get(), node.get());
159  }
160 
161  int substitute(const int& flags = 0)
162  {
163  int substitutions = ::xmlXIncludeProcessFlags(this->doc.get(), flags);
164 
165  if (-1 == substitutions)
166  {
167  throw Exception(::xmlGetLastError()->message);
168  }
169 
170  return substitutions;
171  }
172 
173  protected:
174 
175  private:
176  ::std::unique_ptr< ::xmlDoc, decltype(&::xmlFreeDoc)> doc;
177  };
178  }
179 }
180 
181 #endif // RL_XML_DOCUMENT_H
rl::xml::Document::operator=
Document & operator=(Document &&other)
Definition: Document.h:130
rl::xml::Document::Document
Document(const ::std::string &version="1.0")
Definition: Document.h:49
rl::xml::Document::getVersion
::std::string getVersion() const
Definition: Document.h:120
rl::xml::Document::doc
::std::unique_ptr< ::xmlDoc, decltype(&::xmlFreeDoc)> doc
Definition: Document.h:176
rl::xml::Document::operator*
::xmlDoc & operator*() const
Definition: Document.h:136
rl::xml::Document::getName
::std::string getName() const
Definition: Document.h:105
rl::xml::Document::isStandalone
bool isStandalone() const
Definition: Document.h:125
rl::xml::Document::dumpMemory
::std::string dumpMemory() const
Definition: Document.h:79
rl::xml::Document
Definition: Document.h:47
rl::xml::Document::Document
Document(::xmlDocPtr doc)
Definition: Document.h:54
rl::xml::Document::getEncoding
::std::string getEncoding() const
Definition: Document.h:100
rl::xml::Node::get
::xmlNodePtr get() const
Definition: Node.h:96
rl::xml::Document::get
::xmlDocPtr get() const
Definition: Document.h:90
rl::xml::Node
Definition: Node.h:46
rl::xml::Document::Document
Document(Document &&document)
Definition: Document.h:59
Node.h
std
Definition: thread.h:482
rl::xml::Document::getCompression
int getCompression() const
Definition: Document.h:95
rl::xml::Document::getProperties
int getProperties() const
Definition: Document.h:110
rl::xml::Document::substitute
int substitute(const int &flags=0)
Definition: Document.h:161
rl::xml::Document::save
void save(const ::std::string &filename, const ::std::string &encoding, const bool &format=true) const
Definition: Document.h:151
rl::xml::Document::dumpFormatMemory
::std::string dumpFormatMemory(const bool &format) const
Definition: Document.h:68
rl::xml::Document::~Document
~Document()
Definition: Document.h:64
rl::xml::Document::release
::xmlDocPtr release()
Definition: Document.h:141
rl::xml::Document::save
void save(const ::std::string &filename, const bool &format=true) const
Definition: Document.h:146
Exception.h
rl::xml::Document::getRootElement
Node getRootElement() const
Definition: Document.h:115
rl::xml::Exception
Definition: Exception.h:37
rl::xml::Document::setRootElement
void setRootElement(const Node &node)
Definition: Document.h:156
rl
Robotics Library.
Definition: AnalogInput.cpp:30