@@ -18,7 +18,7 @@ const {
18
18
19
19
// constants
20
20
const {
21
- // RDF,
21
+ RDF ,
22
22
RDF_LIST ,
23
23
RDF_FIRST ,
24
24
RDF_REST ,
@@ -61,19 +61,21 @@ api.fromRDF = async (
61
61
const defaultGraph = { } ;
62
62
const graphMap = { '@default' : defaultGraph } ;
63
63
const referencedOnce = { } ;
64
+ let processCompoundLiterals = false ;
64
65
if ( rdfDirection ) {
65
66
if ( rdfDirection === 'compound-literal' ) {
66
- throw new JsonLdError (
67
- 'Unsupported rdfDirection value.' ,
68
- 'jsonld.InvalidRdfDirection' ,
69
- { value : rdfDirection } ) ;
67
+ processCompoundLiterals = true ;
70
68
} else if ( rdfDirection !== 'i18n-datatype' ) {
71
69
throw new JsonLdError (
72
70
'Unknown rdfDirection value.' ,
73
71
'jsonld.InvalidRdfDirection' ,
74
72
{ value : rdfDirection } ) ;
75
73
}
76
74
}
75
+ let compoundLiteralSubjects ;
76
+ if ( processCompoundLiterals ) {
77
+ compoundLiteralSubjects = { } ;
78
+ }
77
79
78
80
for ( const quad of dataset ) {
79
81
// TODO: change 'name' to 'graph'
@@ -82,11 +84,18 @@ api.fromRDF = async (
82
84
if ( ! ( name in graphMap ) ) {
83
85
graphMap [ name ] = { } ;
84
86
}
87
+ if ( processCompoundLiterals && ! ( name in compoundLiteralSubjects ) ) {
88
+ compoundLiteralSubjects [ name ] = { } ;
89
+ }
85
90
if ( name !== '@default' && ! ( name in defaultGraph ) ) {
86
91
defaultGraph [ name ] = { '@id' : name } ;
87
92
}
88
93
89
94
const nodeMap = graphMap [ name ] ;
95
+ let compoundMap ;
96
+ if ( processCompoundLiterals ) {
97
+ compoundMap = compoundLiteralSubjects [ name ] ;
98
+ }
90
99
91
100
// get subject, predicate, object
92
101
const s = quad . subject . value ;
@@ -97,6 +106,9 @@ api.fromRDF = async (
97
106
nodeMap [ s ] = { '@id' : s } ;
98
107
}
99
108
const node = nodeMap [ s ] ;
109
+ if ( processCompoundLiterals && p === RDF + 'direction' ) {
110
+ compoundMap [ s ] = true ;
111
+ }
100
112
101
113
const objectIsNode = o . termType . endsWith ( 'Node' ) ;
102
114
if ( objectIsNode && ! ( o . value in nodeMap ) ) {
@@ -208,6 +220,64 @@ api.fromRDF = async (
208
220
for ( const name in graphMap ) {
209
221
const graphObject = graphMap [ name ] ;
210
222
223
+ if ( processCompoundLiterals ) {
224
+ if ( name in compoundLiteralSubjects ) {
225
+ const cls = compoundLiteralSubjects [ name ] ;
226
+ for ( const cl of Object . keys ( cls ) ) {
227
+ const clEntry = referencedOnce [ cl ] ;
228
+ if ( ! clEntry ) {
229
+ continue ;
230
+ }
231
+ const node = clEntry . node ;
232
+ const property = clEntry . property ;
233
+ //const value = clEntry.value;
234
+ const clNode = graphObject [ cl ] ;
235
+ if ( ! types . isObject ( clNode ) ) {
236
+ continue ;
237
+ }
238
+ delete graphObject [ cl ] ;
239
+ for ( const clReference of node [ property ] ) {
240
+ if ( clReference [ '@id' ] === cl ) {
241
+ delete clReference [ '@id' ] ;
242
+ }
243
+ const value = clNode [ RDF + 'value' ] ;
244
+ // FIXME: error on !== 1 value
245
+ clReference [ '@value' ] = value [ 0 ] [ '@value' ] ;
246
+ const language = clNode [ RDF + 'language' ] ;
247
+ if ( language ) {
248
+ // FIXME: error on !== 1 language value
249
+ const v = language [ 0 ] [ '@value' ] ;
250
+ if ( ! v . match ( REGEX_BCP47 ) ) {
251
+ throw new JsonLdError (
252
+ 'Invalid RDF syntax; rdf:language must be valid BCP47.' ,
253
+ 'jsonld.SyntaxError' ,
254
+ {
255
+ code : 'invalid language-tagged string' ,
256
+ value : v
257
+ } ) ;
258
+ }
259
+ clReference [ '@language' ] = v ;
260
+ }
261
+ const direction = clNode [ RDF + 'direction' ] ;
262
+ if ( direction ) {
263
+ // FIXME: error on !== 1 direction value
264
+ const v = direction [ 0 ] [ '@value' ] ;
265
+ if ( ! ( v === 'ltr' || v === 'rtl' ) ) {
266
+ throw new JsonLdError (
267
+ 'Invalid RDF syntax; rdf:direction must be "ltr" or "rtl".' ,
268
+ 'jsonld.SyntaxError' ,
269
+ {
270
+ code : 'invalid base direction' ,
271
+ value : v
272
+ } ) ;
273
+ }
274
+ clReference [ '@direction' ] = v ;
275
+ }
276
+ }
277
+ }
278
+ }
279
+ }
280
+
211
281
// no @lists to be converted, continue
212
282
if ( ! ( RDF_NIL in graphObject ) ) {
213
283
continue ;
@@ -296,7 +366,8 @@ api.fromRDF = async (
296
366
*
297
367
* @param o the RDF triple object to convert.
298
368
* @param useNativeTypes true to output native types, false not to.
299
- * @param rdfDirection text direction mode [null, i18n-datatype]
369
+ * @param rdfDirection text direction mode [null, i18n-datatype,
370
+ * compound-literal]
300
371
* @param options top level API options
301
372
*
302
373
* @return the JSON-LD object.
0 commit comments