commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [2/9] incubator-commonsrdf git commit: Moved JenaCommonsRDF methods to RDFTermFactoryJena
Date Fri, 08 Jul 2016 11:53:46 GMT
Moved JenaCommonsRDF methods to RDFTermFactoryJena


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

Branch: refs/heads/jena
Commit: 354798863f6851dae60a75289a8f0c4b2efb29b3
Parents: a7c1dbe
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jul 8 09:28:23 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jul 8 09:28:23 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/jena/JenaCommonsRDF.java | 222 ------------
 .../commons/rdf/jena/JenaRDFParserBuilder.java  |   4 +-
 .../commons/rdf/jena/RDFTermFactoryJena.java    | 343 +++++++++++++++----
 .../apache/commons/rdf/jena/impl/GraphImpl.java |  41 ++-
 .../commons/rdf/jena/impl/JenaFactory.java      | 148 ++++----
 .../apache/commons/rdf/jena/impl/QuadImpl.java  |  10 +-
 .../commons/rdf/jena/impl/TripleImpl.java       |   7 +-
 7 files changed, 398 insertions(+), 377 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/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
deleted file mode 100644
index 397d77c..0000000
--- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaCommonsRDF.java
+++ /dev/null
@@ -1,222 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.commons.rdf.jena;
-
-import java.util.Optional;
-import java.util.UUID;
-import java.util.function.Consumer;
-
-import org.apache.commons.rdf.api.BlankNode;
-import org.apache.commons.rdf.api.BlankNodeOrIRI;
-import org.apache.commons.rdf.api.Graph;
-import org.apache.commons.rdf.api.IRI;
-import org.apache.commons.rdf.api.Literal;
-import org.apache.commons.rdf.api.Quad;
-import org.apache.commons.rdf.api.RDFSyntax;
-import org.apache.commons.rdf.api.RDFTerm;
-import org.apache.commons.rdf.api.RDFTermFactory;
-import org.apache.commons.rdf.api.Triple;
-import org.apache.commons.rdf.jena.impl.JenaFactory;
-import org.apache.jena.datatypes.RDFDatatype ;
-import org.apache.jena.datatypes.xsd.XSDDatatype ;
-import org.apache.jena.graph.Node ;
-import org.apache.jena.graph.NodeFactory ;
-import org.apache.jena.riot.Lang;
-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
- * The {@link RDFTermFactory} for is {@link RDFTermFactoryJena} which
- * creates CommonsRDF objects backed by Jena.   
- * <p>
- * This class encapsulates moving between existing object (RDFTerms, Triples, Graphs)
- * which is usually necessary when working with existing data.   
- * 
- * @see RDFTermFactoryJena
- */
-public class JenaCommonsRDF {
-
-    /** Convert a CommonsRDF RDFTerm to a Jena Node.
-     * If the RDFTerm was from Jena originally, return that original object else
-     * create a copy using Jena objects. 
-     */
-    public static Node toJena(RDFTerm term) {
-    	if (term == null) { 
-    		return null;
-    	}
-        if ( term instanceof JenaNode )
-            return ((JenaNode)term).asJenaNode() ;
-        
-        if ( term instanceof IRI ) 
-            return NodeFactory.createURI(((IRI)term).getIRIString()) ;
-        
-        if ( term instanceof Literal ) {
-            Literal lit = (Literal)term ; 
-            RDFDatatype dt = NodeFactory.getType(lit.getDatatype().getIRIString()) ;
-            String lang = lit.getLanguageTag().orElse("") ;
-            return NodeFactory.createLiteral(lit.getLexicalForm(), lang, dt) ; 
-        }
-        
-        if ( term instanceof BlankNode ) {
-            String id = ((BlankNode)term).uniqueReference() ;
-            return NodeFactory.createBlankNode(id) ;
-        }
-        conversionError("Not a concrete RDF Term: "+term) ;
-        return null ;
-    }
-
-    /** Convert a CommonsRDF Triple to a Jena Triple.
-     * If the Triple was from Jena originally, return that original object else
-     * create a copy using Jena objects. 
-     */
-    public static org.apache.jena.graph.Triple toJena(Triple triple) {
-        if ( triple instanceof JenaTriple )
-            return ((JenaTriple)triple).asJenaTriple() ;
-        return new org.apache.jena.graph.Triple(toJena(triple.getSubject()), toJena(triple.getPredicate()),
toJena(triple.getObject()) ) ;   
-    }
-
-    /** Convert a CommonsRDF Graph to a Jena Graph.
-     * If the Graph was from Jena originally, return that original object else
-     * create a copy using Jena objects. 
-     */
-    public static org.apache.jena.graph.Graph toJena(Graph graph) {
-        if ( graph instanceof JenaGraph )
-            return ((JenaGraph)graph).asJenaGraph() ;
-        org.apache.jena.graph.Graph g = GraphFactory.createGraphMem() ;
-        graph.stream().forEach(t->g.add(toJena(t))) ; 
-        return g ;   
-    }
-
-    /** Adapt an existing Jena Node to CommonsRDF {@link RDFTerm}. 
-     * @param salt */
-    public static RDFTerm fromJena( Node node, UUID salt) {
-        return JenaFactory.fromJena(node, salt) ;
-    }
-
-    /** Adapt an existing Jena Triple to CommonsRDF {@link Triple}. 
-     * @param salt */
-    public static Triple fromJena(org.apache.jena.graph.Triple triple, UUID salt) {
-        return JenaFactory.fromJena(triple, salt) ;
-    }
-
-    /** Adapt an existring Jena Graph to CommonsRDF {@link Graph}.
-     * This does not take a copy.
-     * Changes to the CommonsRDF Graph are reflected in the jena graph.
-     */    
-    public static Graph fromJena(org.apache.jena.graph.Graph graph) {
-        return JenaFactory.fromJena(graph) ;
-    }
-
-    /** Convert from Jena {@link Node} to any RDFCommons implementation 
-     * @param salt */
-    public static RDFTerm fromJena(RDFTermFactory factory, Node node, UUID salt) {
-    	if (node == null) { 
-    		return null;
-    	}
-    	if (factory instanceof RDFTermFactoryJena) {
-    		// No need to convert, just wrap
-    		return fromJena(node, salt);
-    	}
-        if ( node.isURI() )
-            return factory.createIRI(node.getURI()) ;
-        if ( node.isLiteral() ) {
-            String lang = node.getLiteralLanguage() ;
-            if ( lang != null && ! lang.isEmpty() )
-                return factory.createLiteral(node.getLiteralLexicalForm(), lang) ;
-            if ( node.getLiteralDatatype().equals(XSDDatatype.XSDstring) )
-                return factory.createLiteral(node.getLiteralLexicalForm()) ;
-            IRI dt = factory.createIRI(node.getLiteralDatatype().getURI()) ;
-            return factory.createLiteral(node.getLiteralLexicalForm(), dt);
-        }
-        if ( node.isBlank() )
-            return factory.createBlankNode(node.getBlankNodeLabel()) ;
-        throw new ConversionException("Node is not a concrete RDF Term: "+node) ;
-    }
-
-    /** Convert from Jena {@link org.apache.jena.graph.Triple} to any RDFCommons implementation
*/
-   public static Triple fromJena(RDFTermFactory factory, org.apache.jena.graph.Triple triple)
{
-	   	if (factory instanceof RDFTermFactoryJena) {
-			// No need to convert, just wrap
-			return fromJena(triple);
-		}
-        BlankNodeOrIRI subject = (BlankNodeOrIRI)(fromJena(factory, triple.getSubject()))
;
-        IRI predicate = (IRI)(fromJena(factory, triple.getPredicate())) ;
-        RDFTerm object = fromJena(factory, triple.getObject()) ;
-        return factory.createTriple(subject, predicate, object) ;
-    }
-
-   /** Convert from Jena to any RDFCommons implementation.
-    *  This is a copy, even if the factory is a RDFTermFactoryJena.
-    *  Use {@link #fromJena(org.apache.jena.graph.Graph)} for a wrapper.
-    */
-   public static Graph fromJena(RDFTermFactory factory, org.apache.jena.graph.Graph graph)
{
-	   	if (factory instanceof RDFTermFactoryJena) {
-			// No need to convert, just wrap
-			return fromJena(graph);
-		}
-
-	   Graph g = factory.createGraph() ;
-       graph.find(Node.ANY, Node.ANY, Node.ANY).forEachRemaining( t-> {
-           g.add(fromJena(factory, t) );
-       }) ;
-       return g ;
-   }
-   
-   /** Create a {@link StreamRDF} that inserts into any RDFCommons implementation of 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 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, quad.getPredicate())) ;
-    RDFTerm object = fromJena(factory, quad.getObject()) ;
-    return factory.createQuad(graphName, subject, predicate, object);
-}
-
-public static Quad fromJena(org.apache.jena.sparql.core.Quad quad) {	// 
-	return JenaFactory.fromJena(quad) ;
-}
-
-public static void conversionError(String msg) {
-        throw new ConversionException(msg) ;
-    }
-
-   public static Optional<Lang> rdfSyntaxToLang(RDFSyntax rdfSyntax) {
-	   return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType));	
-   }
-   public static Optional<RDFSyntax> langToRdfSyntax(Lang lang) {
-	   return RDFSyntax.byMediaType(lang.getContentType().getContentType());
-   }
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/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 bc73e55..2349f8b 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
@@ -46,10 +46,10 @@ public class JenaRDFParserBuilder extends AbstractRDFParserBuilder<JenaRDFParser
 			Graph jenaGraph = ((JenaGraph)getTargetGraph().get()).asJenaGraph();
 			dest = StreamRDFLib.graph(jenaGraph);
 		} else {		
-			dest = JenaCommonsRDF.streamJenaToCommonsRDF(getRdfTermFactory().get(), getTarget());
+			dest = RDFTermFactoryJena.streamJenaToCommonsRDF(getRdfTermFactory().get(), getTarget());
 		}
 
-		Lang lang = getContentTypeSyntax().flatMap(JenaCommonsRDF::rdfSyntaxToLang).orElse(null);
+		Lang lang = getContentTypeSyntax().flatMap(RDFTermFactoryJena::rdfSyntaxToLang).orElse(null);
 		String baseStr = getBase().map(IRI::getIRIString).orElse(null);
 
 		if (getSourceIri().isPresent()) {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java b/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
index 1284240..12b0904 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
@@ -18,72 +18,295 @@
 
 package org.apache.commons.rdf.jena;
 
+import java.util.Optional;
 import java.util.UUID;
+import java.util.function.Consumer;
 
-import org.apache.commons.rdf.api.* ;
+import org.apache.commons.rdf.api.*;
 import org.apache.commons.rdf.jena.impl.JenaFactory;
+import org.apache.jena.datatypes.RDFDatatype;
+import org.apache.jena.datatypes.xsd.XSDDatatype;
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.riot.Lang;
+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;
 
-/** RDFTermFactory with Jena-backed objects.
- *  See {@link JenaCommonsRDF} for other conversions of existing objects. 
- *  
- *  @see RDFTermFactory
+/**
+ * RDFTermFactory with Jena-backed objects. 
+ * <p>
+ * This factory can also convert existing objects from/to Jena
+ * with methods like {@link #fromJena(org.apache.jena.graph.Graph)} 
+ * and {@link #toJena(Graph)}. 
+ * 
+ * @see RDFTermFactory
  */
 public final class RDFTermFactoryJena implements RDFTermFactory {
-    
-	private UUID salt = UUID.randomUUID(); 
+
+	private UUID salt = UUID.randomUUID();
+
+	@Override
+	public BlankNode createBlankNode() {
+		return JenaFactory.createBlankNode(salt);
+	}
+
+	@Override
+	public BlankNode createBlankNode(String name) {
+		return JenaFactory.createBlankNode(name, salt);
+	}
+
+	@Override
+	public Graph createGraph() {		
+		return JenaFactory.createGraph(salt);
+	}
+
+	@Override
+	public IRI createIRI(String iri) {
+		validateIRI(iri);
+		return JenaFactory.createIRI(iri);
+	}
+
+	// Some simple validations - full IRI parsing is not cheap.
+	private static void validateIRI(String iri) {
+		if (iri.contains(" "))
+			throw new IllegalArgumentException();
+		if (iri.contains("<"))
+			throw new IllegalArgumentException();
+		if (iri.contains(">"))
+			throw new IllegalArgumentException();
+	}
+
+	@Override
+	public Literal createLiteral(String lexicalForm) {
+		return JenaFactory.createLiteral(lexicalForm);
+	}
+
+	@Override
+	public Literal createLiteral(String lexicalForm, IRI dataType) {
+		return JenaFactory.createLiteralDT(lexicalForm, dataType.getIRIString());
+	}
+
+	@Override
+	public Literal createLiteral(String lexicalForm, String languageTag) {
+		validateLang(languageTag);
+		return JenaFactory.createLiteralLang(lexicalForm, languageTag);
+	}
+
+	private static void validateLang(String languageTag) {
+		if (languageTag.contains(" "))
+			throw new IllegalArgumentException("Invalid language tag: " + languageTag);
+	}
+
+	@Override
+	public Triple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		return JenaFactory.createTriple(subject, predicate, object);
+	}
+
+	/**
+	 * Convert a CommonsRDF RDFTerm to a Jena Node. If the RDFTerm was from Jena
+	 * originally, return that original object, else create a copy using Jena
+	 * objects.
+	 */
+	public static Node toJena(RDFTerm term) {
+		if (term == null) {
+			return null;
+		}
+		if (term instanceof JenaNode)
+			// TODO: What if it's a BlankNodeImpl with
+			// a different salt? Do we need to rewrite the
+			// jena blanknode identifier?
+			return ((JenaNode) term).asJenaNode();
+
+		if (term instanceof IRI)
+			return NodeFactory.createURI(((IRI) term).getIRIString());
+
+		if (term instanceof Literal) {
+			Literal lit = (Literal) term;
+			RDFDatatype dt = NodeFactory.getType(lit.getDatatype().getIRIString());
+			String lang = lit.getLanguageTag().orElse("");
+			return NodeFactory.createLiteral(lit.getLexicalForm(), lang, dt);
+		}
+
+		if (term instanceof BlankNode) {
+			String id = ((BlankNode) term).uniqueReference();
+			return NodeFactory.createBlankNode(id);
+		}
+		conversionError("Not a concrete RDF Term: " + term);
+		return null;
+	}
+
+	/**
+	 * Convert a CommonsRDF Triple to a Jena Triple. If the Triple was from Jena
+	 * originally, return that original object else create a copy using Jena
+	 * objects.
+	 */
+	public static org.apache.jena.graph.Triple toJena(Triple triple) {
+		if (triple instanceof JenaTriple)
+			return ((JenaTriple) triple).asJenaTriple();
+		return new org.apache.jena.graph.Triple(
+				toJena(triple.getSubject()), 
+				toJena(triple.getPredicate()),
+				toJena(triple.getObject()));
+	}
+
+	/**
+	 * Convert a CommonsRDF Graph to a Jena Graph. If the Graph was from Jena
+	 * originally, return that original object else create a copy using Jena
+	 * objects.
+	 */
+	public static org.apache.jena.graph.Graph toJena(Graph graph) {
+		if (graph instanceof JenaGraph)
+			return ((JenaGraph) graph).asJenaGraph();
+		org.apache.jena.graph.Graph g = GraphFactory.createGraphMem();
+		graph.stream().forEach(t -> g.add(toJena(t)));
+		return g;
+	}
+
+	/**
+	 * Adapt an existing Jena Node to CommonsRDF {@link RDFTerm}.
+	 * 
+	 * @param salt
+	 */
+	public static RDFTerm fromJena(Node node, UUID salt) {
+		return JenaFactory.fromJena(node, salt);
+	}
+
+	/**
+	 * Adapt an existing Jena Node to CommonsRDF {@link RDFTerm}.
+	 * 
+	 * @param salt
+	 */
+	public RDFTerm fromJena(Node node) {
+		return JenaFactory.fromJena(node, salt);
+	}
+
 	
-    @Override
-    public BlankNode createBlankNode() {
-        return JenaFactory.createBlankNode(salt) ;
-    }
-
-    @Override
-    public BlankNode createBlankNode(String name) {
-        return JenaFactory.createBlankNode(name, salt) ;
-    }
-
-    @Override
-    public Graph createGraph() {
-        return JenaFactory.createGraph() ;
-    }
-
-    @Override
-    public IRI createIRI(String iri) {
-        validateIRI(iri) ;
-        return JenaFactory.createIRI(iri) ;
-    }
-
-    // Some simple validations - full IRI parsing is not cheap. 
-    private static void validateIRI(String iri) {
-        if ( iri.contains(" ") ) throw new IllegalArgumentException() ;
-        if ( iri.contains("<") ) throw new IllegalArgumentException() ;
-        if ( iri.contains(">") ) throw new IllegalArgumentException() ;
-    }
-
-    @Override
-    public Literal createLiteral(String lexicalForm) {
-        return JenaFactory.createLiteral(lexicalForm) ;
-    }
-
-    @Override
-    public Literal createLiteral(String lexicalForm, IRI dataType) {
-        return JenaFactory.createLiteralDT(lexicalForm, dataType.getIRIString()) ;
-    }
-
-    @Override
-    public Literal createLiteral(String lexicalForm, String languageTag) {
-        validateLang(languageTag) ;
-        return JenaFactory.createLiteralLang(lexicalForm, languageTag) ;
-    }
-
-    private static void validateLang(String languageTag) {
-        if ( languageTag.contains(" ") ) throw new IllegalArgumentException() ;
-    }
-
-    @Override
-    public Triple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-        return JenaFactory.createTriple(subject, predicate, object) ;
-    }
+	/**
+	 * Adapt an existing Jena Triple to CommonsRDF {@link Triple}.
+	 * 
+	 * @param salt
+	 */
+	public Triple fromJena(org.apache.jena.graph.Triple triple) {
+		return JenaFactory.fromJena(triple, salt);
+	}
+	
+	/**
+	 * Adapt an existing Jena Triple to CommonsRDF {@link Triple}.
+	 * 
+	 * @param salt
+	 */
+	public static Triple fromJena(org.apache.jena.graph.Triple triple, UUID salt) {
+		return JenaFactory.fromJena(triple, salt);
+	}
 
-}
+	/**
+	 * Adapt an existring Jena Graph to CommonsRDF {@link Graph}. This does not
+	 * take a copy. Changes to the CommonsRDF Graph are reflected in the jena
+	 * graph.
+	 */
+	public static Graph fromJena(org.apache.jena.graph.Graph graph) {
+		return fromJena(new RDFTermFactoryJena(), graph);
+	}
+	
+	/**
+	 * Convert from Jena {@link Node} to any RDFCommons implementation
+	 * 
+	 * @param salt
+	 */
+	public static RDFTerm fromJena(RDFTermFactory factory, Node node, UUID salt) {
+		if (node == null) {
+			return null;
+		}
+		if (factory instanceof RDFTermFactoryJena) {
+			// No need to convert, just wrap (we'll use the parameter-provided salt)
+			return fromJena(node, salt);
+		}
+		if (node.isURI())
+			return factory.createIRI(node.getURI());
+		if (node.isLiteral()) {
+			String lang = node.getLiteralLanguage();
+			if (lang != null && !lang.isEmpty())
+				return factory.createLiteral(node.getLiteralLexicalForm(), lang);
+			if (node.getLiteralDatatype().equals(XSDDatatype.XSDstring))
+				return factory.createLiteral(node.getLiteralLexicalForm());
+			IRI dt = factory.createIRI(node.getLiteralDatatype().getURI());
+			return factory.createLiteral(node.getLiteralLexicalForm(), dt);
+		}
+		if (node.isBlank())
+			return factory.createBlankNode(node.getBlankNodeLabel());
+		throw new ConversionException("Node is not a concrete RDF Term: " + node);
+	}
+
+	/**
+	 * Convert from Jena {@link org.apache.jena.graph.Triple} to any RDFCommons
+	 * implementation
+	 */
+	public static Triple fromJena(RDFTermFactory factory, org.apache.jena.graph.Triple triple)
{
+		if (factory instanceof RDFTermFactoryJena) {
+			// No need to convert, just wrap
+			return ((RDFTermFactoryJena)factory).fromJena(triple);
+		}
+		BlankNodeOrIRI subject = (BlankNodeOrIRI) (fromJena(factory, triple.getSubject()));
+		IRI predicate = (IRI) (fromJena(factory, triple.getPredicate()));
+		RDFTerm object = fromJena(factory, triple.getObject());
+		return factory.createTriple(subject, predicate, object);
+	}
 
+	/**
+	 * Convert from Jena to any RDFCommons implementation. This is a copy, even
+	 * if the factory is a RDFTermFactoryJena. Use
+	 * {@link #fromJena(org.apache.jena.graph.Graph)} for a wrapper.
+	 */
+	public static Graph fromJena(RDFTermFactory factory, org.apache.jena.graph.Graph graph)
{
+		if (factory instanceof RDFTermFactoryJena) {
+			// No need to convert, just wrap
+			return fromJena(graph);
+		}
+
+		Graph g = factory.createGraph();
+		graph.find(Node.ANY, Node.ANY, Node.ANY).forEachRemaining(t -> {
+			g.add(fromJena(factory, t));
+		});
+		return g;
+	}
+
+	/**
+	 * Create a {@link StreamRDF} that inserts into any RDFCommons
+	 * implementation of 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(fromJena(factory, quad));
+			}
+		};
+	}
+
+	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, quad.getPredicate()));
+		RDFTerm object = fromJena(factory, quad.getObject());
+		return factory.createQuad(graphName, subject, predicate, object);
+	}
+
+	public static void conversionError(String msg) {
+		throw new ConversionException(msg);
+	}
+
+	public static Optional<Lang> rdfSyntaxToLang(RDFSyntax rdfSyntax) {
+		return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType));
+	}
+
+	public static Optional<RDFSyntax> langToRdfSyntax(Lang lang) {
+		return RDFSyntax.byMediaType(lang.getContentType().getContentType());
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/jena/src/main/java/org/apache/commons/rdf/jena/impl/GraphImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/GraphImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/GraphImpl.java
index ab684c5..f1243f1 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/GraphImpl.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/GraphImpl.java
@@ -19,6 +19,7 @@
 package org.apache.commons.rdf.jena.impl;
 
 import java.io.StringWriter ;
+import java.util.UUID;
 import java.util.stream.Stream ;
 
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
@@ -26,8 +27,9 @@ import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.Triple;
-import org.apache.commons.rdf.jena.JenaCommonsRDF;
+import org.apache.commons.rdf.jena.RDFTermFactoryJena;
 import org.apache.commons.rdf.jena.JenaGraph;
+import org.apache.commons.rdf.jena.RDFTermFactoryJena;
 import org.apache.jena.atlas.iterator.Iter ;
 import org.apache.jena.graph.Node ;
 import org.apache.jena.riot.Lang ;
@@ -36,9 +38,11 @@ import org.apache.jena.riot.RDFDataMgr ;
 public class GraphImpl implements Graph, JenaGraph {
 
     private org.apache.jena.graph.Graph graph;
+	private UUID salt;
 
-    /*package*/ GraphImpl(org.apache.jena.graph.Graph graph) {
-        this.graph = graph ;
+    /*package*/ GraphImpl(org.apache.jena.graph.Graph graph, UUID salt) {
+        this.graph = graph ;        
+		this.salt = salt;
     }
     
     @Override
@@ -47,35 +51,35 @@ public class GraphImpl implements Graph, JenaGraph {
     }
 
     @Override
-    public void add(Triple triple) { graph.add(JenaCommonsRDF.toJena(triple)); }
+    public void add(Triple triple) { graph.add(RDFTermFactoryJena.toJena(triple)); }
 
     @Override
     public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { 
-        graph.add(org.apache.jena.graph.Triple.create(JenaCommonsRDF.toJena(subject),
-                                                      JenaCommonsRDF.toJena(predicate),
-                                                      JenaCommonsRDF.toJena(object)));
+        graph.add(org.apache.jena.graph.Triple.create(RDFTermFactoryJena.toJena(subject),
+				        		RDFTermFactoryJena.toJena(predicate),
+				        		RDFTermFactoryJena.toJena(object)));
     }
 
     @Override
     public boolean contains(Triple triple) {
-        return graph.contains(JenaCommonsRDF.toJena(triple)) ; 
+        return graph.contains(RDFTermFactoryJena.toJena(triple)) ; 
     }
 
     @Override
     public boolean contains(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-        return graph.contains(JenaCommonsRDF.toJena(subject),
-                              JenaCommonsRDF.toJena(predicate),
-                              JenaCommonsRDF.toJena(object) );
+        return graph.contains(RDFTermFactoryJena.toJena(subject),
+        		RDFTermFactoryJena.toJena(predicate),
+                              RDFTermFactoryJena.toJena(object) );
     }
 
     @Override
-    public void remove(Triple triple) { graph.delete(JenaCommonsRDF.toJena(triple)); }
+    public void remove(Triple triple) { graph.delete(RDFTermFactoryJena.toJena(triple));
}
 
     @Override
     public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-        graph.delete(org.apache.jena.graph.Triple.create(JenaCommonsRDF.toJena(subject),
-                                                         JenaCommonsRDF.toJena(predicate),
-                                                         JenaCommonsRDF.toJena(object)));
+        graph.delete(org.apache.jena.graph.Triple.create(RDFTermFactoryJena.toJena(subject),
+        		RDFTermFactoryJena.toJena(predicate),
+                                                         RDFTermFactoryJena.toJena(object)));
     }
 
     @Override
@@ -88,19 +92,20 @@ public class GraphImpl implements Graph, JenaGraph {
 
     @Override
     public Stream<? extends Triple> stream() {
-    	return Iter.asStream(graph.find(null, null, null), true).map(JenaCommonsRDF::fromJena);
+    	return Iter.asStream(graph.find(null, null, null), true).map(RDFTermFactoryJena::fromJena);
     }
 
     @Override
     public Stream<? extends Triple> stream(BlankNodeOrIRI s, IRI p, RDFTerm o) {
+    	RDFTermFactoryJena factory = new RDFTermFactoryJena();
         return Iter.asStream(graph.find(toJenaAny(s),toJenaAny(p),toJenaAny(o)), true).
-        		map(JenaCommonsRDF::fromJena);
+        		map(factory::fromJena);
     }
 
     private Node toJenaAny(RDFTerm term) {
         if ( term == null )
             return Node.ANY ;
-        return JenaCommonsRDF.toJena(term) ;
+        return RDFTermFactoryJena.toJena(term) ;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
index 73759c9..b94e3bd 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
@@ -17,80 +17,92 @@
  */
 
 package org.apache.commons.rdf.jena.impl;
-import static org.apache.commons.rdf.jena.JenaCommonsRDF.conversionError;
+
+import static org.apache.commons.rdf.jena.RDFTermFactoryJena.conversionError;
 
 import java.util.UUID;
 
-import org.apache.commons.rdf.api.* ;
-import org.apache.commons.rdf.jena.JenaCommonsRDF;
-import org.apache.jena.graph.Node ;
-import org.apache.jena.graph.NodeFactory ;
-import org.apache.jena.sparql.graph.GraphFactory ;
+import org.apache.commons.rdf.api.BlankNode;
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.Graph;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Literal;
+import org.apache.commons.rdf.api.Quad;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.sparql.graph.GraphFactory;
 
 public class JenaFactory {
-    // basic components to commonsrdf backed by Jena. 
-    public static IRI createIRI(String iriStr) {
-        return (IRI)JenaCommonsRDF.fromJena(NodeFactory.createURI(iriStr));
-    }
-
-    public static Literal createLiteral(String lexStr) {
-        return new LiteralImpl(NodeFactory.createLiteral(lexStr));
-    }
-
-    public static Literal createLiteralDT(String lexStr, String datatypeIRI) {
-        return new LiteralImpl(NodeFactory.createLiteral(lexStr, NodeFactory.getType(datatypeIRI)))
;
-    }
-
-    public static Literal createLiteralLang(String lexStr, String langTag) {
-        return new LiteralImpl(NodeFactory.createLiteral(lexStr, langTag));
-    }
-
-    public static BlankNode createBlankNode() {
-        return new BlankNodeImpl(NodeFactory.createBlankNode(), UUID.randomUUID());
-    }
-    
-    public static BlankNode createBlankNode(UUID salt) {
-        return new BlankNodeImpl(NodeFactory.createBlankNode(), salt);
-    }
-
-    public static BlankNode createBlankNode(String id, UUID salt) {
-        return new BlankNodeImpl(NodeFactory.createBlankNode(id), salt);
-    }
-    
-    public static Graph createGraph() { return new GraphImpl(GraphFactory.createDefaultGraph())
; }
-    
-    public static Triple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{ 
-        return new TripleImpl(subject, predicate, object) ;
-    }
-    
-    public static Triple fromJena(org.apache.jena.graph.Triple triple, UUID salt) {
-        return new TripleImpl(triple, salt) ;
-    }
-
-    public static Graph fromJena(org.apache.jena.graph.Graph graph) {
-        return new GraphImpl(graph) ;
-    }
-
-    public static RDFTerm fromJena(Node node, UUID salt) {
-        if ( node.isURI() )
-            return new IRIImpl(node) ; 
-        if ( node.isLiteral() ) {
-            return new LiteralImpl(node) ; 
-//            String lang = node.getLiteralLanguage() ;
-//            if ( lang != null && lang.isEmpty() )
-//                return createLiteralLang(node.getLiteralLexicalForm(), lang) ;
-//            if ( node.getLiteralDatatype().equals(XSDDatatype.XSDstring) )
-//                return createLiteral(node.getLiteralLexicalForm()) ;
-//            return createLiteralDT(node.getLiteralLexicalForm(), node.getLiteralDatatype().getURI());
-        }
-        if ( node.isBlank() )
-            return new BlankNodeImpl(node, salt) ; 
-        conversionError("Node is not a concrete RDF Term: "+node) ;
-        return null ;
-    }
+	// basic components to commonsrdf backed by Jena.
+	public static IRI createIRI(String iriStr) {
+		return new IRIImpl(iriStr);
+	}
+
+	public static Literal createLiteral(String lexStr) {
+		return new LiteralImpl(NodeFactory.createLiteral(lexStr));
+	}
+
+	public static Literal createLiteralDT(String lexStr, String datatypeIRI) {
+		return new LiteralImpl(NodeFactory.createLiteral(lexStr, NodeFactory.getType(datatypeIRI)));
+	}
+
+	public static Literal createLiteralLang(String lexStr, String langTag) {
+		return new LiteralImpl(NodeFactory.createLiteral(lexStr, langTag));
+	}
+
+	public static BlankNode createBlankNode() {
+		return new BlankNodeImpl(NodeFactory.createBlankNode(), UUID.randomUUID());
+	}
+
+	public static BlankNode createBlankNode(UUID salt) {
+		return new BlankNodeImpl(NodeFactory.createBlankNode(), salt);
+	}
+
+	public static BlankNode createBlankNode(String id, UUID salt) {
+		return new BlankNodeImpl(NodeFactory.createBlankNode(id), salt);
+	}
+
+	public static Graph createGraph() {
+		return createGraph(UUID.randomUUID());
+	}
+
+	public static Triple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{
+		return new TripleImpl(subject, predicate, object);
+	}
+
+	public static Triple fromJena(org.apache.jena.graph.Triple triple, UUID salt) {
+		return new TripleImpl(triple, salt);
+	}
+
+	public static Graph fromJena(org.apache.jena.graph.Graph graph) {
+		return new GraphImpl(graph, UUID.randomUUID());
+	}
+
+	public static RDFTerm fromJena(Node node, UUID salt) {
+		if (node.isURI())
+			return new IRIImpl(node);
+		if (node.isLiteral()) {
+			return new LiteralImpl(node);
+//			String lang = node.getLiteralLanguage();
+//			if (lang != null && lang.isEmpty())
+//				return createLiteralLang(node.getLiteralLexicalForm(), lang);
+//			if (node.getLiteralDatatype().equals(XSDDatatype.XSDstring))
+//				return createLiteral(node.getLiteralLexicalForm());
+//			return createLiteralDT(node.getLiteralLexicalForm(), node.getLiteralDatatype().getURI());
+		}
+		if (node.isBlank())
+			return new BlankNodeImpl(node, salt);
+		conversionError("Node is not a concrete RDF Term: " + node);
+		return null;
+	}
 
 	public static Quad fromJena(org.apache.jena.sparql.core.Quad quad, UUID salt) {
-		 return new QuadImpl(quad, salt) ;
+		return new QuadImpl(quad, salt);
 	}
-}
 
+	public static Graph createGraph(UUID salt) {
+		return new GraphImpl(GraphFactory.createDefaultGraph(), salt);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/jena/src/main/java/org/apache/commons/rdf/jena/impl/QuadImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/QuadImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/QuadImpl.java
index 0ecd743..4a347b3 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/QuadImpl.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/QuadImpl.java
@@ -27,7 +27,7 @@ import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Quad;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.Triple;
-import org.apache.commons.rdf.jena.JenaCommonsRDF;
+import org.apache.commons.rdf.jena.RDFTermFactoryJena;
 import org.apache.commons.rdf.jena.JenaQuad;
 
 public class QuadImpl implements Quad, JenaQuad {
@@ -57,10 +57,10 @@ public class QuadImpl implements Quad, JenaQuad {
     public org.apache.jena.sparql.core.Quad asJenaQuad() {
         if ( quad == null ) {
             quad = org.apache.jena.sparql.core.Quad.create(
-            		JenaCommonsRDF.toJena(graphName.orElse(null)), 
-            		JenaCommonsRDF.toJena(subject), 
-            		JenaCommonsRDF.toJena(predicate), 
-            		JenaCommonsRDF.toJena(object)) ;
+            		RDFTermFactoryJena.toJena(graphName.orElse(null)), 
+            		RDFTermFactoryJena.toJena(subject), 
+            		RDFTermFactoryJena.toJena(predicate), 
+            		RDFTermFactoryJena.toJena(object)) ;
         }
         return quad ;
     }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/35479886/jena/src/main/java/org/apache/commons/rdf/jena/impl/TripleImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/TripleImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/TripleImpl.java
index 922d9c2..a778a8e 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/TripleImpl.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/TripleImpl.java
@@ -22,7 +22,7 @@ import java.util.Objects ;
 import java.util.UUID;
 
 import org.apache.commons.rdf.api.* ;
-import org.apache.commons.rdf.jena.JenaCommonsRDF;
+import org.apache.commons.rdf.jena.RDFTermFactoryJena;
 import org.apache.commons.rdf.jena.JenaTriple;
 
 public class TripleImpl implements Triple, JenaTriple {
@@ -47,7 +47,10 @@ public class TripleImpl implements Triple, JenaTriple {
     @Override
     public org.apache.jena.graph.Triple asJenaTriple() {
         if ( triple == null )
-            triple = org.apache.jena.graph.Triple.create(JenaCommonsRDF.toJena(subject),
JenaCommonsRDF.toJena(predicate), JenaCommonsRDF.toJena(object)) ;
+            triple = org.apache.jena.graph.Triple.create(
+            		RDFTermFactoryJena.toJena(subject), 
+            		RDFTermFactoryJena.toJena(predicate), 
+            		RDFTermFactoryJena.toJena(object)) ;
         return triple ;
     }
 



Mime
View raw message