@@ -11,6 +11,7 @@ function TurtleFragmentIterator(source, fragmentUrl) {
11
11
if ( ! ( this instanceof TurtleFragmentIterator ) )
12
12
return new TurtleFragmentIterator ( source , fragmentUrl ) ;
13
13
TransformIterator . call ( this , source ) ;
14
+ this . _fragmentUrl = fragmentUrl ;
14
15
15
16
// Expose an additional metadata stream
16
17
this . metadataStream = new BufferedIterator ( ) ;
@@ -26,19 +27,24 @@ function TurtleFragmentIterator(source, fragmentUrl) {
26
27
27
28
// Convert Turtle into triples using the N3 parser
28
29
this . _parser = new N3 . Parser ( { documentURI : fragmentUrl } ) ;
29
- this . _parser . parse ( function ( error , triple ) {
30
- if ( error )
31
- self . emit ( 'error' , error ) ;
32
- else if ( triple )
33
- self . _processTriple ( triple ) ;
30
+ this . _parser . parse ( {
31
+ // Use dummy stream to capture `data` and `end` callbacks
32
+ on : function ( event , callback ) {
33
+ if ( event === 'data' ) self . _parseData = callback ;
34
+ else if ( event === 'end' ) self . _parseEnd = callback ;
35
+ } ,
36
+ } ,
37
+ // Process each triple and emit possible errors
38
+ function ( error , triple ) {
39
+ if ( error ) self . emit ( 'error' , error ) ;
40
+ else if ( triple ) self . _processTriple ( triple ) ;
34
41
} ) ;
35
- this . _fragmentUrl = fragmentUrl ;
36
42
}
37
43
TransformIterator . subclass ( TurtleFragmentIterator ) ;
38
44
39
45
// Sends a chunk of Turtle to the N3 parser to convert it to triples
40
46
TurtleFragmentIterator . prototype . _transform = function ( chunk , done ) {
41
- this . _parser . addChunk ( chunk ) , done ( ) ;
47
+ this . _parseData ( chunk ) , done ( ) ;
42
48
} ;
43
49
44
50
// Sends the given parsed triple to the data or metadata stream
@@ -54,7 +60,7 @@ TurtleFragmentIterator.prototype._processTriple = function (triple) {
54
60
// Closes the streams after the source has ended
55
61
TurtleFragmentIterator . prototype . _flush = function ( done ) {
56
62
// Ensure the parser processes possible pending triples
57
- this . _parser && this . _parser . end ( ) ;
63
+ this . _parseEnd && this . _parseEnd ( ) ;
58
64
// Once all triples have been processed, close both streams
59
65
this . metadataStream . close ( ) ;
60
66
done ( ) ;
0 commit comments