forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_document.cc
More file actions
629 lines (487 loc) · 22.9 KB
/
xml_document.cc
File metadata and controls
629 lines (487 loc) · 22.9 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Copyright 2009, Squish Tech, LLC.
#include <node.h>
#include <node_buffer.h>
#include <cstring>
//#include <libxml/tree.h>
#include <libxml/HTMLtree.h>
#include <libxml/HTMLparser.h>
#include <libxml/xmlschemas.h>
#include <libxml/relaxng.h>
#include <libxml/xmlsave.h>
#include "xml_document.h"
#include "xml_element.h"
#include "xml_namespace.h"
#include "xml_syntax_error.h"
namespace libxmljs {
Nan::Persistent<v8::FunctionTemplate> XmlDocument::constructor_template;
NAN_METHOD(XmlDocument::Encoding)
{
Nan::HandleScope scope;
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
assert(document);
// if no args, get the encoding
if (info.Length() == 0 || info[0]->IsUndefined()) {
if (document->xml_obj->encoding)
return info.GetReturnValue().Set(Nan::New<v8::String>(
(const char *)document->xml_obj->encoding,
xmlStrlen((const xmlChar*)document->xml_obj->encoding)).ToLocalChecked());
return info.GetReturnValue().Set(Nan::Null());
}
// set the encoding otherwise
v8::String::Utf8Value encoding(info[0]->ToString());
if(document->xml_obj->encoding != NULL) {
xmlFree((xmlChar*)document->xml_obj->encoding);
}
document->xml_obj->encoding = xmlStrdup((const xmlChar*)*encoding);
return info.GetReturnValue().Set(info.Holder());
}
NAN_METHOD(XmlDocument::Version)
{
Nan::HandleScope scope;
XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
assert(document);
if (document->xml_obj->version)
return info.GetReturnValue().Set(Nan::New<v8::String>((const char *)document->xml_obj->version,
xmlStrlen((const xmlChar*)document->xml_obj->version)).ToLocalChecked());
return info.GetReturnValue().Set(Nan::Null());
}
NAN_METHOD(XmlDocument::Root)
{
Nan::HandleScope scope;
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
assert(document);
xmlNode* root = xmlDocGetRootElement(document->xml_obj);
if (info.Length() == 0 || info[0]->IsUndefined())
{
if (!root) {
return info.GetReturnValue().Set(Nan::Null());
}
return info.GetReturnValue().Set(XmlElement::New(root));
}
if (root != NULL) {
return Nan::ThrowError("Holder document already has a root node");
}
// set the element as the root element for the document
// allows for proper retrieval of root later
XmlElement* element = Nan::ObjectWrap::Unwrap<XmlElement>(info[0]->ToObject());
assert(element);
xmlDocSetRootElement(document->xml_obj, element->xml_obj);
element->ref_wrapped_ancestor();
return info.GetReturnValue().Set(info[0]);
}
NAN_METHOD(XmlDocument::GetDtd)
{
Nan::HandleScope scope;
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
assert(document);
xmlDtdPtr dtd = xmlGetIntSubset(document->xml_obj);
if (!dtd) {
return info.GetReturnValue().Set(Nan::Null());
}
const char* name = (const char *)dtd->name;
const char* extId = (const char *)dtd->ExternalID;
const char* sysId = (const char *)dtd->SystemID;
v8::Local<v8::Object> dtdObj = Nan::New<v8::Object>();
v8::Local<v8::Value> nameValue = (v8::Local<v8::Value>)Nan::Null();
v8::Local<v8::Value> extValue = (v8::Local<v8::Value>)Nan::Null();
v8::Local<v8::Value> sysValue = (v8::Local<v8::Value>)Nan::Null();
if (name != NULL) {
nameValue = (v8::Local<v8::Value>)Nan::New<v8::String>(name, strlen(name)).ToLocalChecked();
}
if (extId != NULL) {
extValue = (v8::Local<v8::Value>)Nan::New<v8::String>(extId, strlen(extId)).ToLocalChecked();
}
if (sysId != NULL) {
sysValue = (v8::Local<v8::Value>)Nan::New<v8::String>(sysId, strlen(sysId)).ToLocalChecked();
}
Nan::Set(dtdObj, Nan::New<v8::String>("name").ToLocalChecked(), nameValue);
Nan::Set(dtdObj, Nan::New<v8::String>("externalId").ToLocalChecked(), extValue);
Nan::Set(dtdObj, Nan::New<v8::String>("systemId").ToLocalChecked(), sysValue);
return info.GetReturnValue().Set(dtdObj);
}
NAN_METHOD(XmlDocument::SetDtd)
{
Nan::HandleScope scope;
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
assert(document);
v8::String::Utf8Value name(info[0]);
v8::Local<v8::Value> extIdOpt;
v8::Local<v8::Value> sysIdOpt;
if (info.Length() > 1 && info[1]->IsString()) {
extIdOpt = info[1];
}
if (info.Length() > 2 && info[2]->IsString()) {
sysIdOpt = info[2];
}
v8::String::Utf8Value extIdRaw(extIdOpt);
v8::String::Utf8Value sysIdRaw(sysIdOpt);
//must be set to null in order for xmlCreateIntSubset to ignore them
const char* extId = (extIdRaw.length()) ? *extIdRaw : NULL;
const char* sysId = (sysIdRaw.length()) ? *sysIdRaw : NULL;
//No good way of unsetting the doctype if it is previously set...this allows us to.
xmlDtdPtr dtd = xmlGetIntSubset(document->xml_obj);
xmlUnlinkNode((xmlNodePtr) dtd);
xmlFreeNode((xmlNodePtr) dtd);
xmlCreateIntSubset(document->xml_obj, (const xmlChar *) *name, (const xmlChar *) extId, (const xmlChar *) sysId);
return info.GetReturnValue().Set(info.Holder());
}
NAN_METHOD(XmlDocument::ToString)
{
Nan::HandleScope scope;
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
assert(document);
int options = 0;
if (info.Length() > 0) {
if (info[0]->IsBoolean()) {
if (info[0]->ToBoolean()->BooleanValue() == true) {
options |= XML_SAVE_FORMAT;
}
} else if (info[0]->IsObject()) {
v8::Local<v8::Object> obj = info[0]->ToObject();
// drop the xml declaration
if (obj->Get(Nan::New<v8::String>("declaration").ToLocalChecked())->IsFalse()) {
options |= XML_SAVE_NO_DECL;
}
// format save output
if (obj->Get(Nan::New<v8::String>("format").ToLocalChecked())->IsTrue()) {
options |= XML_SAVE_FORMAT;
}
// no empty tags (only works with XML) ex: <title></title> becomes <title/>
if (obj->Get(Nan::New<v8::String>("selfCloseEmpty").ToLocalChecked())->IsFalse()) {
options |= XML_SAVE_NO_EMPTY;
}
// format with non-significant whitespace
if (obj->Get(Nan::New<v8::String>("whitespace").ToLocalChecked())->IsTrue()) {
options |= XML_SAVE_WSNONSIG;
}
v8::Local<v8::Value> type = obj->Get(Nan::New<v8::String>("type").ToLocalChecked());
if (type->Equals(Nan::New<v8::String>("XML").ToLocalChecked()) ||
type->Equals(Nan::New<v8::String>("xml").ToLocalChecked())) {
options |= XML_SAVE_AS_XML; // force XML serialization on HTML doc
} else if (type->Equals(Nan::New<v8::String>("HTML").ToLocalChecked()) ||
type->Equals(Nan::New<v8::String>("html").ToLocalChecked())) {
options |= XML_SAVE_AS_HTML; // force HTML serialization on XML doc
// if the document is XML and we want formatted HTML output
// we must use the XHTML serializer because the default HTML
// serializer only formats node->type = HTML_NODE and not XML_NODEs
if ((options & XML_SAVE_FORMAT) && (options & XML_SAVE_XHTML) == false) {
options |= XML_SAVE_XHTML;
}
} else if (type->Equals(Nan::New<v8::String>("XHTML").ToLocalChecked()) ||
type->Equals(Nan::New<v8::String>("xhtml").ToLocalChecked())) {
options |= XML_SAVE_XHTML; // force XHTML serialization
}
}
}
xmlBuffer* buf = xmlBufferCreate();
xmlSaveCtxt* savectx = xmlSaveToBuffer(buf, "UTF-8", options);
xmlSaveTree(savectx, (xmlNode*)document->xml_obj);
xmlSaveFlush(savectx);
xmlSaveClose(savectx);
v8::Local<v8::Value> ret = Nan::Null();
if (xmlBufferLength(buf) > 0)
ret = Nan::New<v8::String>((char*)xmlBufferContent(buf), xmlBufferLength(buf)).ToLocalChecked();
xmlBufferFree(buf);
return info.GetReturnValue().Set(ret);
}
// not called from node
// private api
v8::Local<v8::Object>
XmlDocument::New(xmlDoc* doc)
{
Nan::EscapableHandleScope scope;
if (doc->_private) {
return scope.Escape(static_cast<XmlDocument*>(doc->_private)->handle());
}
v8::Local<v8::Object> obj = Nan::NewInstance(Nan::New(constructor_template)->GetFunction()).ToLocalChecked();
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(obj);
// replace the document we created
document->xml_obj->_private = NULL;
xmlFreeDoc(document->xml_obj);
document->xml_obj = doc;
// store ourselves in the document
// this is how we can get instances or already existing v8 objects
doc->_private = document;
return scope.Escape(obj);
}
int getParserOption(v8::Local<v8::Object> props, const char *key, int value, bool defaultValue = true) {
Nan::HandleScope scope;
v8::Local<v8::Value> prop = props->Get(Nan::New<v8::String>(key).ToLocalChecked());
return !prop->IsUndefined() && prop->ToBoolean()->BooleanValue() == defaultValue ? value : 0;
}
xmlParserOption getParserOptions(v8::Local<v8::Object> props) {
int ret = 0;
// http://xmlsoft.org/html/libxml-parser.html#xmlParserOption
// http://www.xmlsoft.org/html/libxml-HTMLparser.html#htmlParserOption
ret |= getParserOption(props, "recover", XML_PARSE_RECOVER); // 1: recover on errors
/*ret |= getParserOption(props, "recover", HTML_PARSE_RECOVER); // 1: Relaxed parsing*/
ret |= getParserOption(props, "noent", XML_PARSE_NOENT); // 2: substitute entities
ret |= getParserOption(props, "dtdload", XML_PARSE_DTDLOAD); // 4: load the external subset
ret |= getParserOption(props, "doctype", HTML_PARSE_NODEFDTD, false); // 4: do not default a doctype if not found
ret |= getParserOption(props, "dtdattr", XML_PARSE_DTDATTR); // 8: default DTD attributes
ret |= getParserOption(props, "dtdvalid", XML_PARSE_DTDVALID); // 16: validate with the DTD
ret |= getParserOption(props, "noerror", XML_PARSE_NOERROR); // 32: suppress error reports
ret |= getParserOption(props, "errors", HTML_PARSE_NOERROR, false); // 32: suppress error reports
ret |= getParserOption(props, "nowarning", XML_PARSE_NOWARNING); // 64: suppress warning reports
ret |= getParserOption(props, "warnings", HTML_PARSE_NOWARNING, false); // 64: suppress warning reports
ret |= getParserOption(props, "pedantic", XML_PARSE_PEDANTIC); // 128: pedantic error reporting
/*ret |= getParserOption(props, "pedantic", HTML_PARSE_PEDANTIC); // 128: pedantic error reporting*/
ret |= getParserOption(props, "noblanks", XML_PARSE_NOBLANKS); // 256: remove blank nodes
ret |= getParserOption(props, "blanks", HTML_PARSE_NOBLANKS, false); // 256: remove blank nodes
ret |= getParserOption(props, "sax1", XML_PARSE_SAX1); // 512: use the SAX1 interface internally
ret |= getParserOption(props, "xinclude", XML_PARSE_XINCLUDE); // 1024: Implement XInclude substitition
ret |= getParserOption(props, "nonet", XML_PARSE_NONET); // 2048: Forbid network access
ret |= getParserOption(props, "net", HTML_PARSE_NONET, false); // 2048: Forbid network access
ret |= getParserOption(props, "nodict", XML_PARSE_NODICT); // 4096: Do not reuse the context dictionnary
ret |= getParserOption(props, "dict", XML_PARSE_NODICT, false); // 4096: Do not reuse the context dictionnary
ret |= getParserOption(props, "nsclean", XML_PARSE_NSCLEAN); // 8192: remove redundant namespaces declarations
ret |= getParserOption(props, "implied", HTML_PARSE_NOIMPLIED, false); // 8192: Do not add implied html/body elements
ret |= getParserOption(props, "nocdata", XML_PARSE_NOCDATA); // 16384: merge CDATA as text nodes
ret |= getParserOption(props, "cdata", XML_PARSE_NOCDATA, false); // 16384: merge CDATA as text nodes
ret |= getParserOption(props, "noxincnode", XML_PARSE_NOXINCNODE); // 32768: do not generate XINCLUDE START/END nodes
ret |= getParserOption(props, "xincnode", XML_PARSE_NOXINCNODE, false); // 32768: do not generate XINCLUDE START/END nodes
ret |= getParserOption(props, "compact", XML_PARSE_COMPACT); // 65536: compact small text nodes; no modification of the tree allowed afterwards (will possibly crash if you try to modify the tree)
/*ret |= getParserOption(props, "compact", HTML_PARSE_COMPACT , false); // 65536: compact small text nodes*/
ret |= getParserOption(props, "old10", XML_PARSE_OLD10); // 131072: parse using XML-1.0 before update 5
ret |= getParserOption(props, "nobasefix", XML_PARSE_NOBASEFIX); // 262144: do not fixup XINCLUDE xml:base uris
ret |= getParserOption(props, "basefix", XML_PARSE_NOBASEFIX, false); // 262144: do not fixup XINCLUDE xml:base uris
ret |= getParserOption(props, "huge", XML_PARSE_HUGE); // 524288: relax any hardcoded limit from the parser
ret |= getParserOption(props, "oldsax", XML_PARSE_OLDSAX); // 1048576: parse using SAX2 interface before 2.7.0
ret |= getParserOption(props, "ignore_enc", XML_PARSE_IGNORE_ENC); // 2097152: ignore internal document encoding hint
/*ret |= getParserOption(props, "ignore_enc", HTML_PARSE_IGNORE_ENC); // 2097152: ignore internal document encoding hint*/
ret |= getParserOption(props, "big_lines", XML_PARSE_BIG_LINES); // 4194304: Store big lines numbers in text PSVI field
return (xmlParserOption)ret;
}
NAN_METHOD(XmlDocument::FromHtml)
{
Nan::HandleScope scope;
v8::Local<v8::Object> options = info[1]->ToObject();
v8::Local<v8::Value> baseUrlOpt = options->Get(
Nan::New<v8::String>("baseUrl").ToLocalChecked());
v8::Local<v8::Value> encodingOpt = options->Get(
Nan::New<v8::String>("encoding").ToLocalChecked());
v8::Local<v8::Value> excludeImpliedElementsOpt = options->Get(
Nan::New<v8::String>("excludeImpliedElements").ToLocalChecked());
// the base URL that will be used for this HTML parsed document
v8::String::Utf8Value baseUrl_(baseUrlOpt->ToString());
const char * baseUrl = *baseUrl_;
if (!baseUrlOpt->IsString()) {
baseUrl = NULL;
}
// the encoding to be used for this document
// (leave NULL for libxml to autodetect)
v8::String::Utf8Value encoding_(encodingOpt->ToString());
const char * encoding = *encoding_;
if (!encodingOpt->IsString()) {
encoding = NULL;
}
v8::Local<v8::Array> errors = Nan::New<v8::Array>();
xmlResetLastError();
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors), XmlSyntaxError::PushToArray);
int opts = (int)getParserOptions(options);
if (excludeImpliedElementsOpt->ToBoolean()->Value())
opts |= HTML_PARSE_NOIMPLIED | HTML_PARSE_NODEFDTD;
htmlDocPtr doc;
if (!node::Buffer::HasInstance(info[0])) {
// Parse a string
v8::String::Utf8Value str(info[0]->ToString());
doc = htmlReadMemory(*str, str.length(), baseUrl, encoding, opts);
}
else {
// Parse a buffer
v8::Local<v8::Object> buf = info[0]->ToObject();
doc = htmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
baseUrl, encoding, opts);
}
xmlSetStructuredErrorFunc(NULL, NULL);
if (!doc) {
xmlError* error = xmlGetLastError();
if (error) {
return Nan::ThrowError(XmlSyntaxError::BuildSyntaxError(error));
}
return Nan::ThrowError("Could not parse XML string");
}
v8::Local<v8::Object> doc_handle = XmlDocument::New(doc);
Nan::Set(doc_handle, Nan::New<v8::String>("errors").ToLocalChecked(), errors);
// create the xml document handle to return
return info.GetReturnValue().Set(doc_handle);
}
// FIXME: this method is almost identical to FromHtml above.
// The two should be refactored to use a common function for most
// of the work
NAN_METHOD(XmlDocument::FromXml)
{
Nan::HandleScope scope;
v8::Local<v8::Array> errors = Nan::New<v8::Array>();
xmlResetLastError();
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
XmlSyntaxError::PushToArray);
v8::Local<v8::Object> options = info[1]->ToObject();
v8::Local<v8::Value> baseUrlOpt = options->Get(
Nan::New<v8::String>("baseUrl").ToLocalChecked());
v8::Local<v8::Value> encodingOpt = options->Get(
Nan::New<v8::String>("encoding").ToLocalChecked());
// the base URL that will be used for this document
v8::String::Utf8Value baseUrl_(baseUrlOpt->ToString());
const char * baseUrl = *baseUrl_;
if (!baseUrlOpt->IsString()) {
baseUrl = NULL;
}
// the encoding to be used for this document
// (leave NULL for libxml to autodetect)
v8::String::Utf8Value encoding_(encodingOpt->ToString());
const char * encoding = *encoding_;
if (!encodingOpt->IsString()) {
encoding = NULL;
}
int opts = (int) getParserOptions(options);
xmlDocPtr doc;
if (!node::Buffer::HasInstance(info[0])) {
// Parse a string
v8::String::Utf8Value str(info[0]->ToString());
doc = xmlReadMemory(*str, str.length(), baseUrl, "UTF-8", opts);
}
else {
// Parse a buffer
v8::Local<v8::Object> buf = info[0]->ToObject();
doc = xmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
baseUrl, encoding, opts);
}
xmlSetStructuredErrorFunc(NULL, NULL);
if (!doc) {
xmlError* error = xmlGetLastError();
if (error) {
return Nan::ThrowError(XmlSyntaxError::BuildSyntaxError(error));
}
return Nan::ThrowError("Could not parse XML string");
}
v8::Local<v8::Object> doc_handle = XmlDocument::New(doc);
Nan::Set(doc_handle, Nan::New<v8::String>("errors").ToLocalChecked(), errors);
xmlNode* root_node = xmlDocGetRootElement(doc);
if (root_node == NULL) {
return Nan::ThrowError("parsed document has no root element");
}
// create the xml document handle to return
return info.GetReturnValue().Set(doc_handle);
}
NAN_METHOD(XmlDocument::Validate)
{
Nan::HandleScope scope;
v8::Local<v8::Array> errors = Nan::New<v8::Array>();
xmlResetLastError();
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
XmlSyntaxError::PushToArray);
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
XmlDocument* documentSchema = Nan::ObjectWrap::Unwrap<XmlDocument>(info[0]->ToObject());
xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(documentSchema->xml_obj);
if (parser_ctxt == NULL) {
return Nan::ThrowError("Could not create context for schema parser");
}
xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt);
if (schema == NULL) {
return Nan::ThrowError("Invalid XSD schema");
}
xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema);
if (valid_ctxt == NULL) {
return Nan::ThrowError("Unable to create a validation context for the schema");
}
bool valid = xmlSchemaValidateDoc(valid_ctxt, document->xml_obj) == 0;
xmlSetStructuredErrorFunc(NULL, NULL);
info.Holder()->Set(Nan::New<v8::String>("validationErrors").ToLocalChecked(), errors);
xmlSchemaFreeValidCtxt(valid_ctxt);
xmlSchemaFree(schema);
xmlSchemaFreeParserCtxt(parser_ctxt);
return info.GetReturnValue().Set(Nan::New<v8::Boolean>(valid));
}
NAN_METHOD(XmlDocument::RngValidate)
{
Nan::HandleScope scope;
v8::Local<v8::Array> errors = Nan::New<v8::Array>();
xmlResetLastError();
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
XmlSyntaxError::PushToArray);
XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
XmlDocument* documentSchema = Nan::ObjectWrap::Unwrap<XmlDocument>(info[0]->ToObject());
xmlRelaxNGParserCtxtPtr parser_ctxt = xmlRelaxNGNewDocParserCtxt(documentSchema->xml_obj);
if (parser_ctxt == NULL) {
return Nan::ThrowError("Could not create context for RELAX NG schema parser");
}
xmlRelaxNGPtr schema = xmlRelaxNGParse(parser_ctxt);
if (schema == NULL) {
return Nan::ThrowError("Invalid RELAX NG schema");
}
xmlRelaxNGValidCtxtPtr valid_ctxt = xmlRelaxNGNewValidCtxt(schema);
if (valid_ctxt == NULL) {
return Nan::ThrowError("Unable to create a validation context for the RELAX NG schema");
}
bool valid = xmlRelaxNGValidateDoc(valid_ctxt, document->xml_obj) == 0;
xmlSetStructuredErrorFunc(NULL, NULL);
info.Holder()->Set(Nan::New<v8::String>("validationErrors").ToLocalChecked(), errors);
xmlRelaxNGFreeValidCtxt(valid_ctxt);
xmlRelaxNGFree(schema);
xmlRelaxNGFreeParserCtxt(parser_ctxt);
return info.GetReturnValue().Set(Nan::New<v8::Boolean>(valid));
}
/// this is a blank object with prototype methods
/// not exposed to the user and not called from js
NAN_METHOD(XmlDocument::New)
{
Nan::HandleScope scope;
v8::String::Utf8Value version(info[0]->ToString());
xmlDoc* doc = xmlNewDoc((const xmlChar*)(*version));
XmlDocument* document = new XmlDocument(doc);
document->Wrap(info.Holder());
return info.GetReturnValue().Set(info.Holder());
}
XmlDocument::XmlDocument(xmlDoc* doc)
: xml_obj(doc)
{
xml_obj->_private = this;
}
XmlDocument::~XmlDocument()
{
xml_obj->_private = NULL;
xmlFreeDoc(xml_obj);
}
void
XmlDocument::Initialize(v8::Handle<v8::Object> target)
{
Nan::HandleScope scope;
v8::Local<v8::FunctionTemplate> tmpl =
Nan::New<v8::FunctionTemplate>(New);
tmpl->SetClassName(Nan::New<v8::String>("Document").ToLocalChecked());
constructor_template.Reset( tmpl);
tmpl->InstanceTemplate()->SetInternalFieldCount(1);
/// setup internal methods for bindings
Nan::SetPrototypeMethod(tmpl,
"_root",
XmlDocument::Root);
Nan::SetPrototypeMethod(tmpl,
"_version",
XmlDocument::Version);
Nan::SetPrototypeMethod(tmpl,
"_encoding",
XmlDocument::Encoding);
Nan::SetPrototypeMethod(tmpl,
"_toString",
XmlDocument::ToString);
Nan::SetPrototypeMethod(tmpl,
"_validate",
XmlDocument::Validate);
Nan::SetPrototypeMethod(tmpl,
"_rngValidate",
XmlDocument::RngValidate);
Nan::SetPrototypeMethod(tmpl,
"_setDtd",
XmlDocument::SetDtd);
Nan::SetPrototypeMethod(tmpl,
"_getDtd",
XmlDocument::GetDtd);
Nan::SetMethod(target, "fromXml", XmlDocument::FromXml);
Nan::SetMethod(target, "fromHtml", XmlDocument::FromHtml);
// used to create new document handles
Nan::Set(target, Nan::New<v8::String>("Document").ToLocalChecked(), tmpl->GetFunction());
XmlNode::Initialize(target);
XmlNamespace::Initialize(target);
}
} // namespcae libxmljs