COMMONSRDF-39 Moved JsonLdParser to experimental package
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/1412edd6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/1412edd6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/1412edd6 Branch: refs/heads/jsonld-java Commit: 1412edd66f24401da13a45de6d06ef68c0e19fa7 Parents: 2510a07 Author: Stian Soiland-Reyes <st...@apache.org> Authored: Wed Sep 28 14:32:07 2016 +0100 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Wed Sep 28 14:32:07 2016 +0100 ---------------------------------------------------------------------- .../rdf/jsonldjava/JsonLdParserBuilder.java | 157 ------------------ .../jsonldjava/experimental/JsonLdParser.java | 160 +++++++++++++++++++ .../jsonldjava/experimental/package-info.java | 34 ++++ .../rdf/jsonldjava/JsonLdParserBuilderTest.java | 7 +- 4 files changed, 198 insertions(+), 160 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1412edd6/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java deleted file mode 100644 index e38af2c..0000000 --- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilder.java +++ /dev/null @@ -1,157 +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.jsonldjava; - -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.util.function.Predicate; - -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.RDFSyntax; -import org.apache.commons.rdf.simple.AbstractRDFParserBuilder; - -import com.github.jsonldjava.core.JsonLdError; -import com.github.jsonldjava.core.JsonLdOptions; -import com.github.jsonldjava.core.JsonLdProcessor; -import com.github.jsonldjava.core.RDFDataset; -import com.github.jsonldjava.utils.JsonUtils; - -public class JsonLdParserBuilder extends AbstractRDFParserBuilder<JsonLdParserBuilder> { - - @Override - protected JsonLdRDFTermFactory createRDFTermFactory() { - return new JsonLdRDFTermFactory(); - } - - @Override - public JsonLdParserBuilder contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException { - if (rdfSyntax != null && rdfSyntax != RDFSyntax.JSONLD) { - throw new IllegalArgumentException("Unsupported contentType: " + rdfSyntax); - } - return super.contentType(rdfSyntax); - } - - @Override - public JsonLdParserBuilder contentType(String contentType) throws IllegalArgumentException { - JsonLdParserBuilder c = (JsonLdParserBuilder) super.contentType(contentType); - if (c.getContentType().filter(Predicate.isEqual(RDFSyntax.JSONLD).negate()).isPresent()) { - throw new IllegalArgumentException("Unsupported contentType: " + contentType); - } - return c; - } - - private static URL asURL(IRI iri) throws IllegalStateException { - try { - return new URI(iri.getIRIString()).toURL(); - } catch (MalformedURLException | URISyntaxException e) { - throw new IllegalStateException("Invalid URL: " + iri.getIRIString()); - } - } - - @Override - protected void checkSource() throws IOException { - super.checkSource(); - // Might throw IllegalStateException if invalid - getSourceIri().map(JsonLdParserBuilder::asURL); - } - - @Override - protected void parseSynchronusly() throws IOException { - Object json = readSource(); - JsonLdOptions options = new JsonLdOptions(); - getBase().map(IRI::getIRIString).ifPresent(options::setBase); - // TODO: base from readSource() (after redirection and Content-Location header) - // should be forwarded - - // TODO: Modify JsonLdProcessor to accept the target RDFDataset - RDFDataset rdfDataset; - try { - rdfDataset = (RDFDataset) JsonLdProcessor.toRDF(json, options); - } catch (JsonLdError e) { - throw new IOException("Could not parse Json-LD", e); - } - if (getTargetGraph().isPresent()) { - Graph intoGraph = getTargetGraph().get(); - if (intoGraph instanceof JsonLdGraph && ! intoGraph.contains(null, null, null)) { - // Empty graph, we can just move over the map content directly: - JsonLdGraph jsonLdGraph = (JsonLdGraph) intoGraph; - jsonLdGraph.getRdfDataSet().putAll(rdfDataset); - return; - // otherwise we have to merge as normal - } - // TODO: Modify JsonLdProcessor to have an actual triple callback - Graph parsedGraph = getJsonLdRDFTermFactory().asGraph(rdfDataset); - // sequential() as we don't know if destination is thread safe :-/ - parsedGraph.stream().sequential().forEach(intoGraph::add); - } else if (getTargetDataset().isPresent()) { - Dataset intoDataset = getTargetDataset().get(); - if (intoDataset instanceof JsonLdDataset && - ! intoDataset.contains(null, null, null, null)) { - JsonLdDataset jsonLdDataset = (JsonLdDataset) intoDataset; - // Empty - we can just do a brave replace! - jsonLdDataset.getRdfDataSet().putAll(rdfDataset); - return; - // otherwise we have to merge.. but also avoid duplicate triples, - // map blank nodes etc, so we'll fall back to normal Dataset appending. - } - Dataset fromDataset = getJsonLdRDFTermFactory().asDataset(rdfDataset); - // .sequential() as we don't know if destination is thread-safe :-/ - fromDataset.stream().sequential().forEach(intoDataset::add); - } else { - Dataset fromDataset = getJsonLdRDFTermFactory().asDataset(rdfDataset); - // No need for .sequential() here - fromDataset.stream().forEach(getTarget()); - } - } - - private JsonLdRDFTermFactory getJsonLdRDFTermFactory() { - if (getRdfTermFactory().isPresent() && getRdfTermFactory().get() instanceof JsonLdRDFTermFactory) { - return (JsonLdRDFTermFactory) getRdfTermFactory().get(); - } - return createRDFTermFactory(); - } - - private Object readSource() throws IOException { - // Due to checked IOException we can't easily - // do this with .map and .orElseGet() - - if (getSourceInputStream().isPresent()) { - return JsonUtils.fromInputStream(getSourceInputStream().get()); - } - if (getSourceIri().isPresent()) { - // TODO: propagate @base from content - return JsonUtils.fromURL(asURL(getSourceIri().get()), - JsonUtils.getDefaultHttpClient()); - } - if (getSourceFile().isPresent()) { - try (InputStream inputStream = Files.newInputStream(getSourceFile().get())){ - return JsonUtils.fromInputStream(inputStream); - } - } - throw new IllegalStateException("No known source found"); - } - -} - http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1412edd6/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/JsonLdParser.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/JsonLdParser.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/JsonLdParser.java new file mode 100644 index 0000000..102b2d4 --- /dev/null +++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/JsonLdParser.java @@ -0,0 +1,160 @@ +/** + * 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.jsonldjava.experimental; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.util.function.Predicate; + +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.RDFSyntax; +import org.apache.commons.rdf.jsonldjava.JsonLdDataset; +import org.apache.commons.rdf.jsonldjava.JsonLdGraph; +import org.apache.commons.rdf.jsonldjava.JsonLdRDFTermFactory; +import org.apache.commons.rdf.simple.experimental.AbstractRDFParser; + +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.JsonLdOptions; +import com.github.jsonldjava.core.JsonLdProcessor; +import com.github.jsonldjava.core.RDFDataset; +import com.github.jsonldjava.utils.JsonUtils; + +public class JsonLdParser extends AbstractRDFParser<JsonLdParser> { + + @Override + protected JsonLdRDFTermFactory createRDFTermFactory() { + return new JsonLdRDFTermFactory(); + } + + @Override + public JsonLdParser contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException { + if (rdfSyntax != null && rdfSyntax != RDFSyntax.JSONLD) { + throw new IllegalArgumentException("Unsupported contentType: " + rdfSyntax); + } + return super.contentType(rdfSyntax); + } + + @Override + public JsonLdParser contentType(String contentType) throws IllegalArgumentException { + JsonLdParser c = (JsonLdParser) super.contentType(contentType); + if (c.getContentType().filter(Predicate.isEqual(RDFSyntax.JSONLD).negate()).isPresent()) { + throw new IllegalArgumentException("Unsupported contentType: " + contentType); + } + return c; + } + + private static URL asURL(IRI iri) throws IllegalStateException { + try { + return new URI(iri.getIRIString()).toURL(); + } catch (MalformedURLException | URISyntaxException e) { + throw new IllegalStateException("Invalid URL: " + iri.getIRIString()); + } + } + + @Override + protected void checkSource() throws IOException { + super.checkSource(); + // Might throw IllegalStateException if invalid + getSourceIri().map(JsonLdParser::asURL); + } + + @Override + protected void parseSynchronusly() throws IOException { + Object json = readSource(); + JsonLdOptions options = new JsonLdOptions(); + getBase().map(IRI::getIRIString).ifPresent(options::setBase); + // TODO: base from readSource() (after redirection and Content-Location header) + // should be forwarded + + // TODO: Modify JsonLdProcessor to accept the target RDFDataset + RDFDataset rdfDataset; + try { + rdfDataset = (RDFDataset) JsonLdProcessor.toRDF(json, options); + } catch (JsonLdError e) { + throw new IOException("Could not parse Json-LD", e); + } + if (getTargetGraph().isPresent()) { + Graph intoGraph = getTargetGraph().get(); + if (intoGraph instanceof JsonLdGraph && ! intoGraph.contains(null, null, null)) { + // Empty graph, we can just move over the map content directly: + JsonLdGraph jsonLdGraph = (JsonLdGraph) intoGraph; + jsonLdGraph.getRdfDataSet().putAll(rdfDataset); + return; + // otherwise we have to merge as normal + } + // TODO: Modify JsonLdProcessor to have an actual triple callback + Graph parsedGraph = getJsonLdRDFTermFactory().asGraph(rdfDataset); + // sequential() as we don't know if destination is thread safe :-/ + parsedGraph.stream().sequential().forEach(intoGraph::add); + } else if (getTargetDataset().isPresent()) { + Dataset intoDataset = getTargetDataset().get(); + if (intoDataset instanceof JsonLdDataset && + ! intoDataset.contains(null, null, null, null)) { + JsonLdDataset jsonLdDataset = (JsonLdDataset) intoDataset; + // Empty - we can just do a brave replace! + jsonLdDataset.getRdfDataSet().putAll(rdfDataset); + return; + // otherwise we have to merge.. but also avoid duplicate triples, + // map blank nodes etc, so we'll fall back to normal Dataset appending. + } + Dataset fromDataset = getJsonLdRDFTermFactory().asDataset(rdfDataset); + // .sequential() as we don't know if destination is thread-safe :-/ + fromDataset.stream().sequential().forEach(intoDataset::add); + } else { + Dataset fromDataset = getJsonLdRDFTermFactory().asDataset(rdfDataset); + // No need for .sequential() here + fromDataset.stream().forEach(getTarget()); + } + } + + private JsonLdRDFTermFactory getJsonLdRDFTermFactory() { + if (getRdfTermFactory().isPresent() && getRdfTermFactory().get() instanceof JsonLdRDFTermFactory) { + return (JsonLdRDFTermFactory) getRdfTermFactory().get(); + } + return createRDFTermFactory(); + } + + private Object readSource() throws IOException { + // Due to checked IOException we can't easily + // do this with .map and .orElseGet() + + if (getSourceInputStream().isPresent()) { + return JsonUtils.fromInputStream(getSourceInputStream().get()); + } + if (getSourceIri().isPresent()) { + // TODO: propagate @base from content + return JsonUtils.fromURL(asURL(getSourceIri().get()), + JsonUtils.getDefaultHttpClient()); + } + if (getSourceFile().isPresent()) { + try (InputStream inputStream = Files.newInputStream(getSourceFile().get())){ + return JsonUtils.fromInputStream(inputStream); + } + } + throw new IllegalStateException("No known source found"); + } + +} + http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1412edd6/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/package-info.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/package-info.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/package-info.java new file mode 100644 index 0000000..fbd595e --- /dev/null +++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/package-info.java @@ -0,0 +1,34 @@ +/* + * 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 RDF4J 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.rdf4j} package. + * <p> + * <ul> + * <li>{@link RDF4JParser} - an RDF4J-backed + * implementations of + * {@link org.apache.commons.rdf.api.experimental.RDFParser}.</li> + * </ul> + */ +package org.apache.commons.rdf.jsonldjava.experimental; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1412edd6/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java b/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java index 37fa560..4d846ee 100644 --- a/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java +++ b/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java @@ -32,6 +32,7 @@ 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.RDFSyntax; +import org.apache.commons.rdf.jsonldjava.experimental.JsonLdParser; import org.apache.commons.rdf.simple.Types; import org.junit.Test; @@ -57,7 +58,7 @@ public class JsonLdParserBuilderTest { assertNotNull("Test resource not found: " + TEST_JSONLD, url); IRI iri = factory.createIRI(url.toString()); Graph g = factory.createGraph(); - new JsonLdParserBuilder() + new JsonLdParser() .contentType(RDFSyntax.JSONLD) .source(iri) .target(g) @@ -75,7 +76,7 @@ public class JsonLdParserBuilderTest { Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING); } Graph g = factory.createGraph(); - new JsonLdParserBuilder() + new JsonLdParser() .contentType(RDFSyntax.JSONLD) .source(path) .target(g) @@ -89,7 +90,7 @@ public class JsonLdParserBuilderTest { Graph g = factory.createGraph(); try (InputStream is = getClass().getResourceAsStream(TEST_JSONLD)) { assertNotNull("Test resource not found: " + TEST_JSONLD, is); - new JsonLdParserBuilder() + new JsonLdParser() .base("http://example.com/base/") .contentType(RDFSyntax.JSONLD).source(is) .target(g)