Skip to content

Commit 72fdaff

Browse files
committed
Can now limit the number of error kept for validations
1 parent 725409d commit 72fdaff

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

libxml-syntax-error.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* This is the Errros Class script, for wellformed & validation DTD
33
*/
44
#include <cstring>
5+
#include <iostream>
56
#include "libxml-syntax-error.h"
67

78
void setStringField(v8::Local<v8::Object> obj, const char* name, const char* value) {
@@ -37,12 +38,20 @@ XmlSyntaxError::BuildSyntaxError(xmlError* error) {
3738
return scope.Escape(err);
3839
}
3940

41+
int XmlSyntaxError::maxError {100};
42+
43+
void XmlSyntaxError::ChangeMaxNumberOfError(int max){
44+
XmlSyntaxError::maxError = max;
45+
}
46+
4047
void
4148
XmlSyntaxError::PushToArray(void* errs, xmlError* error) {
4249
Nan::HandleScope scope;
4350
v8::Local<v8::Array> errors = *reinterpret_cast<v8::Local<v8::Array>*>(errs);
51+
if(errors->Length() >= maxError){
52+
return;
53+
}
4454
v8::Local<v8::Function> push = v8::Local<v8::Function>::Cast(errors->Get(Nan::New<v8::String>("push").ToLocalChecked()));
45-
4655
v8::Local<v8::Value> argv[1] = { XmlSyntaxError::BuildSyntaxError(error) };
4756
push->Call(errors, 1, argv);
4857
}

libxml-syntax-error.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#include "libxml.h"
77

88
class XmlSyntaxError {
9+
static int maxError;
910
public:
11+
static void ChangeMaxNumberOfError(int max);
12+
1013
// push xmlError onto v8::Array
1114
static void PushToArray(void* errs, xmlError* error);
1215

libxml.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ NAN_METHOD(Libxml::validateAgainstDtds){
227227
return;
228228
}
229229

230+
if(int newMaxNbError = info[0]->IntegerValue()){
231+
XmlSyntaxError::ChangeMaxNumberOfError(newMaxNbError);
232+
}
233+
230234
//Setting contexte of validation
231235
const char* dtdValidationErrorsPath;
232236
bool oneOfTheDtdValidate = false;
@@ -238,6 +242,7 @@ NAN_METHOD(Libxml::validateAgainstDtds){
238242

239243
for (vector<xmlDtdPtr>::iterator dtd = libxml->dtdsPaths.begin(); dtd != libxml->dtdsPaths.end() ; ++dtd){
240244
const char* dtdName = (const char *)(*dtd)->SystemID;
245+
//Local<Array> errors = New<v8::Array>(3);
241246
v8::Local<v8::Array> errors = Nan::New<v8::Array>();
242247
xmlResetLastError();
243248
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
@@ -286,6 +291,9 @@ NAN_METHOD(Libxml::validateAgainstSchemas){
286291
return;
287292
}
288293

294+
if(int newMaxNbError = info[0]->IntegerValue()){
295+
XmlSyntaxError::ChangeMaxNumberOfError(newMaxNbError);
296+
}
289297
//Setting contexte of validation
290298
const char* schemaValidationErrorsPath;
291299
bool oneOfTheSchemaValidate = false;

test/data/test-not-valid-dtd.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<!DOCTYPE article PUBLIC "my doctype of doom" "mydoctype.dtd">
22
<data>
33
Parthénogénese du poulpe en milieu sub-aquatique
4+
<jojo>
5+
</jojo>
6+
<bar>
7+
</bar>
8+
<foo>
9+
</foo>
10+
<foo2>
11+
</foo2>
412
</data>

test/libxml-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ describe('Node-Libxml', function () {
3535
let libxml = new Libxml();
3636
let testInvalidWf = libxml.loadXml('test/data/test-not-valid-dtd.xml');
3737
libxml.loadDtds(['test/dtd/mydoctype.dtd']);
38-
let testInvalid = libxml.validateAgainstDtds();
38+
let testInvalid = libxml.validateAgainstDtds(3);
3939
expect(testInvalidWf).to.be.true;
4040
expect(testInvalid).to.be.false;
4141
expect(libxml).to.have.property('validationDtdErrors');
4242
expect(libxml.validationDtdErrors).to.be.an('object');
43+
expect(libxml.validationDtdErrors['test/dtd/mydoctype.dtd'].length).to.be.equal(3);
4344
libxml.freeDtds();
4445
libxml.freeXml();
4546
});

0 commit comments

Comments
 (0)