You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commonsrdf.apache.org by st...@apache.org on 2016/09/09 15:28:48 UTC
[01/11] incubator-commonsrdf git commit: Merge branch 'quad' into
parser-with-quads
Repository: incubator-commonsrdf
Updated Branches:
refs/heads/jsonld-java ddbe6bfb0 -> 3a8cb5635
Merge branch 'quad' into parser-with-quads
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/bb90a6ea
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/bb90a6ea
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/bb90a6ea
Branch: refs/heads/jsonld-java
Commit: bb90a6ea22374d49c4e6d38e07ff98e0c8803b7b
Parents: d153b4c 839a642
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Wed Apr 13 15:35:01 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Wed Apr 13 15:35:01 2016 +0100
----------------------------------------------------------------------
.../org/apache/commons/rdf/api/Dataset.java | 338 +++++++++++++++++++
.../java/org/apache/commons/rdf/api/Quad.java | 207 ++++++++++++
.../apache/commons/rdf/api/RDFTermFactory.java | 40 +++
.../java/org/apache/commons/rdf/api/Triple.java | 2 +-
.../apache/commons/rdf/api/TripleOrQuad.java | 64 ++++
.../commons/rdf/simple/BlankNodeImpl.java | 3 +-
.../apache/commons/rdf/simple/DatasetImpl.java | 226 +++++++++++++
.../apache/commons/rdf/simple/GraphImpl.java | 18 +-
.../org/apache/commons/rdf/simple/IRIImpl.java | 2 +-
.../apache/commons/rdf/simple/LiteralImpl.java | 2 +-
.../org/apache/commons/rdf/simple/QuadImpl.java | 109 ++++++
.../rdf/simple/SimpleRDFTermFactory.java | 25 ++
.../org/apache/commons/rdf/simple/Types.java | 2 +-
13 files changed, 1026 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
[09/11] incubator-commonsrdf git commit: Merge branch 'quad' into
parser-with-quads
Posted by st...@apache.org.
Merge branch 'quad' into parser-with-quads
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/c84a9ea2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/c84a9ea2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/c84a9ea2
Branch: refs/heads/jsonld-java
Commit: c84a9ea2481719cee0c120879263b837eb1a1c12
Parents: 0a5be9d a1d6554
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Thu Jul 7 11:28:09 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Thu Jul 7 11:28:09 2016 +0100
----------------------------------------------------------------------
.../org/apache/commons/rdf/api/GraphLike.java | 21 +++++++++++++++---
.../java/org/apache/commons/rdf/api/Quad.java | 23 +++++++++++---------
.../org/apache/commons/rdf/api/QuadLike.java | 18 +++++++++++----
.../apache/commons/rdf/api/RDFTermFactory.java | 2 +-
.../org/apache/commons/rdf/api/TripleLike.java | 14 +++++++++---
.../commons/rdf/simple/DatasetGraphView.java | 2 +-
6 files changed, 58 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
[07/11] incubator-commonsrdf git commit: Only Quad consumer,
not Triple consumer
Posted by st...@apache.org.
Only Quad consumer, not Triple consumer
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/ddc545e5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/ddc545e5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/ddc545e5
Branch: refs/heads/jsonld-java
Commit: ddc545e511ef9c8459ea5ce81fab750b4cd0a3b0
Parents: 953e9b8
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Jun 20 09:56:51 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Jun 20 09:56:51 2016 +0100
----------------------------------------------------------------------
api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java | 2 --
1 file changed, 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ddc545e5/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 5e8ee33..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
@@ -191,8 +191,6 @@ public interface RDFParserBuilder {
});
}
- RDFParserBuilder target(Consumer<Triple> tripleConsumer);
-
/**
* Specify a {@link Dataset} to add parsed quads to.
* <p>
[11/11] incubator-commonsrdf git commit: Merge branch
'parser-with-quads' into jsonld-java
Posted by st...@apache.org.
Merge branch 'parser-with-quads' into jsonld-java
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/3a8cb563
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/3a8cb563
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/3a8cb563
Branch: refs/heads/jsonld-java
Commit: 3a8cb5635e0aad45ae03ebb221fd777953b1d91a
Parents: ddbe6bf b1b657f
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Fri Sep 9 16:28:41 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Fri Sep 9 16:28:41 2016 +0100
----------------------------------------------------------------------
.../commons/rdf/api/RDFParserBuilder.java | 174 ++++++++++----
.../rdf/simple/AbstractRDFParserBuilder.java | 230 +++++++++++++------
.../commons/rdf/simple/RDFParseException.java | 50 ++++
.../simple/AbstractRDFParserBuilderTest.java | 40 ++--
.../rdf/simple/DummyRDFParserBuilder.java | 36 +--
5 files changed, 379 insertions(+), 151 deletions(-)
----------------------------------------------------------------------
[02/11] incubator-commonsrdf git commit: Quad-support for
RDFParserBuildler
Posted by st...@apache.org.
Quad-support for RDFParserBuildler
This changes intoGraph() to target(?)
where ? can be a Graph, Dataset or Consumer<Quad>
similarly parse() returns a Future<ParseResult> rather than
Future<Graph> - 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/jsonld-java
Commit: defdbd9160561ae93e8558a8f2c6cc27507b22bb
Parents: bb90a6e
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Wed Apr 13 16:50:31 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
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).
* <p>
* This interface follows the
* <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder pattern</a>,
* allowing to set parser settings like {@link #contentType(RDFSyntax)} and
* {@link #base(IRI)}. A caller MUST call one of the <code>source</code> 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 <code>target</code>
+ * 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.
* <p>
- * 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.
* <p>
* 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 <code>null</code> - this may require casting, e.g.
- * <code>contentType( (RDFSyntax) null )</code> to undo a previous call to
- * {@link #contentType(RDFSyntax)}.
+ * can be unset by passing <code>null</code> - note that this may
+ * require casting, e.g. <code>contentType( (RDFSyntax) null )</code>
+ * to undo a previous call to {@link #contentType(RDFSyntax)}.
* <p>
* 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}.
* <p>
* Example usage:
* </p>
*
* <pre>
- * // 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);
* </pre>
- *
*
*/
public interface RDFParserBuilder {
+ /**
+ * The result of {@link RDFParserBuilder#parse()} indicating
+ * parsing completed.
+ * <p>
+ * 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.
* <p>
- * 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.
* <p>
* <strong>Warning:</strong> 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.
- * <p>
- * 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.
- * <p>
- * 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.
+ * <p>
+ * If the source supports datasets (e.g. the {@link #contentType(RDFSyntax)}
+ * set has {@link RDFSyntax#supportsDataset} is true)), then only quads in
+ * the <em>default graph</em> will be added to the Graph as {@link Triple}s.
+ * <p>
+ * 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.
+ * <p>
+ * Calling this method will override any earlier targets set with
+ * {@link #target(Graph)}, {@link #target(Consumer)} or
+ * {@link #target(Dataset)}.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * Calling this method will override any earlier targets set with
+ * {@link #target(Graph)}, {@link #target(Consumer)} or {@link #target(Dataset)}.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * Calling this method will override any earlier targets set with
+ * {@link #target(Graph)}, {@link #target(Consumer)} or
+ * {@link #target(Dataset)}.
+ * <p>
+ * 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.
+ * <p>
+ * This method is typically called with a functional consumer, for example:
+ * <pre>
+ * List<Quad> quads = new ArrayList<Quad>;
+ * parserBuilder.target(quads::add).parse();
+ * </pre>
+ *
+ * @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<Quad> consumer);
+
+ /**
* Specify a base IRI to use for parsing any relative IRI references.
* <p>
* 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.
* <p>
+ * 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.
+ * <p>
* 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 <code>parse()</code>
* call and return a Future that is already {@link Future#isDone()}.
* <p>
- * 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. <code>null</code> is a
+ * possible return value if no details are available, but
+ * parsing succeeded.
* <p>
* 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
* <code>source</code> has not been set.
*/
- Future<Graph> parse() throws IOException, IllegalStateException;
+ Future<? extends ParseResult> 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.
* <p>
* 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<Graph> getIntoGraph() {
- return intoGraph;
+ public Consumer<Quad> getTarget() {
+ return target;
}
/**
@@ -167,11 +167,11 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
private Optional<RDFTermFactory> rdfTermFactory = Optional.empty();
private Optional<RDFSyntax> contentTypeSyntax = Optional.empty();
private Optional<String> contentType = Optional.empty();
- private Optional<Graph> intoGraph = Optional.empty();
private Optional<IRI> base = Optional.empty();
private Optional<InputStream> sourceInputStream = Optional.empty();
private Optional<Path> sourceFile = Optional.empty();
private Optional<IRI> sourceIri = Optional.empty();
+ private Consumer<Quad> 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.
+ * <p>
+ * 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
* <p>
* 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()}.
* <p>
* 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<Graph> parse() throws IOException, IllegalStateException {
+ public Future<ParseResult> parse() throws IOException, IllegalStateException {
final AbstractRDFParserBuilder c = prepareForParsing();
return threadpool.submit(() -> {
c.parseSynchronusly();
- return c.intoGraph.get();
+ return null;
});
}
+ @Override
+ public RDFParserBuilder target(Consumer<Quad> 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("<http://www.example.org/test.rdf>", 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("<http://www.example.net/test.ttl>", 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("<http://www.example.net/test.ttl>", firstPredicate(g, "source"));
assertEquals("<http://www.example.net/test.ttl>", 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<Quad> 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())));
}
}
[08/11] incubator-commonsrdf git commit: AbstractRDFParserBuilder
generic
Posted by st...@apache.org.
AbstractRDFParserBuilder generic
so that the build methods can return the correct subtype
of itself
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/0a5be9db
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/0a5be9db
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/0a5be9db
Branch: refs/heads/jsonld-java
Commit: 0a5be9dbb9634637e19aa3aaeb56e6865b702577
Parents: ddc545e
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Jul 4 12:07:01 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Jul 4 12:07:01 2016 +0100
----------------------------------------------------------------------
.../rdf/simple/AbstractRDFParserBuilder.java | 111 +++++++++----------
.../commons/rdf/simple/RDFParseException.java | 50 +++++++++
.../rdf/simple/DummyRDFParserBuilder.java | 2 +-
3 files changed, 102 insertions(+), 61 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a5be9db/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 cc5fad0..4a5b4d1 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
@@ -33,6 +33,7 @@ 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.RDFParserBuilder.ParseResult;
import org.apache.commons.rdf.api.RDFSyntax;
import org.apache.commons.rdf.api.RDFTermFactory;
@@ -53,28 +54,10 @@ import org.apache.commons.rdf.api.RDFTermFactory;
* multiple {@link #parse()} calls are thread-safe. The default {@link #parse()}
* uses a thread pool in {@link #threadGroup} - but implementations can override
* {@link #parse()} (e.g. because it has its own threading model or use
- * asynchronou remote execution).
+ * asynchronous remote execution).
*/
-public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Cloneable {
-
- public class RDFParseException extends Exception {
- private static final long serialVersionUID = 5427752643780702976L;
- public RDFParseException() {
- super();
- }
- public RDFParseException(String message, Throwable cause) {
- super(message, cause);
- }
- public RDFParseException(String message) {
- super(message);
- }
- public RDFParseException(Throwable cause) {
- super(cause);
- }
- public RDFParserBuilder getRDFParserBuilder() {
- return AbstractRDFParserBuilder.this;
- }
- }
+public abstract class AbstractRDFParserBuilder<T extends AbstractRDFParserBuilder<T>>
+ implements RDFParserBuilder, Cloneable {
public static final ThreadGroup threadGroup = new ThreadGroup("Commons RDF parsers");
private static final ExecutorService threadpool = Executors.newCachedThreadPool(r -> new Thread(threadGroup, r));
@@ -212,79 +195,85 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
private Optional<Dataset> targetDataset;
private Optional<Graph> targetGraph;
+ @SuppressWarnings("unchecked")
@Override
- public AbstractRDFParserBuilder clone() {
+ public T clone() {
try {
- return (AbstractRDFParserBuilder) super.clone();
+ return (T) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
+ @SuppressWarnings("unchecked")
+ protected T asT() {
+ return (T) this;
+ }
+
@Override
- public RDFParserBuilder rdfTermFactory(RDFTermFactory rdfTermFactory) {
- AbstractRDFParserBuilder c = clone();
+ public T rdfTermFactory(RDFTermFactory rdfTermFactory) {
+ AbstractRDFParserBuilder<T> c = clone();
c.rdfTermFactory = Optional.ofNullable(rdfTermFactory);
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException {
- AbstractRDFParserBuilder c = clone();
+ public T contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException {
+ AbstractRDFParserBuilder<T> c = clone();
c.contentTypeSyntax = Optional.ofNullable(rdfSyntax);
c.contentType = c.contentTypeSyntax.map(syntax -> syntax.mediaType);
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder contentType(String contentType) throws IllegalArgumentException {
- AbstractRDFParserBuilder c = clone();
+ public T contentType(String contentType) throws IllegalArgumentException {
+ AbstractRDFParserBuilder<T> c = clone();
c.contentType = Optional.ofNullable(contentType);
c.contentTypeSyntax = c.contentType.flatMap(RDFSyntax::byMediaType);
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder base(IRI base) {
- AbstractRDFParserBuilder c = clone();
+ public T base(IRI base) {
+ AbstractRDFParserBuilder<T> c = clone();
c.base = Optional.ofNullable(base);
c.base.ifPresent(i -> checkIsAbsolute(i));
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder base(String base) throws IllegalArgumentException {
+ public T base(String base) throws IllegalArgumentException {
return base(internalRdfTermFactory.createIRI(base));
}
@Override
- public RDFParserBuilder source(InputStream inputStream) {
- AbstractRDFParserBuilder c = clone();
+ public T source(InputStream inputStream) {
+ AbstractRDFParserBuilder<T> c = clone();
c.resetSource();
c.sourceInputStream = Optional.ofNullable(inputStream);
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder source(Path file) {
- AbstractRDFParserBuilder c = clone();
+ public T source(Path file) {
+ AbstractRDFParserBuilder<T> c = clone();
c.resetSource();
c.sourceFile = Optional.ofNullable(file);
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder source(IRI iri) {
- AbstractRDFParserBuilder c = clone();
+ public T source(IRI iri) {
+ AbstractRDFParserBuilder<T> c = clone();
c.resetSource();
c.sourceIri = Optional.ofNullable(iri);
c.sourceIri.ifPresent(i -> checkIsAbsolute(i));
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder source(String iri) throws IllegalArgumentException {
- AbstractRDFParserBuilder c = clone();
+ public T source(String iri) throws IllegalArgumentException {
+ AbstractRDFParserBuilder<T> c = clone();
c.resetSource();
c.sourceIri = Optional.ofNullable(iri).map(internalRdfTermFactory::createIRI);
c.sourceIri.ifPresent(i -> checkIsAbsolute(i));
@@ -407,7 +396,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
* @throws IOException If the source was not accessible (e.g. a file was not found)
* @throws IllegalStateException If the parser was not in a compatible setting (e.g. contentType was an invalid string)
*/
- protected AbstractRDFParserBuilder prepareForParsing() throws IOException, IllegalStateException {
+ protected T prepareForParsing() throws IOException, IllegalStateException {
checkSource();
checkBaseRequired();
checkContentType();
@@ -415,7 +404,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
// We'll make a clone of our current state which will be passed to
// parseSynchronously()
- AbstractRDFParserBuilder c = clone();
+ AbstractRDFParserBuilder<T> c = clone();
// Use a fresh SimpleRDFTermFactory for each parse
if (!c.rdfTermFactory.isPresent()) {
@@ -428,7 +417,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
c.base = Optional.of(internalRdfTermFactory.createIRI(baseUri.toString()));
}
- return c;
+ return c.asT();
}
/**
@@ -519,7 +508,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
@Override
public Future<ParseResult> parse() throws IOException, IllegalStateException {
- final AbstractRDFParserBuilder c = prepareForParsing();
+ final AbstractRDFParserBuilder<T> c = prepareForParsing();
return threadpool.submit(() -> {
c.parseSynchronusly();
return null;
@@ -527,27 +516,29 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
}
@Override
- public RDFParserBuilder target(Consumer<Quad> consumer) {
- AbstractRDFParserBuilder c = clone();
+ public T target(Consumer<Quad> consumer) {
+ AbstractRDFParserBuilder<T> c = clone();
c.resetTarget();
c.target = consumer;
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder target(Dataset dataset) {
- AbstractRDFParserBuilder c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(dataset);
+ public T target(Dataset dataset) {
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ AbstractRDFParserBuilder<T> c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(dataset);
c.resetTarget();
c.targetDataset = Optional.of(dataset);
- return c;
+ return c.asT();
}
@Override
- public RDFParserBuilder target(Graph graph) {
- AbstractRDFParserBuilder c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(graph);
+ public T target(Graph graph) {
+ @SuppressWarnings({ "rawtypes", "unchecked" }) // super calls our .clone()
+ AbstractRDFParserBuilder<T> c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(graph);
c.resetTarget();
c.targetGraph = Optional.of(graph);
- return c;
+ return c.asT();
}
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a5be9db/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java b/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java
new file mode 100644
index 0000000..ed16bb2
--- /dev/null
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/RDFParseException.java
@@ -0,0 +1,50 @@
+/**
+ * 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.simple;
+
+import org.apache.commons.rdf.api.RDFParserBuilder;
+
+public class RDFParseException extends Exception {
+ private static final long serialVersionUID = 5427752643780702976L;
+ private RDFParserBuilder builder;
+
+ public RDFParseException(RDFParserBuilder builder) {
+ super();
+ this.builder = builder;
+ }
+
+ public RDFParseException(RDFParserBuilder builder, String message, Throwable cause) {
+ super(message, cause);
+ this.builder = builder;
+ }
+
+ public RDFParseException(RDFParserBuilder builder, String message) {
+ super(message);
+ this.builder = builder;
+ }
+
+ public RDFParseException(RDFParserBuilder builder, Throwable cause) {
+ super(cause);
+ this.builder = builder;
+ }
+
+ public RDFParserBuilder getRDFParserBuilder() {
+ return builder;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/0a5be9db/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 0cb7093..baf9768 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
@@ -43,7 +43,7 @@ import org.apache.commons.rdf.api.RDFTermFactory;
*
*
*/
-public class DummyRDFParserBuilder extends AbstractRDFParserBuilder {
+public class DummyRDFParserBuilder extends AbstractRDFParserBuilder<DummyRDFParserBuilder> {
@Override
protected void parseSynchronusly() throws IOException, IllegalStateException, RDFParseException {
[06/11] incubator-commonsrdf git commit: Merge branch
'parser-writer-interface' into parser-with-quads
Posted by st...@apache.org.
Merge branch 'parser-writer-interface' into parser-with-quads
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/953e9b89
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/953e9b89
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/953e9b89
Branch: refs/heads/jsonld-java
Commit: 953e9b8915fd7d42f8beac6fac8cabc39d5351e1
Parents: 7c69d4f c0700de
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Jun 20 09:52:59 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Jun 20 09:52:59 2016 +0100
----------------------------------------------------------------------
api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java | 2 ++
1 file changed, 2 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/953e9b89/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java
----------------------------------------------------------------------
diff --cc api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java
index 0494502,b387406..5e8ee33
--- a/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/RDFParserBuilder.java
@@@ -183,71 -166,13 +183,73 @@@ public interface RDFParserBuilder
* @return An {@link RDFParserBuilder} that will insert triples into the
* specified graph.
*/
- default RDFParserBuilder target(Graph graph) {
- return target(graph::add);
+ default RDFParserBuilder target(Graph graph) {
+ return target(q -> {
+ if (! q.getGraphName().isPresent()) {
+ graph.add(q.asTriple());
+ }
+ });
}
+ RDFParserBuilder target(Consumer<Triple> tripleConsumer);
+
/**
+ * Specify a {@link Dataset} to add parsed quads to.
+ * <p>
+ * 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.
+ * <p>
+ * Calling this method will override any earlier targets set with
+ * {@link #target(Graph)}, {@link #target(Consumer)} or {@link #target(Dataset)}.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * Calling this method will override any earlier targets set with
+ * {@link #target(Graph)}, {@link #target(Consumer)} or
+ * {@link #target(Dataset)}.
+ * <p>
+ * 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.
+ * <p>
+ * This method is typically called with a functional consumer, for example:
+ * <pre>
+ * List<Quad> quads = new ArrayList<Quad>;
+ * parserBuilder.target(quads::add).parse();
+ * </pre>
+ *
+ * @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<Quad> consumer);
+
+ /**
* Specify a base IRI to use for parsing any relative IRI references.
* <p>
* Setting this option will override any protocol-specific base IRI (e.g.
[10/11] incubator-commonsrdf git commit: Merge branch 'master' into
parser-with-quads
Posted by st...@apache.org.
Merge branch 'master' into parser-with-quads
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/b1b657f6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/b1b657f6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/b1b657f6
Branch: refs/heads/jsonld-java
Commit: b1b657f67b2bb3aa65afc32916d6f03b593053bd
Parents: c84a9ea 9782a58
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Fri Sep 9 16:28:11 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Fri Sep 9 16:28:11 2016 +0100
----------------------------------------------------------------------
LICENSE-header.txt | 16 +
RELEASE-PROCESS.md | 20 ++
api/pom.xml | 5 +-
.../org/apache/commons/rdf/api/GraphLike.java | 6 +-
.../java/org/apache/commons/rdf/api/Quad.java | 2 +
.../org/apache/commons/rdf/api/QuadLike.java | 6 +-
.../apache/commons/rdf/api/RDFTermFactory.java | 1 +
.../java/org/apache/commons/rdf/api/Triple.java | 2 +
.../org/apache/commons/rdf/api/TripleLike.java | 6 +-
api/src/site/resources/profile.jacoco | 0
api/src/site/resources/profile.japicmp | 0
pom.xml | 307 ++++++++++++-------
simple/src/site/resources/profile.jacoco | 0
simple/src/site/resources/profile.japicmp | 0
src/conf/checkstyle-suppressions.xml | 32 ++
src/conf/checkstyle.xml | 197 ++++++++++++
src/conf/findbugs-exclude-filter.xml | 27 ++
src/conf/pmd.xml | 25 ++
src/site/markdown/implementations.md | 63 +++-
src/site/markdown/index.md | 58 +++-
src/site/resources/images/class-diagram.nomnoml | 49 ++-
src/site/resources/images/class-diagram.png | Bin 57632 -> 56161 bytes
src/site/site.xml | 3 +-
23 files changed, 664 insertions(+), 161 deletions(-)
----------------------------------------------------------------------
[04/11] incubator-commonsrdf git commit: added
getTargetDataset()/getTargetGraph()
Posted by st...@apache.org.
added getTargetDataset()/getTargetGraph()
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/7c69d4fd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/7c69d4fd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/7c69d4fd
Branch: refs/heads/jsonld-java
Commit: 7c69d4fdc1511fe9cbad1cf4abc00d661d2a7871
Parents: 036029b
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Fri Jun 17 21:18:47 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Fri Jun 17 21:18:47 2016 +0100
----------------------------------------------------------------------
.../rdf/simple/AbstractRDFParserBuilder.java | 78 ++++++++++++++++++++
1 file changed, 78 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/7c69d4fd/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 9544928..cc5fad0 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
@@ -28,6 +28,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
+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.Quad;
import org.apache.commons.rdf.api.RDFParserBuilder;
@@ -123,6 +125,40 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
}
/**
+ * Get the target dataset as set by {@link #target(Dataset)}.
+ * <p>
+ * The return value is {@link Optional#isPresent()} if and only if
+ * {@link #target(Dataset)} has been set, meaning that the implementation
+ * may choose to append parsed quads to the {@link Dataset} directly instead
+ * of relying on the generated {@link #getTarget()} consumer.
+ * <p>
+ * If this value is present, then {@link #getTargetGraph()} MUST
+ * be {@link Optional#empty()}.
+ *
+ * @return The target Dataset, or {@link Optional#empty()} if another kind of target has been set.
+ */
+ public Optional<Dataset> getTargetDataset() {
+ return targetDataset;
+ }
+
+ /**
+ * Get the target graph as set by {@link #target(Graph)}.
+ * <p>
+ * The return value is {@link Optional#isPresent()} if and only if
+ * {@link #target(Graph)} has been set, meaning that the implementation
+ * may choose to append parsed triples to the {@link Graph} directly instead
+ * of relying on the generated {@link #getTarget()} consumer.
+ * <p>
+ * If this value is present, then {@link #getTargetDataset()} MUST
+ * be {@link Optional#empty()}.
+ *
+ * @return The target Graph, or {@link Optional#empty()} if another kind of target has been set.
+ */
+ public Optional<Graph> getTargetGraph() {
+ return targetGraph;
+ }
+
+ /**
* Get the set base {@link IRI}, if present.
* <p>
*
@@ -164,6 +200,7 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
return sourceIri;
}
+
private Optional<RDFTermFactory> rdfTermFactory = Optional.empty();
private Optional<RDFSyntax> contentTypeSyntax = Optional.empty();
private Optional<String> contentType = Optional.empty();
@@ -172,6 +209,8 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
private Optional<Path> sourceFile = Optional.empty();
private Optional<IRI> sourceIri = Optional.empty();
private Consumer<Quad> target;
+ private Optional<Dataset> targetDataset;
+ private Optional<Graph> targetGraph;
@Override
public AbstractRDFParserBuilder clone() {
@@ -319,6 +358,22 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
sourceFile = Optional.empty();
}
+
+ /**
+ * Reset all optional target* fields to Optional.empty()</code>
+ * <p>
+ * Note that the consumer set for {@link #getTarget()} is
+ * NOT reset.
+ * <p>
+ * Subclasses should override this and call <code>super.resetTarget()</code>
+ * if they need to reset any additional target* fields.
+ *
+ */
+ protected void resetTarget() {
+ targetDataset = Optional.empty();
+ targetGraph = Optional.empty();
+ }
+
/**
* Parse {@link #sourceInputStream}, {@link #sourceFile} or
* {@link #sourceIri}.
@@ -387,6 +442,10 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
if (target == null) {
throw new IllegalStateException("target has not been set");
}
+ if (targetGraph.isPresent() && targetDataset.isPresent()) {
+ // This should not happen as each target(..) method resets the optionals
+ throw new IllegalStateException("targetGraph and targetDataset can't both be set");
+ }
}
/**
@@ -470,8 +529,27 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
@Override
public RDFParserBuilder target(Consumer<Quad> consumer) {
AbstractRDFParserBuilder c = clone();
+ c.resetTarget();
c.target = consumer;
return c;
}
+
+ @Override
+ public RDFParserBuilder target(Dataset dataset) {
+ AbstractRDFParserBuilder c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(dataset);
+ c.resetTarget();
+ c.targetDataset = Optional.of(dataset);
+ return c;
+ }
+
+ @Override
+ public RDFParserBuilder target(Graph graph) {
+ AbstractRDFParserBuilder c = (AbstractRDFParserBuilder) RDFParserBuilder.super.target(graph);
+ c.resetTarget();
+ c.targetGraph = Optional.of(graph);
+ return c;
+ }
+
+
}
[05/11] incubator-commonsrdf git commit: target() in javadocs
Posted by st...@apache.org.
target() in javadocs
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/c0700de1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/c0700de1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/c0700de1
Branch: refs/heads/jsonld-java
Commit: c0700de1f2adb83200f3443ef29e884a7ffae289
Parents: d153b4c
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Jun 20 09:48:19 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Jun 20 09:48:19 2016 +0100
----------------------------------------------------------------------
.../org/apache/commons/rdf/api/RDFParserBuilder.java | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c0700de1/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..b387406 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,6 +22,7 @@ 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.
@@ -35,7 +36,7 @@ import java.util.concurrent.Future;
* RDFParserBuilder - however methods can be called in any order.
* <p>
* The call to {@link #parse()} returns a {@link Future}, allowing asynchronous parse
- * operations. This can be combined with {@link #intoGraph(Graph)}
+ * operations. This can be combined with {@link #target(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.
@@ -73,7 +74,7 @@ public interface RDFParserBuilder {
* Specify which {@link RDFTermFactory} to use for generating
* {@link RDFTerm}s.
* <p>
- * 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.
* <p>
* <strong>Warning:</strong> Using the same {@link RDFTermFactory} for
@@ -82,7 +83,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
@@ -165,8 +166,12 @@ public interface RDFParserBuilder {
* @return An {@link RDFParserBuilder} that will insert triples into the
* specified graph.
*/
- RDFParserBuilder intoGraph(Graph graph);
+ default RDFParserBuilder target(Graph graph) {
+ return target(graph::add);
+ }
+ RDFParserBuilder target(Consumer<Triple> tripleConsumer);
+
/**
* Specify a base IRI to use for parsing any relative IRI references.
* <p>
@@ -357,7 +362,7 @@ public interface RDFParserBuilder {
* synchronous implementation MAY be blocking on the <code>parse()</code>
* call and return a Future that is already {@link Future#isDone()}.
* <p>
- * If {@link #intoGraph(Graph)} has been specified, this SHOULD be the same
+ * If {@link #target(Graph)} has been specified, this SHOULD be the same
* {@link Graph} instance returned from {@link Future#get()} once parsing has
* completed successfully.
* <p>
[03/11] incubator-commonsrdf git commit: Merge branch 'quad' into
parser-with-quads
Posted by st...@apache.org.
Merge branch 'quad' into parser-with-quads
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/036029bb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/036029bb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/036029bb
Branch: refs/heads/jsonld-java
Commit: 036029bb50676fa65f873421b4cec556b14867fb
Parents: defdbd9 580484c
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Fri Jun 17 21:17:10 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Fri Jun 17 21:17:10 2016 +0100
----------------------------------------------------------------------
RELEASE-NOTES.txt | 39 ++++
RELEASE-PROCESS.md | 16 ++
api/pom.xml | 2 +-
.../org/apache/commons/rdf/api/Dataset.java | 12 +-
.../java/org/apache/commons/rdf/api/Graph.java | 40 +++-
.../org/apache/commons/rdf/api/GraphLike.java | 98 ++++++++++
.../java/org/apache/commons/rdf/api/Quad.java | 7 +-
.../org/apache/commons/rdf/api/QuadLike.java | 57 ++++++
.../java/org/apache/commons/rdf/api/Triple.java | 2 +-
.../org/apache/commons/rdf/api/TripleLike.java | 61 ++++++
.../apache/commons/rdf/api/TripleOrQuad.java | 64 -------
.../commons/rdf/api/AbstractGraphTest.java | 192 ++++++++++++++++++-
.../rdf/api/AbstractRDFTermFactoryTest.java | 29 +++
examples/pom.xml | 4 +-
examples/src/example/UserGuideTest.java | 1 +
pom.xml | 95 ++++-----
simple/pom.xml | 3 +-
.../commons/rdf/simple/DatasetGraphView.java | 138 +++++++++++++
.../apache/commons/rdf/simple/DatasetImpl.java | 23 +--
.../apache/commons/rdf/simple/GraphImpl.java | 12 +-
.../commons/rdf/simple/TestWritingGraph.java | 8 +-
src/assembly/src.xml | 45 +++++
src/main/assembly/src.xml | 44 -----
src/site/markdown/download.md | 34 ++--
src/site/markdown/userguide.md | 60 ++++--
25 files changed, 840 insertions(+), 246 deletions(-)
----------------------------------------------------------------------