-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxml_wrapper.h
469 lines (381 loc) · 13 KB
/
xml_wrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright (c) 2022, Eugene Gershnik
// SPDX-License-Identifier: BSD-3-Clause
#ifndef HEADER_XML_WRAPPER_H_INCLUDED
#define HEADER_XML_WRAPPER_H_INCLUDED
inline const xmlChar * asXml(const char8_t * str) {
return reinterpret_cast<const xmlChar *>(str);
}
inline xmlChar * asXml(char8_t * str) {
return reinterpret_cast<xmlChar *>(str);
}
inline const char8_t * asNative(const xmlChar * str) {
return reinterpret_cast<const char8_t *>(str);
}
inline char8_t * asNative(xmlChar * str) {
return reinterpret_cast<char8_t *>(str);
}
inline const char8_t * xml_str(const sys_string & str) {
return reinterpret_cast<const char8_t *>(str.c_str());
}
inline const char8_t * xml_str(const std::string & str) {
return reinterpret_cast<const char8_t *>(str.c_str());
}
class XmlCharBuffer {
private:
struct Free {
void operator()(void * ptr) const {
if (ptr)
xmlFree(ptr);
}
};
using MemPtr = std::unique_ptr<char8_t, Free>;
public:
XmlCharBuffer(xmlChar * mem, int size):
m_buf(asNative(mem)),
m_size(size_t(size)) {
}
size_t size() const {
return m_size;
}
char8_t * data() {
return m_buf.get();
}
const char8_t * data() const {
return m_buf.get();
}
private:
MemPtr m_buf;
size_t m_size;
};
struct XmlParserInit {
XmlParserInit() {
xmlInitParser();
//libxml marked xmlThrDefXxx variants deprecated without providing any sane
//alternative to suppress its idiotic default logging to stderr
//We will keep using these for now.
WSDDN_IGNORE_DEPRECATED_BEGIN
xmlSetGenericErrorFunc(nullptr, XmlParserInit::errorFunc);
xmlThrDefSetGenericErrorFunc(nullptr, XmlParserInit::errorFunc);
xmlSetStructuredErrorFunc(nullptr, XmlParserInit::structuredErrorFunc);
xmlThrDefSetStructuredErrorFunc(nullptr, XmlParserInit::structuredErrorFunc);
WSDDN_IGNORE_DEPRECATED_END
}
~XmlParserInit() {
xmlCleanupParser();
}
XmlParserInit(const XmlParserInit &) = delete;
XmlParserInit & operator=(const XmlParserInit &) = delete;
private:
static void errorFunc(void * /*ctx*/, const char * /*msg*/, ...) {
}
//Older versions of LibXml have this callback without const
static void structuredErrorFunc(void * /*userData*/, const xmlError * /*error*/) {
}
static void structuredErrorFunc(void * /*userData*/, xmlError * /*error*/) {
}
};
class XmlException : public std::runtime_error {
public:
XmlException(const xmlError * err):
std::runtime_error(err->message ? err->message : fmt::format("error: {0}, domain: {1}", err->code, err->domain)),
m_domain(err->domain),
m_code(err->code) {
}
XmlException(xmlParserErrors err):
std::runtime_error(fmt::format("XML parser error: {0}", int(err))),
m_domain(XML_FROM_PARSER),
m_code(err){
}
static void raiseFromLastErrorIfPresent() {
auto err = xmlGetLastError();
if (err) {
auto ex = XmlException(err);
xmlResetLastError();
throw ex;
}
}
[[noreturn]] static void raiseFromLastError() {
raiseFromLastErrorIfPresent();
throw std::runtime_error("xml failure with no error set");
}
int domain() const {
return m_domain;
}
int code() const {
return m_code;
}
private:
int m_domain;
int m_code;
};
template<class T, class Derived>
class XmlHandle : protected T
{
protected:
using Wrapped = T;
public:
void operator delete(void *) noexcept
{}
XmlHandle() = delete;
XmlHandle(const XmlHandle &) = delete;
XmlHandle & operator=(const XmlHandle &) = delete;
static Derived * from(T * obj) noexcept
{ return static_cast<Derived *>(obj); }
friend T * c_ptr(const XmlHandle<T, Derived> * obj) noexcept {
return const_cast<T *>(static_cast<const T *>(obj));
}
protected:
~XmlHandle() noexcept
{}
};
class XmlNs : public XmlHandle<xmlNs, XmlNs> {
public:
~XmlNs() {
xmlFreeNs(this);
}
const char8_t * href() const {
return asNative(Wrapped::href);
}
};
class XmlAttr;
class XmlDoc;
class XmlNode : public XmlHandle<xmlNode, XmlNode> {
public:
~XmlNode() {
xmlFreeNode(this);
}
static std::unique_ptr<XmlNode> create(const XmlNs * ns, const char8_t * name) {
if (auto ret = from(xmlNewNode(c_ptr(ns), asXml(name))))
return std::unique_ptr<XmlNode>(ret);
XmlException::raiseFromLastError();
}
static std::unique_ptr<XmlNode> createText(const char8_t * text) {
if (auto ret = from(xmlNewText(asXml(text))))
return std::unique_ptr<XmlNode>(ret);
XmlException::raiseFromLastError();
}
XmlNs & newNs(const char8_t * href, const char8_t * prefix) {
if (auto ret = XmlNs::from(xmlNewNs(this, asXml(href), asXml(prefix))))
return *ret;
XmlException::raiseFromLastError();
}
void setNs(XmlNs * ns) {
xmlSetNs(this, c_ptr(ns));
}
XmlNode & newChild(const XmlNs * ns, const char8_t * name, const char8_t * content = nullptr) {
if (auto ret = from(xmlNewChild(this, c_ptr(ns), asXml(name), asXml(content))))
return *ret;
XmlException::raiseFromLastError();
}
XmlNode & newTextChild(const XmlNs * ns, const char8_t * name, const char8_t * content) {
if (auto ret = from(xmlNewTextChild(this, c_ptr(ns), asXml(name), asXml(content))))
return *ret;
XmlException::raiseFromLastError();
}
XmlNode & addChild(XmlNode & child) {
if (auto ret = from(xmlAddChild(this, &child)))
return *ret;
XmlException::raiseFromLastError();
}
XmlAttr & newAttr(const XmlNs * ns, const char8_t * name, const char8_t * value);
XmlDoc * document() const;
xmlElementType type() const {
return Wrapped::type;
}
XmlAttr * firstProperty() const;
XmlNode * firstChild() const
{ return XmlNode::from(this->children); }
XmlNode * nextSibling() const
{ return XmlNode::from(this->next); }
XmlNode * parent() const
{ return XmlNode::from(c_ptr(this)->parent); }
sys_string getContent() const {
auto chars = asNative(xmlNodeGetContent(this));
if (!chars)
XmlException::raiseFromLastErrorIfPresent();
std::unique_ptr<char8_t, decltype(xmlFree)> holder(chars, xmlFree);
return sys_string(chars);
}
void setContent(const char8_t * content);
};
class XmlAttr : public XmlHandle<xmlAttr, XmlAttr> {
public:
~XmlAttr() {
xmlFreeProp(this);
}
XmlNode * firstChild() const
{ return XmlNode::from(this->children); }
XmlAttr * nextSibling() const
{ return XmlAttr::from(this->next); }
XmlNode * parent() const
{ return XmlNode::from(c_ptr(this)->parent); }
};
inline XmlAttr & XmlNode::newAttr(const XmlNs * ns, const char8_t * name, const char8_t * value) {
if (auto ret = XmlAttr::from(xmlNewNsProp(this, c_ptr(ns), asXml(name), asXml(value))))
return *ret;
XmlException::raiseFromLastError();
}
class XmlDoc : public XmlHandle<xmlDoc, XmlDoc> {
public:
~XmlDoc() {
xmlFreeDoc(this);
}
static std::unique_ptr<XmlDoc> parseMemory(const void * ptr, int size) {
if (auto ret = from(xmlParseMemory((const char *)ptr, size)))
return std::unique_ptr<XmlDoc>(ret);
XmlException::raiseFromLastError();
}
static std::unique_ptr<XmlDoc> create(const char8_t * version) {
if (auto ret = from(xmlNewDoc(asXml(version))))
return std::unique_ptr<XmlDoc>(ret);
XmlException::raiseFromLastError();
}
XmlCharBuffer dump(bool format = 0) {
xmlChar * mem = 0;
int size;
xmlDocDumpFormatMemoryEnc(this, &mem, &size, "utf-8", format);
if (!mem)
XmlException::raiseFromLastError();
return XmlCharBuffer(mem, size);
}
XmlNode * asNode() {
return XmlNode::from(this->children);
}
XmlNode * getRootElement() {
return XmlNode::from(xmlDocGetRootElement(this));
}
std::unique_ptr<XmlNode> setRootElement(std::unique_ptr<XmlNode> root) {
auto ret = XmlNode::from(xmlDocSetRootElement(this, c_ptr(root.get())));
if (xmlDocGetRootElement(this) != c_ptr(root.get()))
XmlException::raiseFromLastError();
root.release();
return std::unique_ptr<XmlNode>(ret);
}
XmlNs * searchNs(XmlNode & node, const char8_t * nameSpace) const {
auto ret = XmlNs::from(xmlSearchNs(c_ptr(this), c_ptr(&node), asXml(nameSpace)));
if (!ret) {
XmlException::raiseFromLastErrorIfPresent();
}
return ret;
}
std::unique_ptr<XmlNode> copyNode(XmlNode & node) {
auto ret = XmlNode::from(xmlDocCopyNode(c_ptr(&node), c_ptr(this), 1));
if (!ret) {
XmlException::raiseFromLastError();
}
return std::unique_ptr<XmlNode>(ret);
}
};
inline XmlDoc * XmlNode::document() const {
return XmlDoc::from(this->doc);
}
inline XmlAttr * XmlNode::firstProperty() const {
return XmlAttr::from(this->properties);
}
inline void XmlNode::setContent(const char8_t * content) {
auto encoded = asNative(xmlEncodeSpecialChars(c_ptr(document()), asXml(content)));
if (!encoded)
XmlException::raiseFromLastError();
std::unique_ptr<char8_t, decltype(xmlFree)> holder(encoded, xmlFree);
xmlNodeSetContent(this, asXml(encoded));
XmlException::raiseFromLastErrorIfPresent();
}
class XmlNodeset : public XmlHandle<xmlNodeSet, XmlNodeset> {
public:
~XmlNodeset() {
xmlXPathFreeNodeSet(this);
}
bool empty() const {
return !(this->nodeNr) || !(this->nodeTab);
}
size_t size() const {
if (!this->nodeTab)
return 0;
return this->nodeNr;
}
XmlNode * operator[](size_t idx) {
return XmlNode::from(this->nodeTab[idx]);
}
};
class XPathObject : public XmlHandle<xmlXPathObject, XPathObject> {
public:
~XPathObject() {
xmlXPathFreeObject(this);
}
XmlNodeset * nodeset() const {
return XmlNodeset::from(this->nodesetval);
}
XmlNode * firstNode() {
auto nodes = nodeset();
if (!nodes || nodes->empty())
return nullptr;
return (*nodes)[0];
}
const char8_t * stringval() const {
return asNative(Wrapped::stringval);
}
};
class XPathContext : public XmlHandle<xmlXPathContext, XPathContext> {
public:
~XPathContext() {
xmlXPathFreeContext(this);
}
static std::unique_ptr<XPathContext> create(XmlDoc & doc) {
if (auto ret = from(xmlXPathNewContext(c_ptr(&doc))))
return std::unique_ptr<XPathContext>(ret);
XmlException::raiseFromLastError();
}
void registerNs(const char8_t * prefix, const char8_t * uri) {
if (xmlXPathRegisterNs(this, asXml(prefix), asXml(uri)) != 0)
XmlException::raiseFromLastError();
}
void setContextNode(XmlNode & node) {
if (xmlXPathSetContextNode(c_ptr(&node), this) != 0)
XmlException::raiseFromLastError();
}
std::unique_ptr<XPathObject> eval(const char8_t * expr) {
if (auto ret = XPathObject::from(xmlXPathEval(asXml(expr), this)))
return std::unique_ptr<XPathObject>(ret);
XmlException::raiseFromLastError();
}
};
class XmlParserContext : public XmlHandle<xmlParserCtxt, XmlParserContext> {
public:
~XmlParserContext() noexcept {
if (this->myDoc)
xmlFreeDoc(this->myDoc);
xmlFreeParserCtxt(this);
}
static std::unique_ptr<XmlParserContext> createPush(const uint8_t * chunk, int size, const char * filename = nullptr) {
if (auto ret = from(xmlCreatePushParserCtxt(nullptr, nullptr, (const char *)chunk, size, filename)))
return std::unique_ptr<XmlParserContext>(ret);
XmlException::raiseFromLastError();
}
static std::unique_ptr<XmlParserContext> createPush(const char8_t * encoding = nullptr, const char * filename = nullptr) {
auto ret = std::unique_ptr<XmlParserContext>(from(xmlCreatePushParserCtxt(nullptr, nullptr, nullptr, 0, filename)));
if (!ret)
XmlException::raiseFromLastError();
if (encoding) {
ret->encoding = xmlStrdup(asXml(encoding));
if (!ret->encoding)
XmlException::raiseFromLastError();
}
return ret;
}
void parseChunk(const uint8_t * chunk, int size, bool last) {
if (auto err = xmlParserErrors(xmlParseChunk(this, (const char *)chunk, size, last)))
throw XmlException(err);
}
std::unique_ptr<XmlDoc> extractDoc() {
std::unique_ptr<XmlDoc> ret(XmlDoc::from(this->myDoc));
this->myDoc = nullptr;
return ret;
}
XmlDoc * doc() const {
return XmlDoc::from(this->myDoc);
}
bool wellFormed() const {
return Wrapped::wellFormed;
}
};
#endif