Skip to content

Commit 75c7f66

Browse files
committed
WIP: fix 226
1 parent a694914 commit 75c7f66

8 files changed

Lines changed: 116 additions & 33 deletions

File tree

core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public Object compact(Context activeCtx, String activeProperty, Object element,
271271
}
272272
if (value instanceof List) {
273273
((List<Object>) result.get(property))
274-
.addAll((List<Object>) value);
274+
.addAll((List<Object>) value);
275275
} else {
276276
((List<Object>) result.get(property)).add(value);
277277
}
@@ -373,7 +373,7 @@ else if (JsonLdConsts.INDEX.equals(expandedProperty)
373373
// true
374374
activeCtx.compactIri(JsonLdConsts.INDEX, true),
375375
((Map<String, Object>) expandedItem)
376-
.get(JsonLdConsts.INDEX));
376+
.get(JsonLdConsts.INDEX));
377377
}
378378
}
379379
// 7.6.4.3)
@@ -398,7 +398,7 @@ else if (result.containsKey(itemActiveProperty)) {
398398
// 7.6.5.2)
399399
if (JsonLdConsts.LANGUAGE.equals(container) && (compactedItem instanceof Map
400400
&& ((Map<String, Object>) compactedItem)
401-
.containsKey(JsonLdConsts.VALUE))) {
401+
.containsKey(JsonLdConsts.VALUE))) {
402402
compactedItem = ((Map<String, Object>) compactedItem)
403403
.get(JsonLdConsts.VALUE);
404404
}
@@ -443,7 +443,7 @@ else if (result.containsKey(itemActiveProperty)) {
443443
}
444444
if (compactedItem instanceof List) {
445445
((List<Object>) result.get(itemActiveProperty))
446-
.addAll((List<Object>) compactedItem);
446+
.addAll((List<Object>) compactedItem);
447447
} else {
448448
((List<Object>) result.get(itemActiveProperty)).add(compactedItem);
449449
}
@@ -721,7 +721,7 @@ else if (JsonLdConsts.REVERSE.equals(expandedProperty)) {
721721
// 7.4.11.2.2)
722722
if (item instanceof List) {
723723
((List<Object>) result.get(property))
724-
.addAll((List<Object>) item);
724+
.addAll((List<Object>) item);
725725
} else {
726726
((List<Object>) result.get(property)).add(item);
727727
}
@@ -752,7 +752,7 @@ else if (JsonLdConsts.REVERSE.equals(expandedProperty)) {
752752
if (item instanceof Map && (((Map<String, Object>) item)
753753
.containsKey(JsonLdConsts.VALUE)
754754
|| ((Map<String, Object>) item)
755-
.containsKey(JsonLdConsts.LIST))) {
755+
.containsKey(JsonLdConsts.LIST))) {
756756
throw new JsonLdError(Error.INVALID_REVERSE_PROPERTY_VALUE);
757757
}
758758
// 7.4.11.3.3.1.2)
@@ -893,7 +893,7 @@ else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key))
893893
// 7.10.4.3)
894894
if (item instanceof List) {
895895
((List<Object>) reverseMap.get(expandedProperty))
896-
.addAll((List<Object>) item);
896+
.addAll((List<Object>) item);
897897
} else {
898898
((List<Object>) reverseMap.get(expandedProperty)).add(item);
899899
}
@@ -908,7 +908,7 @@ else if (JsonLdConsts.INDEX.equals(activeCtx.getContainer(key))
908908
// 7.11.2)
909909
if (expandedValue instanceof List) {
910910
((List<Object>) result.get(expandedProperty))
911-
.addAll((List<Object>) expandedValue);
911+
.addAll((List<Object>) expandedValue);
912912
} else {
913913
((List<Object>) result.get(expandedProperty)).add(expandedValue);
914914
}
@@ -1044,7 +1044,7 @@ void generateNodeMap(Object element, Map<String, Object> nodeMap, String activeG
10441044

10451045
void generateNodeMap(Object element, Map<String, Object> nodeMap, String activeGraph,
10461046
Object activeSubject, String activeProperty, Map<String, Object> list)
1047-
throws JsonLdError {
1047+
throws JsonLdError {
10481048
// 1)
10491049
if (element instanceof List) {
10501050
// 1.1)
@@ -1712,6 +1712,7 @@ private boolean filterNode(FramingContext state, Map<String, Object> node,
17121712
// 1. Node matches if it has an @id property including any IRI or
17131713
// blank node in the @id property in frame.
17141714
if (frameIds != null) {
1715+
System.out.println(frameIds.getClass());
17151716
if (frameIds instanceof String) {
17161717
final Object nodeId = node.get(JsonLdConsts.ID);
17171718
if (nodeId == null) {
@@ -1720,6 +1721,11 @@ private boolean filterNode(FramingContext state, Map<String, Object> node,
17201721
if (JsonLdUtils.deepCompare(nodeId, frameIds)) {
17211722
return true;
17221723
}
1724+
} else if (frameIds instanceof LinkedHashMap) {
1725+
if (node.containsKey(JsonLdConsts.ID)) {
1726+
return true;
1727+
}
1728+
return false;
17231729
} else if (!(frameIds instanceof List)) {
17241730
throw new JsonLdError(Error.SYNTAX_ERROR, "frame @id must be an array");
17251731
} else {
@@ -2001,7 +2007,7 @@ public List<Object> fromRDF(final RDFDataset dataset, boolean noDuplicatesInData
20012007
if (object.isBlankNode() || object.isIRI()) {
20022008
// 3.5.8.1-3)
20032009
nodeMap.get(object.getValue()).usages
2004-
.add(new UsagesNode(node, predicate, value));
2010+
.add(new UsagesNode(node, predicate, value));
20052011
}
20062012
}
20072013
}
@@ -2201,7 +2207,7 @@ public Object normalize(Map<String, Object> dataset) throws JsonLdError {
22012207
});
22022208
}
22032209
((List<Object>) ((Map<String, Object>) bnodes.get(id)).get("quads"))
2204-
.add(quad);
2210+
.add(quad);
22052211
}
22062212
}
22072213
}

core/src/main/java/com/github/jsonldjava/core/JsonLdOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void setOmitDefault(Boolean omitDefault) {
133133
}
134134

135135
public Boolean getPruneBlankNodeIdentifiers() {
136-
return pruneBlankNodeIdentifiers && getProcessingMode().equals(JSON_LD_1_1);
136+
return pruneBlankNodeIdentifiers || getProcessingMode().equals(JSON_LD_1_1);
137137
}
138138

139139
public void setPruneBlankNodeIdentifiers(Boolean pruneBlankNodeIdentifiers) {

core/src/main/java/com/github/jsonldjava/core/JsonLdProcessor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,23 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
329329
.parse(((Map<String, Object>) frame).get(JsonLdConsts.CONTEXT));
330330
final List<Object> framed = api.frame(expandedInput, expandedFrame);
331331

332+
Map<String, Object> rval;
333+
if (opts.getPruneBlankNodeIdentifiers()) {
334+
rval = activeCtx.serialize();
335+
final Set<Object> toPrune = blankNodeIdsToPrune(rval);
336+
JsonLdUtils.pruneBlankNodes(framed, toPrune);
337+
}
338+
332339
Object compacted = api.compact(activeCtx, null, framed, opts.getCompactArrays());
333340
if (!(compacted instanceof List)) {
334341
final List<Object> tmp = new ArrayList<Object>();
335342
tmp.add(compacted);
336343
compacted = tmp;
337344
}
338345
final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH);
339-
final Map<String, Object> rval = activeCtx.serialize();
346+
rval = activeCtx.serialize();
340347
rval.put(alias, compacted);
341-
342-
final Set<Object> toPrune = opts.getPruneBlankNodeIdentifiers() ? blankNodeIdsToPrune(rval)
343-
: Collections.emptySet();
344-
JsonLdUtils.removePreserveAndPrune(activeCtx, rval, opts, toPrune);
348+
JsonLdUtils.removePreserve(activeCtx, rval, opts);
345349
return rval;
346350
}
347351

@@ -356,7 +360,7 @@ private static Map<Object, Integer> countBlankNodeIds(Object input,
356360
((List<?>) input).forEach(e -> countBlankNodeIds(e, frequencies));
357361
} else if (input instanceof Map) {
358362
((Map<?, ?>) input).entrySet()
359-
.forEach(e -> countBlankNodeIds(e.getValue(), frequencies));
363+
.forEach(e -> countBlankNodeIds(e.getValue(), frequencies));
360364
} else if (input instanceof String) {
361365
final String p = (String) input;
362366
if (p.startsWith("_:")) {

core/src/main/java/com/github/jsonldjava/core/JsonLdUtils.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,18 @@ public static boolean isRelativeIri(String value) {
191191
* the active context used to compact the input.
192192
* @param input
193193
* the framed, compacted output.
194-
* @param toPrune
195-
* The blank node IDs to prune.
196194
* @param options
197195
* the compaction options used.
198196
*
199197
* @return the resulting output.
200198
* @throws JsonLdError
201199
*/
202-
static Object removePreserveAndPrune(Context ctx, Object input, JsonLdOptions opts,
203-
Set<Object> toPrune) throws JsonLdError {
200+
static Object removePreserve(Context ctx, Object input, JsonLdOptions opts) throws JsonLdError {
204201
// recurse through arrays
205202
if (isArray(input)) {
206203
final List<Object> output = new ArrayList<Object>();
207204
for (final Object i : (List<Object>) input) {
208-
final Object result = removePreserveAndPrune(ctx, i, opts, toPrune);
205+
final Object result = removePreserve(ctx, i, opts);
209206
// drop nulls from arrays
210207
if (result != null) {
211208
output.add(result);
@@ -228,29 +225,66 @@ static Object removePreserveAndPrune(Context ctx, Object input, JsonLdOptions op
228225

229226
// recurse through @lists
230227
if (isList(input)) {
231-
((Map<String, Object>) input).put("@list", removePreserveAndPrune(ctx,
232-
((Map<String, Object>) input).get("@list"), opts, toPrune));
228+
((Map<String, Object>) input).put("@list", removePreserve(ctx,
229+
((Map<String, Object>) input).get("@list"), opts));
233230
return input;
234231
}
235232

236233
// recurse through properties
237234
for (final String prop : new LinkedHashSet<>(((Map<String, Object>) input).keySet())) {
238-
Object result = removePreserveAndPrune(ctx, ((Map<String, Object>) input).get(prop),
239-
opts, toPrune);
235+
Object result = removePreserve(ctx, ((Map<String, Object>) input).get(prop),
236+
opts);
240237
final String container = ctx.getContainer(prop);
241238
if (opts.getCompactArrays() && isArray(result)
242239
&& ((List<Object>) result).size() == 1 && container == null) {
243240
result = ((List<Object>) result).get(0);
244241
}
245-
if (ctx.expandIri(prop, false, false, null, null).equals(JsonLdConsts.ID)
246-
&& toPrune.contains(result)) {
247-
((Map<String, Object>) input).remove(prop);
242+
}
243+
}
244+
return input;
245+
}
246+
247+
/**
248+
* Removes the @preserve keywords and blank node IDs to prune as the last
249+
* step of the framing algorithm.
250+
*
251+
* @param input
252+
* the framed, compacted output.
253+
* @param toPrune
254+
* The blank node IDs to prune.
255+
*/
256+
static void pruneBlankNodes(Object input, Set<Object> toPrune) {
257+
// recurse through arrays
258+
if (isArray(input)) {
259+
final List<Object> output = new ArrayList<Object>();
260+
for (final Object i : (List<Object>) input) {
261+
pruneBlankNodes(i, toPrune);
262+
}
263+
input = output;
264+
} else if (isObject(input)) {
265+
// skip @values
266+
if (isValue(input)) {
267+
return;
268+
}
269+
270+
// recurse through @lists
271+
if (isList(input)) {
272+
pruneBlankNodes(((Map<String, Object>) input).get("@list"), toPrune);
273+
return;
274+
}
275+
276+
// recurse through properties
277+
for (final String prop : new LinkedHashSet<>(((Map<String, Object>) input).keySet())) {
278+
if (prop.equals(JsonLdConsts.ID)) {
279+
final String id = (String) ((Map<String, Object>) input).get(JsonLdConsts.ID);
280+
if (toPrune.contains(id)) {
281+
((Map<String, Object>) input).remove(JsonLdConsts.ID);
282+
}
248283
} else {
249-
((Map<String, Object>) input).put(prop, result);
284+
pruneBlankNodes(((Map<String, Object>) input).get(prop), toPrune);
250285
}
251286
}
252287
}
253-
return input;
254288
}
255289

256290
/**
@@ -307,7 +341,7 @@ static boolean compareValues(Object v1, Object v2) {
307341
if ((v1 instanceof Map && ((Map<String, Object>) v1).containsKey("@id"))
308342
&& (v2 instanceof Map && ((Map<String, Object>) v2).containsKey("@id"))
309343
&& ((Map<String, Object>) v1).get("@id")
310-
.equals(((Map<String, Object>) v2).get("@id"))) {
344+
.equals(((Map<String, Object>) v2).get("@id"))) {
311345
return true;
312346
}
313347

core/src/test/java/com/github/jsonldjava/core/JsonLdFramingTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,20 @@ public void testFrame0008() throws IOException, JsonLdError {
139139
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0008-out.jsonld"));
140140
assertEquals(out, frame2);
141141
}
142+
143+
@Test
144+
public void testFramep050() throws IOException, JsonLdError {
145+
final Object frame = JsonUtils
146+
.fromInputStream(getClass().getResourceAsStream("/json-ld.org/frame-p050-frame.jsonld"));
147+
final Object in = JsonUtils
148+
.fromInputStream(getClass().getResourceAsStream("/json-ld.org/frame-p050-in.jsonld"));
149+
150+
final JsonLdOptions opts = new JsonLdOptions();
151+
opts.setProcessingMode("json-ld-1.1");
152+
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);
153+
154+
final Object out = JsonUtils
155+
.fromInputStream(getClass().getResourceAsStream("/json-ld.org/frame-p050-out.jsonld"));
156+
assertEquals(out, frame2);
157+
}
142158
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"@context": {
3+
"@vocab": "http://example/",
4+
"id": "@id"
5+
},
6+
"id": {},
7+
"name": {}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"@context": {
3+
"@vocab": "http://example/",
4+
"id": "@id"
5+
},
6+
"id": "_:bnode0",
7+
"name": "foo"
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"@context": {
3+
"@vocab": "http://example/",
4+
"id": "@id"
5+
},
6+
"@graph": [{"name": "foo"}]
7+
}

0 commit comments

Comments
 (0)