commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [10/50] incubator-commonsrdf git commit: parse into JsonLdGraph, or copy into new graph
Date Tue, 04 Oct 2016 01:30:31 GMT
parse into JsonLdGraph, or copy into new graph


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/7cb7ecba
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/7cb7ecba
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/7cb7ecba

Branch: refs/heads/rdf4j
Commit: 7cb7ecbab590a6826bdaddac99adf0e0236c07cb
Parents: 5bb0369
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue Apr 12 09:16:17 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue Apr 12 09:16:17 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdGraph.java     |  6 ++-
 .../rdf/jsonldjava/JsonLdParserBuilder.java     | 41 ++++++++++++++++----
 2 files changed, 39 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/7cb7ecba/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
index 194479c..0fe3690 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
@@ -49,6 +49,10 @@ public class JsonLdGraph implements Graph {
 	 */
 	private RDFDataset rdfDataSet;
 
+	public RDFDataset getRdfDataSet() {
+		return rdfDataSet;
+	}
+
 	private JsonLdRDFTermFactory rdfTermFactory;
 
 	/**
@@ -115,7 +119,7 @@ public class JsonLdGraph implements Graph {
 		}
 	}
 	@Override
-	public void close() throws Exception {
+	public void close() {
 		// Drop the memory reference, but don't clear it
 		rdfDataSet = null;		
 	}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/7cb7ecba/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 aeb3af2..ff9131e 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
@@ -24,18 +24,19 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.file.Files;
-import java.nio.file.Path;
-import java.text.ParseException;
-import java.util.Optional;
 import java.util.function.Predicate;
 
+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.fasterxml.jackson.core.JsonParseException;
+import com.github.jsonldjava.core.JsonLdError;
+import com.github.jsonldjava.core.JsonLdOptions;
+import com.github.jsonldjava.core.JsonLdProcessor;
+import com.github.jsonldjava.core.RDFDataset;
 import com.github.jsonldjava.utils.JsonUtils;
 
 public class JsonLdParserBuilder extends AbstractRDFParserBuilder {
@@ -85,11 +86,36 @@ public class JsonLdParserBuilder extends AbstractRDFParserBuilder {
 	}
 	
 	@Override
-	protected void parseSynchronusly() throws IOException, ParseException {		
+	protected void parseSynchronusly() throws IOException, RDFParseException {		
 		Object json = readSource();
-	}
+		JsonLdOptions options = new JsonLdOptions();
+		getBase().map(IRI::getIRIString).ifPresent(options::setBase);
+		// TODO: base from readSource() (after redirection and Content-Location header) 
+		// should be forwarded		
+
+		// TODO: Modify JsonLdProcessor to accept the target RDFDataset
+		RDFDataset rdfDataset;
+		try {
+			rdfDataset = (RDFDataset) JsonLdProcessor.toRDF(json, options);
+		} catch (JsonLdError e) {
+			throw new RDFParseException(e);
+		}
 
-	private Object readSource() throws IOException, ParseException {
+		Graph intoGraph = getIntoGraph().get();
+		if (intoGraph instanceof JsonLdGraph) {
+			// We can just move over the map content directly:
+			JsonLdGraph jsonLdGraph = (JsonLdGraph) intoGraph;
+			jsonLdGraph.getRdfDataSet().putAll(rdfDataset);				
+		} else {
+			// 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.getTriples().sequential().forEach(intoGraph::add);
+			}
+		}
+	}
+	
+	private Object readSource() throws IOException, RDFParseException {
 		// Due to checked IOException we can't easily 
 		// do this with .map and .orElseGet()
 		
@@ -97,6 +123,7 @@ public class JsonLdParserBuilder extends AbstractRDFParserBuilder {
 			return JsonUtils.fromInputStream(getSourceInputStream().get());
 		}
 		if (getSourceIri().isPresent()) {
+			// TODO: propagate @base from content
 			return JsonUtils.fromURL(asURL(getSourceIri().get()), 
 					JsonUtils.getDefaultHttpClient());			
 		}


Mime
View raw message