JenaParser implementation
Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/1125dd8d Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/1125dd8d Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/1125dd8d Branch: refs/heads/fluent-parser-impl Commit: 1125dd8dda8c3de1cddfa6b0d8c85ef3d0dba4f6 Parents: 9be11ed Author: Stian Soiland-Reyes <st...@apache.org> Authored: Wed Feb 28 23:38:19 2018 +0000 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Wed Feb 28 23:38:19 2018 +0000 ---------------------------------------------------------------------- .../org/apache/commons/rdf/jena/JenaRDF.java | 7 + .../rdf/jena/experimental/JenaRDFParser.java | 106 --------------- .../rdf/jena/experimental/package-info.java | 31 ----- .../commons/rdf/jena/impl/JenaParser.java | 135 +++++++++++++++++++ 4 files changed, 142 insertions(+), 137 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/1125dd8d/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java ---------------------------------------------------------------------- diff --git a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java b/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java index 62327a8..30ead8c 100644 --- a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java +++ b/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java @@ -34,7 +34,9 @@ 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.api.io.Parser; import org.apache.commons.rdf.jena.impl.InternalJenaFactory; +import org.apache.commons.rdf.jena.impl.JenaParser; import org.apache.jena.datatypes.RDFDatatype; import org.apache.jena.datatypes.xsd.XSDDatatype; import org.apache.jena.graph.Node; @@ -830,5 +832,10 @@ public final class JenaRDF implements RDF { public UUID salt() { return salt; } + + @Override + public Optional<Parser> parser(RDFSyntax syntax) { + return Optional.of(new JenaParser(syntax)); + } } http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/1125dd8d/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java ---------------------------------------------------------------------- diff --git a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java b/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java deleted file mode 100644 index 2513e82..0000000 --- a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/JenaRDFParser.java +++ /dev/null @@ -1,106 +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.experimental; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.util.function.Consumer; - -import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.QuadLike; -import org.apache.commons.rdf.api.RDF; -import org.apache.commons.rdf.api.RDFTerm; -import org.apache.commons.rdf.api.TripleLike; -import org.apache.commons.rdf.jena.JenaGraph; -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; -import org.apache.jena.riot.RDFParser; -import org.apache.jena.riot.system.StreamRDF; -import org.apache.jena.riot.system.StreamRDFLib; - -public class JenaRDFParser extends AbstractRDFParser<JenaRDFParser> { - - private Consumer<TripleLike> generalizedConsumerTriple; - private Consumer<QuadLike<RDFTerm>> generalizedConsumerQuad; - - @Override - protected RDF createRDFTermFactory() { - return new JenaRDF(); - } - - public JenaRDFParser targetGeneralizedTriple(final Consumer<TripleLike> consumer) { - final JenaRDFParser c = this.clone(); - c.resetTarget(); - c.generalizedConsumerTriple = consumer; - return c; - } - - public JenaRDFParser targetGeneralizedQuad(final Consumer<QuadLike<RDFTerm>> consumer) { - final JenaRDFParser c = this.clone(); - c.resetTarget(); - c.generalizedConsumerQuad = consumer; - return c; - } - - @Override - protected void resetTarget() { - super.resetTarget(); - this.generalizedConsumerTriple = null; - this.generalizedConsumerQuad = null; - } - - @Override - protected void parseSynchronusly() throws IOException { - StreamRDF dest; - final JenaRDF jenaRDF = getJenaFactory(); - if (getTargetGraph().isPresent() && getTargetGraph().get() instanceof JenaGraph) { - final Graph jenaGraph = ((JenaGraph) getTargetGraph().get()).asJenaGraph(); - dest = StreamRDFLib.graph(jenaGraph); - } else { - if (generalizedConsumerQuad != null) { - dest = jenaRDF.streamJenaToGeneralizedQuad(generalizedConsumerQuad); - } else if (generalizedConsumerTriple != null) { - dest = jenaRDF.streamJenaToGeneralizedTriple(generalizedConsumerTriple); - } else { - dest = JenaRDF.streamJenaToQuad(getRdfTermFactory().get(), getTarget()); - } - } - - final Lang lang = getContentTypeSyntax().flatMap(jenaRDF::asJenaLang).orElse(null); - final String baseStr = getBase().map(IRI::getIRIString).orElse(null); - - if (getSourceIri().isPresent()) { - RDFParser.source(getSourceIri().get().toString()).base(baseStr).lang(lang).parse(dest); - } else if (getSourceFile().isPresent()) { - try (InputStream s = Files.newInputStream(getSourceFile().get())) { - RDFParser.source(s).base(baseStr).lang(lang).parse(dest); - } - } else { - RDFParser.source(getSourceInputStream().get()).base(baseStr).lang(lang).parse(dest); - } - } - - private JenaRDF getJenaFactory() { - return (JenaRDF) getRdfTermFactory().filter(JenaRDF.class::isInstance).orElseGet(this::createRDFTermFactory); - } - -} http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/1125dd8d/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/package-info.java ---------------------------------------------------------------------- diff --git a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/package-info.java b/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/package-info.java deleted file mode 100644 index 83252b0..0000000 --- a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/experimental/package-info.java +++ /dev/null @@ -1,31 +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. - */ -/** - * Experimental Commons RDF Jena implementations. - * <p> - * Classes in this package should be considered <strong>at risk</strong>; they - * might change or be removed in the next minor update of Commons RDF. - * <p> - * When a class has stabilized, it will move to the - * {@link org.apache.commons.rdf.jena} package. - * <ul> - * <li>{@link org.apache.commons.rdf.jena.experimental.JenaRDFParser} - a Jena-backed implementations of - * {@link org.apache.commons.rdf.experimental.RDFParser}.</li> - * </ul> - */ -package org.apache.commons.rdf.jena.experimental; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/1125dd8d/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaParser.java ---------------------------------------------------------------------- diff --git a/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaParser.java b/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaParser.java new file mode 100644 index 0000000..6952083 --- /dev/null +++ b/commons-rdf-jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaParser.java @@ -0,0 +1,135 @@ +/* + * 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.impl; + +import java.io.IOException; +import java.nio.file.Path; + +import org.apache.commons.rdf.api.Dataset; +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.RDF; +import org.apache.commons.rdf.api.RDFSyntax; +import org.apache.commons.rdf.api.io.Parsed; +import org.apache.commons.rdf.api.io.Parser; +import org.apache.commons.rdf.api.io.ParserConfig; +import org.apache.commons.rdf.api.io.ParserConfig.ImmutableParserConfig; +import org.apache.commons.rdf.api.io.ParserSource; +import org.apache.commons.rdf.api.io.ParserTarget; +import org.apache.commons.rdf.jena.JenaDataset; +import org.apache.commons.rdf.jena.JenaGraph; +import org.apache.commons.rdf.jena.JenaRDF; +import org.apache.jena.riot.RDFParser; +import org.apache.jena.riot.RDFParserBuilder; + +public class JenaParser implements Parser { + + private final RDFSyntax defaultSyntax; + + public JenaParser() { + defaultSyntax = null; // unspecified/guess + } + + public JenaParser(RDFSyntax syntax) { + this.defaultSyntax = syntax; + } + + @SuppressWarnings("rawtypes") + @Override + public Parsed parse(ParserConfig config) throws IOException { + return parseImpl(completeConfig(config)); + } + + private ImmutableParserConfig completeConfig(ParserConfig config) { + ImmutableParserConfig completed = config.asImmutableConfig(); + if (!completed.source().isPresent()) { + throw new IllegalStateException("source missing from ParserConfig"); + } + + if (!completed.syntax().isPresent()) { + // Might still be null + completed = completed.withSyntax(defaultSyntax); + } + if (!completed.rdf().isPresent()) { + completed = completed.withRDF(new JenaRDF()); + } + RDF rdf = completed.rdf().get(); + if (!completed.target().isPresent()) { + Dataset ds = rdf.createDataset(); + completed = completed.withTarget(ParserTarget.toDataset(ds)); + } + ParserSource<?> source = completed.source().get(); + if (!completed.base().isPresent() && source.iri().isPresent()) { + // Use base from source.iri() - but only if it's from a source + // type Jena does not recognize + Object src = source.src(); + if (!(src instanceof IRI) && !(src instanceof Path)) { + completed = completed.withBase(source.iri().get()); + } + } + return completed; + } + + @SuppressWarnings("rawtypes") + private Parsed parseImpl(ImmutableParserConfig config) throws IOException { + RDF rdf = config.rdf().get(); + JenaRDF jenaRDF = jenaRDF(rdf); + ParserSource<?> source = config.source().get(); + ParserTarget<?> target = config.target().get(); + RDFParserBuilder jenaParser = RDFParser.create(); + + config.base().map(IRI::getIRIString).map(jenaParser::base); + config.syntax().flatMap(jenaRDF::asJenaLang).map(jenaParser::lang); + + // Handle jena-supported sources first + if (source.src() instanceof Path) { + Path path = (Path) source.src(); + jenaParser.source(path); + } else if (source.src() instanceof IRI) { + IRI iri = (IRI) source.src(); + jenaParser.source(iri.getIRIString()); + } else if (source.src() instanceof String) { + jenaParser.fromString(source.src().toString()); + } else { + // This fallback should always work + jenaParser.source(source.inputStream()); + } + + // Handle jena implementations firsts + if (target.dest() instanceof JenaDataset) { + JenaDataset jenaDataset = (JenaDataset) target.dest(); + jenaParser.parse(jenaDataset.asJenaDatasetGraph()); + } else if (target.dest() instanceof JenaGraph) { + JenaGraph jenaGraph = (JenaGraph) target.dest(); + jenaParser.parse(jenaGraph.asJenaGraph()); + } else { + // General approach, stream to Quads + jenaParser.parse(JenaRDF.streamJenaToQuad(rdf, target)); + } + + // Parsing finished + return Parsed.from(source, target); + } + + private JenaRDF jenaRDF(RDF rdf) { + if (rdf instanceof JenaRDF) { + return (JenaRDF) rdf; + } else { + return new JenaRDF(); + } + } + +}