Robotics Library  0.7.0
Stylesheet.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_STYLESHEET_H
28 #define RL_XML_STYLESHEET_H
29 
30 #include <map>
31 #include <memory>
32 #include <string>
33 #include <vector>
34 #include <boost/shared_array.hpp>
35 #include <libxslt/transform.h>
36 #include <libxslt/xsltInternals.h>
37 
38 #include "Document.h"
39 #include "Exception.h"
40 
41 namespace rl
42 {
43  namespace xml
44  {
45  class Stylesheet
46  {
47  public:
48  explicit Stylesheet() :
49  stylesheet(::xsltNewStylesheet(), ::xsltFreeStylesheet)
50  {
51  }
52 
53  explicit Stylesheet(::xsltStylesheetPtr stylesheet) :
54  stylesheet(stylesheet, ::xsltFreeStylesheet)
55  {
56  }
57 
58  explicit Stylesheet(Document& document) :
59  stylesheet(::xsltParseStylesheetDoc(document.get()), ::xsltFreeStylesheet)
60  {
61  document.release();
62  }
63 
64  explicit Stylesheet(const ::std::string& filename) :
65  stylesheet(::xsltParseStylesheetFile(reinterpret_cast<const ::xmlChar*>(filename.c_str())), ::xsltFreeStylesheet)
66  {
67  }
68 
70  {
71  }
72 
74  {
75  return Document(::xsltApplyStylesheet(this->stylesheet.get(), this->stylesheet.get()->doc, nullptr));
76  }
77 
78  Document apply(const ::std::map< ::std::string, ::std::string>& parameters)
79  {
80  ::std::vector<const char*> params;
81 
82  for (::std::map< ::std::string, ::std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
83  {
84  params.push_back(i->first.c_str());
85  params.push_back(i->second.c_str());
86  }
87 
88  params.push_back(nullptr);
89 
90  return Document(::xsltApplyStylesheet(this->stylesheet.get(), this->stylesheet.get()->doc, params.data()));
91  }
92 
93  Document apply(const Document& document)
94  {
95  return Document(::xsltApplyStylesheet(this->stylesheet.get(), document.get(), nullptr));
96  }
97 
98  Document apply(const Document& document, const ::std::map< ::std::string, ::std::string>& parameters)
99  {
100  ::std::vector<const char*> params;
101 
102  for (::std::map< ::std::string, ::std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
103  {
104  params.push_back(i->first.c_str());
105  params.push_back(i->second.c_str());
106  }
107 
108  params.push_back(nullptr);
109 
110  return Document(::xsltApplyStylesheet(this->stylesheet.get(), document.get(), params.data()));
111  }
112 
113  ::std::string dumpFormatMemory(const bool& format) const
114  {
115  ::xmlChar* mem;
116  int size;
117 
118  ::xmlDocDumpFormatMemory(this->stylesheet.get()->doc, &mem, &size, format ? 1 : 0);
119  ::boost::shared_array< ::xmlChar> memory(mem, ::xmlFree);
120 
121  return nullptr != memory.get() ? reinterpret_cast<const char*>(memory.get()) : ::std::string();
122  }
123 
124  ::std::string dumpMemory() const
125  {
126  ::xmlChar* mem;
127  int size;
128 
129  ::xmlDocDumpMemory(this->stylesheet.get()->doc, &mem, &size);
130  ::boost::shared_array< ::xmlChar> memory(mem, ::xmlFree);
131 
132  return nullptr != memory.get() ? reinterpret_cast<const char*>(memory.get()) : ::std::string();
133  }
134 
135  ::xsltStylesheetPtr get() const
136  {
137  return this->stylesheet.get();
138  }
139 
140  ::std::string getEncoding() const
141  {
142  return nullptr != this->stylesheet.get()->doc->encoding ? reinterpret_cast<const char*>(this->stylesheet.get()->doc->encoding) : ::std::string();
143  }
144 
146  {
147  return Node(::xmlDocGetRootElement(this->stylesheet.get()->doc));
148  }
149 
150  ::std::string getVersion() const
151  {
152  return nullptr != this->stylesheet.get()->doc->version ? reinterpret_cast<const char*>(this->stylesheet.get()->doc->version) : ::std::string();
153  }
154 
155  ::xsltStylesheet& operator*() const
156  {
157  return *this->stylesheet;
158  }
159 
160  void save(const ::std::string& filename, const bool& format = true) const
161  {
162  ::xmlSaveFormatFile(filename.c_str(), this->stylesheet.get()->doc, format ? 1 : 0);
163  }
164 
165  void save(const ::std::string& filename, const ::std::string& encoding, const bool& format = true) const
166  {
167  ::xmlSaveFormatFileEnc(filename.c_str(), this->stylesheet.get()->doc, encoding.c_str(), format ? 1 : 0);
168  }
169 
170  int substitute(const int& flags = 0)
171  {
172  int substitutions = ::xmlXIncludeProcessFlags(this->stylesheet.get()->doc, flags);
173 
174  if (-1 == substitutions)
175  {
176  throw Exception(::xmlGetLastError()->message);
177  }
178 
179  return substitutions;
180  }
181 
182  protected:
183 
184  private:
185  ::std::shared_ptr< ::xsltStylesheet> stylesheet;
186  };
187  }
188 }
189 
190 #endif // RL_XML_STYLESHEET_H
rl::xml::Stylesheet::Stylesheet
Stylesheet(const ::std::string &filename)
Definition: Stylesheet.h:64
rl::xml::Stylesheet::~Stylesheet
~Stylesheet()
Definition: Stylesheet.h:69
rl::xml::Stylesheet::dumpFormatMemory
::std::string dumpFormatMemory(const bool &format) const
Definition: Stylesheet.h:113
rl::xml::Stylesheet::Stylesheet
Stylesheet(::xsltStylesheetPtr stylesheet)
Definition: Stylesheet.h:53
rl::xml::Stylesheet::substitute
int substitute(const int &flags=0)
Definition: Stylesheet.h:170
rl::xml::Document
Definition: Document.h:47
rl::xml::Stylesheet::Stylesheet
Stylesheet(Document &document)
Definition: Stylesheet.h:58
rl::xml::Stylesheet::getEncoding
::std::string getEncoding() const
Definition: Stylesheet.h:140
rl::xml::Stylesheet::dumpMemory
::std::string dumpMemory() const
Definition: Stylesheet.h:124
rl::xml::Stylesheet::getRootElement
Node getRootElement() const
Definition: Stylesheet.h:145
rl::xml::Stylesheet::apply
Document apply(const ::std::map< ::std::string, ::std::string > &parameters)
Definition: Stylesheet.h:78
rl::xml::Stylesheet::save
void save(const ::std::string &filename, const bool &format=true) const
Definition: Stylesheet.h:160
rl::xml::Stylesheet::getVersion
::std::string getVersion() const
Definition: Stylesheet.h:150
rl::xml::Document::get
::xmlDocPtr get() const
Definition: Document.h:90
rl::xml::Stylesheet::save
void save(const ::std::string &filename, const ::std::string &encoding, const bool &format=true) const
Definition: Stylesheet.h:165
rl::xml::Node
Definition: Node.h:46
rl::xml::Stylesheet::apply
Document apply(const Document &document)
Definition: Stylesheet.h:93
rl::xml::Stylesheet
Definition: Stylesheet.h:46
rl::xml::Stylesheet::get
::xsltStylesheetPtr get() const
Definition: Stylesheet.h:135
rl::xml::Stylesheet::apply
Document apply(const Document &document, const ::std::map< ::std::string, ::std::string > &parameters)
Definition: Stylesheet.h:98
rl::xml::Stylesheet::stylesheet
::std::shared_ptr< ::xsltStylesheet > stylesheet
Definition: Stylesheet.h:185
rl::xml::Stylesheet::Stylesheet
Stylesheet()
Definition: Stylesheet.h:48
rl::xml::Stylesheet::apply
Document apply()
Definition: Stylesheet.h:73
rl::xml::Stylesheet::operator*
::xsltStylesheet & operator*() const
Definition: Stylesheet.h:155
rl::xml::Document::release
::xmlDocPtr release()
Definition: Document.h:141
Exception.h
Document.h
rl::xml::Exception
Definition: Exception.h:37
rl
Robotics Library.
Definition: AnalogInput.cpp:30