Updated for latest RDFPArserBuilder
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/3d63a0e0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/3d63a0e0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/3d63a0e0 Branch: refs/heads/jena-jsonld-rdf4j-integration Commit: 3d63a0e0fdd50843c342e516514a528bc3316874 Parents: 7010541 Author: Stian Soiland-Reyes <st...@apache.org> Authored: Fri Sep 9 17:14:26 2016 +0100 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Fri Sep 9 17:14:26 2016 +0100 ---------------------------------------------------------------------- .../rdf/jsonldjava/JsonLdParserBuilder.java | 66 ++++++++++++++------ .../rdf/jsonldjava/JsonLdRDFTermFactory.java | 2 +- 2 files changed, 47 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3d63a0e0/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java index d609aa8..e38af2c 100644 --- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java +++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java @@ -26,11 +26,10 @@ import java.net.URL; import java.nio.file.Files; import java.util.function.Predicate; +import org.apache.commons.rdf.api.Dataset; import org.apache.commons.rdf.api.Graph; import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.RDFParserBuilder; import org.apache.commons.rdf.api.RDFSyntax; -import org.apache.commons.rdf.api.RDFTermFactory; import org.apache.commons.rdf.simple.AbstractRDFParserBuilder; import com.github.jsonldjava.core.JsonLdError; @@ -39,15 +38,15 @@ import com.github.jsonldjava.core.JsonLdProcessor; import com.github.jsonldjava.core.RDFDataset; import com.github.jsonldjava.utils.JsonUtils; -public class JsonLdParserBuilder extends AbstractRDFParserBuilder { +public class JsonLdParserBuilder extends AbstractRDFParserBuilder<JsonLdParserBuilder> { @Override - protected RDFTermFactory createRDFTermFactory() { + protected JsonLdRDFTermFactory createRDFTermFactory() { return new JsonLdRDFTermFactory(); } @Override - public RDFParserBuilder contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException { + public JsonLdParserBuilder contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException { if (rdfSyntax != null && rdfSyntax != RDFSyntax.JSONLD) { throw new IllegalArgumentException("Unsupported contentType: " + rdfSyntax); } @@ -55,7 +54,7 @@ public class JsonLdParserBuilder extends AbstractRDFParserBuilder { } @Override - public RDFParserBuilder contentType(String contentType) throws IllegalArgumentException { + public JsonLdParserBuilder contentType(String contentType) throws IllegalArgumentException { JsonLdParserBuilder c = (JsonLdParserBuilder) super.contentType(contentType); if (c.getContentType().filter(Predicate.isEqual(RDFSyntax.JSONLD).negate()).isPresent()) { throw new IllegalArgumentException("Unsupported contentType: " + contentType); @@ -79,7 +78,7 @@ public class JsonLdParserBuilder extends AbstractRDFParserBuilder { } @Override - protected void parseSynchronusly() throws IOException, RDFParseException { + protected void parseSynchronusly() throws IOException { Object json = readSource(); JsonLdOptions options = new JsonLdOptions(); getBase().map(IRI::getIRIString).ifPresent(options::setBase); @@ -91,23 +90,50 @@ public class JsonLdParserBuilder extends AbstractRDFParserBuilder { try { rdfDataset = (RDFDataset) JsonLdProcessor.toRDF(json, options); } catch (JsonLdError e) { - throw new RDFParseException(e); - } - Graph intoGraph = getIntoGraph().get(); - if (intoGraph instanceof JsonLdGraph && ! intoGraph.contains(null, null, null)) { - // Empty graph, we can just move over the map content directly: - JsonLdGraph jsonLdGraph = (JsonLdGraph) intoGraph; - jsonLdGraph.getRdfDataSet().putAll(rdfDataset); - } else { + throw new IOException("Could not parse Json-LD", e); + } + if (getTargetGraph().isPresent()) { + Graph intoGraph = getTargetGraph().get(); + if (intoGraph instanceof JsonLdGraph && ! intoGraph.contains(null, null, null)) { + // Empty graph, we can just move over the map content directly: + JsonLdGraph jsonLdGraph = (JsonLdGraph) intoGraph; + jsonLdGraph.getRdfDataSet().putAll(rdfDataset); + return; + // otherwise we have to merge as normal + } // TODO: Modify JsonLdProcessor to have an actual triple callback - try (JsonLdGraph parsedGraph = new JsonLdGraph(rdfDataset)) { - // sequential() as we don't know if intoGraph is thread safe :-/ - parsedGraph.stream().sequential().forEach(intoGraph::add); - } + Graph parsedGraph = getJsonLdRDFTermFactory().asGraph(rdfDataset); + // sequential() as we don't know if destination is thread safe :-/ + parsedGraph.stream().sequential().forEach(intoGraph::add); + } else if (getTargetDataset().isPresent()) { + Dataset intoDataset = getTargetDataset().get(); + if (intoDataset instanceof JsonLdDataset && + ! intoDataset.contains(null, null, null, null)) { + JsonLdDataset jsonLdDataset = (JsonLdDataset) intoDataset; + // Empty - we can just do a brave replace! + jsonLdDataset.getRdfDataSet().putAll(rdfDataset); + return; + // otherwise we have to merge.. but also avoid duplicate triples, + // map blank nodes etc, so we'll fall back to normal Dataset appending. + } + Dataset fromDataset = getJsonLdRDFTermFactory().asDataset(rdfDataset); + // .sequential() as we don't know if destination is thread-safe :-/ + fromDataset.stream().sequential().forEach(intoDataset::add); + } else { + Dataset fromDataset = getJsonLdRDFTermFactory().asDataset(rdfDataset); + // No need for .sequential() here + fromDataset.stream().forEach(getTarget()); } } - private Object readSource() throws IOException, RDFParseException { + private JsonLdRDFTermFactory getJsonLdRDFTermFactory() { + if (getRdfTermFactory().isPresent() && getRdfTermFactory().get() instanceof JsonLdRDFTermFactory) { + return (JsonLdRDFTermFactory) getRdfTermFactory().get(); + } + return createRDFTermFactory(); + } + + private Object readSource() throws IOException { // Due to checked IOException we can't easily // do this with .map and .orElseGet() http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3d63a0e0/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java index fa60213..4a05724 100644 --- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java +++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java @@ -41,7 +41,7 @@ import com.github.jsonldjava.core.RDFDataset.Node; public final class JsonLdRDFTermFactory implements RDFTermFactory { - private final String bnodePrefix; + final String bnodePrefix; public JsonLdRDFTermFactory() { // An "outside Graph" bnodePrefix