commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [48/50] incubator-commonsrdf git commit: parse into quads
Date Mon, 04 Jul 2016 09:37:52 GMT
parse into quads


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

Branch: refs/heads/jena
Commit: 40b94d32d7398fc68934d509310dbc9ebace6464
Parents: f371de8
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Mon Jul 4 10:35:17 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Jul 4 10:35:17 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/jena/JenaCommonsRDF.java | 34 ++++++++++++++++++--
 .../commons/rdf/jena/JenaRDFParserBuilder.java  | 10 +++++-
 .../commons/rdf/jena/TestRDFParserBuilder.java  |  4 ++-
 3 files changed, 43 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/40b94d32/jena/src/main/java/org/apache/commons/rdf/jena/JenaCommonsRDF.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaCommonsRDF.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaCommonsRDF.java
index 881b176..338a47d 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaCommonsRDF.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaCommonsRDF.java
@@ -19,6 +19,7 @@
 package org.apache.commons.rdf.jena;
 
 import java.util.Optional;
+import java.util.function.Consumer;
 
 import org.apache.commons.rdf.api.* ;
 import org.apache.commons.rdf.jena.impl.*;
@@ -30,6 +31,7 @@ import org.apache.jena.riot.Lang;
 import org.apache.jena.riot.RDFDataMgr;
 import org.apache.jena.riot.RDFLanguages;
 import org.apache.jena.riot.system.StreamRDF ;
+import org.apache.jena.riot.system.StreamRDFBase;
 import org.apache.jena.sparql.graph.GraphFactory ;
 
 /** A set of utilities for moving between CommonsRDF and Jena
@@ -111,6 +113,9 @@ public class JenaCommonsRDF {
 
     /** Convert from Jena {@link Node} to any RDFCommons implementation */
     public static RDFTerm fromJena(RDFTermFactory factory, Node node) {
+    	if (node == null) { 
+    		return null;
+    	}
     	if (factory instanceof RDFTermFactoryJena) {
     		// No need to convert, just wrap
     		return fromJena(node);
@@ -161,11 +166,34 @@ public class JenaCommonsRDF {
    }
    
    /** Create a {@link StreamRDF} that inserts into any RDFCommons implementation of Graph
*/
-   public static StreamRDF streamJenaToCommonsRDF(RDFTermFactory factory, Graph graph) {
-       return new ToGraph(factory, graph) ;
+   public static StreamRDF streamJenaToCommonsRDF(RDFTermFactory factory, Consumer<Quad>
consumer) {
+       return new StreamRDFBase() {
+    	   @Override
+    	public void quad(org.apache.jena.sparql.core.Quad quad) {
+    		consumer.accept(JenaCommonsRDF.fromJena(factory, quad));
+    	}
+       };
    }
 
-   public static void conversionError(String msg) {
+
+
+public static Quad fromJena(RDFTermFactory factory, org.apache.jena.sparql.core.Quad quad)
{
+   	if (factory instanceof RDFTermFactoryJena) {
+		// No need to convert, just wrap
+		return fromJena(quad);
+	}
+    BlankNodeOrIRI graphName = (BlankNodeOrIRI)(fromJena(factory, quad.getGraph())) ;
+    BlankNodeOrIRI subject = (BlankNodeOrIRI)(fromJena(factory, quad.getSubject())) ;
+    IRI predicate = (IRI)(fromJena(factory, triple.getPredicate())) ;
+    RDFTerm object = fromJena(factory, triple.getObject()) ;
+    return factory.createQuad(graphName, subject, predicate, object);
+}
+
+public static Quad fromJena(org.apache.jena.sparql.core.Quad quad) {	// 
+	return JCR_Factory.fromJena(quad) ;
+}
+
+public static void conversionError(String msg) {
         throw new ConversionException(msg) ;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/40b94d32/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFParserBuilder.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFParserBuilder.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFParserBuilder.java
index 604db1a..afd71d4 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFParserBuilder.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFParserBuilder.java
@@ -25,9 +25,11 @@ import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.RDFParserBuilder;
 import org.apache.commons.rdf.api.RDFTermFactory;
 import org.apache.commons.rdf.simple.AbstractRDFParserBuilder;
+import org.apache.jena.graph.Graph;
 import org.apache.jena.riot.Lang;
 import org.apache.jena.riot.RDFDataMgr;
 import org.apache.jena.riot.system.StreamRDF;
+import org.apache.jena.riot.system.StreamRDFLib;
 
 public class JenaRDFParserBuilder extends AbstractRDFParserBuilder implements RDFParserBuilder
{
 
@@ -37,7 +39,13 @@ public class JenaRDFParserBuilder extends AbstractRDFParserBuilder implements
RD
 
 	@Override
 	protected void parseSynchronusly() throws IOException {
-		StreamRDF dest = JenaCommonsRDF.streamJenaToCommonsRDF(getRdfTermFactory().get(), getIntoGraph().get());
+		StreamRDF dest;
+		if (getTargetGraph().isPresent() && getTargetGraph().get() instanceof JenaGraph)
{
+			Graph jenaGraph = ((JenaGraph)getTargetGraph().get()).getGraph();
+			dest = StreamRDFLib.graph(jenaGraph);
+		} else {		
+			dest = JenaCommonsRDF.streamJenaToCommonsRDF(getRdfTermFactory().get(), getTarget());
+		}
 
 		Lang lang = getContentTypeSyntax().flatMap(JenaCommonsRDF::rdfSyntaxToLang).orElse(null);
 		String baseStr = getBase().map(IRI::getIRIString).orElse(null);

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/40b94d32/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFParserBuilder.java
----------------------------------------------------------------------
diff --git a/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFParserBuilder.java b/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFParserBuilder.java
index 8d15bf1..88fc002 100644
--- a/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFParserBuilder.java
+++ b/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFParserBuilder.java
@@ -25,6 +25,7 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.rdf.api.Graph;
+import org.apache.commons.rdf.api.RDFParserBuilder.ParseResult;
 import org.apache.commons.rdf.api.RDFSyntax;
 import org.junit.Test;
 
@@ -32,7 +33,8 @@ public class TestRDFParserBuilder {
 	@Test
 	public void parseTurtle() throws Exception {
 		Path d = Paths.get("D.ttl");
-		Future<Graph> gFuture = new JenaRDFParserBuilder().contentType(RDFSyntax.TURTLE).source(d).parse();
+		Graph g = new RDFTermFactoryJena().createGraph();
+		Future<ParseResult> gFuture = new JenaRDFParserBuilder().contentType(RDFSyntax.TURTLE).source(d).target(g).parse();
 		Graph g = gFuture.get(5, TimeUnit.SECONDS);
 		assertEquals(3, g.size());
 	}


Mime
View raw message