Skip to content

Commit b717927

Browse files
committed
imp: readding loadDtds function
1 parent 887cbf0 commit b717927

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

libxml-syntax-error.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ void XmlSyntaxError::PushToArray(void *errs, xmlError *error)
6060
}
6161
Napi::Value castedError = {XmlSyntaxError::BuildSyntaxError(error, *XmlSyntaxError::env)};
6262
errors.Set(errors.Length(), castedError);
63+
}
64+
65+
void XmlSyntaxError::PushToArray(Napi::Array& errors, const char* errorMessage)
66+
{
67+
if (errors.Length() >= maxError)
68+
{
69+
return;
70+
}
71+
Napi::String messageToPush = Napi::String::New(*XmlSyntaxError::env, errorMessage);
72+
errors.Set(errors.Length(), messageToPush);
6373
}

libxml-syntax-error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class XmlSyntaxError
1313
static Napi::Env* env;
1414
static void ChangeMaxNumberOfError(int max);
1515

16-
// push xmlError onto v8::Array
16+
// push xmlError onto Napi::Array
1717
static void PushToArray(void *errs, xmlError *error);
18+
static void PushToArray(Napi::Array& errs, const char* errorMessage);
1819

19-
// create a v8 object for the syntax eror
20+
// create a Napi object for the syntax eror
2021
static Napi::Value BuildSyntaxError(xmlError *error, Napi::Env env);
2122
};
2223

libxml.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Napi::Object Libxml::Init(Napi::Env env, Napi::Object exports)
1414
Napi::Function func = DefineClass(env, "Libxml", {
1515
InstanceMethod("loadXml", &Libxml::loadXml),
1616
InstanceMethod("loadXmlFromString", &Libxml::loadXmlFromString),
17+
InstanceMethod("loadDtds", &Libxml::loadDtds),
1718
InstanceMethod("getDtd", &Libxml::getDtd),
1819
InstanceMethod("freeXml", &Libxml::freeXml)
1920
});
@@ -118,6 +119,47 @@ Napi::Value Libxml::loadXmlFromString(const Napi::CallbackInfo& info) {
118119
return Napi::Boolean::New(env, true);
119120
}
120121

122+
Napi::Value Libxml::loadDtds(const Napi::CallbackInfo& info) {
123+
Napi::Env env = info.Env();
124+
if (info.Length() < 1){
125+
Napi::TypeError::New(env, "loadDtds requires at least 1 argument, an array of DTDs").ThrowAsJavaScriptException();
126+
return env.Undefined();
127+
}
128+
if(!info[0].IsArray()){
129+
Napi::TypeError::New(env, "loadDtds requires an array").ThrowAsJavaScriptException();
130+
return env.Undefined();
131+
}
132+
Napi::EscapableHandleScope scope(env);
133+
Napi::Array dtdPaths = info[0].As<Napi::Array>();
134+
Napi::Array errors = Napi::Array::New(env);
135+
xmlResetLastError();
136+
XmlSyntaxError::env = &env;
137+
xmlSetStructuredErrorFunc(reinterpret_cast<void*>(&errors),
138+
XmlSyntaxError::PushToArray);
139+
for (unsigned int i = 0; i < dtdPaths.Length(); i++){
140+
//Skip elements silently which are not strings
141+
if(dtdPaths.Get(i).IsString()) {
142+
std::string dtdPath = dtdPaths.Get(i).ToString().Utf8Value();
143+
xmlChar* pathDTDCasted = xmlCharStrdup(dtdPath.c_str());
144+
xmlDtdPtr dtd = xmlParseDTD(NULL, pathDTDCasted);
145+
if (dtd == nullptr) {
146+
//DTD is bad, we set error and not assign it
147+
XmlSyntaxError::PushToArray(errors, dtdPath.c_str());
148+
continue;
149+
}
150+
this->dtdsPaths.push_back(dtd);
151+
}
152+
}
153+
xmlSetStructuredErrorFunc(nullptr, nullptr);
154+
// We set dtdsLoadedErrors property for js side
155+
if(errors.Length()){
156+
this->Value().Set("dtdsLoadedErrors", errors);
157+
} else {
158+
this->Value().Delete("dtdsLoadedErrors");
159+
}
160+
return env.Undefined();
161+
}
162+
121163
Napi::Value Libxml::getDtd(const Napi::CallbackInfo& info) {
122164
Napi::Env env = info.Env();
123165
Napi::HandleScope scope(env);

libxml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Libxml : public Napi::ObjectWrap<Libxml>
4040
static Napi::FunctionReference constructor;
4141
Napi::Value loadXml(const Napi::CallbackInfo &info);
4242
Napi::Value loadXmlFromString(const Napi::CallbackInfo& info);
43-
// static Napi::Value loadDtds(const Napi::CallbackInfo& info);
43+
Napi::Value loadDtds(const Napi::CallbackInfo& info);
4444
// // static Napi::Value loadDtdsFromString(const Napi::CallbackInfo& info);
4545
// static Napi::Value loadSchemas(const Napi::CallbackInfo& info);
4646
// static Napi::Value validateAgainstDtds(const Napi::CallbackInfo& info);

test/libxml-test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const expect = require('chai').expect;
55
const fs = require('fs');
66

77
describe('Node-Libxml', function () {
8-
// it('Should return a list of errored path if dtd path is bad', function () {
9-
// let libxml = new Libxml();
10-
// libxml.loadDtds(['test/dtd/mydoctype-not-existing.dtd']);
11-
// expect(libxml).to.have.a.property('dtdsLoadedErrors');
12-
// expect(libxml.dtdsLoadedErrors).to.be.a('array');
13-
// expect(libxml.dtdsLoadedErrors).to.include('test/dtd/mydoctype-not-existing.dtd');
14-
// libxml.freeXml();
15-
// libxml.freeDtds();
16-
// });
8+
it('Should return a list of errored path if dtd path is bad', function () {
9+
let libxml = new Libxml();
10+
libxml.loadDtds(['test/dtd/mydoctype-not-existing.dtd']);
11+
expect(libxml).to.have.a.property('dtdsLoadedErrors');
12+
expect(libxml.dtdsLoadedErrors).to.be.a('array');
13+
expect(libxml.dtdsLoadedErrors).to.include('test/dtd/mydoctype-not-existing.dtd');
14+
libxml.freeXml();
15+
// libxml.freeDtds();
16+
});
1717
// Wellformed & valid
1818
// it('Should return wellformed & valid on a wellformed & valid xml', function () {
1919
// let libxml = new Libxml();

0 commit comments

Comments
 (0)