@@ -10,6 +10,10 @@ const jsonCanonicalize = require('canonicalize');
10
10
const types = require ( './types' ) ;
11
11
const util = require ( './util' ) ;
12
12
13
+ const {
14
+ handleEvent : _handleEvent
15
+ } = require ( './events' ) ;
16
+
13
17
const {
14
18
// RDF,
15
19
// RDF_LIST,
@@ -66,6 +70,20 @@ api.toRDF = (input, options) => {
66
70
graphTerm . value = graphName ;
67
71
} else {
68
72
// skip relative IRIs (not valid RDF)
73
+ if ( options . eventHandler ) {
74
+ _handleEvent ( {
75
+ event : {
76
+ type : [ 'JsonLdEvent' ] ,
77
+ code : 'relative graph reference' ,
78
+ level : 'warning' ,
79
+ message : 'Relative graph reference found.' ,
80
+ details : {
81
+ graph : graphName
82
+ }
83
+ } ,
84
+ options
85
+ } ) ;
86
+ }
69
87
continue ;
70
88
}
71
89
_graphToRDF ( dataset , nodeMap [ graphName ] , graphTerm , issuer , options ) ;
@@ -107,6 +125,20 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
107
125
108
126
// skip relative IRI subjects (not valid RDF)
109
127
if ( ! _isAbsoluteIri ( id ) ) {
128
+ if ( options . eventHandler ) {
129
+ _handleEvent ( {
130
+ event : {
131
+ type : [ 'JsonLdEvent' ] ,
132
+ code : 'relative subject reference' ,
133
+ level : 'warning' ,
134
+ message : 'Relative subject reference found.' ,
135
+ details : {
136
+ subject : id
137
+ }
138
+ } ,
139
+ options
140
+ } ) ;
141
+ }
110
142
continue ;
111
143
}
112
144
@@ -118,18 +150,48 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
118
150
119
151
// skip relative IRI predicates (not valid RDF)
120
152
if ( ! _isAbsoluteIri ( property ) ) {
153
+ if ( options . eventHandler ) {
154
+ _handleEvent ( {
155
+ event : {
156
+ type : [ 'JsonLdEvent' ] ,
157
+ code : 'relative property reference' ,
158
+ level : 'warning' ,
159
+ message : 'Relative property reference found.' ,
160
+ details : {
161
+ property
162
+ }
163
+ } ,
164
+ options
165
+ } ) ;
166
+ }
121
167
continue ;
122
168
}
123
169
124
170
// skip blank node predicates unless producing generalized RDF
125
171
if ( predicate . termType === 'BlankNode' &&
126
172
! options . produceGeneralizedRdf ) {
173
+ if ( options . eventHandler ) {
174
+ _handleEvent ( {
175
+ event : {
176
+ type : [ 'JsonLdEvent' ] ,
177
+ code : 'blank node predicate' ,
178
+ level : 'warning' ,
179
+ message : 'Dropping blank node predicate.' ,
180
+ details : {
181
+ // FIXME: add better issuer API to get reverse mapping
182
+ property : issuer . getOldIds ( )
183
+ . find ( key => issuer . getId ( key ) === property )
184
+ }
185
+ } ,
186
+ options
187
+ } ) ;
188
+ }
127
189
continue ;
128
190
}
129
191
130
192
// convert list, value or node object to triple
131
- const object =
132
- _objectToRDF ( item , issuer , dataset , graphTerm , options . rdfDirection ) ;
193
+ const object = _objectToRDF (
194
+ item , issuer , dataset , graphTerm , options . rdfDirection , options ) ;
133
195
// skip null objects (they are relative IRIs)
134
196
if ( object ) {
135
197
dataset . push ( {
@@ -152,10 +214,11 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
152
214
* @param issuer a IdentifierIssuer for assigning blank node names.
153
215
* @param dataset the array of quads to append to.
154
216
* @param graphTerm the graph term for each quad.
217
+ * @param options the RDF serialization options.
155
218
*
156
219
* @return the head of the list.
157
220
*/
158
- function _listToRDF ( list , issuer , dataset , graphTerm , rdfDirection ) {
221
+ function _listToRDF ( list , issuer , dataset , graphTerm , rdfDirection , options ) {
159
222
const first = { termType : 'NamedNode' , value : RDF_FIRST } ;
160
223
const rest = { termType : 'NamedNode' , value : RDF_REST } ;
161
224
const nil = { termType : 'NamedNode' , value : RDF_NIL } ;
@@ -166,7 +229,8 @@ function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
166
229
let subject = result ;
167
230
168
231
for ( const item of list ) {
169
- const object = _objectToRDF ( item , issuer , dataset , graphTerm , rdfDirection ) ;
232
+ const object = _objectToRDF (
233
+ item , issuer , dataset , graphTerm , rdfDirection , options ) ;
170
234
const next = { termType : 'BlankNode' , value : issuer . getId ( ) } ;
171
235
dataset . push ( {
172
236
subject,
@@ -185,7 +249,8 @@ function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
185
249
186
250
// Tail of list
187
251
if ( last ) {
188
- const object = _objectToRDF ( last , issuer , dataset , graphTerm , rdfDirection ) ;
252
+ const object = _objectToRDF (
253
+ last , issuer , dataset , graphTerm , rdfDirection , options ) ;
189
254
dataset . push ( {
190
255
subject,
191
256
predicate : first ,
@@ -211,10 +276,13 @@ function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
211
276
* @param issuer a IdentifierIssuer for assigning blank node names.
212
277
* @param dataset the dataset to append RDF quads to.
213
278
* @param graphTerm the graph term for each quad.
279
+ * @param options the RDF serialization options.
214
280
*
215
281
* @return the RDF literal or RDF resource.
216
282
*/
217
- function _objectToRDF ( item , issuer , dataset , graphTerm , rdfDirection ) {
283
+ function _objectToRDF (
284
+ item , issuer , dataset , graphTerm , rdfDirection , options
285
+ ) {
218
286
const object = { } ;
219
287
220
288
// convert value object to RDF
@@ -260,8 +328,8 @@ function _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection) {
260
328
object . datatype . value = datatype || XSD_STRING ;
261
329
}
262
330
} else if ( graphTypes . isList ( item ) ) {
263
- const _list =
264
- _listToRDF ( item [ '@list' ] , issuer , dataset , graphTerm , rdfDirection ) ;
331
+ const _list = _listToRDF (
332
+ item [ '@list' ] , issuer , dataset , graphTerm , rdfDirection , options ) ;
265
333
object . termType = _list . termType ;
266
334
object . value = _list . value ;
267
335
} else {
@@ -273,6 +341,20 @@ function _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection) {
273
341
274
342
// skip relative IRIs, not valid RDF
275
343
if ( object . termType === 'NamedNode' && ! _isAbsoluteIri ( object . value ) ) {
344
+ if ( options . eventHandler ) {
345
+ _handleEvent ( {
346
+ event : {
347
+ type : [ 'JsonLdEvent' ] ,
348
+ code : 'relative type reference' ,
349
+ level : 'warning' ,
350
+ message : 'Relative type reference found.' ,
351
+ details : {
352
+ type : object . value
353
+ }
354
+ } ,
355
+ options
356
+ } ) ;
357
+ }
276
358
return null ;
277
359
}
278
360
0 commit comments