JenaFactory -> JenaRDF
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/384a8337 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/384a8337 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/384a8337 Branch: refs/heads/master Commit: 384a83377e8f643d61ffc91d202e84053450fa55 Parents: ff94b9d Author: Stian Soiland-Reyes <st...@apache.org> Authored: Sun Oct 23 00:44:43 2016 +0100 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Sun Oct 23 00:44:43 2016 +0100 ---------------------------------------------------------------------- .../apache/commons/rdf/jena/JenaFactory.java | 727 ------------------- .../org/apache/commons/rdf/jena/JenaRDF.java | 727 +++++++++++++++++++ .../rdf/jena/experimental/JenaRDFParser.java | 20 +- .../commons/rdf/jena/impl/AbstractQuadLike.java | 6 +- .../rdf/jena/impl/InternalJenaFactory.java | 4 +- .../commons/rdf/jena/impl/JenaDatasetImpl.java | 14 +- .../commons/rdf/jena/impl/JenaGraphImpl.java | 12 +- .../org.apache.commons.rdf.api.RDFFactory | 2 +- .../commons/rdf/jena/JenaServiceLoaderTest.java | 4 +- .../apache/commons/rdf/jena/TestGraphJena.java | 2 +- .../jena/TestJenaGraphToCommonsRDFGraph.java | 4 +- .../commons/rdf/jena/TestRDFParserBuilder.java | 2 +- .../rdf/jena/TestRDFTermFactoryJena.java | 2 +- 13 files changed, 763 insertions(+), 763 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/JenaFactory.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaFactory.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaFactory.java deleted file mode 100644 index 4832e44..0000000 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaFactory.java +++ /dev/null @@ -1,727 +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.Dataset; -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.QuadLike; -import org.apache.commons.rdf.api.RDFSyntax; -import org.apache.commons.rdf.api.RDFTerm; -import org.apache.commons.rdf.api.RDF; -import org.apache.commons.rdf.api.Triple; -import org.apache.commons.rdf.api.TripleLike; -import org.apache.commons.rdf.jena.impl.InternalJenaFactory; -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.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.core.DatasetGraph; -import org.apache.jena.sparql.graph.GraphFactory; - -/** - * RDF 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)}. - * <p> - * For the purpose of {@link BlankNode} identity, this factory will use an internal - * {@link UUID} as a salt. See {@link BlankNode#uniqueReference()} for details. - * - * @see RDF - */ -public final class JenaFactory implements RDF { - - private static InternalJenaFactory internalJenaFactory = new InternalJenaFactory(){}; - - private final UUID salt; - - /** - * Create a JenaFactory. - * <p> - * This constructor will use a randomly generated {@link UUID} as a salt - * for the purposes of {@link BlankNode} identity, see {@link #getSalt()}. - */ - public JenaFactory() { - this.salt = UUID.randomUUID(); - } - - /** - * Create a JenaFactory. - * <p> - * This constructor will use the specified {@link UUID} as a salt for - * the purposes of {@link BlankNode} identity, and should only be used in - * cases where predictable and consistent - * {@link BlankNode#uniqueReference()} are important. - * - * @param salt - * {@link UUID} to use as salt for {@link BlankNode} equality - */ - public JenaFactory(UUID salt) { - this.salt = salt; - } - - @Override - public JenaBlankNode createBlankNode() { - return internalJenaFactory.createBlankNode(getSalt()); - } - - @Override - public JenaBlankNode createBlankNode(String name) { - return internalJenaFactory.createBlankNode(name, getSalt()); - } - - @Override - public JenaDataset createDataset() { - return internalJenaFactory.createDataset(getSalt()); - } - - @Override - public JenaGraph createGraph() { - return internalJenaFactory.createGraph(getSalt()); - } - - @Override - public JenaIRI createIRI(String iri) { - validateIRI(iri); - return internalJenaFactory.createIRI(iri); - } - - @Override - public JenaLiteral createLiteral(String lexicalForm) { - return internalJenaFactory.createLiteral(lexicalForm); - } - - @Override - public JenaLiteral createLiteral(String lexicalForm, IRI dataType) { - return internalJenaFactory.createLiteralDT(lexicalForm, dataType.getIRIString()); - } - - @Override - public JenaLiteral createLiteral(String lexicalForm, String languageTag) { - validateLang(languageTag); - return internalJenaFactory.createLiteralLang(lexicalForm, languageTag); - } - - @Override - public JenaTriple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { - return internalJenaFactory.createTriple(subject, predicate, object); - } - - @Override - public JenaQuad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm object) - throws IllegalArgumentException, UnsupportedOperationException { - return internalJenaFactory.createQuad(subject, predicate, object, graphName); - } - - /** - * Create a generalized Jena triple. - * <p> - * The <em>generalized triple</em> supports any {@link RDFTerm} as its - * {@link TripleLike#getSubject()} {@link TripleLike#getPredicate()} or - * {@link TripleLike#getObject()}. - * - * @see #createTriple(BlankNodeOrIRI, IRI, RDFTerm) - * @see #createGeneralizedQuad(RDFTerm, RDFTerm, RDFTerm, RDFTerm) - * - * @param subject - * The subject of the statement - * @param predicate - * The predicate of the statement - * @param object - * The object of the statement - * @return Generalized {@link TripleLike}. Note that the generalized triple - * does <strong>not</strong> implement {@link Triple#equals(Object)} - * or {@link Triple#hashCode()}. - */ - public JenaGeneralizedTripleLike createGeneralizedTriple( - RDFTerm subject, RDFTerm predicate, RDFTerm object) { - return internalJenaFactory.createGeneralizedTriple(subject, predicate, object); - } - - /** - * Create a generalized Jena quad. - * <p> - * The <em>generalized quad</em> supports any {@link RDFTerm} as its - * {@link QuadLike#getSubject()} {@link QuadLike#getPredicate()}, - * {@link QuadLike#getObject()} or {@link QuadLike#getObject()}. - * - * @see #createQuad(BlankNodeOrIRI, BlankNodeOrIRI, IRI, RDFTerm) - * @see #createGeneralizedTriple(RDFTerm, RDFTerm, RDFTerm) - * - * @param subject - * The subject of the statement - * @param predicate - * The predicate of the statement - * @param object - * The object of the statement - * @param graphName - * The graph name of the statement - * @return Generalized {@link QuadLike}. Note that the generalized quad does - * <strong>not</strong> implement {@link Quad#equals(Object)} or - * {@link Quad#hashCode()}. - */ - public JenaGeneralizedQuadLike createGeneralizedQuad( - RDFTerm subject, RDFTerm predicate, RDFTerm object, RDFTerm graphName) { - return internalJenaFactory.createGeneralizedQuad(subject, predicate, object, graphName); - } - /** - * 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 {$@link Node#isBlank()}, - * the returned value is a {@link BlankNode}, which will use a {@link UUID} - * salt from this {@link JenaFactory} instance in combination with - * {@link Node#getBlankNodeId()} for the purpose of its - * {@link BlankNode#uniqueReference()}. - * - * @see #fromJena(RDF, Node) - * - * @param node - * The Jena Node to adapt. It's {@link Node#isConcrete()} must be - * <code>true</code>. - * @return Adapted {@link JenaRDFTerm} - * @throws ConversionException If the {@link Node} can't be represented as an {@link RDFTerm}, e.g. - * if the node is not concrete or represents a variable in Jena. - */ - public JenaRDFTerm fromJena(Node node) throws ConversionException { - return internalJenaFactory.fromJena(node, getSalt()); - } - - /** - * Convert from Jena {@link Node} to any Commons RDF implementation. - * <p> - * Note that if the {@link Node#isBlank()}, then the factory's - * {@link RDF#createBlankNode(String)} will be used, meaning - * that care should be taken if reusing an {@link RDF} instance - * for multiple conversion sessions. - * - * @see #fromJena(Node) - * - * @param factory {@link RDF} to use for creating {@link RDFTerm}. - * @param node - * The Jena Node to adapt. It's {@link Node#isConcrete()} must be - * <code>true</code>. - * @return Adapted {@link RDFTerm} - * @throws ConversionException If the {@link Node} can't be represented as an {@link RDFTerm}, e.g. - * if the node is not concrete or represents a variable in Jena. - */ - public static RDFTerm fromJena(RDF factory, Node node) { - if (node == null) { - return null; - } - if (factory instanceof JenaFactory) { - // No need to convert, just wrap - return ((JenaFactory) factory).fromJena(node); - } - 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()) - // The factory - return factory.createBlankNode(node.getBlankNodeLabel()); - throw new ConversionException("Node is not a concrete RDF Term: " + node); - } - - /** - * 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 JenaFactory} instance in combination with - * {@link Node#getBlankNodeId()} for the purpose of its - * {@link BlankNode#uniqueReference()}. - * - * @see #fromJena(RDF, org.apache.jena.graph.Triple) - * - * @param triple - * Jena {@link org.apache.jena.graph.Triple} to adapt - * @return Adapted {@link JenaTriple} - * @throws ConversionException - * if any of the triple's nodes are not concrete or the triple - * is a generalized triple - */ - public JenaTriple fromJena(org.apache.jena.graph.Triple triple) throws ConversionException { - return internalJenaFactory.fromJena(triple, getSalt()); - } - - /** - * Adapt a generalized Jena {@link org.apache.jena.graph.Triple} to a CommonsRDF {@link TripleLike}. - * <p> - * The generalized triple supports any {@link RDFTerm} as its {@link TripleLike#getSubject()} - * {@link TripleLike#getPredicate()} or {@link TripleLike#getObject()}. - * <p> - * If the Jena triple contains any {@link Node#isBlank()}, then any corresponding - * {@link BlankNode} will use a {@link UUID} salt from this - * {@link JenaFactory} instance in combination with - * {@link Node#getBlankNodeId()} for the purpose of its - * {@link BlankNode#uniqueReference()}. - * - * @see #fromJena(RDF, org.apache.jena.graph.Triple) - * - * @param triple - * Jena triple - * @return Adapted {@link TripleLike}. Note that the generalized triple does - * <strong>not</strong> implement {@link Triple#equals(Object)} or - * {@link Triple#hashCode()}. - * @throws ConversionException - * if any of the triple's nodes are not concrete - */ - public JenaTripleLike fromJenaGeneralized(org.apache.jena.graph.Triple triple) throws ConversionException { - return internalJenaFactory.fromJenaGeneralized(triple, getSalt()); - } - - /** - * Adapt a generalized Jena {@link org.apache.jena.sparql.core.Quad} to a CommonsRDF {@link QuadLike}. - * <p> - * The generalized quad supports any {@link RDFTerm} as its - * {@link QuadLike#getGraphName()}, - * {@link QuadLike#getSubject()} - * {@link QuadLike#getPredicate()} or - * {@link QuadLike#getObject()}. - * <p> - * If the Jena quad contains any {@link Node#isBlank()}, then any corresponding - * {@link BlankNode} will use a {@link UUID} salt from this - * {@link JenaFactory} instance in combination with - * {@link Node#getBlankNodeId()} for the purpose of its - * {@link BlankNode#uniqueReference()}. - * - * @see #fromJena(org.apache.jena.sparql.core.Quad) - * @see #fromJenaGeneralized(org.apache.jena.graph.Triple) - * - * @param quad - * Jena quad - * @return Adapted {@link QuadLike}. Note that the generalized quad does - * <strong>not</strong> implement {@link Quad#equals(Object)} or - * {@link Quad#hashCode()}. - * @throws ConversionException - * if any of the quad nodes are not concrete - */ - public JenaQuadLike<RDFTerm> fromJenaGeneralized(org.apache.jena.sparql.core.Quad quad) throws ConversionException { - return internalJenaFactory.fromJenaGeneralized(quad, getSalt()); - } - - /** - * Convert from Jena {@link org.apache.jena.graph.Triple} to a Commons RDF - * {@link Triple}. - * <p> - * Note that if any of the triple's nodes {@link Node#isBlank()}, then the factory's - * {@link RDF#createBlankNode(String)} will be used, meaning - * that care should be taken if reusing an {@link RDF} instance - * for multiple conversion sessions. - * - * @see #fromJena(org.apache.jena.graph.Triple) - * - * @param factory {@link RDF} to use for creating the {@link Triple} and its - * {@link RDFTerm}s. - * @param triple - * Jena triple - * @return Converted triple - * @throws ConversionException - * if any of the triple's nodes are not concrete or the triple - * is a generalized triple - */ - public static Triple fromJena(RDF factory, org.apache.jena.graph.Triple triple) - throws ConversionException{ - if (factory instanceof JenaFactory) { - // No need to convert, just wrap - return ((JenaFactory) factory).fromJena(triple); - } - final BlankNodeOrIRI subject; - final IRI predicate; - try { - subject = (BlankNodeOrIRI) fromJena(factory, triple.getSubject()); - predicate = (IRI) fromJena(factory, triple.getPredicate()); - } catch (ClassCastException ex) { - throw new ConversionException("Can't convert generalized triple: " + triple, ex); - } - RDFTerm object = fromJena(factory, triple.getObject()); - return factory.createTriple(subject, predicate, object); - } - - /** - * Adapt an existing Jena {@link org.apache.jena.sparql.core.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 JenaFactory} instance - * in combination with {@link Node#getBlankNodeId()} - * for the purpose of its {@link BlankNode#uniqueReference()}. - * - * @param quad - * Jena quad - * @return Adapted quad - */ - public JenaQuad fromJena(org.apache.jena.sparql.core.Quad quad) { - return internalJenaFactory.fromJena(quad, getSalt()); - } - - /** - * Adapt an existing Jena {@link org.apache.jena.graph.Graph} to CommonsRDF - * {@link Graph}. - * <p> - * This does not take a copy, changes to the CommonsRDF Graph are reflected - * in the jena graph, which is accessible from - * {@link JenaGraph#asJenaGraph()}. - * <p> - * If the graph contains any {@link Node#isBlank()}, then any corresponding - * {@link BlankNode} will use a {@link UUID} salt from this - * {@link JenaFactory} instance in combination with - * {@link Node#getBlankNodeId()} for the purpose of its - * {@link BlankNode#uniqueReference()}. - * - * @param graph - * Jena {@link org.apache.jena.graph.Graph} to adapt - * @return Adapted {@link JenaGraph} - */ - public JenaGraph fromJena(org.apache.jena.graph.Graph graph) { - return internalJenaFactory.fromJena(graph, getSalt()); - } - - /** - * Adapt an existing Jena {@link org.apache.jena.rdf.model.Model} to CommonsRDF {@link Graph}. - * <p> - * This does not ake a copy, changes to the CommonsRDF Graph are reflected in the jena - * graph, which is accessible from {@link JenaGraph#asJenaGraph()}. - * <p> - * If the graph contains any {@link Node#isBlank()}, then any corresponding - * {@link BlankNode} will use a {@link UUID} salt from this - * {@link JenaFactory} instance - * in combination with {@link Node#getBlankNodeId()} - * for the purpose of its {@link BlankNode#uniqueReference()}. - * @param model - * Jena {@link org.apache.jena.rdf.model.Model} to adapt - * @return Adapted {@link JenaGraph} - */ - public JenaGraph fromJena(org.apache.jena.rdf.model.Model model) { - return internalJenaFactory.fromJena(model, getSalt()); - } - - /** - * Adapt an existing Jena {@link DatasetGraph} to CommonsRDF {@link Dataset}. - * <p> - * This does not - * take a copy, changes to the CommonsRDF Dataset are reflected in the jena - * dataset graph, which is accessible from {@link JenaDataset#asJenaDatasetGraph()}. - * <p> - * If the dataset contains any {@link Node#isBlank()}, then any corresponding - * {@link BlankNode} will use a {@link UUID} salt from this - * {@link JenaFactory} instance - * in combination with {@link Node#getBlankNodeId()} - * for the purpose of its {@link BlankNode#uniqueReference()}. - * - * @param datasetGraph Jena {@link DatasetGraph} to adapt - * @return Adapted {@link JenaDataset} - */ - public JenaDataset fromJena(DatasetGraph datasetGraph) { - return internalJenaFactory.fromJena(datasetGraph, getSalt()); - } - - /** - * Adapt an existing Jena {@link org.apache.jena.query.Dataset} to CommonsRDF {@link Dataset}. - * <p> - * This does not - * take a copy, changes to the CommonsRDF Dataset are reflected in the jena - * dataset graph, which is accessible from {@link JenaDataset#asJenaDatasetGraph()}. - * <p> - * If the dataset contains any {@link Node#isBlank()}, then any corresponding - * {@link BlankNode} will use a {@link UUID} salt from this - * {@link JenaFactory} instance - * in combination with {@link Node#getBlankNodeId()} - * for the purpose of its {@link BlankNode#uniqueReference()}. - * - * @param datasetGraph Jena {@link org.apache.jena.query.Dataset} to adapt - * @return Adapted {@link JenaDataset} - */ - public JenaDataset fromJena(org.apache.jena.query.Dataset datasetGraph) { - return internalJenaFactory.fromJena(datasetGraph.asDatasetGraph(), getSalt()); - } - - /** - * Convert from Jena {@link org.apache.jena.sparql.core.Quad} to a Commons - * RDF {@link Quad}. - * <p> - * Note that if any of the quad's nodes {@link Node#isBlank()}, then the - * factory's {@link RDF#createBlankNode(String)} will be used, - * meaning that care should be taken if reusing an {@link RDF} - * instance for multiple conversion sessions. - * - * @see #fromJena(org.apache.jena.sparql.core.Quad) - * @see #fromJenaGeneralized(org.apache.jena.sparql.core.Quad) - * - * @param factory - * {@link RDF} to use for creating the {@link Triple} - * and its {@link RDFTerm}s. - * @param quad - * Jena {@link org.apache.jena.sparql.core.Quad} to adapt - * @return Converted {@link Quad} - * @throws ConversionException - * if any of the quad's nodes are not concrete or the quad - * is a generalized quad - */ - public static Quad fromJena(RDF factory, org.apache.jena.sparql.core.Quad quad) { - if (factory instanceof JenaFactory) { - // No need to convert, just wrap - return ((JenaFactory) 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); - } - - /** - * Return {@link RDFSyntax} corresponding to a Jena {@link Lang}. - * - * @param lang {@link Lang} to convert - * @return Matched {@link RDFSyntax}, otherwise {@link Optional#empty()} - */ - public Optional<RDFSyntax> langToRdfSyntax(Lang lang) { - return RDFSyntax.byMediaType(lang.getContentType().getContentType()); - } - - /** - * Return Jena {@link Lang} corresponding to a {@link RDFSyntax}. - * - * @param rdfSyntax {@link RDFSyntax} to convert - * @return Matched {@link Lang}, otherwise {@link Optional#empty()} - */ - public Optional<Lang> rdfSyntaxToLang(RDFSyntax rdfSyntax) { - return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType)); - } - - /** - * Create a {@link StreamRDF} instance that inserts the converted - * {@link Quad}s. into a the provided {@link Consumer}. - * <p> - * The returned {@link StreamRDF} can be used for instance with Jena's - * {@link RDFDataMgr#parse(StreamRDF, String)}. - * - * @param factory - * {@link RDF} to use for creating {@link RDFTerm}s - * and {@link Quad}s. - * @param consumer - * A {@link Consumer} of {@link Quad}s - * @return A {@link StreamRDF} that will stream converted quads to the - * consumer - */ - public static StreamRDF streamJenaToCommonsRDF(RDF factory, Consumer<Quad> consumer) { - return new StreamRDFBase() { - @Override - public void quad(org.apache.jena.sparql.core.Quad quad) { - consumer.accept(fromJena(factory, quad)); - } - }; - } - - /** - * Create a {@link StreamRDF} instance that inserts generalized - * {@link TripleLike}s. into a the provided {@link Consumer}. - * <p> - * A generalized triple allows any {@link RDFTerm} for - * {@link TripleLike#getSubject()}, {@link TripleLike#getPredicate()} and - * {@link TripleLike#getObject()}. - * <p> - * The returned {@link StreamRDF} can be used for instance with Jena's - * {@link RDFDataMgr#parse(StreamRDF, String)}. - * - * @param generalizedConsumer - * A {@link Consumer} of generalized {@link TripleLike}s - * @return A {@link StreamRDF} that will stream generalized triples to the - * consumer - */ - public StreamRDF streamJenaToGeneralizedTriple(Consumer<TripleLike> generalizedConsumer) { - return new StreamRDFBase() { - @Override - public void triple(org.apache.jena.graph.Triple triple) { - generalizedConsumer.accept(fromJenaGeneralized(triple)); - } - }; - } - - /** - * Create a {@link StreamRDF} instance that inserts generalized - * {@link QuadLike}s. into a the provided {@link Consumer}. - * <p> - * A generalized quad allows any {@link RDFTerm} for - * {@link QuadLike#getSubject()}, {@link TripleLike#getPredicate()}, - * {@link QuadLike#getObject()} and {@link QuadLike#getGraphName()} . - * <p> - * The returned {@link StreamRDF} can be used for instance with Jena's - * {@link RDFDataMgr#parse(StreamRDF, String)}. - * - * @param generalizedConsumer - * A {@link Consumer} of generalized {@link QuadLike}s - * @return A {@link StreamRDF} that will stream generalized quads to the - * consumer - */ - public StreamRDF streamJenaToGeneralizedQuad(Consumer<QuadLike<RDFTerm>> generalizedConsumer) { - return new StreamRDFBase() { - @Override - public void quad(org.apache.jena.sparql.core.Quad quad) { - generalizedConsumer.accept(fromJenaGeneralized(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. - * - * @param graph Commons RDF {@link Graph} to convert - * @return Converted Jena {@link org.apache.jena.graph.Graph} - */ - public 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. - * - * @param term Commons RDF {@link RDFTerm} to convert - * @return Converted Jena {@link Node} - */ - public Node toJena(RDFTerm term) { - if (term == null) { - return null; - } - if (term instanceof JenaRDFTerm) - // TODO: What if it's a JenaBlankNodeImpl with - // a different salt? Do we need to rewrite the - // jena blanknode identifier? - return ((JenaRDFTerm) 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); - } - - /** - * Convert a CommonsRDF {@link Triple} to a Jena - * {@link org.apache.jena.graph.Triple}. - * <p> - * If the triple was from Jena originally, return that original object, else - * create a copy using Jena objects. - * - * @param triple Commons RDF {@link Triple} to convert - * @return Converted Jena {@link org.apache.jena.graph.Triple} - */ - public org.apache.jena.graph.Triple toJena(Triple triple) { - if (triple instanceof JenaTriple) - return ((JenaTriple) triple).asJenaTriple(); - return org.apache.jena.graph.Triple.create( - toJena(triple.getSubject()), - toJena(triple.getPredicate()), - toJena(triple.getObject())); - } - - - /** - * Convert a CommonsRDF {@link Quad} to a Jena - * {@link org.apache.jena.sparql.core.Quad}. - * <p> - * If the quad was from Jena originally, return that original object, - * otherwise create a copy using Jena objects. - * - * @param quad Commons RDF {@link Quad} to convert - * @return Converted Jena {@link org.apache.jena.sparql.core.Quad} - */ - public org.apache.jena.sparql.core.Quad toJena(Quad quad) { - if (quad instanceof JenaQuad) { - return ((JenaQuad) quad).asJenaQuad(); - } - return org.apache.jena.sparql.core.Quad.create( - toJena(quad.getGraphName().orElse(null)), - toJena(quad.getSubject()), - toJena(quad.getPredicate()), - toJena(quad.getObject())); - } - - // Some simple validations - full IRI parsing is not cheap. - private void validateIRI(String iri) { - if (iri.contains(" ")) - throw new IllegalArgumentException(); - if (iri.contains("<")) - throw new IllegalArgumentException(); - if (iri.contains(">")) - throw new IllegalArgumentException(); - } - - private static void validateLang(String languageTag) { - if (languageTag.contains(" ")) - throw new IllegalArgumentException("Invalid language tag: " + languageTag); - } - - /** - * Return the {@link UUID} salt used by this factory. - * <p> - * The salt is used for the purposes of {@link BlankNode} identity, see - * {@link BlankNode#uniqueReference()} for details. - * <p> - * This salt can be used with the constructor - * {@link JenaFactory#JenaFactory(UUID)} - * if consistent or reproducible {@link BlankNode}s are desirable. - * - * @return The {@link UUID} used as salt - */ - public UUID getSalt() { - return salt; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java new file mode 100644 index 0000000..e3c2a86 --- /dev/null +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java @@ -0,0 +1,727 @@ +/** + * 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.Dataset; +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.QuadLike; +import org.apache.commons.rdf.api.RDFSyntax; +import org.apache.commons.rdf.api.RDFTerm; +import org.apache.commons.rdf.api.RDF; +import org.apache.commons.rdf.api.Triple; +import org.apache.commons.rdf.api.TripleLike; +import org.apache.commons.rdf.jena.impl.InternalJenaFactory; +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.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.core.DatasetGraph; +import org.apache.jena.sparql.graph.GraphFactory; + +/** + * RDF 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)}. + * <p> + * For the purpose of {@link BlankNode} identity, this factory will use an internal + * {@link UUID} as a salt. See {@link BlankNode#uniqueReference()} for details. + * + * @see RDF + */ +public final class JenaRDF implements RDF { + + private static InternalJenaFactory internalJenaFactory = new InternalJenaFactory(){}; + + private final UUID salt; + + /** + * Create a JenaRDF. + * <p> + * This constructor will use a randomly generated {@link UUID} as a salt + * for the purposes of {@link BlankNode} identity, see {@link #getSalt()}. + */ + public JenaRDF() { + this.salt = UUID.randomUUID(); + } + + /** + * Create a JenaRDF. + * <p> + * This constructor will use the specified {@link UUID} as a salt for + * the purposes of {@link BlankNode} identity, and should only be used in + * cases where predictable and consistent + * {@link BlankNode#uniqueReference()} are important. + * + * @param salt + * {@link UUID} to use as salt for {@link BlankNode} equality + */ + public JenaRDF(UUID salt) { + this.salt = salt; + } + + @Override + public JenaBlankNode createBlankNode() { + return internalJenaFactory.createBlankNode(getSalt()); + } + + @Override + public JenaBlankNode createBlankNode(String name) { + return internalJenaFactory.createBlankNode(name, getSalt()); + } + + @Override + public JenaDataset createDataset() { + return internalJenaFactory.createDataset(getSalt()); + } + + @Override + public JenaGraph createGraph() { + return internalJenaFactory.createGraph(getSalt()); + } + + @Override + public JenaIRI createIRI(String iri) { + validateIRI(iri); + return internalJenaFactory.createIRI(iri); + } + + @Override + public JenaLiteral createLiteral(String lexicalForm) { + return internalJenaFactory.createLiteral(lexicalForm); + } + + @Override + public JenaLiteral createLiteral(String lexicalForm, IRI dataType) { + return internalJenaFactory.createLiteralDT(lexicalForm, dataType.getIRIString()); + } + + @Override + public JenaLiteral createLiteral(String lexicalForm, String languageTag) { + validateLang(languageTag); + return internalJenaFactory.createLiteralLang(lexicalForm, languageTag); + } + + @Override + public JenaTriple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + return internalJenaFactory.createTriple(subject, predicate, object); + } + + @Override + public JenaQuad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm object) + throws IllegalArgumentException, UnsupportedOperationException { + return internalJenaFactory.createQuad(subject, predicate, object, graphName); + } + + /** + * Create a generalized Jena triple. + * <p> + * The <em>generalized triple</em> supports any {@link RDFTerm} as its + * {@link TripleLike#getSubject()} {@link TripleLike#getPredicate()} or + * {@link TripleLike#getObject()}. + * + * @see #createTriple(BlankNodeOrIRI, IRI, RDFTerm) + * @see #createGeneralizedQuad(RDFTerm, RDFTerm, RDFTerm, RDFTerm) + * + * @param subject + * The subject of the statement + * @param predicate + * The predicate of the statement + * @param object + * The object of the statement + * @return Generalized {@link TripleLike}. Note that the generalized triple + * does <strong>not</strong> implement {@link Triple#equals(Object)} + * or {@link Triple#hashCode()}. + */ + public JenaGeneralizedTripleLike createGeneralizedTriple( + RDFTerm subject, RDFTerm predicate, RDFTerm object) { + return internalJenaFactory.createGeneralizedTriple(subject, predicate, object); + } + + /** + * Create a generalized Jena quad. + * <p> + * The <em>generalized quad</em> supports any {@link RDFTerm} as its + * {@link QuadLike#getSubject()} {@link QuadLike#getPredicate()}, + * {@link QuadLike#getObject()} or {@link QuadLike#getObject()}. + * + * @see #createQuad(BlankNodeOrIRI, BlankNodeOrIRI, IRI, RDFTerm) + * @see #createGeneralizedTriple(RDFTerm, RDFTerm, RDFTerm) + * + * @param subject + * The subject of the statement + * @param predicate + * The predicate of the statement + * @param object + * The object of the statement + * @param graphName + * The graph name of the statement + * @return Generalized {@link QuadLike}. Note that the generalized quad does + * <strong>not</strong> implement {@link Quad#equals(Object)} or + * {@link Quad#hashCode()}. + */ + public JenaGeneralizedQuadLike createGeneralizedQuad( + RDFTerm subject, RDFTerm predicate, RDFTerm object, RDFTerm graphName) { + return internalJenaFactory.createGeneralizedQuad(subject, predicate, object, graphName); + } + /** + * 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 {$@link Node#isBlank()}, + * the returned value is a {@link BlankNode}, which will use a {@link UUID} + * salt from this {@link JenaRDF} instance in combination with + * {@link Node#getBlankNodeId()} for the purpose of its + * {@link BlankNode#uniqueReference()}. + * + * @see #fromJena(RDF, Node) + * + * @param node + * The Jena Node to adapt. It's {@link Node#isConcrete()} must be + * <code>true</code>. + * @return Adapted {@link JenaRDFTerm} + * @throws ConversionException If the {@link Node} can't be represented as an {@link RDFTerm}, e.g. + * if the node is not concrete or represents a variable in Jena. + */ + public JenaRDFTerm fromJena(Node node) throws ConversionException { + return internalJenaFactory.fromJena(node, getSalt()); + } + + /** + * Convert from Jena {@link Node} to any Commons RDF implementation. + * <p> + * Note that if the {@link Node#isBlank()}, then the factory's + * {@link RDF#createBlankNode(String)} will be used, meaning + * that care should be taken if reusing an {@link RDF} instance + * for multiple conversion sessions. + * + * @see #fromJena(Node) + * + * @param factory {@link RDF} to use for creating {@link RDFTerm}. + * @param node + * The Jena Node to adapt. It's {@link Node#isConcrete()} must be + * <code>true</code>. + * @return Adapted {@link RDFTerm} + * @throws ConversionException If the {@link Node} can't be represented as an {@link RDFTerm}, e.g. + * if the node is not concrete or represents a variable in Jena. + */ + public static RDFTerm fromJena(RDF factory, Node node) { + if (node == null) { + return null; + } + if (factory instanceof JenaRDF) { + // No need to convert, just wrap + return ((JenaRDF) factory).fromJena(node); + } + 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()) + // The factory + return factory.createBlankNode(node.getBlankNodeLabel()); + throw new ConversionException("Node is not a concrete RDF Term: " + node); + } + + /** + * 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 JenaRDF} instance in combination with + * {@link Node#getBlankNodeId()} for the purpose of its + * {@link BlankNode#uniqueReference()}. + * + * @see #fromJena(RDF, org.apache.jena.graph.Triple) + * + * @param triple + * Jena {@link org.apache.jena.graph.Triple} to adapt + * @return Adapted {@link JenaTriple} + * @throws ConversionException + * if any of the triple's nodes are not concrete or the triple + * is a generalized triple + */ + public JenaTriple fromJena(org.apache.jena.graph.Triple triple) throws ConversionException { + return internalJenaFactory.fromJena(triple, getSalt()); + } + + /** + * Adapt a generalized Jena {@link org.apache.jena.graph.Triple} to a CommonsRDF {@link TripleLike}. + * <p> + * The generalized triple supports any {@link RDFTerm} as its {@link TripleLike#getSubject()} + * {@link TripleLike#getPredicate()} or {@link TripleLike#getObject()}. + * <p> + * If the Jena triple contains any {@link Node#isBlank()}, then any corresponding + * {@link BlankNode} will use a {@link UUID} salt from this + * {@link JenaRDF} instance in combination with + * {@link Node#getBlankNodeId()} for the purpose of its + * {@link BlankNode#uniqueReference()}. + * + * @see #fromJena(RDF, org.apache.jena.graph.Triple) + * + * @param triple + * Jena triple + * @return Adapted {@link TripleLike}. Note that the generalized triple does + * <strong>not</strong> implement {@link Triple#equals(Object)} or + * {@link Triple#hashCode()}. + * @throws ConversionException + * if any of the triple's nodes are not concrete + */ + public JenaTripleLike fromJenaGeneralized(org.apache.jena.graph.Triple triple) throws ConversionException { + return internalJenaFactory.fromJenaGeneralized(triple, getSalt()); + } + + /** + * Adapt a generalized Jena {@link org.apache.jena.sparql.core.Quad} to a CommonsRDF {@link QuadLike}. + * <p> + * The generalized quad supports any {@link RDFTerm} as its + * {@link QuadLike#getGraphName()}, + * {@link QuadLike#getSubject()} + * {@link QuadLike#getPredicate()} or + * {@link QuadLike#getObject()}. + * <p> + * If the Jena quad contains any {@link Node#isBlank()}, then any corresponding + * {@link BlankNode} will use a {@link UUID} salt from this + * {@link JenaRDF} instance in combination with + * {@link Node#getBlankNodeId()} for the purpose of its + * {@link BlankNode#uniqueReference()}. + * + * @see #fromJena(org.apache.jena.sparql.core.Quad) + * @see #fromJenaGeneralized(org.apache.jena.graph.Triple) + * + * @param quad + * Jena quad + * @return Adapted {@link QuadLike}. Note that the generalized quad does + * <strong>not</strong> implement {@link Quad#equals(Object)} or + * {@link Quad#hashCode()}. + * @throws ConversionException + * if any of the quad nodes are not concrete + */ + public JenaQuadLike<RDFTerm> fromJenaGeneralized(org.apache.jena.sparql.core.Quad quad) throws ConversionException { + return internalJenaFactory.fromJenaGeneralized(quad, getSalt()); + } + + /** + * Convert from Jena {@link org.apache.jena.graph.Triple} to a Commons RDF + * {@link Triple}. + * <p> + * Note that if any of the triple's nodes {@link Node#isBlank()}, then the factory's + * {@link RDF#createBlankNode(String)} will be used, meaning + * that care should be taken if reusing an {@link RDF} instance + * for multiple conversion sessions. + * + * @see #fromJena(org.apache.jena.graph.Triple) + * + * @param factory {@link RDF} to use for creating the {@link Triple} and its + * {@link RDFTerm}s. + * @param triple + * Jena triple + * @return Converted triple + * @throws ConversionException + * if any of the triple's nodes are not concrete or the triple + * is a generalized triple + */ + public static Triple fromJena(RDF factory, org.apache.jena.graph.Triple triple) + throws ConversionException{ + if (factory instanceof JenaRDF) { + // No need to convert, just wrap + return ((JenaRDF) factory).fromJena(triple); + } + final BlankNodeOrIRI subject; + final IRI predicate; + try { + subject = (BlankNodeOrIRI) fromJena(factory, triple.getSubject()); + predicate = (IRI) fromJena(factory, triple.getPredicate()); + } catch (ClassCastException ex) { + throw new ConversionException("Can't convert generalized triple: " + triple, ex); + } + RDFTerm object = fromJena(factory, triple.getObject()); + return factory.createTriple(subject, predicate, object); + } + + /** + * Adapt an existing Jena {@link org.apache.jena.sparql.core.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 JenaRDF} instance + * in combination with {@link Node#getBlankNodeId()} + * for the purpose of its {@link BlankNode#uniqueReference()}. + * + * @param quad + * Jena quad + * @return Adapted quad + */ + public JenaQuad fromJena(org.apache.jena.sparql.core.Quad quad) { + return internalJenaFactory.fromJena(quad, getSalt()); + } + + /** + * Adapt an existing Jena {@link org.apache.jena.graph.Graph} to CommonsRDF + * {@link Graph}. + * <p> + * This does not take a copy, changes to the CommonsRDF Graph are reflected + * in the jena graph, which is accessible from + * {@link JenaGraph#asJenaGraph()}. + * <p> + * If the graph contains any {@link Node#isBlank()}, then any corresponding + * {@link BlankNode} will use a {@link UUID} salt from this + * {@link JenaRDF} instance in combination with + * {@link Node#getBlankNodeId()} for the purpose of its + * {@link BlankNode#uniqueReference()}. + * + * @param graph + * Jena {@link org.apache.jena.graph.Graph} to adapt + * @return Adapted {@link JenaGraph} + */ + public JenaGraph fromJena(org.apache.jena.graph.Graph graph) { + return internalJenaFactory.fromJena(graph, getSalt()); + } + + /** + * Adapt an existing Jena {@link org.apache.jena.rdf.model.Model} to CommonsRDF {@link Graph}. + * <p> + * This does not ake a copy, changes to the CommonsRDF Graph are reflected in the jena + * graph, which is accessible from {@link JenaGraph#asJenaGraph()}. + * <p> + * If the graph contains any {@link Node#isBlank()}, then any corresponding + * {@link BlankNode} will use a {@link UUID} salt from this + * {@link JenaRDF} instance + * in combination with {@link Node#getBlankNodeId()} + * for the purpose of its {@link BlankNode#uniqueReference()}. + * @param model + * Jena {@link org.apache.jena.rdf.model.Model} to adapt + * @return Adapted {@link JenaGraph} + */ + public JenaGraph fromJena(org.apache.jena.rdf.model.Model model) { + return internalJenaFactory.fromJena(model, getSalt()); + } + + /** + * Adapt an existing Jena {@link DatasetGraph} to CommonsRDF {@link Dataset}. + * <p> + * This does not + * take a copy, changes to the CommonsRDF Dataset are reflected in the jena + * dataset graph, which is accessible from {@link JenaDataset#asJenaDatasetGraph()}. + * <p> + * If the dataset contains any {@link Node#isBlank()}, then any corresponding + * {@link BlankNode} will use a {@link UUID} salt from this + * {@link JenaRDF} instance + * in combination with {@link Node#getBlankNodeId()} + * for the purpose of its {@link BlankNode#uniqueReference()}. + * + * @param datasetGraph Jena {@link DatasetGraph} to adapt + * @return Adapted {@link JenaDataset} + */ + public JenaDataset fromJena(DatasetGraph datasetGraph) { + return internalJenaFactory.fromJena(datasetGraph, getSalt()); + } + + /** + * Adapt an existing Jena {@link org.apache.jena.query.Dataset} to CommonsRDF {@link Dataset}. + * <p> + * This does not + * take a copy, changes to the CommonsRDF Dataset are reflected in the jena + * dataset graph, which is accessible from {@link JenaDataset#asJenaDatasetGraph()}. + * <p> + * If the dataset contains any {@link Node#isBlank()}, then any corresponding + * {@link BlankNode} will use a {@link UUID} salt from this + * {@link JenaRDF} instance + * in combination with {@link Node#getBlankNodeId()} + * for the purpose of its {@link BlankNode#uniqueReference()}. + * + * @param datasetGraph Jena {@link org.apache.jena.query.Dataset} to adapt + * @return Adapted {@link JenaDataset} + */ + public JenaDataset fromJena(org.apache.jena.query.Dataset datasetGraph) { + return internalJenaFactory.fromJena(datasetGraph.asDatasetGraph(), getSalt()); + } + + /** + * Convert from Jena {@link org.apache.jena.sparql.core.Quad} to a Commons + * RDF {@link Quad}. + * <p> + * Note that if any of the quad's nodes {@link Node#isBlank()}, then the + * factory's {@link RDF#createBlankNode(String)} will be used, + * meaning that care should be taken if reusing an {@link RDF} + * instance for multiple conversion sessions. + * + * @see #fromJena(org.apache.jena.sparql.core.Quad) + * @see #fromJenaGeneralized(org.apache.jena.sparql.core.Quad) + * + * @param factory + * {@link RDF} to use for creating the {@link Triple} + * and its {@link RDFTerm}s. + * @param quad + * Jena {@link org.apache.jena.sparql.core.Quad} to adapt + * @return Converted {@link Quad} + * @throws ConversionException + * if any of the quad's nodes are not concrete or the quad + * is a generalized quad + */ + public static Quad fromJena(RDF factory, org.apache.jena.sparql.core.Quad quad) { + if (factory instanceof JenaRDF) { + // No need to convert, just wrap + return ((JenaRDF) 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); + } + + /** + * Return {@link RDFSyntax} corresponding to a Jena {@link Lang}. + * + * @param lang {@link Lang} to convert + * @return Matched {@link RDFSyntax}, otherwise {@link Optional#empty()} + */ + public Optional<RDFSyntax> langToRdfSyntax(Lang lang) { + return RDFSyntax.byMediaType(lang.getContentType().getContentType()); + } + + /** + * Return Jena {@link Lang} corresponding to a {@link RDFSyntax}. + * + * @param rdfSyntax {@link RDFSyntax} to convert + * @return Matched {@link Lang}, otherwise {@link Optional#empty()} + */ + public Optional<Lang> rdfSyntaxToLang(RDFSyntax rdfSyntax) { + return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType)); + } + + /** + * Create a {@link StreamRDF} instance that inserts the converted + * {@link Quad}s. into a the provided {@link Consumer}. + * <p> + * The returned {@link StreamRDF} can be used for instance with Jena's + * {@link RDFDataMgr#parse(StreamRDF, String)}. + * + * @param factory + * {@link RDF} to use for creating {@link RDFTerm}s + * and {@link Quad}s. + * @param consumer + * A {@link Consumer} of {@link Quad}s + * @return A {@link StreamRDF} that will stream converted quads to the + * consumer + */ + public static StreamRDF streamJenaToCommonsRDF(RDF factory, Consumer<Quad> consumer) { + return new StreamRDFBase() { + @Override + public void quad(org.apache.jena.sparql.core.Quad quad) { + consumer.accept(fromJena(factory, quad)); + } + }; + } + + /** + * Create a {@link StreamRDF} instance that inserts generalized + * {@link TripleLike}s. into a the provided {@link Consumer}. + * <p> + * A generalized triple allows any {@link RDFTerm} for + * {@link TripleLike#getSubject()}, {@link TripleLike#getPredicate()} and + * {@link TripleLike#getObject()}. + * <p> + * The returned {@link StreamRDF} can be used for instance with Jena's + * {@link RDFDataMgr#parse(StreamRDF, String)}. + * + * @param generalizedConsumer + * A {@link Consumer} of generalized {@link TripleLike}s + * @return A {@link StreamRDF} that will stream generalized triples to the + * consumer + */ + public StreamRDF streamJenaToGeneralizedTriple(Consumer<TripleLike> generalizedConsumer) { + return new StreamRDFBase() { + @Override + public void triple(org.apache.jena.graph.Triple triple) { + generalizedConsumer.accept(fromJenaGeneralized(triple)); + } + }; + } + + /** + * Create a {@link StreamRDF} instance that inserts generalized + * {@link QuadLike}s. into a the provided {@link Consumer}. + * <p> + * A generalized quad allows any {@link RDFTerm} for + * {@link QuadLike#getSubject()}, {@link TripleLike#getPredicate()}, + * {@link QuadLike#getObject()} and {@link QuadLike#getGraphName()} . + * <p> + * The returned {@link StreamRDF} can be used for instance with Jena's + * {@link RDFDataMgr#parse(StreamRDF, String)}. + * + * @param generalizedConsumer + * A {@link Consumer} of generalized {@link QuadLike}s + * @return A {@link StreamRDF} that will stream generalized quads to the + * consumer + */ + public StreamRDF streamJenaToGeneralizedQuad(Consumer<QuadLike<RDFTerm>> generalizedConsumer) { + return new StreamRDFBase() { + @Override + public void quad(org.apache.jena.sparql.core.Quad quad) { + generalizedConsumer.accept(fromJenaGeneralized(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. + * + * @param graph Commons RDF {@link Graph} to convert + * @return Converted Jena {@link org.apache.jena.graph.Graph} + */ + public 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. + * + * @param term Commons RDF {@link RDFTerm} to convert + * @return Converted Jena {@link Node} + */ + public Node toJena(RDFTerm term) { + if (term == null) { + return null; + } + if (term instanceof JenaRDFTerm) + // TODO: What if it's a JenaBlankNodeImpl with + // a different salt? Do we need to rewrite the + // jena blanknode identifier? + return ((JenaRDFTerm) 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); + } + + /** + * Convert a CommonsRDF {@link Triple} to a Jena + * {@link org.apache.jena.graph.Triple}. + * <p> + * If the triple was from Jena originally, return that original object, else + * create a copy using Jena objects. + * + * @param triple Commons RDF {@link Triple} to convert + * @return Converted Jena {@link org.apache.jena.graph.Triple} + */ + public org.apache.jena.graph.Triple toJena(Triple triple) { + if (triple instanceof JenaTriple) + return ((JenaTriple) triple).asJenaTriple(); + return org.apache.jena.graph.Triple.create( + toJena(triple.getSubject()), + toJena(triple.getPredicate()), + toJena(triple.getObject())); + } + + + /** + * Convert a CommonsRDF {@link Quad} to a Jena + * {@link org.apache.jena.sparql.core.Quad}. + * <p> + * If the quad was from Jena originally, return that original object, + * otherwise create a copy using Jena objects. + * + * @param quad Commons RDF {@link Quad} to convert + * @return Converted Jena {@link org.apache.jena.sparql.core.Quad} + */ + public org.apache.jena.sparql.core.Quad toJena(Quad quad) { + if (quad instanceof JenaQuad) { + return ((JenaQuad) quad).asJenaQuad(); + } + return org.apache.jena.sparql.core.Quad.create( + toJena(quad.getGraphName().orElse(null)), + toJena(quad.getSubject()), + toJena(quad.getPredicate()), + toJena(quad.getObject())); + } + + // Some simple validations - full IRI parsing is not cheap. + private void validateIRI(String iri) { + if (iri.contains(" ")) + throw new IllegalArgumentException(); + if (iri.contains("<")) + throw new IllegalArgumentException(); + if (iri.contains(">")) + throw new IllegalArgumentException(); + } + + private static void validateLang(String languageTag) { + if (languageTag.contains(" ")) + throw new IllegalArgumentException("Invalid language tag: " + languageTag); + } + + /** + * Return the {@link UUID} salt used by this factory. + * <p> + * The salt is used for the purposes of {@link BlankNode} identity, see + * {@link BlankNode#uniqueReference()} for details. + * <p> + * This salt can be used with the constructor + * {@link JenaRDF#JenaFactory(UUID)} + * if consistent or reproducible {@link BlankNode}s are desirable. + * + * @return The {@link UUID} used as salt + */ + public UUID getSalt() { + return salt; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java b/jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java index ffbb61c..d78870c 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java @@ -30,7 +30,7 @@ import org.apache.commons.rdf.api.RDF; import org.apache.commons.rdf.api.TripleLike; import org.apache.commons.rdf.experimental.RDFParser; import org.apache.commons.rdf.jena.JenaGraph; -import org.apache.commons.rdf.jena.JenaFactory; +import org.apache.commons.rdf.jena.JenaRDF; import org.apache.commons.rdf.simple.experimental.AbstractRDFParser; import org.apache.jena.graph.Graph; import org.apache.jena.riot.Lang; @@ -44,7 +44,7 @@ public class JenaRDFParser extends AbstractRDFParser<JenaRDFParser> implements R private Consumer<QuadLike<RDFTerm>> generalizedConsumerQuad; protected RDF createRDFTermFactory() { - return new JenaFactory(); + return new JenaRDF(); } public JenaRDFParser targetGeneralizedTriple(Consumer<TripleLike> consumer) { @@ -71,21 +71,21 @@ public class JenaRDFParser extends AbstractRDFParser<JenaRDFParser> implements R @Override protected void parseSynchronusly() throws IOException { StreamRDF dest; - JenaFactory jenaFactory = getJenaFactory(); + JenaRDF jenaRDF = getJenaFactory(); if (getTargetGraph().isPresent() && getTargetGraph().get() instanceof JenaGraph) { Graph jenaGraph = ((JenaGraph) getTargetGraph().get()).asJenaGraph(); dest = StreamRDFLib.graph(jenaGraph); } else { if (generalizedConsumerQuad != null) { - dest = jenaFactory.streamJenaToGeneralizedQuad(generalizedConsumerQuad); + dest = jenaRDF.streamJenaToGeneralizedQuad(generalizedConsumerQuad); } else if (generalizedConsumerTriple != null) { - dest = jenaFactory.streamJenaToGeneralizedTriple(generalizedConsumerTriple); + dest = jenaRDF.streamJenaToGeneralizedTriple(generalizedConsumerTriple); } else { - dest = JenaFactory.streamJenaToCommonsRDF(getRdfTermFactory().get(), getTarget()); + dest = JenaRDF.streamJenaToCommonsRDF(getRdfTermFactory().get(), getTarget()); } } - Lang lang = getContentTypeSyntax().flatMap(jenaFactory::rdfSyntaxToLang).orElse(null); + Lang lang = getContentTypeSyntax().flatMap(jenaRDF::rdfSyntaxToLang).orElse(null); String baseStr = getBase().map(IRI::getIRIString).orElse(null); if (getSourceIri().isPresent()) { @@ -99,9 +99,9 @@ public class JenaRDFParser extends AbstractRDFParser<JenaRDFParser> implements R } } - private JenaFactory getJenaFactory() { - return (JenaFactory) getRdfTermFactory() - .filter(JenaFactory.class::isInstance) + private JenaRDF getJenaFactory() { + return (JenaRDF) getRdfTermFactory() + .filter(JenaRDF.class::isInstance) .orElseGet(this::createRDFTermFactory); } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java index c66a1da..d538076 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java @@ -26,7 +26,7 @@ import org.apache.commons.rdf.api.QuadLike; import org.apache.commons.rdf.api.RDFTerm; import org.apache.commons.rdf.jena.JenaQuad; import org.apache.commons.rdf.jena.JenaQuadLike; -import org.apache.commons.rdf.jena.JenaFactory; +import org.apache.commons.rdf.jena.JenaRDF; import org.apache.commons.rdf.jena.JenaTriple; import org.apache.jena.graph.Triple; import org.apache.jena.sparql.core.Quad; @@ -86,7 +86,7 @@ abstract class AbstractQuadLike<S extends RDFTerm, P extends RDFTerm, O extends @Override public org.apache.jena.sparql.core.Quad asJenaQuad() { - JenaFactory factory = new JenaFactory(); + JenaRDF factory = new JenaRDF(); if (quad == null) { quad = org.apache.jena.sparql.core.Quad.create( factory.toJena(graphName.orElse(null)), @@ -99,7 +99,7 @@ abstract class AbstractQuadLike<S extends RDFTerm, P extends RDFTerm, O extends @Override public org.apache.jena.graph.Triple asJenaTriple() { - JenaFactory factory = new JenaFactory(); + JenaRDF factory = new JenaRDF(); if (triple == null) { triple = org.apache.jena.graph.Triple.create( factory.toJena(subject), http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/impl/InternalJenaFactory.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/InternalJenaFactory.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/InternalJenaFactory.java index 67fccb7..66ef867 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/InternalJenaFactory.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/InternalJenaFactory.java @@ -35,7 +35,7 @@ import org.apache.commons.rdf.jena.JenaIRI; import org.apache.commons.rdf.jena.JenaLiteral; import org.apache.commons.rdf.jena.JenaQuad; import org.apache.commons.rdf.jena.JenaRDFTerm; -import org.apache.commons.rdf.jena.JenaFactory; +import org.apache.commons.rdf.jena.JenaRDF; import org.apache.commons.rdf.jena.JenaTriple; import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; @@ -49,7 +49,7 @@ import org.apache.jena.sparql.graph.GraphFactory; * <p> * This class is deliberately an abstract class, as it is an internal helper * which <strong>may change</strong> in any minor version update; users should - * instead use {@link JenaFactory}. + * instead use {@link JenaRDF}. * <p> * For the purpose of blank node identity, some of these methods require a * {@link UUID} to use as a salt. See {@link BlankNode#uniqueReference()} for http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaDatasetImpl.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaDatasetImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaDatasetImpl.java index 67b2988..81c9a07 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaDatasetImpl.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaDatasetImpl.java @@ -32,7 +32,7 @@ import org.apache.commons.rdf.api.Quad; import org.apache.commons.rdf.api.RDFTerm; import org.apache.commons.rdf.jena.JenaDataset; import org.apache.commons.rdf.jena.JenaGraph; -import org.apache.commons.rdf.jena.JenaFactory; +import org.apache.commons.rdf.jena.JenaRDF; import org.apache.jena.atlas.iterator.Iter; import org.apache.jena.graph.Node; import org.apache.jena.riot.Lang; @@ -44,12 +44,12 @@ class JenaDatasetImpl implements JenaDataset { private DatasetGraph graph; private UUID salt; - private JenaFactory factory; + private JenaRDF factory; JenaDatasetImpl(DatasetGraph graph, UUID salt) { this.graph = graph; this.salt = salt; - this.factory = new JenaFactory(salt); + this.factory = new JenaRDF(salt); } @Override @@ -136,14 +136,14 @@ class JenaDatasetImpl implements JenaDataset { @Override public Stream<? extends Quad> stream() { - JenaFactory factory = new JenaFactory(salt); + JenaRDF factory = new JenaRDF(salt); return Iter.asStream(graph.find(ANY, ANY, ANY, ANY), true) .map(factory::fromJena); } @Override public Stream<? extends Quad> stream(Optional<BlankNodeOrIRI> g, BlankNodeOrIRI s, IRI p, RDFTerm o) { - JenaFactory factory = new JenaFactory(salt); + JenaRDF factory = new JenaRDF(salt); return Iter.asStream(graph.find(toJenaPattern(g), toJenaPattern(s), toJenaPattern(p), toJenaPattern(o)), true) .map(factory::fromJena); } @@ -175,14 +175,14 @@ class JenaDatasetImpl implements JenaDataset { @Override public Stream<BlankNodeOrIRI> getGraphNames() { - JenaFactory factory = new JenaFactory(salt); + JenaRDF factory = new JenaRDF(salt); return Iter.asStream(graph.listGraphNodes()).map(node -> (BlankNodeOrIRI) factory.fromJena(node)); } @Override public Iterable<Quad> iterate() { - final JenaFactory factory = new JenaFactory(salt); + final JenaRDF factory = new JenaRDF(salt); return Iter.asStream(graph.find(), false) .map(q -> (Quad) factory.fromJena(q)) ::iterator; http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaGraphImpl.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaGraphImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaGraphImpl.java index bad996a..eab4089 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaGraphImpl.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaGraphImpl.java @@ -27,7 +27,7 @@ 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.JenaGraph; -import org.apache.commons.rdf.jena.JenaFactory; +import org.apache.commons.rdf.jena.JenaRDF; import org.apache.jena.atlas.iterator.Iter; import org.apache.jena.graph.Node; import org.apache.jena.rdf.model.Model; @@ -39,20 +39,20 @@ class JenaGraphImpl implements JenaGraph { private final org.apache.jena.graph.Graph graph; private final UUID salt; - private final transient JenaFactory factory; + private final transient JenaRDF factory; private Model model; JenaGraphImpl(org.apache.jena.graph.Graph graph, UUID salt) { this.graph = graph; this.salt = salt; - this.factory = new JenaFactory(salt); + this.factory = new JenaRDF(salt); } JenaGraphImpl(Model model, UUID salt) { this.model = model; this.graph = model.getGraph(); this.salt = salt; - this.factory = new JenaFactory(salt); + this.factory = new JenaRDF(salt); } @Override @@ -116,13 +116,13 @@ class JenaGraphImpl implements JenaGraph { @Override public Stream<? extends Triple> stream() { - JenaFactory factory = new JenaFactory(salt); + JenaRDF factory = new JenaRDF(salt); return Iter.asStream(graph.find(null, null, null), true).map(factory::fromJena); } @Override public Stream<? extends Triple> stream(BlankNodeOrIRI s, IRI p, RDFTerm o) { - JenaFactory factory = new JenaFactory(salt); + JenaRDF factory = new JenaRDF(salt); return Iter.asStream(graph.find(toJenaAny(s), toJenaAny(p), toJenaAny(o)), true) .map(factory::fromJena); } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/main/resources/META-INF/services/org.apache.commons.rdf.api.RDFFactory ---------------------------------------------------------------------- diff --git a/jena/src/main/resources/META-INF/services/org.apache.commons.rdf.api.RDFFactory b/jena/src/main/resources/META-INF/services/org.apache.commons.rdf.api.RDFFactory index f39e71d..afe6af3 100644 --- a/jena/src/main/resources/META-INF/services/org.apache.commons.rdf.api.RDFFactory +++ b/jena/src/main/resources/META-INF/services/org.apache.commons.rdf.api.RDFFactory @@ -1 +1 @@ -org.apache.commons.rdf.jena.JenaFactory +org.apache.commons.rdf.jena.JenaRDF http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/test/java/org/apache/commons/rdf/jena/JenaServiceLoaderTest.java ---------------------------------------------------------------------- diff --git a/jena/src/test/java/org/apache/commons/rdf/jena/JenaServiceLoaderTest.java b/jena/src/test/java/org/apache/commons/rdf/jena/JenaServiceLoaderTest.java index 87ce543..836e2ec 100644 --- a/jena/src/test/java/org/apache/commons/rdf/jena/JenaServiceLoaderTest.java +++ b/jena/src/test/java/org/apache/commons/rdf/jena/JenaServiceLoaderTest.java @@ -31,10 +31,10 @@ public class JenaServiceLoaderTest { public void testServiceLoaderLookup() { ServiceLoader<RDF> loader = ServiceLoader.load(RDF.class); for (RDF impl : loader) { - if (impl instanceof JenaFactory) { + if (impl instanceof JenaRDF) { return; // yay } } - fail("JenaFactory not found in ServiceLoader"); + fail("JenaRDF not found in ServiceLoader"); } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/test/java/org/apache/commons/rdf/jena/TestGraphJena.java ---------------------------------------------------------------------- diff --git a/jena/src/test/java/org/apache/commons/rdf/jena/TestGraphJena.java b/jena/src/test/java/org/apache/commons/rdf/jena/TestGraphJena.java index 3c7caa8..a11fdc6 100644 --- a/jena/src/test/java/org/apache/commons/rdf/jena/TestGraphJena.java +++ b/jena/src/test/java/org/apache/commons/rdf/jena/TestGraphJena.java @@ -25,7 +25,7 @@ public class TestGraphJena extends AbstractGraphTest { @Override public RDF createFactory() { - return new JenaFactory() ; + return new JenaRDF() ; } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/test/java/org/apache/commons/rdf/jena/TestJenaGraphToCommonsRDFGraph.java ---------------------------------------------------------------------- diff --git a/jena/src/test/java/org/apache/commons/rdf/jena/TestJenaGraphToCommonsRDFGraph.java b/jena/src/test/java/org/apache/commons/rdf/jena/TestJenaGraphToCommonsRDFGraph.java index ad71a58..16e3e67 100644 --- a/jena/src/test/java/org/apache/commons/rdf/jena/TestJenaGraphToCommonsRDFGraph.java +++ b/jena/src/test/java/org/apache/commons/rdf/jena/TestJenaGraphToCommonsRDFGraph.java @@ -56,10 +56,10 @@ public class TestJenaGraphToCommonsRDFGraph { RDFDataMgr.read(jGraph, turtleFile.toUri().toString()) ; // "graph" is a CommonsRDF graph - Graph graph = new JenaFactory().fromJena(jGraph) ; + Graph graph = new JenaRDF().fromJena(jGraph) ; // Add to CommonsRDF Graph - RDF rft = new JenaFactory() ; + RDF rft = new JenaRDF() ; graph.add(rft.createIRI("http://example/s2"), rft.createIRI("http://example/p2"), rft.createLiteral("foo")) ; http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/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 b5b3363..7760e1f 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 @@ -54,7 +54,7 @@ public class TestRDFParserBuilder { @Test public void parseTurtle() throws Exception { - Graph g = new JenaFactory().createGraph(); + Graph g = new JenaRDF().createGraph(); Future<ParseResult> gFuture = new JenaRDFParser().contentType(RDFSyntax.TURTLE).source(turtleFile) .target(g).parse(); gFuture.get(5, TimeUnit.SECONDS); http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/384a8337/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFTermFactoryJena.java ---------------------------------------------------------------------- diff --git a/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFTermFactoryJena.java b/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFTermFactoryJena.java index f917a68..327f218 100644 --- a/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFTermFactoryJena.java +++ b/jena/src/test/java/org/apache/commons/rdf/jena/TestRDFTermFactoryJena.java @@ -25,7 +25,7 @@ public class TestRDFTermFactoryJena extends AbstractRDFTermFactoryTest { @Override public RDF createFactory() { - return new JenaFactory() ; + return new JenaRDF() ; } }