forked from xmppo/node-expat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-expat.cc
453 lines (371 loc) · 12.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
#include <node.h>
#include <node_version.h>
#include <node_object_wrap.h>
#include <node_buffer.h>
extern "C" {
#include <expat.h>
}
using namespace v8;
using namespace node;
static Persistent<String> sym_startElement, sym_endElement,
sym_startCdata, sym_endCdata,
sym_text, sym_processingInstruction,
sym_comment, sym_xmlDecl, sym_entityDecl,
sym_emit;
class Parser : public ObjectWrap {
public:
static void Initialize(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "parse", Parse);
NODE_SET_PROTOTYPE_METHOD(t, "setEncoding", SetEncoding);
NODE_SET_PROTOTYPE_METHOD(t, "getError", GetError);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
NODE_SET_PROTOTYPE_METHOD(t, "resume", Resume);
NODE_SET_PROTOTYPE_METHOD(t, "reset", Reset);
NODE_SET_PROTOTYPE_METHOD(t, "getCurrentLineNumber", GetCurrentLineNumber);
NODE_SET_PROTOTYPE_METHOD(t, "getCurrentColumnNumber", GetCurrentColumnNumber);
NODE_SET_PROTOTYPE_METHOD(t, "getCurrentByteIndex", GetCurrentByteIndex);
target->Set(String::NewSymbol("Parser"), t->GetFunction());
sym_startElement = NODE_PSYMBOL("startElement");
sym_endElement = NODE_PSYMBOL("endElement");
sym_startCdata = NODE_PSYMBOL("startCdata");
sym_endCdata = NODE_PSYMBOL("endCdata");
sym_text = NODE_PSYMBOL("text");
sym_processingInstruction = NODE_PSYMBOL("processingInstruction");
sym_comment = NODE_PSYMBOL("comment");
sym_xmlDecl = NODE_PSYMBOL("xmlDecl");
sym_entityDecl = NODE_PSYMBOL("entityDecl");
sym_emit = NODE_PSYMBOL("emit");
}
protected:
/*** Constructor ***/
static Handle<Value> New(const Arguments& args)
{
HandleScope scope;
XML_Char *encoding = NULL;
if (args.Length() == 1 && args[0]->IsString())
{
encoding = new XML_Char[32];
args[0]->ToString()->WriteAscii(encoding, 0, 32);
}
Parser *parser = new Parser(encoding);
if (encoding)
delete[] encoding;
parser->Wrap(args.This());
return args.This();
}
Parser(const XML_Char *encoding)
: 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);
}
/*** parse() ***/
static Handle<Value> Parse(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
Local<String> str;
int isFinal = 0;
/* Argument 2: isFinal :: Bool */
if (args.Length() >= 2)
{
isFinal = args[1]->IsTrue();
}
/* Argument 1: buf :: String or Buffer */
if (args.Length() >= 1 && args[0]->IsString())
{
str = args[0]->ToString();
return scope.Close(parser->parseString(**str, isFinal) ? True() : False());
}
else if (args.Length() >= 1 && args[0]->IsObject())
{
Local<Object> obj = args[0]->ToObject();
if (Buffer::HasInstance(obj))
{
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj);
return scope.Close(parser->parseBuffer(*buffer, isFinal) ? True() : False());
#else
return scope.Close(parser->parseBuffer(obj, isFinal) ? True() : False());
#endif
}
else
return ThrowException(
Exception::TypeError(
String::New("Parse buffer must be String or Buffer")));
}
else
return ThrowException(
Exception::TypeError(
String::New("Parse buffer must be String or Buffer")));
}
/** Parse a v8 String by first writing it to the expat parser's
buffer */
bool parseString(String &str, int isFinal)
{
int len = str.Utf8Length();
if (len == 0)
return true;
void *buf = XML_GetBuffer(parser, len);
assert(buf != NULL);
assert(str.WriteUtf8(static_cast<char *>(buf), len) == len);
return XML_ParseBuffer(parser, len, isFinal) != XML_STATUS_ERROR;
}
/** Parse a node.js Buffer directly */
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3
bool parseBuffer(Buffer &buffer, int isFinal)
{
return XML_Parse(parser, buffer.data(), buffer.length(), isFinal) != XML_STATUS_ERROR;
}
#else
bool parseBuffer(Local<Object> buffer, int isFinal)
{
return XML_Parse(parser, Buffer::Data(buffer), Buffer::Length(buffer), isFinal) != XML_STATUS_ERROR;
}
#endif
/*** setEncoding() ***/
static Handle<Value> SetEncoding(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
if (args.Length() == 1 && args[0]->IsString())
{
XML_Char *encoding = new XML_Char[32];
args[0]->ToString()->WriteAscii(encoding, 0, 32);
int status = parser->setEncoding(encoding);
delete[] encoding;
return scope.Close(status ? True() : False());
}
else
return False();
}
int setEncoding(XML_Char *encoding)
{
return XML_SetEncoding(parser, encoding) != 0;
}
/*** getError() ***/
static Handle<Value> GetError(const Arguments& args)
{
HandleScope scope;
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
const XML_LChar *error = parser->getError();
if (error)
return scope.Close(String::New(error));
else
return scope.Close(Null());
}
/*** stop() ***/
static Handle<Value> Stop(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
int status = parser->stop();
return scope.Close(status ? True() : False());
}
int stop()
{
return XML_StopParser(parser, XML_TRUE) != 0;
}
/*** resume() ***/
static Handle<Value> Resume(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
int status = parser->resume();
return scope.Close(status ? True() : False());
}
int resume()
{
return XML_ResumeParser(parser) != 0;
}
static Handle<Value> Reset(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
XML_Char *encoding = NULL;
if (args.Length() == 1 && args[0]->IsString())
{
encoding = new XML_Char[32];
args[0]->ToString()->WriteAscii(encoding, 0, 32);
}
int status = parser->reset(encoding);
if (status)
parser->attachHandlers();
return scope.Close(status ? True() : 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 Handle<Value> GetCurrentLineNumber(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
return scope.Close(Integer::NewFromUnsigned(parser->getCurrentLineNumber()));
}
uint32_t getCurrentLineNumber()
{
return XML_GetCurrentLineNumber(parser);
}
static Handle<Value> GetCurrentColumnNumber(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
return scope.Close(Integer::NewFromUnsigned(parser->getCurrentColumnNumber()));
}
uint32_t getCurrentColumnNumber()
{
return XML_GetCurrentColumnNumber(parser);
}
static Handle<Value> GetCurrentByteIndex(const Arguments& args)
{
Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
HandleScope scope;
return scope.Close(Integer::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)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Collect atts into JS object */
Local<Object> attr = Object::New();
for(const XML_Char **atts1 = atts; *atts1; atts1 += 2)
attr->Set(String::New(atts1[0]), String::New(atts1[1]));
/* Trigger event */
Handle<Value> argv[3] = { sym_startElement,
String::New(name),
attr };
parser->Emit(3, argv);
}
static void EndElement(void *userData,
const XML_Char *name)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[2] = { sym_endElement, String::New(name) };
parser->Emit(2, argv);
}
static void StartCdata(void *userData)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[1] = { sym_startCdata };
parser->Emit(1, argv);
}
static void EndCdata(void *userData)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[1] = { sym_endCdata };
parser->Emit(1, argv);
}
static void Text(void *userData,
const XML_Char *s, int len)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[2] = { sym_text,
String::New(s, len) };
parser->Emit(2, argv);
}
static void ProcessingInstruction(void *userData,
const XML_Char *target, const XML_Char *data)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[3] = { sym_processingInstruction,
String::New(target),
String::New(data) };
parser->Emit(3, argv);
}
static void Comment(void *userData,
const XML_Char *data)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[2] = { sym_comment, String::New(data) };
parser->Emit(2, argv);
}
static void XmlDecl(void *userData,
const XML_Char *version, const XML_Char *encoding,
int standalone)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[4] = { sym_xmlDecl,
version ? String::New(version) : Null(),
encoding ? String::New(encoding) : Null(),
Boolean::New(standalone) };
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)
{
Parser *parser = reinterpret_cast<Parser *>(userData);
/* Trigger event */
Handle<Value> argv[8] = { sym_entityDecl,
entityName ? String::New(entityName) : Null(),
Boolean::New(is_parameter_entity),
value ? String::New(value, value_length) : Null(),
base ? String::New(base) : Null(),
systemId ? String::New(systemId) : Null(),
publicId ? String::New(publicId) : Null(),
notationName ? String::New(notationName) : Null(),
};
parser->Emit(8, argv);
}
void Emit(int argc, Handle<Value> argv[])
{
HandleScope scope;
Local<Function> emit = Local<Function>::Cast(handle_->Get(sym_emit));
emit->Call(handle_, argc, argv);
}
};
extern "C"
{
void init(Handle<Object> target)
{
HandleScope scope;
Parser::Initialize(target);
}
// Toby
NODE_MODULE(expat,init);
}