Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 04CEF200B92 for ; Wed, 28 Sep 2016 15:52:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 02D02160AC1; Wed, 28 Sep 2016 13:52:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9BE2E160AB4 for ; Wed, 28 Sep 2016 15:52:03 +0200 (CEST) Received: (qmail 40419 invoked by uid 500); 28 Sep 2016 13:52:02 -0000 Mailing-List: contact commits-help@commonsrdf.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commonsrdf.incubator.apache.org Delivered-To: mailing list commits@commonsrdf.incubator.apache.org Received: (qmail 40410 invoked by uid 99); 28 Sep 2016 13:52:02 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Sep 2016 13:52:02 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 67BC3CA853 for ; Wed, 28 Sep 2016 13:52:02 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.344 X-Spam-Level: X-Spam-Status: No, score=-4.344 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.124] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id FVIV3mwh-rgM for ; Wed, 28 Sep 2016 13:52:00 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 280FF60E13 for ; Wed, 28 Sep 2016 13:51:54 +0000 (UTC) Received: (qmail 38498 invoked by uid 99); 28 Sep 2016 13:51:54 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Sep 2016 13:51:54 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E6523E2F35; Wed, 28 Sep 2016 13:51:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stain@apache.org To: commits@commonsrdf.incubator.apache.org Date: Wed, 28 Sep 2016 13:52:13 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [21/36] incubator-commonsrdf git commit: Quad-support for RDFParserBuildler archived-at: Wed, 28 Sep 2016 13:52:05 -0000 Quad-support for RDFParserBuildler This changes intoGraph() to target(?) where ? can be a Graph, Dataset or Consumer similarly parse() returns a Future rather than Future - as there might not be any Graph Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/defdbd91 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/defdbd91 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/defdbd91 Branch: refs/heads/master Commit: defdbd9160561ae93e8558a8f2c6cc27507b22bb Parents: bb90a6e Author: Stian Soiland-Reyes Authored: Wed Apr 13 16:50:31 2016 +0100 Committer: Stian Soiland-Reyes Committed: Wed Apr 13 16:50:31 2016 +0100 ---------------------------------------------------------------------- .../commons/rdf/api/RDFParserBuilder.java | 174 ++++++++++++++----- .../rdf/simple/AbstractRDFParserBuilder.java | 61 ++++--- .../simple/AbstractRDFParserBuilderTest.java | 40 +++-- .../rdf/simple/DummyRDFParserBuilder.java | 34 ++-- 4 files changed, 209 insertions(+), 100 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/defdbd91/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java b/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java index 1425263..0494502 100644 --- a/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java +++ b/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java @@ -22,58 +22,71 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.concurrent.Future; +import java.util.function.Consumer; /** - * Builder for parsing an RDF source into a Graph. + * Builder for parsing an RDF source into a target (e.g. a Graph/Dataset). *

* This interface follows the * Builder pattern, * allowing to set parser settings like {@link #contentType(RDFSyntax)} and * {@link #base(IRI)}. A caller MUST call one of the source methods * (e.g. {@link #source(IRI)}, {@link #source(Path)}, - * {@link #source(InputStream)}) before calling {@link #parse()} on the returned + * {@link #source(InputStream)}), and MUST call one of the target + * methods (e.g. {@link #target(Consumer)}, {@link #target(Dataset)}, + * {@link #target(Graph)}) before calling {@link #parse()} on the returned * RDFParserBuilder - however methods can be called in any order. *

- * The call to {@link #parse()} returns a {@link Future}, allowing asynchronous parse - * operations. This can be combined with {@link #intoGraph(Graph)} - * allowing access to the graph before parsing has completed, - * however callers are still recommended to to check - * {@link Future#get()} for any exceptions thrown during parsing. + * The call to {@link #parse()} returns a {@link Future}, allowing asynchronous + * parse operations. Callers are recommended to check {@link Future#get()} to + * ensure parsing completed successfully, or catch exceptions thrown during + * parsing. *

* Setting a method that has already been set will override any existing value * in the returned builder - irregardless of the parameter type (e.g. * {@link #source(IRI)} will override a previous {@link #source(Path)}. Settings - * can be unset by passing null - this may require casting, e.g. - * contentType( (RDFSyntax) null ) to undo a previous call to - * {@link #contentType(RDFSyntax)}. + * can be unset by passing null - note that this may + * require casting, e.g. contentType( (RDFSyntax) null ) + * to undo a previous call to {@link #contentType(RDFSyntax)}. *

* It is undefined if a RDFParserBuilder is mutable or thread-safe, so callers * should always use the returned modified RDFParserBuilder from the builder - * methods. The builder may return itself, or a cloned builder with the modified - * settings applied. Implementations are however encouraged to be - * immutable and thread-safe and document this, - * as an example starting point, see + * methods. The builder may return itself after modification, + * or a cloned builder with the modified settings applied. + * Implementations are however encouraged to be immutable, + * thread-safe and document this. As an example starting point, see * {@link org.apache.commons.rdf.simple.AbstractRDFParserBuilder}. *

* Example usage: *

* *
- *   // Retrieve populated graph from the Future
- *   Graph g1 = ExampleRDFParserBuilder.source("http://example.com/graph.rdf").parse().get(30, TimeUnit.Seconds);
- *   // Or parsing into an existing Graph:
- *   ExampleRDFParserBuilder.source(Paths.get("/tmp/graph.ttl").contentType(RDFSyntax.TURTLE).intoGraph(g1).parse();
+ *   Graph g1 = rDFTermFactory.createGraph();
+ *   new ExampleRDFParserBuilder()
+ *    	.source(Paths.get("/tmp/graph.ttl"))
+ *    	.contentType(RDFSyntax.TURTLE)
+ *   	.target(g1)
+ *   	.parse().get(30, TimeUnit.Seconds);
  * 
- * * */ public interface RDFParserBuilder { + /** + * The result of {@link RDFParserBuilder#parse()} indicating + * parsing completed. + *

+ * This is a marker interface that may be subclassed to include + * parser details, e.g. warning messages or triple counts. + */ + public interface ParseResult { + } + /** * Specify which {@link RDFTermFactory} to use for generating * {@link RDFTerm}s. *

- * This option may be used together with {@link #intoGraph(Graph)} to + * This option may be used together with {@link #target(Graph)} to * override the implementation's default factory and graph. *

* Warning: Using the same {@link RDFTermFactory} for @@ -82,7 +95,7 @@ public interface RDFParserBuilder { * use the {@link RDFTermFactory#createBlankNode(String)} method * from the parsed blank node labels. * - * @see #intoGraph(Graph) + * @see #target(Graph) * @param rdfTermFactory * {@link RDFTermFactory} to use for generating RDFTerms. * @return An {@link RDFParserBuilder} that will use the specified @@ -144,30 +157,97 @@ public interface RDFParserBuilder { RDFParserBuilder contentType(String contentType) throws IllegalArgumentException; /** - * Specify which {@link Graph} to add triples to. - *

- * The default (if this option has not been set) is that each call to - * {@link #parse()} return a new {@link Graph}, which is created using - * {@link RDFTermFactory#createGraph()} if - * {@link #rdfTermFactory(RDFTermFactory)} has been set. - *

- * It is undefined if any triples are added to the specified - * {@link Graph} if the {@link Future#get()} returned from - * {@link #parse()} throws any exceptions. (However - * implementations are free to prevent this using transaction - * mechanisms or similar). However, if {@link Future#get()} - * does not indicatean exception, the - * parser implementation SHOULD have inserted all parsed triples - * to the specified graph. + * Specify a {@link Graph} to add parsed triples to. + *

+ * If the source supports datasets (e.g. the {@link #contentType(RDFSyntax)} + * set has {@link RDFSyntax#supportsDataset} is true)), then only quads in + * the default graph will be added to the Graph as {@link Triple}s. + *

+ * It is undefined if any triples are added to the specified {@link Graph} + * if {@link #parse()} throws any exceptions. (However implementations are + * free to prevent this using transaction mechanisms or similar). If + * {@link Future#get()} does not indicate an exception, the parser + * implementation SHOULD have inserted all parsed triples to the specified + * graph. + *

+ * Calling this method will override any earlier targets set with + * {@link #target(Graph)}, {@link #target(Consumer)} or + * {@link #target(Dataset)}. + *

+ * The default implementation of this method calls {@link #target(Consumer)} + * with a {@link Consumer} that does {@link Graph#add(Triple)} with + * {@link Quad#asTriple()} if the quad is in the default graph. * * @param graph - * The {@link Graph} to add triples into. + * The {@link Graph} to add triples to. * @return An {@link RDFParserBuilder} that will insert triples into the * specified graph. */ - RDFParserBuilder intoGraph(Graph graph); + default RDFParserBuilder target(Graph graph) { + return target(q -> { + if (! q.getGraphName().isPresent()) { + graph.add(q.asTriple()); + } + }); + } /** + * Specify a {@link Dataset} to add parsed quads to. + *

+ * It is undefined if any quads are added to the specified + * {@link Dataset} if {@link #parse()} throws any exceptions. + * (However implementations are free to prevent this using transaction + * mechanisms or similar). On the other hand, if {@link #parse()} + * does not indicate an exception, the + * implementation SHOULD have inserted all parsed quads + * to the specified dataset. + *

+ * Calling this method will override any earlier targets set with + * {@link #target(Graph)}, {@link #target(Consumer)} or {@link #target(Dataset)}. + *

+ * The default implementation of this method calls {@link #target(Consumer)} + * with a {@link Consumer} that does {@link Dataset#add(Quad)}. + * + * @param dataset + * The {@link Dataset} to add quads to. + * @return An {@link RDFParserBuilder} that will insert triples into the + * specified dataset. + */ + default RDFParserBuilder target(Dataset dataset) { + return target(dataset::add); + } + + /** + * Specify a consumer for parsed quads. + *

+ * It is undefined if any quads are consumed if {@link #parse()} throws any + * exceptions. On the other hand, if {@link #parse()} does not indicate an + * exception, the implementation SHOULD have produced all parsed quads to + * the specified consumer. + *

+ * Calling this method will override any earlier targets set with + * {@link #target(Graph)}, {@link #target(Consumer)} or + * {@link #target(Dataset)}. + *

+ * The consumer is not assumed to be thread safe - only one + * {@link Consumer#accept(Object)} is delivered at a time for a given + * {@link RDFParserBuilder#parse()} call. + *

+ * This method is typically called with a functional consumer, for example: + *

+	 * List quads = new ArrayList;
+	 * parserBuilder.target(quads::add).parse();
+	 * 
+ * + * @param consumer + * A {@link Consumer} of {@link Quad}s + * @return An {@link RDFParserBuilder} that will call the consumer for into + * the specified dataset. + * @return + */ + RDFParserBuilder target(Consumer consumer); + + /** * Specify a base IRI to use for parsing any relative IRI references. *

* Setting this option will override any protocol-specific base IRI (e.g. @@ -345,6 +425,11 @@ public interface RDFParserBuilder { * method) MUST have been called before calling this method, otherwise an * {@link IllegalStateException} will be thrown. *

+ * A target method (e.g. {@link #target(Consumer)}, {@link #target(Dataset)} + * , {@link #target(Graph)} or an equivalent subclass method) MUST have been + * called before calling this method, otherwise an + * {@link IllegalStateException} will be thrown. + *

* It is undefined if this method is thread-safe, however the * {@link RDFParserBuilder} may be reused (e.g. setting a different source) * as soon as the {@link Future} has been returned from this method. @@ -357,12 +442,15 @@ public interface RDFParserBuilder { * synchronous implementation MAY be blocking on the parse() * call and return a Future that is already {@link Future#isDone()}. *

- * If {@link #intoGraph(Graph)} has been specified, this SHOULD be the same - * {@link Graph} instance returned from {@link Future#get()} once parsing has - * completed successfully. + * The returned {@link Future} contains a {@link ParseResult}. + * Implementations may subclass this interface to provide any + * parser details, e.g. list of warnings. null is a + * possible return value if no details are available, but + * parsing succeeded. *

* If an exception occurs during parsing, (e.g. {@link IOException} or - * {@link java.text.ParseException}), it should be indicated as the + * {@link org.apache.commons.rdf.simple.AbstractRDFParserBuilder.RDFParseException}), + * it should be indicated as the * {@link java.util.concurrent.ExecutionException#getCause()} in the * {@link java.util.concurrent.ExecutionException} thrown on * {@link Future#get()}. @@ -380,5 +468,5 @@ public interface RDFParserBuilder { * If the builder is in an invalid state, e.g. a * source has not been set. */ - Future parse() throws IOException, IllegalStateException; + Future parse() throws IOException, IllegalStateException; } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/defdbd91/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java b/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java index ab37eef..9544928 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java @@ -26,9 +26,10 @@ import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.function.Consumer; -import org.apache.commons.rdf.api.Graph; import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.Quad; import org.apache.commons.rdf.api.RDFParserBuilder; import org.apache.commons.rdf.api.RDFSyntax; import org.apache.commons.rdf.api.RDFTermFactory; @@ -111,15 +112,14 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon } /** - * Get the set {@link Graph} to insert into, if any. + * Get the target to consume parsed Quads. *

* From the call to {@link #parseSynchronusly()}, this - * method is always {@link Optional#isPresent()} - * with a new {@link Graph} instance, and - * will be the value returned from {@link #parse()}. + * method is always {@link Optional#isPresent()}. + * */ - public Optional getIntoGraph() { - return intoGraph; + public Consumer getTarget() { + return target; } /** @@ -167,11 +167,11 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon private Optional rdfTermFactory = Optional.empty(); private Optional contentTypeSyntax = Optional.empty(); private Optional contentType = Optional.empty(); - private Optional intoGraph = Optional.empty(); private Optional base = Optional.empty(); private Optional sourceInputStream = Optional.empty(); private Optional sourceFile = Optional.empty(); private Optional sourceIri = Optional.empty(); + private Consumer target; @Override public AbstractRDFParserBuilder clone() { @@ -206,13 +206,6 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon } @Override - public RDFParserBuilder intoGraph(Graph graph) { - AbstractRDFParserBuilder c = clone(); - c.intoGraph = Optional.ofNullable(graph); - return c; - } - - @Override public RDFParserBuilder base(IRI base) { AbstractRDFParserBuilder c = clone(); c.base = Optional.ofNullable(base); @@ -361,8 +354,10 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon */ protected AbstractRDFParserBuilder prepareForParsing() throws IOException, IllegalStateException { checkSource(); - checkBaseRequired(); + checkBaseRequired(); checkContentType(); + checkTarget(); + // We'll make a clone of our current state which will be passed to // parseSynchronously() AbstractRDFParserBuilder c = clone(); @@ -371,10 +366,6 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon if (!c.rdfTermFactory.isPresent()) { c.rdfTermFactory = Optional.of(createRDFTermFactory()); } - // No graph? We'll create one. - if (!c.intoGraph.isPresent()) { - c.intoGraph = c.rdfTermFactory.map(RDFTermFactory::createGraph); - } // sourceFile, but no base? Let's follow any symlinks and use // the file:/// URI if (c.sourceFile.isPresent() && !c.base.isPresent()) { @@ -386,6 +377,19 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon } /** + * Subclasses can override this method to check the target is + * valid. + *

+ * The default implementation throws an IllegalStateException if the + * target has not been set. + */ + protected void checkTarget() { + if (target == null) { + throw new IllegalStateException("target has not been set"); + } + } + + /** * Subclasses can override this method to check compatibility with the * contentType setting. * @@ -441,13 +445,11 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon *

* This is called by {@link #parse()} to set * {@link #rdfTermFactory(RDFTermFactory)} if it is - * {@link Optional#empty()}, and therefore used also for - * creating a new {@link Graph} if - * {@link #getIntoGraph()} is {@link Optional#empty()}. + * {@link Optional#empty()}. *

* As parsed blank nodes might be made with * {@link RDFTermFactory#createBlankNode(String)}, - * each call to this method should return + * each call to this method SHOULD return * a new RDFTermFactory instance. * * @return A new {@link RDFTermFactory} @@ -457,12 +459,19 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon } @Override - public Future parse() throws IOException, IllegalStateException { + public Future parse() throws IOException, IllegalStateException { final AbstractRDFParserBuilder c = prepareForParsing(); return threadpool.submit(() -> { c.parseSynchronusly(); - return c.intoGraph.get(); + return null; }); } + @Override + public RDFParserBuilder target(Consumer consumer) { + AbstractRDFParserBuilder c = clone(); + c.target = consumer; + return c; + } + } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/defdbd91/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java ---------------------------------------------------------------------- diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java b/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java index d23e1c8..439bacb 100644 --- a/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java +++ b/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java @@ -17,7 +17,10 @@ */ package org.apache.commons.rdf.simple; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -102,9 +105,10 @@ public class AbstractRDFParserBuilderTest { } @Test - public void parseFile() throws Exception { - RDFParserBuilder parser = dummyParser.source(testNt); - Graph g = parser.parse().get(5, TimeUnit.SECONDS); + public void parseFile() throws Exception { + Graph g = factory.createGraph(); + RDFParserBuilder parser = dummyParser.source(testNt).target(g); + parser.parse().get(5, TimeUnit.SECONDS); checkGraph(g); // FIXME: this could potentially break if the equivalent of /tmp includes // international characters @@ -148,8 +152,12 @@ public class AbstractRDFParserBuilderTest { @Test public void parseFileContentType() throws Exception { - RDFParserBuilder parser = dummyParser.source(testNt).contentType(RDFSyntax.NTRIPLES); - Graph g = parser.parse().get(5, TimeUnit.SECONDS); + Graph g = factory.createGraph(); + RDFParserBuilder parser = dummyParser + .source(testNt) + .contentType(RDFSyntax.NTRIPLES) + .target(g); + parser.parse().get(5, TimeUnit.SECONDS); checkGraph(g); // FIXME: this could potentially break if the equivalent of /tmp includes // international characters @@ -183,8 +191,9 @@ public class AbstractRDFParserBuilderTest { public void parseInputStreamWithBase() throws Exception { InputStream inputStream = new ByteArrayInputStream(new byte[0]); IRI base = dummyParser.createRDFTermFactory().createIRI("http://www.example.org/test.rdf"); - RDFParserBuilder parser = dummyParser.source(inputStream).base(base); - Graph g = parser.parse().get(5, TimeUnit.SECONDS); + Graph g = factory.createGraph(); + RDFParserBuilder parser = dummyParser.source(inputStream).base(base).target(g); + parser.parse().get(5, TimeUnit.SECONDS); checkGraph(g); assertEquals("", firstPredicate(g, "base")); // in our particular debug output, @@ -197,8 +206,9 @@ public class AbstractRDFParserBuilderTest { @Test public void parseInputStreamWithNQuads() throws Exception { InputStream inputStream = new ByteArrayInputStream(new byte[0]); - RDFParserBuilder parser = dummyParser.source(inputStream).contentType(RDFSyntax.NQUADS); - Graph g = parser.parse().get(5, TimeUnit.SECONDS); + Graph g = factory.createGraph(); + RDFParserBuilder parser = dummyParser.source(inputStream).contentType(RDFSyntax.NQUADS).target(g); + parser.parse().get(5, TimeUnit.SECONDS); checkGraph(g); assertNull(firstPredicate(g, "base")); // in our particular debug output, @@ -211,8 +221,9 @@ public class AbstractRDFParserBuilderTest { @Test public void parseIRI() throws Exception { IRI iri = dummyParser.createRDFTermFactory().createIRI("http://www.example.net/test.ttl"); - RDFParserBuilder parser = dummyParser.source(iri); - Graph g = parser.parse().get(5, TimeUnit.SECONDS); + Graph g = factory.createGraph(); + RDFParserBuilder parser = dummyParser.source(iri).target(g); + parser.parse().get(5, TimeUnit.SECONDS); checkGraph(g); assertEquals("", firstPredicate(g, "source")); // No base - assuming the above IRI is always @@ -228,8 +239,9 @@ public class AbstractRDFParserBuilderTest { @Test public void parseIRIBaseContentType() throws Exception { IRI iri = dummyParser.createRDFTermFactory().createIRI("http://www.example.net/test.ttl"); - RDFParserBuilder parser = dummyParser.source(iri).base(iri).contentType(RDFSyntax.TURTLE); - Graph g = parser.parse().get(5, TimeUnit.SECONDS); + Graph g = factory.createGraph(); + RDFParserBuilder parser = dummyParser.source(iri).base(iri).contentType(RDFSyntax.TURTLE).target(g); + parser.parse().get(5, TimeUnit.SECONDS); checkGraph(g); assertEquals("", firstPredicate(g, "source")); assertEquals("", firstPredicate(g, "base")); http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/defdbd91/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java ---------------------------------------------------------------------- diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java index cc62f0c..0cb7093 100644 --- a/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java +++ b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java @@ -18,11 +18,11 @@ package org.apache.commons.rdf.simple; import java.io.IOException; -import java.text.ParseException; import java.util.UUID; +import java.util.function.Consumer; -import org.apache.commons.rdf.api.Graph; import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.Quad; import org.apache.commons.rdf.api.RDFParserBuilder; import org.apache.commons.rdf.api.RDFTermFactory; @@ -49,47 +49,47 @@ public class DummyRDFParserBuilder extends AbstractRDFParserBuilder { protected void parseSynchronusly() throws IOException, IllegalStateException, RDFParseException { // From parseSynchronusly both of these are always present RDFTermFactory factory = getRdfTermFactory().get(); - Graph graph = getIntoGraph().get(); + Consumer t = getTarget(); // well - each parsing is unique. This should hopefully // catch any accidental double parsing IRI parsing = factory.createIRI("urn:uuid:" + UUID.randomUUID()); - graph.add(parsing, factory.createIRI("http://example.com/greeting"), - factory.createLiteral("Hello world")); + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/greeting"), + factory.createLiteral("Hello world"))); // Now we'll expose the finalized AbstractRDFParserBuilder settings // so they can be inspected by the junit test if (getSourceIri().isPresent()) { - graph.add(parsing, + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/source"), - getSourceIri().get()); + getSourceIri().get())); } if (getSourceFile().isPresent()) { - graph.add(parsing, + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/source"), - factory.createIRI(getSourceFile().get().toUri().toString())); + factory.createIRI(getSourceFile().get().toUri().toString()))); } if (getSourceInputStream().isPresent()) { - graph.add(parsing, + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/source"), - factory.createBlankNode()); + factory.createBlankNode())); } if (getBase().isPresent()) { - graph.add(parsing, + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/base"), - getBase().get()); + getBase().get())); } if (getContentType().isPresent()) { - graph.add(parsing, + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/contentType"), - factory.createLiteral(getContentType().get())); + factory.createLiteral(getContentType().get()))); } if (getContentTypeSyntax().isPresent()) { - graph.add(parsing, + t.accept(factory.createQuad(null, parsing, factory.createIRI("http://example.com/contentTypeSyntax"), - factory.createLiteral(getContentTypeSyntax().get().name())); + factory.createLiteral(getContentTypeSyntax().get().name()))); } }