xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
stylesheet.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 * Copyright (C) 2013 Vaclav Slavik <vslavik@gmail.com>
4 * All Rights Reserved
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of the Author nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
24 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/**
35 @file
36
37 This file contains the definition of the xslt::stylesheet class.
38 */
39
40#ifndef _xsltwrapp_stylesheet_h_
41#define _xsltwrapp_stylesheet_h_
42
43// xmlwrapp includes
44#include "xsltwrapp/init.h"
45#include "xmlwrapp/document.h"
46#include "xmlwrapp/export.h"
47#include "xmlwrapp/errors.h"
48
49// standard includes
50#include <map>
51#include <memory>
52#include <string>
53
54XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
55
56namespace xslt
57{
58
59/**
60 The xslt::stylesheet class is used to hold information about an XSLT
61 stylesheet. You can use it to load in a stylesheet and then use that
62 stylesheet to transform an XML document to something else.
63 */
64class XSLTWRAPP_API stylesheet
65{
66public:
67 struct pimpl;
68
69 /// Type for passing parameters to the stylesheet
70 using param_type = std::map<std::string, std::string>;
71
72 /**
73 Create a new xslt::stylesheet object and load and parse the
74 stylesheet in the given filename.
75
76 Errors are handled by @a on_error handler; by default, xml::exception
77 is thrown on errors. If there's a fatal error that prevents the
78 stylesheet from being loaded and the error handler doesn't throw an
79 exception, the constructor will throw xml::exception anyway.
80
81 @param filename The name of the file that contains the stylesheet.
82 @param on_error Handler called to process errors and warnings (since 0.7.0).
83 */
84 explicit stylesheet(const char *filename,
86
87 /**
88 Create a new xslt::stylesheet object from an xml::document object
89 that contains the parsed stylesheet. The given xml::document is
90 passed by value. This is needed because the stylesheet will own the
91 document and free it.
92
93 Errors are handled by @a on_error handler; by default, xml::exception
94 is thrown on errors. If there's a fatal error that prevents the
95 stylesheet from being loaded and the error handler doesn't throw an
96 exception, the constructor will throw xml::exception anyway.
97
98 @param doc The parsed stylesheet.
99 @param on_error Handler called to process errors and warnings (since 0.7.0).
100 */
103
104 /**
105 Clean up after an xslt::stylesheet.
106 */
108
109 /**
110 Apply this stylesheet to the given XML document. The result document
111 is placed in the second document parameter.
112
113 @param doc The XML document to transform.
114 @param result The result tree after applying this stylesheet.
115
116 @return True if the transformation was successful and the results placed in result.
117 @return False if there was an error, result is not modified.
118
119 @deprecated Use the form that takes xml::error_handler argument.
120 */
121 XMLWRAPP_DEPRECATED("use the form that takes error_handler argument")
122 bool apply(const xml::document& doc, xml::document& result);
123
124 /**
125 Apply this stylesheet to the given XML document. The result document
126 is placed in the second document parameter.
127
128 @param doc The XML document to transform.
129 @param result The result tree after applying this stylesheet.
130 @param with_params Override xsl:param elements using the given key/value map
131
132 @return True if the transformation was successful and the results placed in result.
133 @return False if there was an error, result is not modified.
134
135 @deprecated Use the form that takes xml::error_handler argument.
136 */
137 XMLWRAPP_DEPRECATED("use the form that takes error_handler argument")
138 bool apply(const xml::document& doc, xml::document& result, const param_type& with_params);
139
140 /**
141 Apply this stylesheet to the given XML document. The result document
142 is placed in the second document parameter.
143
144 @param doc The XML document to transform.
145 @param result The result tree after applying this stylesheet.
146 @param on_error Handler called to process errors and warnings (since 0.7.0).
147
148 @return True if the transformation was successful and the results placed in result.
149 @return False if there was an error, result is not modified.
150 */
151 bool apply(const xml::document& doc,
152 xml::document& result,
153 xml::error_handler& on_error);
154
155 /**
156 Apply this stylesheet to the given XML document. The result document
157 is placed in the second document parameter.
158
159 @param doc The XML document to transform.
160 @param result The result tree after applying this stylesheet.
161 @param with_params Override xsl:param elements using the given key/value map
162 @param on_error Handler called to process errors and warnings (since 0.7.0).
163
164 @return True if the transformation was successful and the results placed in result.
165 @return False if there was an error, result is not modified.
166 */
167 bool apply(const xml::document& doc,
168 xml::document& result,
169 const param_type& with_params,
170 xml::error_handler& on_error);
171
172 /**
173 Apply this stylesheet to the given XML document. The results document
174 is returned. If there is an error during transformation, this
175 function will throw a xml::exception exception.
176
177 Each time you call this member function, the xml::document object
178 that was returned from the last call becomes invalid. That is, of
179 course, unless you copied it first.
180
181 @param doc The XML document to transform.
182 @param on_error Handler called to process errors and warnings (since 0.7.0).
183
184 @return A reference to the result tree.
185 */
186 xml::document& apply(const xml::document& doc,
187 xml::error_handler& on_error = xml::throw_on_error);
188
189 /**
190 Apply this stylesheet to the given XML document. The results document
191 is returned. If there is an error during transformation, this
192 function will throw a xml::exception exception.
193
194 Each time you call this member function, the xml::document object
195 that was returned from the last call becomes invalid. That is, of
196 course, unless you copied it first.
197
198 @param doc The XML document to transform.
199 @param with_params Override xsl:param elements using the given key/value map
200 @param on_error Handler called to process errors and warnings (since 0.7.0).
201
202 @return A reference to the result tree.
203 */
204 xml::document& apply(const xml::document& doc,
205 const param_type& with_params,
206 xml::error_handler& on_error = xml::throw_on_error);
207
208 /**
209 If you used one of the xslt::stylesheet::apply member functions that
210 return a bool, you can use this function to get the text message for
211 the transformation error.
212
213 If you are using one of the apply member functions that throws
214 exceptions, this function should not be used. The text message for
215 the transformation error will be given to the xml::exception
216 constructor.
217
218 @return The last error message.
219
220 @deprecated Use apply() variants that take xml::error_handler argument.
221 */
222 XMLWRAPP_DEPRECATED("use apply() variants that take error_handler argument")
223 const std::string& get_error_message() const;
224
225private:
226 void init(xml::document& doc, xml::error_handler& on_error);
227
228 std::unique_ptr<pimpl> pimpl_;
229
230 // an xslt::stylesheet cannot yet be copied or assigned to.
231 stylesheet(const stylesheet&) = delete;
232 stylesheet& operator=(const stylesheet&) = delete;
233}; // end xslt::stylesheet class
234
235} // end xslt namespace
236
237XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
238
239#endif // _xsltwrapp_stylesheet_h_
The xml::document class is used to hold the XML tree and various bits of information about it.
Definition document.h:88
The xml::error_handler class is used to handle libxml2 errors and warnings emitted during parsing,...
Definition errors.h:89
The xslt::init class is used to configure the XSLT engine.
Definition init.h:64
The xslt::stylesheet class is used to hold information about an XSLT stylesheet.
Definition stylesheet.h:65
stylesheet(xml::document doc, xml::error_handler &on_error=xml::throw_on_error)
Create a new xslt::stylesheet object from an xml::document object that contains the parsed stylesheet...
~stylesheet()
Clean up after an xslt::stylesheet.
std::map< std::string, std::string > param_type
Type for passing parameters to the stylesheet.
Definition stylesheet.h:70
stylesheet(const char *filename, xml::error_handler &on_error=xml::throw_on_error)
Create a new xslt::stylesheet object and load and parse the stylesheet in the given filename.
This file contains the definition of the xml::document class.
This file contains errors-handling classes: xml::exception and xml::error_handler and derived classes...
XML library namespace.
Definition attributes.h:55
error_handler_throw_on_error throw_on_error
Error handler object that throws on any error.
XSLT library namespace.
Definition document.h:59
This file contains the definition of the xslt::init class.