commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [5/9] incubator-commonsrdf git commit: code tidy
Date Fri, 08 Jul 2016 11:53:49 GMT
code tidy


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

Branch: refs/heads/jena
Commit: 8a7b3ada5d43d997b55123dc55ee0840dbadf016
Parents: 7136d55
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jul 8 11:32:34 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jul 8 11:32:34 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jena/RDFTermFactoryJena.java    | 278 +++++++++++--------
 .../commons/rdf/jena/impl/JenaFactory.java      |   8 +-
 2 files changed, 168 insertions(+), 118 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/8a7b3ada/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 f1ac450..1515a78 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
@@ -84,16 +84,6 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		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);
@@ -110,97 +100,58 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		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}.
+	 * <p>
+	 * If {@link Node#isLiteral()}, then the returned value is a 
+	 * {@link Literal}. If {@link Node#isURI(), the returned value is a 
+	 * IRI. If Node#isBlank(), the returned value is a {@link BlankNode}, 
+	 * which will use a {@link UUID} salt from this {@link RDFTermFactoryJena} instance
+	 * in combination with {@link Node#getBlankNodeId()} 
+	 * for the purpose of its {@link BlankNode#uniqueReference()}.
 	 * 
-	 * @param salt
+	 * @param node The Jena Node to adapt. It's {@link Node#isConcrete()} must be <code>true</code>.
+	 * @throws ConversionException if the node is not concrete.
 	 */
-	public static RDFTerm fromJena(Node node, UUID salt) {
+	public RDFTerm fromJena(Node node) throws ConversionException {
 		return JenaFactory.fromJena(node, salt);
 	}
 
 	/**
 	 * Adapt an existing Jena Node to CommonsRDF {@link RDFTerm}.
+	 * <p>
+	 * If {@link Node#isLiteral()}, then the returned value is a 
+	 * {@link Literal}. If {@link Node#isURI(), the returned value is a 
+	 * IRI. If Node#isBlank(), the returned value is a {@link BlankNode}, 
+	 * which will use the provided {@link UUID} salt 
+	 * in combination with {@link Node#getBlankNodeId()} 
+	 * for the purpose of its {@link BlankNode#uniqueReference()}.
 	 * 
-	 * @param salt
+	 * @param node The Jena Node to adapt. It's {@link Node#isConcrete()} must be <code>true</code>.
+	 * @param salt UUID salt for the purpose of {@link BlankNode#uniqueReference()}
+	 * @throws ConversionException if the node is not concrete.
 	 */
-	public RDFTerm fromJena(Node node) {
+	public static RDFTerm fromJena(Node node, UUID salt) {
 		return JenaFactory.fromJena(node, salt);
 	}
-
+	
 	/**
 	 * Adapt an existing Jena Triple to CommonsRDF {@link Triple}.
+	 * <p>
+	 * If the triple contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use a {@link UUID} salt from this 
+	 * {@link RDFTermFactoryJena} instance
+	 * in combination with {@link Node#getBlankNodeId()} 
+	 * for the purpose of its {@link BlankNode#uniqueReference()}.
 	 * 
 	 * @param triple
 	 *            Jena triple
+	 * @return Adapted triple          
 	 */
 	public Triple fromJena(org.apache.jena.graph.Triple triple) {
 		return JenaFactory.fromJena(triple, salt);
@@ -208,23 +159,22 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 
 	/**
 	 * Adapt an existing Jena Triple to CommonsRDF {@link Triple}.
+	 * <p>
+	 * If the triple contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use the provided a {@link UUID} salt
+	 * in combination with {@link Node#getBlankNodeId()} 
+	 * for the purpose of its {@link BlankNode#uniqueReference()}.
 	 * 
 	 * @param triple
 	 *            Jena triple
 	 * @param salt
 	 *            A {@link UUID} salt for adapting any {@link BlankNode}s
+	 * @return Adapted triple          
 	 */
 	public static Triple fromJena(org.apache.jena.graph.Triple triple, UUID salt) {
 		return JenaFactory.fromJena(triple, salt);
 	}
-
-	public Quad fromJena(org.apache.jena.sparql.core.Quad quad) {
-		return JenaFactory.fromJena(quad, salt);
-	}
-
-	public static Quad fromJena(org.apache.jena.sparql.core.Quad quad, UUID salt) {
-		return JenaFactory.fromJena(quad, salt);
-	}
+	
 
 	/**
 	 * Adapt an existring Jena Graph to CommonsRDF {@link Graph}. This does not
@@ -235,6 +185,40 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		// NOTE: This generates a new UUID salt per graph
 		return JenaFactory.fromJena(graph);
 	}
+	
+	/**
+	 * Adapt an existing Jena Quad to CommonsRDF {@link Quad}.
+	 * <p>
+	 * If the quad contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use a {@link UUID} salt from this 
+	 * {@link RDFTermFactoryJena} instance
+	 * in combination with {@link Node#getBlankNodeId()} 
+	 * for the purpose of its {@link BlankNode#uniqueReference()}.
+	 * 
+	 * @param quad
+	 *            Jena quad
+	 * @return Adapted quad
+	 */	
+	public Quad fromJena(org.apache.jena.sparql.core.Quad quad) {
+		return JenaFactory.fromJena(quad, salt);
+	}
+	
+	/**
+	 * Adapt an existing Jena Quad to CommonsRDF {@link Quad}.
+	 * <p>
+	 * If the quad contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use a {@link UUID} salt from this 
+	 * {@link RDFTermFactoryJena} instance
+	 * in combination with {@link Node#getBlankNodeId()} 
+	 * for the purpose of its {@link BlankNode#uniqueReference()}.
+	 * 
+	 * @param quad
+	 *            Jena quad
+	 * @return Adapted quad
+	 */		
+	public static Quad fromJena(org.apache.jena.sparql.core.Quad quad, UUID salt) {
+		return JenaFactory.fromJena(quad, salt);
+	}
 
 	/**
 	 * Convert from Jena {@link Node} to any RDFCommons implementation
@@ -266,6 +250,24 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 	}
 
 	/**
+	 * 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;
+	}
+
+	/**
 	 * Convert from Jena {@link org.apache.jena.graph.Triple} to any RDFCommons
 	 * implementation
 	 */
@@ -280,22 +282,24 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		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)
{
+	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(graph);
+			return ((RDFTermFactoryJena) factory).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);
+	}
 
-		Graph g = factory.createGraph();
-		graph.find(Node.ANY, Node.ANY, Node.ANY).forEachRemaining(t -> {
-			g.add(fromJena(factory, t));
-		});
-		return g;
+	public static Optional<RDFSyntax> langToRdfSyntax(Lang lang) {
+		return RDFSyntax.byMediaType(lang.getContentType().getContentType());
+	}
+
+	public static Optional<Lang> rdfSyntaxToLang(RDFSyntax rdfSyntax) {
+		return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType));
 	}
 
 	/**
@@ -311,28 +315,76 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		};
 	}
 
-	public static Quad fromJena(RDFTermFactory factory, org.apache.jena.sparql.core.Quad quad)
{
-		if (factory instanceof RDFTermFactoryJena) {
-			// No need to convert, just wrap
-			return ((RDFTermFactoryJena) factory).fromJena(quad);
+	/**
+	 * 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;
+	}
+
+	/**
+	 * 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;
 		}
-		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);
+		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);
+		}
+		throw new ConversionException("Not a concrete RDF Term: " + term);
 	}
 
-	public static void conversionError(String msg) {
-		throw new ConversionException(msg);
+	/**
+	 * 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()));
 	}
 
-	public static Optional<Lang> rdfSyntaxToLang(RDFSyntax rdfSyntax) {
-		return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType));
+	// 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();
 	}
 
-	public static Optional<RDFSyntax> langToRdfSyntax(Lang lang) {
-		return RDFSyntax.byMediaType(lang.getContentType().getContentType());
+	private static void validateLang(String languageTag) {
+		if (languageTag.contains(" "))
+			throw new IllegalArgumentException("Invalid language tag: " + languageTag);
 	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/8a7b3ada/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 c67bb7c..2b4eab9 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
@@ -18,8 +18,6 @@
 
 package org.apache.commons.rdf.jena.impl;
 
-import static org.apache.commons.rdf.jena.RDFTermFactoryJena.conversionError;
-
 import java.util.UUID;
 
 import org.apache.commons.rdf.api.BlankNode;
@@ -30,6 +28,7 @@ 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.commons.rdf.jena.ConversionException;
 import org.apache.jena.graph.Node;
 import org.apache.jena.graph.NodeFactory;
 import org.apache.jena.sparql.graph.GraphFactory;
@@ -76,7 +75,7 @@ public class JenaFactory {
 		return new TripleImpl(subject, predicate, object);
 	}
 
-	public static RDFTerm fromJena(Node node, UUID salt) {
+	public static RDFTerm fromJena(Node node, UUID salt) throws ConversionException {
 		if (node.isURI())
 			return new IRIImpl(node);
 		if (node.isLiteral()) {
@@ -90,8 +89,7 @@ public class JenaFactory {
 		}
 		if (node.isBlank())
 			return new BlankNodeImpl(node, salt);
-		conversionError("Node is not a concrete RDF Term: " + node);
-		return null;
+		throw new ConversionException("Node is not a concrete RDF Term: " + node);
 	}
 
 	public static Graph fromJena(org.apache.jena.graph.Graph graph) {


Mime
View raw message