-
Notifications
You must be signed in to change notification settings - Fork 154
/
JsonLdUtils.java
935 lines (856 loc) · 31.5 KB
/
JsonLdUtils.java
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
package com.github.jsonldjava.core;
import static com.github.jsonldjava.utils.Obj.newMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.github.jsonldjava.utils.JsonLdUrl;
import com.github.jsonldjava.utils.Obj;
public class JsonLdUtils {
private static final int MAX_CONTEXT_URLS = 10;
/**
* Returns whether or not the given value is a keyword (or a keyword alias).
*
* @param v
* the value to check.
* @param [ctx] the active context to check against.
*
* @return true if the value is a keyword, false if not.
*/
static boolean isKeyword(Object key) {
if (!isString(key)) {
return false;
}
return "@base".equals(key) || "@context".equals(key) || "@container".equals(key)
|| "@default".equals(key) || "@embed".equals(key) || "@explicit".equals(key)
|| "@graph".equals(key) || "@id".equals(key) || "@index".equals(key)
|| "@language".equals(key) || "@list".equals(key) || "@omitDefault".equals(key)
|| "@reverse".equals(key) || "@preserve".equals(key) || "@set".equals(key)
|| "@type".equals(key) || "@value".equals(key) || "@vocab".equals(key);
}
public static Boolean deepCompare(Object v1, Object v2, Boolean listOrderMatters) {
if (v1 == null) {
return v2 == null;
} else if (v2 == null) {
return v1 == null;
} else if (v1 instanceof Map && v2 instanceof Map) {
final Map<String, Object> m1 = (Map<String, Object>) v1;
final Map<String, Object> m2 = (Map<String, Object>) v2;
if (m1.size() != m2.size()) {
return false;
}
for (final String key : m1.keySet()) {
if (!m2.containsKey(key)
|| !deepCompare(m1.get(key), m2.get(key), listOrderMatters)) {
return false;
}
}
return true;
} else if (v1 instanceof List && v2 instanceof List) {
final List<Object> l1 = (List<Object>) v1;
final List<Object> l2 = (List<Object>) v2;
if (l1.size() != l2.size()) {
return false;
}
// used to mark members of l2 that we have already matched to avoid
// matching the same item twice for lists that have duplicates
final boolean alreadyMatched[] = new boolean[l2.size()];
for (int i = 0; i < l1.size(); i++) {
final Object o1 = l1.get(i);
Boolean gotmatch = false;
if (listOrderMatters) {
gotmatch = deepCompare(o1, l2.get(i), listOrderMatters);
} else {
for (int j = 0; j < l2.size(); j++) {
if (!alreadyMatched[j] && deepCompare(o1, l2.get(j), listOrderMatters)) {
alreadyMatched[j] = true;
gotmatch = true;
break;
}
}
}
if (!gotmatch) {
return false;
}
}
return true;
} else {
return v1.equals(v2);
}
}
public static Boolean deepCompare(Object v1, Object v2) {
return deepCompare(v1, v2, false);
}
public static boolean deepContains(List<Object> values, Object value) {
for (final Object item : values) {
if (deepCompare(item, value, false)) {
return true;
}
}
return false;
}
static void mergeValue(Map<String, Object> obj, String key, Object value) {
if (obj == null) {
return;
}
List<Object> values = (List<Object>) obj.get(key);
if (values == null) {
values = new ArrayList<Object>();
obj.put(key, values);
}
if ("@list".equals(key)
|| (value instanceof Map && ((Map<String, Object>) value).containsKey("@list"))
|| !deepContains(values, value)) {
values.add(value);
}
}
static void laxMergeValue(Map<String, Object> obj, String key, Object value) {
if (obj == null) {
return;
}
List<Object> values = (List<Object>) obj.get(key);
if (values == null) {
values = new ArrayList<Object>();
obj.put(key, values);
}
//if ("@list".equals(key)
// || (value instanceof Map && ((Map<String, Object>) value).containsKey("@list"))
//|| !deepContains(values, value)
// ) {
values.add(value);
//}
}
static void mergeCompactedValue(Map<String, Object> obj, String key, Object value) {
if (obj == null) {
return;
}
final Object prop = obj.get(key);
if (prop == null) {
obj.put(key, value);
return;
}
if (!(prop instanceof List)) {
final List<Object> tmp = new ArrayList<Object>();
tmp.add(prop);
}
if (value instanceof List) {
((List<Object>) prop).addAll((List<Object>) value);
} else {
((List<Object>) prop).add(value);
}
}
public static boolean isAbsoluteIri(String value) {
// TODO: this is a bit simplistic!
return value.contains(":");
}
/**
* Returns true if the given value is a subject with properties.
*
* @param v
* the value to check.
*
* @return true if the value is a subject with properties, false if not.
*/
static boolean isNode(Object v) {
// Note: A value is a subject if all of these hold true:
// 1. It is an Object.
// 2. It is not a @value, @set, or @list.
// 3. It has more than 1 key OR any existing key is not @id.
if (v instanceof Map
&& !(((Map) v).containsKey("@value") || ((Map) v).containsKey("@set") || ((Map) v)
.containsKey("@list"))) {
return ((Map<String, Object>) v).size() > 1 || !((Map) v).containsKey("@id");
}
return false;
}
/**
* Returns true if the given value is a subject reference.
*
* @param v
* the value to check.
*
* @return true if the value is a subject reference, false if not.
*/
static boolean isNodeReference(Object v) {
// Note: A value is a subject reference if all of these hold true:
// 1. It is an Object.
// 2. It has a single key: @id.
return (v instanceof Map && ((Map<String, Object>) v).size() == 1 && ((Map<String, Object>) v)
.containsKey("@id"));
}
// TODO: fix this test
public static boolean isRelativeIri(String value) {
if (!(isKeyword(value) || isAbsoluteIri(value))) {
return true;
}
return false;
}
// //////////////////////////////////////////////////// OLD CODE BELOW
/**
* Adds a value to a subject. If the value is an array, all values in the
* array will be added.
*
* Note: If the value is a subject that already exists as a property of the
* given subject, this method makes no attempt to deeply merge properties.
* Instead, the value will not be added.
*
* @param subject
* the subject to add the value to.
* @param property
* the property that relates the value to the subject.
* @param value
* the value to add.
* @param [propertyIsArray] true if the property is always an array, false
* if not (default: false).
* @param [allowDuplicate] true if the property is a @list, false if not
* (default: false).
*/
static void addValue(Map<String, Object> subject, String property, Object value,
boolean propertyIsArray, boolean allowDuplicate) {
if (isArray(value)) {
if (((List) value).size() == 0 && propertyIsArray && !subject.containsKey(property)) {
subject.put(property, new ArrayList<Object>());
}
for (final Object val : (List) value) {
addValue(subject, property, val, propertyIsArray, allowDuplicate);
}
} else if (subject.containsKey(property)) {
// check if subject already has the value if duplicates not allowed
final boolean hasValue = !allowDuplicate && hasValue(subject, property, value);
// make property an array if value not present or always an array
if (!isArray(subject.get(property)) && (!hasValue || propertyIsArray)) {
final List<Object> tmp = new ArrayList<Object>();
tmp.add(subject.get(property));
subject.put(property, tmp);
}
// add new value
if (!hasValue) {
((List<Object>) subject.get(property)).add(value);
}
} else {
// add new value as a set or single value
Object tmp;
if (propertyIsArray) {
tmp = new ArrayList<Object>();
((List<Object>) tmp).add(value);
} else {
tmp = value;
}
subject.put(property, tmp);
}
}
static void addValue(Map<String, Object> subject, String property, Object value,
boolean propertyIsArray) {
addValue(subject, property, value, propertyIsArray, true);
}
static void addValue(Map<String, Object> subject, String property, Object value) {
addValue(subject, property, value, false, true);
}
/**
* Prepends a base IRI to the given relative IRI.
*
* @param base
* the base IRI.
* @param iri
* the relative IRI.
*
* @return the absolute IRI.
*
* TODO: the JsonLdUrl class isn't as forgiving as the Node.js url
* parser, we may need to re-implement the parser here to support
* the flexibility required
*/
private static String prependBase(Object baseobj, String iri) {
// already an absolute IRI
if (iri.indexOf(":") != -1) {
return iri;
}
// parse base if it is a string
JsonLdUrl base;
if (isString(baseobj)) {
base = JsonLdUrl.parse((String) baseobj);
} else {
// assume base is already a JsonLdUrl
base = (JsonLdUrl) baseobj;
}
final JsonLdUrl rel = JsonLdUrl.parse(iri);
// start hierarchical part
String hierPart = base.protocol;
if (!"".equals(rel.authority)) {
hierPart += "//" + rel.authority;
} else if (!"".equals(base.href)) {
hierPart += "//" + base.authority;
}
// per RFC3986 normalize
String path;
// IRI represents an absolute path
if (rel.pathname.indexOf("/") == 0) {
path = rel.pathname;
} else {
path = base.pathname;
// append relative path to the end of the last directory from base
if (!"".equals(rel.pathname)) {
path = path.substring(0, path.lastIndexOf("/") + 1);
if (path.length() > 0 && !path.endsWith("/")) {
path += "/";
}
path += rel.pathname;
}
}
// remove slashes anddots in path
path = JsonLdUrl.removeDotSegments(path, !"".equals(hierPart));
// add query and hash
if (!"".equals(rel.query)) {
path += "?" + rel.query;
}
if (!"".equals(rel.hash)) {
path += rel.hash;
}
final String rval = hierPart + path;
if ("".equals(rval)) {
return "./";
}
return rval;
}
/**
* Expands a language map.
*
* @param languageMap
* the language map to expand.
*
* @return the expanded language map.
* @throws JsonLdError
*/
static List<Object> expandLanguageMap(Map<String, Object> languageMap) throws JsonLdError {
final List<Object> rval = new ArrayList<Object>();
final List<String> keys = new ArrayList<String>(languageMap.keySet());
Collections.sort(keys); // lexicographically sort languages
for (final String key : keys) {
List<Object> val;
if (!isArray(languageMap.get(key))) {
val = new ArrayList<Object>();
val.add(languageMap.get(key));
} else {
val = (List<Object>) languageMap.get(key);
}
for (final Object item : val) {
if (!isString(item)) {
throw new JsonLdError(JsonLdError.Error.SYNTAX_ERROR);
}
final Map<String, Object> tmp = newMap();
tmp.put("@value", item);
tmp.put("@language", key.toLowerCase());
rval.add(tmp);
}
}
return rval;
}
/**
* Throws an exception if the given value is not a valid @type value.
*
* @param v
* the value to check.
* @throws JsonLdError
*/
static boolean validateTypeValue(Object v) throws JsonLdError {
if (v == null) {
throw new NullPointerException("\"@type\" value cannot be null");
}
// must be a string, subject reference, or empty object
if (v instanceof String
|| (v instanceof Map && (((Map<String, Object>) v).containsKey("@id") || ((Map<String, Object>) v)
.size() == 0))) {
return true;
}
// must be an array
boolean isValid = false;
if (v instanceof List) {
isValid = true;
for (final Object i : (List) v) {
if (!(i instanceof String || i instanceof Map
&& ((Map<String, Object>) i).containsKey("@id"))) {
isValid = false;
break;
}
}
}
if (!isValid) {
throw new JsonLdError(JsonLdError.Error.SYNTAX_ERROR);
}
return true;
}
/**
* Removes a base IRI from the given absolute IRI.
*
* @param base
* the base IRI.
* @param iri
* the absolute IRI.
*
* @return the relative IRI if relative to base, otherwise the absolute IRI.
*/
private static String removeBase(Object baseobj, String iri) {
JsonLdUrl base;
if (isString(baseobj)) {
base = JsonLdUrl.parse((String) baseobj);
} else {
base = (JsonLdUrl) baseobj;
}
// establish base root
String root = "";
if (!"".equals(base.href)) {
root += (base.protocol) + "//" + base.authority;
}
// support network-path reference with empty base
else if (iri.indexOf("//") != 0) {
root += "//";
}
// IRI not relative to base
if (iri.indexOf(root) != 0) {
return iri;
}
// remove root from IRI and parse remainder
final JsonLdUrl rel = JsonLdUrl.parse(iri.substring(root.length()));
// remove path segments that match
final List<String> baseSegments = _split(base.normalizedPath, "/");
final List<String> iriSegments = _split(rel.normalizedPath, "/");
while (baseSegments.size() > 0 && iriSegments.size() > 0) {
if (!baseSegments.get(0).equals(iriSegments.get(0))) {
break;
}
if (baseSegments.size() > 0) {
baseSegments.remove(0);
}
if (iriSegments.size() > 0) {
iriSegments.remove(0);
}
}
// use '../' for each non-matching base segment
String rval = "";
if (baseSegments.size() > 0) {
// don't count the last segment if it isn't a path (doesn't end in
// '/')
// don't count empty first segment, it means base began with '/'
if (!base.normalizedPath.endsWith("/") || "".equals(baseSegments.get(0))) {
baseSegments.remove(baseSegments.size() - 1);
}
for (int i = 0; i < baseSegments.size(); ++i) {
rval += "../";
}
}
// prepend remaining segments
rval += _join(iriSegments, "/");
// add query and hash
if (!"".equals(rel.query)) {
rval += "?" + rel.query;
}
if (!"".equals(rel.hash)) {
rval += rel.hash;
}
if ("".equals(rval)) {
rval = "./";
}
return rval;
}
/**
* Removes the @preserve keywords as the last step of the framing algorithm.
*
* @param ctx
* the active context used to compact the input.
* @param input
* the framed, compacted output.
* @param options
* the compaction options used.
*
* @return the resulting output.
* @throws JsonLdError
*/
static Object removePreserve(Context ctx, Object input, JsonLdOptions opts) throws JsonLdError {
// recurse through arrays
if (isArray(input)) {
final List<Object> output = new ArrayList<Object>();
for (final Object i : (List<Object>) input) {
final Object result = removePreserve(ctx, i, opts);
// drop nulls from arrays
if (result != null) {
output.add(result);
}
}
input = output;
} else if (isObject(input)) {
// remove @preserve
if (((Map<String, Object>) input).containsKey("@preserve")) {
if ("@null".equals(((Map<String, Object>) input).get("@preserve"))) {
return null;
}
return ((Map<String, Object>) input).get("@preserve");
}
// skip @values
if (isValue(input)) {
return input;
}
// recurse through @lists
if (isList(input)) {
((Map<String, Object>) input).put("@list",
removePreserve(ctx, ((Map<String, Object>) input).get("@list"), opts));
return input;
}
// recurse through properties
for (final String prop : ((Map<String, Object>) input).keySet()) {
Object result = removePreserve(ctx, ((Map<String, Object>) input).get(prop), opts);
final String container = ctx.getContainer(prop);
if (opts.getCompactArrays() && isArray(result)
&& ((List<Object>) result).size() == 1 && container == null) {
result = ((List<Object>) result).get(0);
}
((Map<String, Object>) input).put(prop, result);
}
}
return input;
}
/**
* replicate javascript .join because i'm too lazy to keep doing it manually
*
* @param iriSegments
* @param string
* @return
*/
private static String _join(List<String> list, String joiner) {
String rval = "";
if (list.size() > 0) {
rval += list.get(0);
}
for (int i = 1; i < list.size(); i++) {
rval += joiner + list.get(i);
}
return rval;
}
/**
* replicates the functionality of javascript .split, which has different
* results to java's String.split if there is a trailing /
*
* @param string
* @param delim
* @return
*/
private static List<String> _split(String string, String delim) {
final List<String> rval = new ArrayList<String>(Arrays.asList(string.split(delim)));
if (string.endsWith("/")) {
// javascript .split includes a blank entry if the string ends with
// the delimiter, java .split does not so we need to add it manually
rval.add("");
}
return rval;
}
/**
* Compares two strings first based on length and then lexicographically.
*
* @param a
* the first string.
* @param b
* the second string.
*
* @return -1 if a < b, 1 if a > b, 0 if a == b.
*/
static int compareShortestLeast(String a, String b) {
if (a.length() < b.length()) {
return -1;
} else if (b.length() < a.length()) {
return 1;
}
return Integer.signum(a.compareTo(b));
}
/**
* Determines if the given value is a property of the given subject.
*
* @param subject
* the subject to check.
* @param property
* the property to check.
* @param value
* the value to check.
*
* @return true if the value exists, false if not.
*/
static boolean hasValue(Map<String, Object> subject, String property, Object value) {
boolean rval = false;
if (hasProperty(subject, property)) {
Object val = subject.get(property);
final boolean isList = isList(val);
if (isList || val instanceof List) {
if (isList) {
val = ((Map<String, Object>) val).get("@list");
}
for (final Object i : (List) val) {
if (compareValues(value, i)) {
rval = true;
break;
}
}
} else if (!(value instanceof List)) {
rval = compareValues(value, val);
}
}
return rval;
}
private static boolean hasProperty(Map<String, Object> subject, String property) {
boolean rval = false;
if (subject.containsKey(property)) {
final Object value = subject.get(property);
rval = (!(value instanceof List) || ((List) value).size() > 0);
}
return rval;
}
/**
* Compares two JSON-LD values for equality. Two JSON-LD values will be
* considered equal if:
*
* 1. They are both primitives of the same type and value. 2. They are both @values
* with the same @value, @type, and @language, OR 3. They both have @ids
* they are the same.
*
* @param v1
* the first value.
* @param v2
* the second value.
*
* @return true if v1 and v2 are considered equal, false if not.
*/
static boolean compareValues(Object v1, Object v2) {
if (v1.equals(v2)) {
return true;
}
if (isValue(v1)
&& isValue(v2)
&& Obj.equals(((Map<String, Object>) v1).get("@value"),
((Map<String, Object>) v2).get("@value"))
&& Obj.equals(((Map<String, Object>) v1).get("@type"),
((Map<String, Object>) v2).get("@type"))
&& Obj.equals(((Map<String, Object>) v1).get("@language"),
((Map<String, Object>) v2).get("@language"))
&& Obj.equals(((Map<String, Object>) v1).get("@index"),
((Map<String, Object>) v2).get("@index"))) {
return true;
}
if ((v1 instanceof Map && ((Map<String, Object>) v1).containsKey("@id"))
&& (v2 instanceof Map && ((Map<String, Object>) v2).containsKey("@id"))
&& ((Map<String, Object>) v1).get("@id").equals(
((Map<String, Object>) v2).get("@id"))) {
return true;
}
return false;
}
/**
* Removes a value from a subject.
*
* @param subject
* the subject.
* @param property
* the property that relates the value to the subject.
* @param value
* the value to remove.
* @param [options] the options to use: [propertyIsArray] true if the
* property is always an array, false if not (default: false).
*/
static void removeValue(Map<String, Object> subject, String property, Map<String, Object> value) {
removeValue(subject, property, value, false);
}
static void removeValue(Map<String, Object> subject, String property,
Map<String, Object> value, boolean propertyIsArray) {
// filter out value
final List<Object> values = new ArrayList<Object>();
if (subject.get(property) instanceof List) {
for (final Object e : ((List) subject.get(property))) {
if (!(value.equals(e))) {
values.add(value);
}
}
} else {
if (!value.equals(subject.get(property))) {
values.add(subject.get(property));
}
}
if (values.size() == 0) {
subject.remove(property);
} else if (values.size() == 1 && !propertyIsArray) {
subject.put(property, values.get(0));
} else {
subject.put(property, values);
}
}
/**
* Returns true if the given value is a blank node.
*
* @param v
* the value to check.
*
* @return true if the value is a blank node, false if not.
*/
static boolean isBlankNode(Object v) {
// Note: A value is a blank node if all of these hold true:
// 1. It is an Object.
// 2. If it has an @id key its value begins with '_:'.
// 3. It has no keys OR is not a @value, @set, or @list.
if (v instanceof Map) {
if (((Map) v).containsKey("@id")) {
return ((String) ((Map) v).get("@id")).startsWith("_:");
} else {
return ((Map) v).size() == 0
|| !(((Map) v).containsKey("@value") || ((Map) v).containsKey("@set") || ((Map) v)
.containsKey("@list"));
}
}
return false;
}
/**
* Finds all @context URLs in the given JSON-LD input.
*
* @param input
* the JSON-LD input.
* @param urls
* a map of URLs (url => false/@contexts).
* @param replace
* true to replace the URLs in the given input with the
* @contexts from the urls map, false not to.
*
* @return true if new URLs to resolve were found, false if not.
*/
private static boolean findContextUrls(Object input, Map<String, Object> urls, Boolean replace) {
final int count = urls.size();
if (input instanceof List) {
for (final Object i : (List) input) {
findContextUrls(i, urls, replace);
}
return count < urls.size();
} else if (input instanceof Map) {
for (final String key : ((Map<String, Object>) input).keySet()) {
if (!"@context".equals(key)) {
findContextUrls(((Map) input).get(key), urls, replace);
continue;
}
// get @context
final Object ctx = ((Map) input).get(key);
// array @context
if (ctx instanceof List) {
int length = ((List) ctx).size();
for (int i = 0; i < length; i++) {
Object _ctx = ((List) ctx).get(i);
if (_ctx instanceof String) {
// replace w/@context if requested
if (replace) {
_ctx = urls.get(_ctx);
if (_ctx instanceof List) {
// add flattened context
((List) ctx).remove(i);
((List) ctx).addAll((Collection) _ctx);
i += ((List) _ctx).size();
length += ((List) _ctx).size();
} else {
((List) ctx).set(i, _ctx);
}
}
// @context JsonLdUrl found
else if (!urls.containsKey(_ctx)) {
urls.put((String) _ctx, Boolean.FALSE);
}
}
}
}
// string @context
else if (ctx instanceof String) {
// replace w/@context if requested
if (replace) {
((Map) input).put(key, urls.get(ctx));
}
// @context JsonLdUrl found
else if (!urls.containsKey(ctx)) {
urls.put((String) ctx, Boolean.FALSE);
}
}
}
return (count < urls.size());
}
return false;
}
static Object clone(Object value) {// throws
// CloneNotSupportedException {
Object rval = null;
if (value instanceof Cloneable) {
try {
rval = value.getClass().getMethod("clone").invoke(value);
} catch (final Exception e) {
rval = e;
}
}
if (rval == null || rval instanceof Exception) {
// the object wasn't cloneable, or an error occured
if (value == null || value instanceof String || value instanceof Number
|| value instanceof Boolean) {
// strings numbers and booleans are immutable
rval = value;
} else {
// TODO: making this throw runtime exception so it doesn't have
// to be caught
// because simply it should never fail in the case of JSON-LD
// and means that
// the input JSON-LD is invalid
throw new RuntimeException(new CloneNotSupportedException(
(rval instanceof Exception ? ((Exception) rval).getMessage() : "")));
}
}
return rval;
}
/**
* Returns true if the given value is a JSON-LD Array
*
* @param v
* the value to check.
* @return
*/
static Boolean isArray(Object v) {
return (v instanceof List);
}
/**
* Returns true if the given value is a JSON-LD List
*
* @param v
* the value to check.
* @return
*/
static Boolean isList(Object v) {
return (v instanceof Map && ((Map<String, Object>) v).containsKey("@list"));
}
/**
* Returns true if the given value is a JSON-LD Object
*
* @param v
* the value to check.
* @return
*/
static Boolean isObject(Object v) {
return (v instanceof Map);
}
/**
* Returns true if the given value is a JSON-LD value
*
* @param v
* the value to check.
* @return
*/
static Boolean isValue(Object v) {
return (v instanceof Map && ((Map<String, Object>) v).containsKey("@value"));
}
/**
* Returns true if the given value is a JSON-LD string
*
* @param v
* the value to check.
* @return
*/
static Boolean isString(Object v) {
// TODO: should this return true for arrays of strings as well?
return (v instanceof String);
}
}