-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (99 loc) · 3.02 KB
/
index.js
File metadata and controls
116 lines (99 loc) · 3.02 KB
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
var stream = require('stream'),
// bindings = require(__dirname + '/build/Debug/bindings');
bindings = require(__dirname + '/build/Release/bindings');
exports.createParser = function (syntax) {
syntax = syntax || 'guess';
var parser = new bindings.Parser(syntax);
return new StreamParser(parser);
};
function StreamParser(parser) {
stream.Transform.call(this, { objectMode: true });
this._parser = parser;
this._started = false;
var self = this;
this._parser.setStatementHandler(function (statement) {
self.push(statement);
});
this._parser.setNamespaceHandler(function (namespaceURI, prefix) {
self.emit('namespace', namespaceURI, prefix);
});
this._parser.setMessageHandler(function (type, message) {
self.emit('message', {
type: type,
text: message
});
});
}
StreamParser.prototype = Object.create(stream.Transform.prototype);
StreamParser.prototype.setBaseURI = function (baseURI) {
this._baseURI = baseURI;
return this;
};
StreamParser.prototype._transform = function (chunk, encoding, cb) {
try {
if (!this._started) {
if (!this._baseURI) {
throw RangeError('base URI not set');
}
this._parser.parseStart(this._baseURI);
this._started = true;
}
this._parser.parseBuffer(chunk);
} catch (e) {
this.emit('error', e);
}
cb();
};
StreamParser.prototype._flush = function (cb) {
try {
this._parser.parseEnd();
} catch (e) {
this.emit('error', e);
}
this.push(null);
cb();
};
exports.createSerializer = function (syntax) {
var serializer = new bindings.Serializer(syntax);
return new StreamSerializer(serializer);
};
function StreamSerializer(serializer) {
stream.Transform.call(this, { objectMode: true });
this._serializer = serializer;
this._started = false;
var self = this;
this._serializer.setDataHandler(function (data) {
self.push(data);
});
this._serializer.setEndHandler(function () {
self.push();
});
}
StreamSerializer.prototype = Object.create(stream.Transform.prototype);
StreamSerializer.prototype.setBaseURI = function (baseURI) {
this._baseURI = baseURI;
return this;
};
StreamSerializer.prototype.serializeStatement = function (statement) {
if (!this._started) {
if (!this._baseURI) {
throw RangeError('base URI not set');
}
this._serializer.serializeStart(this._baseURI);
this._started = true;
}
this._serializer.serializeStatement(statement);
return this;
};
StreamSerializer.prototype.serializeEnd = function (statement) {
this._serializer.serializeEnd();
return this;
};
StreamSerializer.prototype._transform = function (chunk, encoding, cb) {
StreamSerializer.prototype.serializeStatement.call(this, chunk);
cb();
};
StreamSerializer.prototype._flush = function (cb) {
StreamSerializer.prototype.serializeEnd.call(this);
cb();
};