Skip to content

Commit 9c32828

Browse files
Merge branch 'master' into implicitSubframe
simplify test
2 parents 33a9223 + 4dd9cd3 commit 9c32828

File tree

11 files changed

+357
-564
lines changed

11 files changed

+357
-564
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: java
2-
sudo: false
3-
cache:
4-
directories:
5-
- $HOME/.m2
2+
dist: trusty
63
jdk:
74
- oraclejdk8
5+
- oraclejdk9
86
notifications:
97
email:
108
- ansell.peter@gmail.com

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
import java.util.ArrayList;
1212
import java.util.Collection;
1313
import java.util.Collections;
14+
import java.util.HashMap;
1415
import java.util.HashSet;
1516
import java.util.LinkedHashMap;
1617
import java.util.List;
1718
import java.util.Map;
19+
import java.util.Map.Entry;
1820
import java.util.Set;
1921
import java.util.TreeMap;
22+
import java.util.stream.Collectors;
23+
import java.util.stream.Stream;
2024

2125
import org.slf4j.Logger;
2226
import org.slf4j.LoggerFactory;

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
public class JsonLdOptions {
1212

13+
private static final String JSON_LD_1_0 = "json-ld-1.0";
14+
15+
private static final String JSON_LD_1_1 = "json-ld-1.1";
16+
1317
public static final boolean DEFAULT_COMPACT_ARRAYS = true;
1418

1519
/**
@@ -47,7 +51,7 @@ public JsonLdOptions(String base) {
4751
/**
4852
* http://www.w3.org/TR/json-ld-api/#widl-JsonLdOptions-processingMode
4953
*/
50-
private String processingMode = "json-ld-1.0";
54+
private String processingMode = JSON_LD_1_0;
5155
/**
5256
* http://www.w3.org/TR/json-ld-api/#widl-JsonLdOptions-documentLoader
5357
*/
@@ -58,6 +62,7 @@ public JsonLdOptions(String base) {
5862
private Boolean embed = null;
5963
private Boolean explicit = null;
6064
private Boolean omitDefault = null;
65+
private Boolean pruneBlankNodeIdentifiers = true;
6166

6267
// RDF conversion options :
6368
// http://www.w3.org/TR/json-ld-api/#serialize-rdf-as-json-ld-algorithm
@@ -90,6 +95,17 @@ public void setOmitDefault(Boolean omitDefault) {
9095
this.omitDefault = omitDefault;
9196
}
9297

98+
public Boolean getPruneBlankNodeIdentifiers() {
99+
return pruneBlankNodeIdentifiers && getProcessingMode().equals(JSON_LD_1_1);
100+
}
101+
102+
public void setPruneBlankNodeIdentifiers(Boolean pruneBlankNodeIdentifiers) {
103+
if(pruneBlankNodeIdentifiers) {
104+
setProcessingMode(JSON_LD_1_1);
105+
}
106+
this.pruneBlankNodeIdentifiers = pruneBlankNodeIdentifiers;
107+
}
108+
93109
public Boolean getCompactArrays() {
94110
return compactArrays;
95111
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
import java.util.ArrayList;
66
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.HashSet;
79
import java.util.LinkedHashMap;
810
import java.util.List;
911
import java.util.Map;
12+
import java.util.Set;
13+
import java.util.Map.Entry;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
1016

1117
import com.github.jsonldjava.core.JsonLdError.Error;
1218
import com.github.jsonldjava.impl.NQuadRDFParser;
@@ -319,10 +325,33 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
319325
final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH);
320326
final Map<String, Object> rval = activeCtx.serialize();
321327
rval.put(alias, compacted);
322-
JsonLdUtils.removePreserve(activeCtx, rval, opts);
328+
329+
Set<Object> toPrune = opts.getPruneBlankNodeIdentifiers() ?
330+
blankNodeIdsToPrune(rval, new HashSet<>()) : Collections.emptySet();
331+
JsonLdUtils.removePreserveAndPrune(activeCtx, rval, opts, toPrune);
323332
return rval;
324333
}
325334

335+
private static Set<Object> blankNodeIdsToPrune(Object input, Set<Object> set) {
336+
if (input instanceof List) {
337+
((List<?>) input).forEach(e -> blankNodeIdsToPrune(e, set));
338+
} else if (input instanceof Map) {
339+
((Map<?, ?>) input).entrySet().forEach(e -> blankNodeIdsToPrune(e.getValue(), set));
340+
} else if (input instanceof String) {
341+
String p = (String) input;
342+
if (p.startsWith("_:")) {
343+
if(set.contains(p)){
344+
// more than 1, don't prune
345+
set.remove(p);
346+
} else {
347+
// exactly 1, prune
348+
set.add(p);
349+
}
350+
}
351+
}
352+
return set;
353+
}
354+
326355
/**
327356
* A registry for RDF Parsers (in this case, JSONLDSerializers) used by
328357
* fromRDF if no specific serializer is specified and options.format is set.

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.util.Arrays;
77
import java.util.Collection;
88
import java.util.Collections;
9+
import java.util.LinkedHashSet;
910
import java.util.List;
1011
import java.util.Map;
12+
import java.util.Set;
1113

1214
import com.github.jsonldjava.utils.JsonLdUrl;
1315
import com.github.jsonldjava.utils.Obj;
@@ -187,24 +189,25 @@ public static boolean isRelativeIri(String value) {
187189
}
188190

189191
/**
190-
* Removes the @preserve keywords as the last step of the framing algorithm.
192+
* Removes the @preserve keywords and blank node IDs to prune as the last step of the framing algorithm.
191193
*
192194
* @param ctx
193195
* the active context used to compact the input.
194196
* @param input
195197
* the framed, compacted output.
198+
* @param toPrune The blank node IDs to prune.
196199
* @param options
197200
* the compaction options used.
198201
*
199202
* @return the resulting output.
200203
* @throws JsonLdError
201204
*/
202-
static Object removePreserve(Context ctx, Object input, JsonLdOptions opts) throws JsonLdError {
205+
static Object removePreserveAndPrune(Context ctx, Object input, JsonLdOptions opts, Set<Object> toPrune) throws JsonLdError {
203206
// recurse through arrays
204207
if (isArray(input)) {
205208
final List<Object> output = new ArrayList<Object>();
206209
for (final Object i : (List<Object>) input) {
207-
final Object result = removePreserve(ctx, i, opts);
210+
final Object result = removePreserveAndPrune(ctx, i, opts, toPrune);
208211
// drop nulls from arrays
209212
if (result != null) {
210213
output.add(result);
@@ -228,19 +231,23 @@ static Object removePreserve(Context ctx, Object input, JsonLdOptions opts) thro
228231
// recurse through @lists
229232
if (isList(input)) {
230233
((Map<String, Object>) input).put("@list",
231-
removePreserve(ctx, ((Map<String, Object>) input).get("@list"), opts));
234+
removePreserveAndPrune(ctx, ((Map<String, Object>) input).get("@list"), opts, toPrune));
232235
return input;
233236
}
234237

235238
// recurse through properties
236-
for (final String prop : ((Map<String, Object>) input).keySet()) {
237-
Object result = removePreserve(ctx, ((Map<String, Object>) input).get(prop), opts);
239+
for (final String prop : new LinkedHashSet<>(((Map<String, Object>) input).keySet())) {
240+
Object result = removePreserveAndPrune(ctx, ((Map<String, Object>) input).get(prop), opts, toPrune);
238241
final String container = ctx.getContainer(prop);
239242
if (opts.getCompactArrays() && isArray(result)
240243
&& ((List<Object>) result).size() == 1 && container == null) {
241244
result = ((List<Object>) result).get(0);
242245
}
243-
((Map<String, Object>) input).put(prop, result);
246+
if(ctx.expandIri(prop, false, false, null, null).equals(JsonLdConsts.ID) && toPrune.contains(result)) {
247+
((Map<String, Object>) input).remove(prop);
248+
} else {
249+
((Map<String, Object>) input).put(prop, result);
250+
}
244251
}
245252
}
246253
return input;

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.jsonldjava.core;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
45

56
import java.io.IOException;
67
import java.util.Map;
@@ -41,6 +42,24 @@ public void testFrame0002() throws IOException, JsonLdError {
4142
assertEquals(out, frame2);
4243
}
4344

45+
@Test
46+
public void testFrame0003() throws IOException, JsonLdError {
47+
final Object frame = JsonUtils
48+
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-frame.jsonld"));
49+
final Object in = JsonUtils
50+
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-in.jsonld"));
51+
52+
JsonLdOptions opts = new JsonLdOptions();
53+
opts.setCompactArrays(false);
54+
opts.setProcessingMode("json-ld-1.1");
55+
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);
56+
assertFalse("Result should contain no blank nodes", frame2.toString().contains("_:"));
57+
58+
final Object out = JsonUtils
59+
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0003-out.jsonld"));
60+
assertEquals(out, frame2);
61+
}
62+
4463
@Test
4564
public void testFrame0004() throws IOException, JsonLdError {
4665
final Object frame = JsonUtils
@@ -54,9 +73,6 @@ public void testFrame0004() throws IOException, JsonLdError {
5473

5574
final Object out = JsonUtils
5675
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0004-out.jsonld"));
57-
//System.out.println(JsonUtils.toPrettyString(out));
58-
//System.out.println(JsonUtils.toPrettyString(frame2));
5976
assertEquals(out, frame2);
6077
}
61-
6278
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"@context" : {
3+
"@vocab" : "http://xmlns.com/foaf/0.1/"
4+
},
5+
"@graph" : [ {
6+
"@type" : "Person",
7+
"member" : [{
8+
"@type" : "Group"
9+
}]
10+
} ]
11+
}

0 commit comments

Comments
 (0)