Skip to content

Commit 59fc87b

Browse files
committed
Add toRDF events and tests.
- Add various toRDF relative reference events. - Update test support. - Add tests.
1 parent 7ad86dc commit 59fc87b

File tree

3 files changed

+409
-11
lines changed

3 files changed

+409
-11
lines changed

lib/events.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ const _notSafeEventCodes = new Set([
117117
'relative @vocab reference',
118118
'reserved @id value',
119119
'reserved @reverse value',
120-
'reserved term'
120+
'reserved term',
121+
// toRDF
122+
'blank node predicate',
123+
'relative graph reference',
124+
'relative property reference',
125+
'relative subject reference',
126+
'relative type reference'
121127
]);
122128

123129
// safe handler that rejects unsafe warning conditions

lib/toRdf.js

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const jsonCanonicalize = require('canonicalize');
1010
const types = require('./types');
1111
const util = require('./util');
1212

13+
const {
14+
handleEvent: _handleEvent
15+
} = require('./events');
16+
1317
const {
1418
// RDF,
1519
// RDF_LIST,
@@ -66,6 +70,20 @@ api.toRDF = (input, options) => {
6670
graphTerm.value = graphName;
6771
} else {
6872
// 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+
}
6987
continue;
7088
}
7189
_graphToRDF(dataset, nodeMap[graphName], graphTerm, issuer, options);
@@ -107,6 +125,20 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
107125

108126
// skip relative IRI subjects (not valid RDF)
109127
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+
}
110142
continue;
111143
}
112144

@@ -118,18 +150,48 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
118150

119151
// skip relative IRI predicates (not valid RDF)
120152
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+
}
121167
continue;
122168
}
123169

124170
// skip blank node predicates unless producing generalized RDF
125171
if(predicate.termType === 'BlankNode' &&
126172
!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+
}
127189
continue;
128190
}
129191

130192
// 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);
133195
// skip null objects (they are relative IRIs)
134196
if(object) {
135197
dataset.push({
@@ -152,10 +214,11 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
152214
* @param issuer a IdentifierIssuer for assigning blank node names.
153215
* @param dataset the array of quads to append to.
154216
* @param graphTerm the graph term for each quad.
217+
* @param options the RDF serialization options.
155218
*
156219
* @return the head of the list.
157220
*/
158-
function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
221+
function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection, options) {
159222
const first = {termType: 'NamedNode', value: RDF_FIRST};
160223
const rest = {termType: 'NamedNode', value: RDF_REST};
161224
const nil = {termType: 'NamedNode', value: RDF_NIL};
@@ -166,7 +229,8 @@ function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
166229
let subject = result;
167230

168231
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);
170234
const next = {termType: 'BlankNode', value: issuer.getId()};
171235
dataset.push({
172236
subject,
@@ -185,7 +249,8 @@ function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
185249

186250
// Tail of list
187251
if(last) {
188-
const object = _objectToRDF(last, issuer, dataset, graphTerm, rdfDirection);
252+
const object = _objectToRDF(
253+
last, issuer, dataset, graphTerm, rdfDirection, options);
189254
dataset.push({
190255
subject,
191256
predicate: first,
@@ -211,10 +276,13 @@ function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
211276
* @param issuer a IdentifierIssuer for assigning blank node names.
212277
* @param dataset the dataset to append RDF quads to.
213278
* @param graphTerm the graph term for each quad.
279+
* @param options the RDF serialization options.
214280
*
215281
* @return the RDF literal or RDF resource.
216282
*/
217-
function _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection) {
283+
function _objectToRDF(
284+
item, issuer, dataset, graphTerm, rdfDirection, options
285+
) {
218286
const object = {};
219287

220288
// convert value object to RDF
@@ -260,8 +328,8 @@ function _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection) {
260328
object.datatype.value = datatype || XSD_STRING;
261329
}
262330
} 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);
265333
object.termType = _list.termType;
266334
object.value = _list.value;
267335
} else {
@@ -273,6 +341,20 @@ function _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection) {
273341

274342
// skip relative IRIs, not valid RDF
275343
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+
}
276358
return null;
277359
}
278360

0 commit comments

Comments
 (0)