@@ -2,19 +2,21 @@ const concat = require('concat-stream')
2
2
const jsonld = require ( 'jsonld' )
3
3
const rdf = require ( 'rdf-data-model' )
4
4
const Readable = require ( 'readable-stream' )
5
+ const dataModel = require ( '@rdfjs/data-model' )
6
+ const terms = require ( 'rdf-terms' )
5
7
6
8
class ParserStream extends Readable {
7
9
constructor ( input , options ) {
8
10
super ( {
9
11
objectMode : true ,
10
- read : ( ) => { }
12
+ read : ( ) => {
13
+ }
11
14
} )
12
15
13
16
options = options || { }
14
17
15
18
this . baseIRI = options . baseIRI || ''
16
19
this . context = options . context
17
- this . factory = options . factory || rdf
18
20
this . blankNodes = { }
19
21
20
22
const concatStream = concat ( { encoding : 'string' } , ( data ) => {
@@ -30,59 +32,49 @@ class ParserStream extends Readable {
30
32
this . emit ( 'error' , err )
31
33
} )
32
34
} )
33
-
34
35
input . pipe ( concatStream )
35
36
36
37
input . on ( 'error' , ( err ) => {
37
38
this . emit ( 'error' , err )
38
39
} )
39
40
}
40
41
41
- term ( options ) {
42
- if ( options . type === 'IRI' ) {
43
- return this . factory . namedNode ( options . value )
44
- }
45
-
46
- if ( options . type === 'blank node' ) {
47
- if ( ! ( options . value in this . blankNodes ) ) {
48
- this . blankNodes [ options . value ] = this . factory . blankNode ( )
49
- }
50
-
51
- return this . blankNodes [ options . value ]
42
+ term ( term ) {
43
+ switch ( term . termType ) {
44
+ case 'NamedNode' :
45
+ return dataModel . namedNode ( term . value )
46
+ case 'BlankNode' :
47
+ return dataModel . blankNode ( term . value . substr ( 2 ) ) // Remove the '_:' prefix.
48
+ case 'Literal' :
49
+ return dataModel . literal ( term . value , term . language || term . datatype )
50
+ case 'DefaultGraph' :
51
+ return dataModel . defaultGraph ( )
52
52
}
53
-
54
- return this . factory . literal ( options . value , options . language || options . datatype )
55
53
}
56
54
57
55
parse ( data ) {
58
56
return ParserStream . toJson ( data ) . then ( ( json ) => {
59
- // forward context as prefixes if available
57
+ // forward context as prefixes if available
60
58
if ( '@context' in json ) {
61
59
Object . keys ( json [ '@context' ] ) . forEach ( ( prefix ) => {
62
- this . emit ( 'prefix' , prefix , this . factory . namedNode ( json [ '@context' ] [ prefix ] ) )
60
+ this . emit ( 'prefix' , prefix , dataModel . namedNode ( json [ '@context' ] [ prefix ] ) )
63
61
} )
64
62
}
65
63
66
64
const toRdfOptions = { base : this . baseIRI }
67
65
68
- // use context from options if given
66
+ // use context from options if given
69
67
if ( this . context ) {
70
68
toRdfOptions . expandContext = this . context
71
69
}
72
70
73
71
return jsonld . promises ( ) . toRDF ( json , toRdfOptions )
74
72
} ) . then ( ( rawGraph ) => {
75
- Object . keys ( rawGraph ) . forEach ( ( graphIri ) => {
76
- const graph = graphIri !== '@default' ? this . factory . namedNode ( graphIri ) : null
77
-
78
- rawGraph [ graphIri ] . forEach ( ( triple ) => {
79
- this . push ( this . factory . quad (
80
- this . term ( triple . subject ) ,
81
- this . term ( triple . predicate ) ,
82
- this . term ( triple . object ) ,
83
- graph ) )
84
- } )
85
- } )
73
+ for ( let index in rawGraph ) {
74
+ this . push ( terms . mapTerms ( rawGraph [ index ] , this . term ) )
75
+ }
76
+ } ) . catch ( ( error ) => {
77
+ this . emit ( 'error' , error )
86
78
} )
87
79
}
88
80
0 commit comments