-
Notifications
You must be signed in to change notification settings - Fork 97
/
node-expat.cc
500 lines (409 loc) · 14.2 KB
/
node-expat.cc
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
#include <nan.h>
extern "C" {
#include <expat.h>
}
using namespace v8;
using namespace node;
class Parser : public Nan::ObjectWrap {
public:
static void Initialize(Local<Object> target)
{
Nan::HandleScope scope;
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(t, "parse", Parse);
Nan::SetPrototypeMethod(t, "setEncoding", SetEncoding);
Nan::SetPrototypeMethod(t, "setUnknownEncoding", SetUnknownEncoding);
Nan::SetPrototypeMethod(t, "getError", GetError);
Nan::SetPrototypeMethod(t, "stop", Stop);
Nan::SetPrototypeMethod(t, "resume", Resume);
Nan::SetPrototypeMethod(t, "reset", Reset);
Nan::SetPrototypeMethod(t, "getCurrentLineNumber", GetCurrentLineNumber);
Nan::SetPrototypeMethod(t, "getCurrentColumnNumber", GetCurrentColumnNumber);
Nan::SetPrototypeMethod(t, "getCurrentByteIndex", GetCurrentByteIndex);
Nan::Set(target, Nan::New("Parser").ToLocalChecked(), Nan::GetFunction(t).ToLocalChecked());
}
protected:
/*** Constructor ***/
static NAN_METHOD(New)
{
Nan::HandleScope scope;
XML_Char *encoding = NULL;
if (info.Length() == 1 && info[0]->IsString())
{
Nan::Utf8String encodingArg(info[0]);
encoding = new XML_Char[encodingArg.length() + 1];
strcpy(encoding, *encodingArg);
}
Parser *parser = new Parser(encoding);
if (encoding)
delete[] encoding;
parser->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
Parser(const XML_Char *encoding)
: Nan::ObjectWrap()
{
parser = XML_ParserCreate(encoding);
assert(parser != NULL);
attachHandlers();
}
~Parser()
{
XML_ParserFree(parser);
}
void attachHandlers()
{
XML_SetUserData(parser, this);
XML_SetElementHandler(parser, StartElement, EndElement);
XML_SetCharacterDataHandler(parser, Text);
XML_SetCdataSectionHandler(parser, StartCdata, EndCdata);
XML_SetProcessingInstructionHandler(parser, ProcessingInstruction);
XML_SetCommentHandler(parser, Comment);
XML_SetXmlDeclHandler(parser, XmlDecl);
XML_SetEntityDeclHandler(parser, EntityDecl);
XML_SetUnknownEncodingHandler(parser, UnknownEncoding, this);
}
/*** parse() ***/
static NAN_METHOD(Parse)
{
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
Nan::HandleScope scope;
int isFinal = 0;
/* Argument 2: isFinal :: Bool */
if (info.Length() >= 2)
{
isFinal = info[1]->IsTrue();
}
/* Argument 1: buf :: String or Buffer */
if (info.Length() >= 1 && info[0]->IsString())
{
Local<String> str = Nan::To<String>(info[0]).ToLocalChecked();
info.GetReturnValue().Set(parser->parseString(str, isFinal) ? Nan::True() : Nan::False());
}
else if (info.Length() >= 1 && info[0]->IsObject())
{
Local<Object> obj = Nan::To<Object>(info[0]).ToLocalChecked();
if (Buffer::HasInstance(obj))
{
info.GetReturnValue().Set(parser->parseBuffer(obj, isFinal) ? Nan::True() : Nan::False());
}
else
{
Nan::ThrowTypeError("Parse buffer must be String or Buffer");
return;
}
}
else {
Nan::ThrowTypeError("Parse buffer must be String or Buffer");
return;
}
}
/** Parse a v8 String by first writing it to the expat parser's
buffer */
bool parseString(Local<String> str, int isFinal)
{
int len = Nan::Utf8String(str).length();
if (len == 0)
return true;
void *buf = XML_GetBuffer(parser, len);
assert(buf != NULL);
assert(Nan::DecodeWrite(static_cast<char *>(buf), len, str, Nan::Encoding::UTF8) == len);
return XML_ParseBuffer(parser, len, isFinal) != XML_STATUS_ERROR;
}
/** Parse a node.js Buffer directly */
bool parseBuffer(Local<Object> buffer, int isFinal)
{
return XML_Parse(parser, Buffer::Data(buffer), Buffer::Length(buffer), isFinal) != XML_STATUS_ERROR;
}
/*** setEncoding() ***/
static NAN_METHOD(SetEncoding)
{
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
Nan::HandleScope scope;
if (info.Length() == 1 && info[0]->IsString())
{
Nan::Utf8String encoding(info[0]);
int status = parser->setEncoding(*encoding);
info.GetReturnValue().Set(status ? Nan::True() : Nan::False());
}
else
info.GetReturnValue().Set(Nan::False());
}
int setEncoding(XML_Char *encoding)
{
return XML_SetEncoding(parser, encoding) != 0;
}
/*** getError() ***/
static NAN_METHOD(GetError)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
const XML_LChar *error = parser->getError();
if (error)
info.GetReturnValue().Set(Nan::New(error).ToLocalChecked());
else
info.GetReturnValue().Set(Nan::Null());
}
/*** stop() ***/
static NAN_METHOD(Stop)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
int status = parser->stop();
info.GetReturnValue().Set(status ? Nan::True() : Nan::False());
}
int stop()
{
return XML_StopParser(parser, XML_TRUE) != 0;
}
/*** resume() ***/
static NAN_METHOD(Resume)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
int status = parser->resume();
info.GetReturnValue().Set(status ? Nan::True() : Nan::False());
}
int resume()
{
return XML_ResumeParser(parser) != 0;
}
static NAN_METHOD(Reset)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
XML_Char *encoding = NULL;
if (info.Length() == 1 && info[0]->IsString())
{
Nan::Utf8String encodingArg(info[0]);
encoding = new XML_Char[encodingArg.length() + 1];
strcpy(encoding, *encodingArg);
}
int status = parser->reset(encoding);
if (encoding)
delete[] encoding;
if (status)
parser->attachHandlers();
info.GetReturnValue().Set(status ? Nan::True() : Nan::False());
}
int reset(XML_Char *encoding)
{
return XML_ParserReset(parser, encoding) != 0;
}
const XML_LChar *getError()
{
enum XML_Error code;
code = XML_GetErrorCode(parser);
return XML_ErrorString(code);
}
static NAN_METHOD(GetCurrentLineNumber)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
info.GetReturnValue().Set(Nan::New(parser->getCurrentLineNumber()));
}
uint32_t getCurrentLineNumber()
{
return XML_GetCurrentLineNumber(parser);
}
static NAN_METHOD(GetCurrentColumnNumber)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
info.GetReturnValue().Set(Nan::New(parser->getCurrentColumnNumber()));
}
uint32_t getCurrentColumnNumber()
{
return XML_GetCurrentColumnNumber(parser);
}
static NAN_METHOD(GetCurrentByteIndex)
{
Nan::HandleScope scope;
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
info.GetReturnValue().Set(Nan::New(parser->getCurrentByteIndex()));
}
int32_t getCurrentByteIndex()
{
return XML_GetCurrentByteIndex(parser);
}
private:
/* expat instance */
XML_Parser parser;
/* no default ctor */
Parser();
/*** SAX callbacks ***/
/* Should a local HandleScope be used in those callbacks? */
static void StartElement(void *userData,
const XML_Char *name, const XML_Char **atts)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Collect atts into JS object */
Local<Object> attr = Nan::New<Object>();
for(const XML_Char **atts1 = atts; *atts1; atts1 += 2)
Nan::Set(attr, Nan::New(atts1[0]).ToLocalChecked(), Nan::New(atts1[1]).ToLocalChecked());
/* Trigger event */
Local<Value> argv[3] = { Nan::New("startElement").ToLocalChecked(),
Nan::New(name).ToLocalChecked(),
attr };
parser->Emit(3, argv);
}
static void EndElement(void *userData,
const XML_Char *name)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[2] = { Nan::New("endElement").ToLocalChecked(), Nan::New(name).ToLocalChecked() };
parser->Emit(2, argv);
}
static void StartCdata(void *userData)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[1] = { Nan::New("startCdata").ToLocalChecked() };
parser->Emit(1, argv);
}
static void EndCdata(void *userData)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[1] = { Nan::New("endCdata").ToLocalChecked() };
parser->Emit(1, argv);
}
static void Text(void *userData,
const XML_Char *s, int len)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[2] = { Nan::New("text").ToLocalChecked(),
Nan::New(s, len).ToLocalChecked() };
parser->Emit(2, argv);
}
static void ProcessingInstruction(void *userData,
const XML_Char *target, const XML_Char *data)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[3] = { Nan::New("processingInstruction").ToLocalChecked(),
Nan::New(target).ToLocalChecked(),
Nan::New(data).ToLocalChecked() };
parser->Emit(3, argv);
}
static void Comment(void *userData,
const XML_Char *data)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[2] = { Nan::New("comment").ToLocalChecked(), Nan::New(data).ToLocalChecked() };
parser->Emit(2, argv);
}
static void XmlDecl(void *userData,
const XML_Char *version, const XML_Char *encoding,
int standalone)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[4];
argv[0] = Nan::New("xmlDecl").ToLocalChecked();
if (version) argv[1] = Nan::New(version).ToLocalChecked();
else argv[1] = Nan::Null();
if (encoding) argv[2] = Nan::New(encoding).ToLocalChecked();
else argv[2] = Nan::Null();
if (standalone) argv[3] = Nan::True();
else argv[3] = Nan::False();
parser->Emit(4, argv);
}
static void EntityDecl(void *userData, const XML_Char *entityName, int is_parameter_entity,
const XML_Char *value, int value_length, const XML_Char *base,
const XML_Char *systemId, const XML_Char *publicId, const XML_Char *notationName)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Local<Value> argv[8];
argv[0] = Nan::New("entityDecl").ToLocalChecked();
if (entityName) argv[1] = Nan::New(entityName).ToLocalChecked();
else argv[1] = Nan::Null();
if (is_parameter_entity) argv[2] = Nan::True();
else argv[2] = Nan::False();
if (value) argv[3] = Nan::New(value, value_length).ToLocalChecked();
else argv[3] = Nan::Null();
if (base) argv[4] = Nan::New(base).ToLocalChecked();
else argv[4] = Nan::Null();
if (systemId) argv[5] = Nan::New(systemId).ToLocalChecked();
else argv[5] = Nan::Null();
if (publicId) argv[6] = Nan::New(publicId).ToLocalChecked();
else argv[6] = Nan::Null();
if (notationName) argv[7] = Nan::New(notationName).ToLocalChecked();
else argv[7] = Nan::Null();
parser->Emit(8, argv);
}
XML_Encoding *xmlEncodingInfo;
static int UnknownEncoding(void *encodingHandlerData, const XML_Char *name, XML_Encoding *info)
{
Nan::HandleScope scope;
Parser *parser = reinterpret_cast<Parser *>(encodingHandlerData);
/* Trigger event */
parser->xmlEncodingInfo = info;
Local<Value> argv[2];
argv[0] = Nan::New("unknownEncoding").ToLocalChecked();
if (name) argv[1] = Nan::New(name).ToLocalChecked();
else argv[1] = Nan::Null();
parser->Emit(2, argv);
/* Did no event handler invoke setUnknownEncoding()? */
if (parser->xmlEncodingInfo) {
parser->xmlEncodingInfo = NULL;
return XML_STATUS_ERROR;
} else {
return XML_STATUS_OK;
}
}
/**
* Fills xmlEncodingInfo
*/
static NAN_METHOD(SetUnknownEncoding)
{
Parser *parser = Nan::ObjectWrap::Unwrap<Parser>(info.This());
Nan::HandleScope scope;
if (!parser->xmlEncodingInfo)
Nan::ThrowError("setUnknownEncoding() must be synchronously invoked from an unknownEncoding event handler");
if (info.Length() >= 1 && info[0]->IsArray()) {
Local<Array> map = info[0].As<Array>();
/* Copy map */
for(int i = 0; i < 256; i++) {
Local<Value> m = Nan::Get(map, Nan::New(i)).ToLocalChecked();
if (m->IsInt32())
parser->xmlEncodingInfo->map[i] = Nan::To<int32_t>(m).FromJust();
else
Nan::ThrowTypeError("UnknownEncoding map must consist of 256 ints");
}
} else
Nan::ThrowTypeError("SetUnknownEncoding expects a map array");
parser->xmlEncodingInfo = NULL;
return;
}
void Emit(int argc, Local<Value> argv[])
{
Nan::HandleScope scope;
Local<Object> handle = this->handle();
Local<Function> emit = Nan::Get(handle, Nan::New("emit").ToLocalChecked()).ToLocalChecked().As<Function>();
Nan::Callback emitCallback(emit);
Nan::Call(emitCallback, argc, argv);
}
};
extern "C" {
static NAN_MODULE_INIT(InitAll)
{
Parser::Initialize(target);
}
//Changed the name cause I couldn't load the module with - in their names
NODE_MODULE(node_expat, InitAll);
};