forked from MatthD/node-libxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibxml.cpp
517 lines (473 loc) · 17.4 KB
/
libxml.cpp
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
/**
* This is the main Class script, We use NODE-ADDON-API (N-API) as a wrapper to libxml2 c++ functions
*/
#include "libxml.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <assert.h>
Napi::Object Libxml::Init(Napi::Env env, Napi::Object exports)
{
// This method is used to hook the accessor and method callbacks
Napi::Function func = DefineClass(env, "Libxml", {
InstanceMethod("loadXml", &Libxml::loadXml),
InstanceMethod("loadXmlFromString", &Libxml::loadXmlFromString),
InstanceMethod("loadDtds", &Libxml::loadDtds),
InstanceMethod("loadSchemas", &Libxml::loadSchemas),
InstanceMethod("validateAgainstDtds", &Libxml::validateAgainstDtds),
InstanceMethod("validateAgainstSchemas", &Libxml::validateAgainstSchemas),
InstanceMethod("xpathSelect", &Libxml::xpathSelect),
InstanceMethod("getDtd", &Libxml::getDtd),
InstanceMethod("freeXml", &Libxml::freeXml),
InstanceMethod("freeDtds", &Libxml::freeDtds),
InstanceMethod("freeSchemas", &Libxml::freeSchemas),
InstanceMethod("clearAll", &Libxml::clearAll),
InstanceMethod("getMaxErrorNumber", &Libxml::getMaxErrorNumber),
InstanceMethod("setMaxErrorNumber", &Libxml::setMaxErrorNumber),
});
// Create a peristent reference to the class constructor. This will allow
// a function called on a class prototype and a function
// called on instance of a class to be distinguished from each other.
constructor = Napi::Persistent(func);
// Call the SuppressDestruct() method on the static data prevent the calling
// to this destructor to reset the reference when the environment is no longer
// available.
constructor.SuppressDestruct();
exports.Set("Libxml", func);
return exports;
}
Libxml::Libxml(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Libxml>(info)
{
Napi::Env env = info.Env();
xmlInitParser();
}
Napi::FunctionReference Libxml::constructor;
Napi::Value Libxml::loadXml(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
if (info.Length() < 1)
{
Napi::TypeError::New(env, "loadXml requires at least 1 argument").ThrowAsJavaScriptException();
return Napi::Boolean::New(env, false);
}
int options;
options = (XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
Napi::Array errors = Napi::Array::New(env);
xmlResetLastError();
XmlSyntaxError::env = &env;
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
XmlSyntaxError::PushToArray);
if (this->docPtr != nullptr)
{
xmlFreeDoc(this->docPtr);
this->docPtr = nullptr;
}
Napi::String path = info[0].As<Napi::String>();
this->path = path.ToString();
const char *pathToRead = this->path.c_str();
this->docPtr = xmlReadFile(pathToRead, nullptr, options);
xmlSetStructuredErrorFunc(nullptr, nullptr);
if (this->docPtr == nullptr)
{
this->Value().Set("wellformedErrors", errors);
return Napi::Boolean::New(env, false);
} else {
this->Value().Delete("wellformedErrors");
}
return Napi::Boolean::New(env, true);
}
Napi::Value Libxml::loadXmlFromString(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1)
{
Napi::TypeError::New(env, "loadXmlFromString requires at least 1 argument").ThrowAsJavaScriptException();
return Napi::Boolean::New(env, false);
}
Napi::Array errors = Napi::Array::New(env);
xmlResetLastError();
XmlSyntaxError::env = &env;
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
XmlSyntaxError::PushToArray);
// Those options should be send by the user, it enable/disbale errors, warnings ..
int options = (XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
if(this->docPtr != nullptr){
xmlFreeDoc(this->docPtr);
this->docPtr = nullptr;
}
Napi::String txt = info[0].As<Napi::String>();
std::string txtToRead = txt.Utf8Value();
this->docPtr = xmlReadMemory(txtToRead.c_str(), strlen(txtToRead.c_str()), nullptr, nullptr, options);
xmlSetStructuredErrorFunc(nullptr, nullptr);
if(this->docPtr == nullptr){
// We set property to libxml element only if notWellformed
this->Value().Set("wellformedErrors", errors);
return Napi::Boolean::New(env, false);
}else{
this->Value().Delete("wellformedErrors");
}
return Napi::Boolean::New(env, true);
}
Napi::Value Libxml::loadDtds(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1){
Napi::TypeError::New(env, "loadDtds requires at least 1 argument, an array of DTDs").ThrowAsJavaScriptException();
return env.Undefined();
}
if(!info[0].IsArray()){
Napi::TypeError::New(env, "loadDtds requires an array").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::EscapableHandleScope scope(env);
Napi::Array dtdPaths = info[0].As<Napi::Array>();
Napi::Array errors = Napi::Array::New(env);
xmlResetLastError();
XmlSyntaxError::env = &env;
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
XmlSyntaxError::PushToArray);
for (unsigned int i = 0; i < dtdPaths.Length(); i++){
//Skip elements silently which are not strings
if(dtdPaths.Get(i).IsString()) {
std::string dtdPath = dtdPaths.Get(i).ToString().Utf8Value();
xmlChar* pathDTDCasted = xmlCharStrdup(dtdPath.c_str());
xmlDtdPtr dtd = xmlParseDTD(nullptr, pathDTDCasted);
if (dtd == nullptr) {
//DTD is bad, we set error and not assign it
XmlSyntaxError::PushToArray(errors, dtdPath.c_str());
continue;
}
this->dtdsPaths.push_back(dtd);
}
}
xmlSetStructuredErrorFunc(nullptr, nullptr);
// We set dtdsLoadedErrors property for js side
if(errors.Length()){
this->Value().Set("dtdsLoadedErrors", errors);
} else {
this->Value().Delete("dtdsLoadedErrors");
}
return env.Undefined();
}
Napi::Value Libxml::loadSchemas(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1){
Napi::TypeError::New(env, "loadSchemas requires at least 1 argument, an array of Schemas").ThrowAsJavaScriptException();
return env.Undefined();
}
if(!info[0].IsArray()){
Napi::TypeError::New(env, "loadSchemas requires an array").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::EscapableHandleScope scope(env);
Napi::Array schemasPathsLocal = info[0].As<Napi::Array>();
//set up error handlers
Napi::Array errors = Napi::Array::New(env);
xmlResetLastError();
XmlSyntaxError::env = &env;
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
XmlSyntaxError::PushToArray);
for (unsigned int i = 0; i < schemasPathsLocal.Length(); i++){
// Handle value if string and drop it silently otherwise
if(schemasPathsLocal.Get(i).IsString()) {
Napi::String value = schemasPathsLocal.Get(i).As<Napi::String>();
string pathStr (value.Utf8Value());
const char* path (pathStr.c_str());
xmlSchemaParserCtxtPtr pctxt;
xmlSchemaPtr schema;
// If cannot create Parse schema, just continue
if ((pctxt = xmlSchemaNewParserCtxt(path)) == nullptr) {
XmlSyntaxError::PushToArray(errors, path);
continue;
}
// Loading XML Schema content
schema = xmlSchemaParse(pctxt);
xmlSchemaFreeParserCtxt(pctxt);
if (schema == nullptr) {
XmlSyntaxError::PushToArray(errors, path);
continue;
}
this->schemasPaths.push_back(schema);
}
}
xmlSetStructuredErrorFunc(nullptr, nullptr);
// We set dtdLoadedErrors property for js side
if(errors.Length()){
this->Value().Set("schemasLoadedErrors", errors);
} else {
this->Value().Delete("schemasLoadedErrors");
}
return env.Undefined();
}
Napi::Value Libxml::validateAgainstDtds(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if(this->dtdsPaths.empty()){
return env.Null();
}
// if first param is number then apply it. If not then silently drop it
MaxErrorNumberRestorer maxNumberRestorer;
if(info.Length() > 0 && info[0].IsNumber()) {
XmlSyntaxError::ChangeMaxNumberOfError(info[0].ToNumber());
}
//Setting context of validation
bool oneOfTheDtdValidate = false;
string dtdValidateName;
//If length 0, return null; to implement
Napi::Object errorsValidations = Napi::Object::New(env);
for (vector<xmlDtdPtr>::iterator dtd = this->dtdsPaths.begin(); dtd != this->dtdsPaths.end() ; ++dtd){
const char* dtdName = (const char *)(*dtd)->SystemID;
//set up error handling
Napi::Array errors = Napi::Array::New(env);
xmlResetLastError();
XmlSyntaxError::env = &env;
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
XmlSyntaxError::PushToArray);
Napi::String SystemIDString = Napi::String::New(env, dtdName);
// Context creation for validation
xmlValidCtxtPtr vctxt;
if ((vctxt = xmlNewValidCtxt()) == nullptr) {
continue;
}
//Instead we could set this to disable output : xmlSetStructuredErrorFunc(vctxt,errorsHandler);
//xmlSetStructuredErrorFunc(vctxt, nullptr);
vctxt->userData = nullptr;
vctxt->error = nullptr;
vctxt->warning = nullptr;
// Validation
int result = xmlValidateDtd(vctxt, this->docPtr, *dtd);
// drop error handling function and free context
xmlSetStructuredErrorFunc(nullptr, nullptr);
xmlFreeValidCtxt(vctxt);
//If validation was successfull than break the loop
if(result != 0){
oneOfTheDtdValidate = true;
dtdValidateName = dtdName;
break;
}
// If validation failed add result to error object
errorsValidations.Set(SystemIDString, errors);
}
if(oneOfTheDtdValidate){
this->Value().Delete("validationDtdErrors");
if(dtdValidateName.length()){
return Napi::String::New(env, dtdValidateName);
}else{
return Napi::Boolean::New(env, true);
}
}else{
this->Value().Set("validationDtdErrors", errorsValidations);
return Napi::Boolean::New(env, false);
}
}
Napi::Value Libxml::validateAgainstSchemas(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if(this->schemasPaths.empty()){
return env.Null();
}
MaxErrorNumberRestorer maxNumberRestorer;
if(info[0].IsNumber()){
XmlSyntaxError::ChangeMaxNumberOfError(info[0].ToNumber().Int32Value());
}
//Setting context of validation
const char* schemaValidationErrorsPath;
bool oneOfTheSchemaValidate = false;
string schemaValidateName;
//If length 0, return null; to implement
Napi::Object errorsValidations = Napi::Object::New(env);
for (vector<xmlSchemaPtr>::iterator xsd = this->schemasPaths.begin(); xsd != this->schemasPaths.end() ; ++xsd){
//set up error handling
Napi::Array errors = Napi::Array::New(env);
xmlResetLastError();
XmlSyntaxError::env = &env;
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
XmlSyntaxError::PushToArray);
const char* xsdName = (const char *)(*xsd)->doc->URL;
Napi::String urlSchema = Napi::String::New(env, xsdName);
// Creating the validation context
xmlSchemaValidCtxtPtr vctxt;
if ((vctxt = xmlSchemaNewValidCtxt(*xsd)) == nullptr) {
continue;
}
//Instead we could set this to disable output : xmlSetStructuredErrorFunc(vctxt,errorsHandler);
// xmlSchemaSetValidErrors(vctxt, (xmlSchemaValidityErrorFunc) Libxml::errorsHandler, (xmlSchemaValidityWarningFunc) Libxml::errorsHandler, (void *) Libxml::errorsHandler);
xmlSchemaSetValidErrors(vctxt, nullptr, nullptr, nullptr);
int result = xmlSchemaValidateDoc(vctxt, this->docPtr);
// Stop listening for errors
xmlSetStructuredErrorFunc(nullptr, nullptr);
xmlSchemaFreeValidCtxt(vctxt);
if(result == 0){
oneOfTheSchemaValidate = true;
schemaValidateName = xsdName;
break;
}
errorsValidations.Set(urlSchema, errors);
schemaValidationErrorsPath = xsdName;
}
if(oneOfTheSchemaValidate){
this->Value().Delete("validationSchemaErrors");
if(schemaValidateName.length()){
return Napi::String::New(env, schemaValidateName);
}else{
return Napi::Boolean::New(env, true);
}
}else{
this->Value().Set("validationSchemaErrors", errorsValidations);
return Napi::Boolean::New(env, false);
}
}
Napi::Value Libxml::xpathSelect(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1){
Napi::TypeError::New(env, "xpathSelect requires at least 1 argument").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::EscapableHandleScope scope(env);
Napi::Value res;
Napi::String val = info[0].ToString();
//Need to keep string in memory and get its const char* casted version for libxml2
std::string xpathStr = val.Utf8Value();
const char* xpathToGet(xpathStr.c_str());
xmlChar * xpathExpr = xmlCharStrdup(xpathToGet);
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(this->docPtr);
if(xpathCtx == nullptr) {
Napi::Error::New(env, "Error: unable to create new XPath context").ThrowAsJavaScriptException();
return env.Undefined();
}
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
if(xpathObj == nullptr) {
xmlXPathFreeContext(xpathCtx);
return Napi::Boolean::New(env, false);
}
else if(xpathObj) {
switch (xpathObj->type) {
case XPATH_BOOLEAN:
res = Napi::Boolean::New(env, xpathObj->boolval);
break;
case XPATH_NUMBER:
res = Napi::Number::New(env, xpathObj->floatval);
break;
case XPATH_STRING:
res = Napi::String::New(env, (const char *)xpathObj->stringval);
break;
default:
res = env.Null();
break;
}
}
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return scope.Escape(res);
}
Napi::Value Libxml::getDtd(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// Get DTD using libxml2
xmlDtdPtr dtd = xmlGetIntSubset(this->docPtr);
//return null if no valid xml loaded so far
if (!dtd) {
return env.Null();
}
//Set up return object.
const char* name = (const char *)dtd->name;
const char* extId = (const char *)dtd->ExternalID;
const char* sysId = (const char *)dtd->SystemID;
Napi::Object dtdObject = Napi::Object::New(env);
Napi::Value nameValue = name
? Napi::String::New(env, name)
: env.Null();
Napi::Value extValue = extId
? Napi::String::New(env, extId)
: env.Null();
Napi::Value sysValue = sysId
? Napi::String::New(env, sysId)
: env.Null();
// to get publicId or systemId it's the same ... http://xmlsoft.org/html/libxml-tree.html#xmlDtd
dtdObject.Set("name", nameValue);
dtdObject.Set("externalId", extValue);
dtdObject.Set("systemId", sysValue);
return dtdObject;
}
void Libxml::freeXml(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// Delete Javascript property
this->Value().Delete("wellformedErrors");
// If doc is already null, just do nothing and only available on manual mod
if(this->docPtr == nullptr){
return;
}
// Force clear the memory loaded for XML
xmlFreeDoc(this->docPtr);
this->docPtr = nullptr;
}
void Libxml::freeDtds(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// Delete Javascript property
this->Value().Delete("dtdsLoadedErrors");
this->Value().Delete("validationDtdErrors");
// If dtds is already empty, just stop here
if(this->dtdsPaths.empty()){
return;
}
for (vector<xmlDtdPtr>::iterator dtd = this->dtdsPaths.begin(); dtd != this->dtdsPaths.end() ; ++dtd){
if(*dtd != nullptr){
// Force clear memory DTD loaded
xmlFreeDtd(*dtd);
*dtd = nullptr;
}
}
// clear the vector of dtds
this->dtdsPaths.clear();
return;
}
void Libxml::freeSchemas(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// Delete Javascript property
this->Value().Delete("schemasLoadedErrors");
this->Value().Delete("validationSchemasErrors");
// If dtds is already empty, just stop here
if(this->schemasPaths.empty()){
return;
}
for (vector<xmlSchemaPtr>::iterator xsd = this->schemasPaths.begin(); xsd != this->schemasPaths.end() ; ++xsd){
if(*xsd != nullptr){
// Force clear memory xsd loaded
xmlSchemaFree(*xsd);
*xsd = nullptr;
}
}
// clear the vector of dtds
this->schemasPaths.clear();
//
}
void Libxml::clearAll(const Napi::CallbackInfo& info) {
this->freeXml(info);
this->freeDtds(info);
this->freeSchemas(info);
xmlCleanupParser();
}
Napi::Value Libxml::getMaxErrorNumber(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), XmlSyntaxError::GetMaxNumberOfError());
}
Napi::Value Libxml::setMaxErrorNumber(const Napi::CallbackInfo& info) {
if(info.Length() && info[0].IsNumber()) {
Napi::Number rawJsNumber = info[0].As<Napi::Number>();
if(rawJsNumber.DoubleValue() >= 0) {
XmlSyntaxError::ChangeMaxNumberOfError(rawJsNumber.Uint32Value());
}
}
return Napi::Number::New(info.Env(), XmlSyntaxError::GetMaxNumberOfError());
}
// Initialize native add-on
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
Libxml::Init(env, exports);
return exports;
}
// Register and initialize native add-on
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)