-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprocessor.go
568 lines (520 loc) · 21.6 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package gojsonld
// /**
// * Compacts the given input using the context according to the steps in the
// * <a href="http://www.w3.org/TR/json-ld-api/#compaction-algorithm">
// * Compaction algorithm</a>.
// *
// * @param input
// * The input JSON-LD object.
// * @param context
// * The context object to use for the compaction algorithm.
// * @param opts
// * The {@link JsonLdOptions} that are to be sent to the
// * compaction algorithm.
// * @return The compacted JSON-LD document
// * @throws JsonLdError
// * If there is an error while compacting.
// */
// public static Map<String, Object> compact(Object input, Object context, JsonLdOptions opts)
// throws JsonLdError {
// // 1)
// // TODO: look into java futures/promises
// // 2-6) NOTE: these are all the same steps as in expand
// final Object expanded = expand(input, opts);
// // 7)
// if (context instanceof Map && ((Map<String, Object>) context).containsKey("@context")) {
// context = ((Map<String, Object>) context).get("@context");
// }
// Context activeCtx = new Context(opts);
// activeCtx = activeCtx.parse(context);
// // 8)
// Object compacted = new JsonLdApi(opts).compact(activeCtx, null, expanded,
// opts.getCompactArrays());
// // final step of Compaction Algorithm
// // TODO: SPEC: the result result is a NON EMPTY array,
// if (compacted instanceof List) {
// if (((List<Object>) compacted).isEmpty()) {
// compacted = new LinkedHashMap<String, Object>();
// } else {
// final Map<String, Object> tmp = new LinkedHashMap<String, Object>();
// // TODO: SPEC: doesn't specify to use vocab = true here
// tmp.put(activeCtx.compactIri("@graph", true), compacted);
// compacted = tmp;
// }
// }
// if (compacted != null && context != null) {
// // TODO: figure out if we can make "@context" appear at the start of
// // the keySet
// if ((context instanceof Map && !((Map<String, Object>) context).isEmpty())
// || (context instanceof List && !((List<Object>) context).isEmpty())) {
// ((Map<String, Object>) compacted).put("@context", context);
// }
// }
// // 9)
// return (Map<String, Object>) compacted;
// }
// /**
// * Expands the given input according to the steps in the <a
// * href="http://www.w3.org/TR/json-ld-api/#expansion-algorithm">Expansion
// * algorithm</a>.
// *
// * @param input
// * The input JSON-LD object.
// * @param opts
// * The {@link JsonLdOptions} that are to be sent to the expansion
// * algorithm.
// * @return The expanded JSON-LD document
// * @throws JsonLdError
// * If there is an error while expanding.
// */
// public static List<Object> expand(Object input, JsonLdOptions opts) throws JsonLdError {
// // 1)
// // TODO: look into java futures/promises
// // 2) TODO: better verification of DOMString IRI
// if (input instanceof String && ((String) input).contains(":")) {
// try {
// final RemoteDocument tmp = opts.getDocumentLoader().loadDocument((String) input);
// input = tmp.document;
// // TODO: figure out how to deal with remote context
// } catch (final Exception e) {
// throw new JsonLdError(Error.LOADING_DOCUMENT_FAILED, e.getMessage());
// }
// // if set the base in options should override the base iri in the
// // active context
// // thus only set this as the base iri if it's not already set in
// // options
// if (opts.getBase() == null) {
// opts.setBase((String) input);
// }
// }
// // 3)
// Context activeCtx = new Context(opts);
// // 4)
// if (opts.getExpandContext() != null) {
// Object exCtx = opts.getExpandContext();
// if (exCtx instanceof Map && ((Map<String, Object>) exCtx).containsKey("@context")) {
// exCtx = ((Map<String, Object>) exCtx).get("@context");
// }
// activeCtx = activeCtx.parse(exCtx);
// }
// // 5)
// // TODO: add support for getting a context from HTTP when content-type
// // is set to a jsonld compatable format
// // 6)
// Object expanded = new JsonLdApi(opts).expand(activeCtx, input);
// // final step of Expansion Algorithm
// if (expanded instanceof Map && ((Map) expanded).containsKey("@graph")
// && ((Map) expanded).size() == 1) {
// expanded = ((Map<String, Object>) expanded).get("@graph");
// } else if (expanded == null) {
// expanded = new ArrayList<Object>();
// }
// // normalize to an array
// if (!(expanded instanceof List)) {
// final List<Object> tmp = new ArrayList<Object>();
// tmp.add(expanded);
// expanded = tmp;
// }
// return (List<Object>) expanded;
// }
// /**
// * Expands the given input according to the steps in the <a
// * href="http://www.w3.org/TR/json-ld-api/#expansion-algorithm">Expansion
// * algorithm</a>, using the default {@link JsonLdOptions}.
// *
// * @param input
// * The input JSON-LD object.
// * @return The expanded JSON-LD document
// * @throws JsonLdError
// * If there is an error while expanding.
// */
// public static List<Object> expand(Object input) throws JsonLdError {
// return expand(input, new JsonLdOptions(""));
// }
// public static Object flatten(Object input, Object context, JsonLdOptions opts)
// throws JsonLdError {
// // 2-6) NOTE: these are all the same steps as in expand
// final Object expanded = expand(input, opts);
// // 7)
// if (context instanceof Map && ((Map<String, Object>) context).containsKey("@context")) {
// context = ((Map<String, Object>) context).get("@context");
// }
// // 8) NOTE: blank node generation variables are members of JsonLdApi
// // 9) NOTE: the next block is the Flattening Algorithm described in
// // http://json-ld.org/spec/latest/json-ld-api/#flattening-algorithm
// // 1)
// final Map<String, Object> nodeMap = new LinkedHashMap<String, Object>();
// nodeMap.put("@default", new LinkedHashMap<String, Object>());
// // 2)
// new JsonLdApi(opts).generateNodeMap(expanded, nodeMap);
// // 3)
// final Map<String, Object> defaultGraph = (Map<String, Object>) nodeMap.remove("@default");
// // 4)
// for (final String graphName : nodeMap.keySet()) {
// final Map<String, Object> graph = (Map<String, Object>) nodeMap.get(graphName);
// // 4.1+4.2)
// Map<String, Object> entry;
// if (!defaultGraph.containsKey(graphName)) {
// entry = new LinkedHashMap<String, Object>();
// entry.put("@id", graphName);
// defaultGraph.put(graphName, entry);
// } else {
// entry = (Map<String, Object>) defaultGraph.get(graphName);
// }
// // 4.3)
// // TODO: SPEC doesn't specify that this should only be added if it
// // doesn't exists
// if (!entry.containsKey("@graph")) {
// entry.put("@graph", new ArrayList<Object>());
// }
// final List<String> keys = new ArrayList<String>(graph.keySet());
// Collections.sort(keys);
// for (final String id : keys) {
// final Map<String, Object> node = (Map<String, Object>) graph.get(id);
// if (!(node.containsKey("@id") && node.size() == 1)) {
// ((List<Object>) entry.get("@graph")).add(node);
// }
// }
// }
// // 5)
// final List<Object> flattened = new ArrayList<Object>();
// // 6)
// final List<String> keys = new ArrayList<String>(defaultGraph.keySet());
// Collections.sort(keys);
// for (final String id : keys) {
// final Map<String, Object> node = (Map<String, Object>) defaultGraph.get(id);
// if (!(node.containsKey("@id") && node.size() == 1)) {
// flattened.add(node);
// }
// }
// // 8)
// if (context != null && !flattened.isEmpty()) {
// Context activeCtx = new Context(opts);
// activeCtx = activeCtx.parse(context);
// // TODO: only instantiate one jsonldapi
// Object compacted = new JsonLdApi(opts).compact(activeCtx, null, flattened,
// opts.getCompactArrays());
// if (!(compacted instanceof List)) {
// final List<Object> tmp = new ArrayList<Object>();
// tmp.add(compacted);
// compacted = tmp;
// }
// final String alias = activeCtx.compactIri("@graph");
// final Map<String, Object> rval = activeCtx.serialize();
// rval.put(alias, compacted);
// return rval;
// }
// return flattened;
// }
// /**
// * Flattens the given input and compacts it using the passed context
// * according to the steps in the <a
// * href="http://www.w3.org/TR/json-ld-api/#flattening-algorithm">Flattening
// * algorithm</a>:
// *
// * @param input
// * The input JSON-LD object.
// * @param opts
// * The {@link JsonLdOptions} that are to be sent to the
// * flattening algorithm.
// * @return The flattened JSON-LD document
// * @throws JsonLdError
// * If there is an error while flattening.
// */
// public static Object flatten(Object input, JsonLdOptions opts) throws JsonLdError {
// return flatten(input, null, opts);
// }
// /**
// * Frames the given input using the frame according to the steps in the <a
// * href="http://json-ld.org/spec/latest/json-ld-framing/#framing-algorithm">
// * Framing Algorithm</a>.
// *
// * @param input
// * The input JSON-LD object.
// * @param frame
// * The frame to use when re-arranging the data of input; either
// * in the form of an JSON object or as IRI.
// * @param opts
// * The {@link JsonLdOptions} that are to be sent to the framing
// * algorithm.
// * @return The framed JSON-LD document
// * @throws JsonLdError
// * If there is an error while framing.
// */
// public static Map<String, Object> frame(Object input, Object frame, JsonLdOptions opts)
// throws JsonLdError {
// if (frame instanceof Map) {
// frame = JsonLdUtils.clone(frame);
// }
// // TODO string/IO input
// final Object expandedInput = expand(input, opts);
// final List<Object> expandedFrame = expand(frame, opts);
// final JsonLdApi api = new JsonLdApi(expandedInput, opts);
// final List<Object> framed = api.frame(expandedInput, expandedFrame);
// final Context activeCtx = api.context.parse(((Map<String, Object>) frame).get("@context"));
// Object compacted = api.compact(activeCtx, null, framed);
// if (!(compacted instanceof List)) {
// final List<Object> tmp = new ArrayList<Object>();
// tmp.add(compacted);
// compacted = tmp;
// }
// final String alias = activeCtx.compactIri("@graph");
// final Map<String, Object> rval = activeCtx.serialize();
// rval.put(alias, compacted);
// JsonLdUtils.removePreserve(activeCtx, rval, opts);
// return rval;
// }
// /**
// * A registry for RDF Parsers (in this case, JSONLDSerializers) used by
// * fromRDF if no specific serializer is specified and options.format is set.
// *
// * TODO: this would fit better in the document loader class
// */
// private static Map<String, RDFParser> rdfParsers = new LinkedHashMap<String, RDFParser>() {
// {
// // automatically register nquad serializer
// put("application/nquads", new NQuadRDFParser());
// put("text/turtle", new TurtleRDFParser());
// }
// };
// public static void registerRDFParser(String format, RDFParser parser) {
// rdfParsers.put(format, parser);
// }
// public static void removeRDFParser(String format) {
// rdfParsers.remove(format);
// }
// /**
// * Converts an RDF dataset to JSON-LD.
// *
// * @param dataset
// * a serialized string of RDF in a format specified by the format
// * option or an RDF dataset to convert.
// * @param options
// * the options to use: [format] the format if input is not an
// * array: 'application/nquads' for N-Quads (default).
// * [useRdfType] true to use rdf:type, false to use @type
// * (default: false). [useNativeTypes] true to convert XSD types
// * into native types (boolean, integer, double), false not to
// * (default: true).
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object fromRDF(Object dataset, JsonLdOptions options) throws JsonLdError {
// // handle non specified serializer case
// RDFParser parser = null;
// if (options.format == null && dataset instanceof String) {
// // attempt to parse the input as nquads
// options.format = "application/nquads";
// }
// if (rdfParsers.containsKey(options.format)) {
// parser = rdfParsers.get(options.format);
// } else {
// throw new JsonLdError(JsonLdError.Error.UNKNOWN_FORMAT, options.format);
// }
// // convert from RDF
// return fromRDF(dataset, options, parser);
// }
// /**
// * Converts an RDF dataset to JSON-LD, using the default
// * {@link JsonLdOptions}.
// *
// * @param dataset
// * a serialized string of RDF in a format specified by the format
// * option or an RDF dataset to convert.
// * @return The JSON-LD object represented by the given RDF dataset
// * @throws JsonLdError
// * If there was an error converting from RDF to JSON-LD
// */
// public static Object fromRDF(Object dataset) throws JsonLdError {
// return fromRDF(dataset, new JsonLdOptions(""));
// }
// /**
// * Converts an RDF dataset to JSON-LD, using a specific instance of
// * {@link RDFParser}.
// *
// * @param input
// * a serialized string of RDF in a format specified by the format
// * option or an RDF dataset to convert.
// * @param options
// * the options to use: [format] the format if input is not an
// * array: 'application/nquads' for N-Quads (default).
// * [useRdfType] true to use rdf:type, false to use @type
// * (default: false). [useNativeTypes] true to convert XSD types
// * into native types (boolean, integer, double), false not to
// * (default: true).
// * @param parser
// * A specific instance of {@link RDFParser} to use for the
// * conversion.
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object fromRDF(Object input, JsonLdOptions options, RDFParser parser)
// throws JsonLdError {
// final RDFDataset dataset = parser.parse(input);
// // convert from RDF
// final Object rval = new JsonLdApi(options).fromRDF(dataset);
// // re-process using the generated context if outputForm is set
// if (options.outputForm != null) {
// if ("expanded".equals(options.outputForm)) {
// return rval;
// } else if ("compacted".equals(options.outputForm)) {
// return compact(rval, dataset.getContext(), options);
// } else if ("flattened".equals(options.outputForm)) {
// return flatten(rval, dataset.getContext(), options);
// } else {
// throw new JsonLdError(JsonLdError.Error.UNKNOWN_ERROR);
// }
// }
// return rval;
// }
// /**
// * Converts an RDF dataset to JSON-LD, using a specific instance of
// * {@link RDFParser}, and the default {@link JsonLdOptions}.
// *
// * @param input
// * a serialized string of RDF in a format specified by the format
// * option or an RDF dataset to convert.
// * @param parser
// * A specific instance of {@link RDFParser} to use for the
// * conversion.
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object fromRDF(Object input, RDFParser parser) throws JsonLdError {
// return fromRDF(input, new JsonLdOptions(""), parser);
// }
// /**
// * Outputs the RDF dataset found in the given JSON-LD object.
// *
// * @param input
// * the JSON-LD input.
// * @param callback
// * A callback that is called when the input has been converted to
// * Quads (null to use options.format instead).
// * @param options
// * the options to use: [base] the base IRI to use. [format] the
// * format to use to output a string: 'application/nquads' for
// * N-Quads (default). [loadContext(url, callback(err, url,
// * result))] the context loader.
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object toRDF(Object input, JsonLdTripleCallback callback, JsonLdOptions options)
// throws JsonLdError {
// final Object expandedInput = expand(input, options);
// final JsonLdApi api = new JsonLdApi(expandedInput, options);
// final RDFDataset dataset = api.toRDF();
// // generate namespaces from context
// if (options.useNamespaces) {
// List<Map<String, Object>> _input;
// if (input instanceof List) {
// _input = (List<Map<String, Object>>) input;
// } else {
// _input = new ArrayList<Map<String, Object>>();
// _input.add((Map<String, Object>) input);
// }
// for (final Map<String, Object> e : _input) {
// if (e.containsKey("@context")) {
// dataset.parseContext(e.get("@context"));
// }
// }
// }
// if (callback != null) {
// return callback.call(dataset);
// }
// if (options.format != null) {
// if ("application/nquads".equals(options.format)) {
// return new NQuadTripleCallback().call(dataset);
// } else if ("text/turtle".equals(options.format)) {
// return new TurtleTripleCallback().call(dataset);
// } else {
// throw new JsonLdError(JsonLdError.Error.UNKNOWN_FORMAT, options.format);
// }
// }
// return dataset;
// }
// /**
// * Outputs the RDF dataset found in the given JSON-LD object.
// *
// * @param input
// * the JSON-LD input.
// * @param options
// * the options to use: [base] the base IRI to use. [format] the
// * format to use to output a string: 'application/nquads' for
// * N-Quads (default). [loadContext(url, callback(err, url,
// * result))] the context loader.
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object toRDF(Object input, JsonLdOptions options) throws JsonLdError {
// return toRDF(input, null, options);
// }
// /**
// * Outputs the RDF dataset found in the given JSON-LD object, using the
// * default {@link JsonLdOptions}.
// *
// * @param input
// * the JSON-LD input.
// * @param callback
// * A callback that is called when the input has been converted to
// * Quads (null to use options.format instead).
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object toRDF(Object input, JsonLdTripleCallback callback) throws JsonLdError {
// return toRDF(input, callback, new JsonLdOptions(""));
// }
// /**
// * Outputs the RDF dataset found in the given JSON-LD object, using the
// * default {@link JsonLdOptions}.
// *
// * @param input
// * the JSON-LD input.
// * @return A JSON-LD object.
// * @throws JsonLdError
// * If there is an error converting the dataset to JSON-LD.
// */
// public static Object toRDF(Object input) throws JsonLdError {
// return toRDF(input, new JsonLdOptions(""));
// }
// /**
// * Performs RDF dataset normalization on the given JSON-LD input. The output
// * is an RDF dataset unless the 'format' option is used.
// *
// * @param input
// * the JSON-LD input to normalize.
// * @param options
// * the options to use: [base] the base IRI to use. [format] the
// * format if output is a string: 'application/nquads' for
// * N-Quads. [loadContext(url, callback(err, url, result))] the
// * context loader.
// * @return The JSON-LD object
// * @throws JsonLdError
// * If there is an error normalizing the dataset.
// */
// public static Object normalize(Object input, JsonLdOptions options) throws JsonLdError {
// final JsonLdOptions opts = new JsonLdOptions(options.getBase());
// opts.format = null;
// final RDFDataset dataset = (RDFDataset) toRDF(input, opts);
// return new JsonLdApi(options).normalize(dataset);
// }
// /**
// * Performs RDF dataset normalization on the given JSON-LD input. The output
// * is an RDF dataset unless the 'format' option is used. Uses the default
// * {@link JsonLdOptions}.
// *
// * @param input
// * the JSON-LD input to normalize.
// * @return The JSON-LD object
// * @throws JsonLdError
// * If there is an error normalizing the dataset.
// */
// public static Object normalize(Object input) throws JsonLdError {
// return normalize(input, new JsonLdOptions(""));
// }