Robotics Library  0.6.2
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 <string>
31 #include <boost/shared_array.hpp>
32 #include <boost/shared_ptr.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 {
41  namespace xml
42  {
43  class Document
44  {
45  public:
46  Document(xmlDocPtr doc) :
47  doc(doc, xmlFreeDoc)
48  {
49  }
50 
51  virtual ~Document()
52  {
53  }
54 
55  ::std::string dumpFormatMemory(const bool& format) const
56  {
57  xmlChar* mem;
58  int size;
59 
60  xmlDocDumpFormatMemory(this->doc.get(), &mem, &size, format ? 1 : 0);
61  ::boost::shared_array< xmlChar > memory(mem, xmlFree);
62 
63  return reinterpret_cast< const char* >(memory.get());
64  }
65 
66  ::std::string dumpMemory() const
67  {
68  xmlChar* mem;
69  int size;
70 
71  xmlDocDumpMemory(this->doc.get(), &mem, &size);
72  ::boost::shared_array< xmlChar > memory(mem, xmlFree);
73 
74  return reinterpret_cast< const char* >(memory.get());
75  }
76 
77  ::std::string getEncoding() const
78  {
79  return reinterpret_cast< const char* >(this->doc->encoding);
80  }
81 
83  {
84  return xmlDocGetRootElement(this->doc.get());
85  }
86 
87  ::std::string getVersion() const
88  {
89  return reinterpret_cast< const char* >(this->doc->version);
90  }
91 
92  xmlDocPtr operator()() const
93  {
94  return this->doc.get();
95  }
96 
97  void save(const ::std::string& filename, const bool& format = true) const
98  {
99  xmlSaveFormatFile(filename.c_str(), this->doc.get(), format ? 1 : 0);
100  }
101 
102  void save(const ::std::string& filename, const ::std::string& encoding, const bool& format = true) const
103  {
104  xmlSaveFormatFileEnc(filename.c_str(), this->doc.get(), encoding.c_str(), format ? 1 : 0);
105  }
106 
107  void setRootElement(const Node& node)
108  {
109  xmlDocSetRootElement(this->doc.get(), node());
110  }
111 
112  int substitute(const int& flags = 0)
113  {
114  int substitutions = xmlXIncludeProcessFlags(this->doc.get(), flags);
115 
116  if (-1 == substitutions)
117  {
118  throw Exception(xmlGetLastError()->message);
119  }
120 
121  return substitutions;
122  }
123 
124  protected:
125 
126  private:
127  ::boost::shared_ptr< xmlDoc > doc;
128  };
129  }
130 }
131 
132 #endif // _RL_XML_DOCUMENT_H_
rl::xml::Document::getVersion
::std::string getVersion() const
Definition: Document.h:87
rl::xml::Document::doc
::boost::shared_ptr< xmlDoc > doc
Definition: Document.h:127
rl::xml::Document::~Document
virtual ~Document()
Definition: Document.h:51
rl::xml::Document::dumpMemory
::std::string dumpMemory() const
Definition: Document.h:66
rl::xml::Document
Definition: Document.h:44
rl::xml::Document::Document
Document(xmlDocPtr doc)
Definition: Document.h:46
rl::xml::Document::getEncoding
::std::string getEncoding() const
Definition: Document.h:77
rl::xml::Document::operator()
xmlDocPtr operator()() const
Definition: Document.h:92
rl::xml::Node
Definition: Node.h:45
Node.h
rl::xml::Document::substitute
int substitute(const int &flags=0)
Definition: Document.h:112
rl::xml::Document::save
void save(const ::std::string &filename, const ::std::string &encoding, const bool &format=true) const
Definition: Document.h:102
rl::xml::Document::dumpFormatMemory
::std::string dumpFormatMemory(const bool &format) const
Definition: Document.h:55
rl::xml::Document::save
void save(const ::std::string &filename, const bool &format=true) const
Definition: Document.h:97
Exception.h
rl::xml::Document::getRootElement
Node getRootElement() const
Definition: Document.h:82
rl::xml::Exception
Definition: Exception.h:37
rl::xml::Document::setRootElement
void setRootElement(const Node &node)
Definition: Document.h:107
rl
Definition: Ati.cpp:35