Repository: commons-rdf
Updated Branches:
  refs/heads/fluent-parser 9c22589a6 -> 11a81fec6


Builders for ParserSource/ParserTarget


Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/b08b5bc0
Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/b08b5bc0
Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/b08b5bc0

Branch: refs/heads/fluent-parser
Commit: b08b5bc0290f662ac511ba6b18dc08da8ecba6a9
Parents: 9c22589
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Feb 19 14:42:46 2018 +0000
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Feb 19 14:42:46 2018 +0000

----------------------------------------------------------------------
 .../commons/rdf/api/io/IRIParserSource.java     | 47 ----------
 .../commons/rdf/api/io/InputParserSource.java   | 44 ---------
 .../commons/rdf/api/io/ParserConfigBuilder.java | 15 ++--
 .../apache/commons/rdf/api/io/ParserSource.java | 94 +++++++++++++++++++-
 .../apache/commons/rdf/api/io/ParserTarget.java | 80 +++++++++++++++--
 .../commons/rdf/api/io/PathParserSource.java    | 49 ----------
 .../rdf/api/fluentparser/TestParseConfig.java   | 28 ++++++
 7 files changed, 198 insertions(+), 159 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/IRIParserSource.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/IRIParserSource.java
 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/IRIParserSource.java
deleted file mode 100644
index 83a5ea4..0000000
--- 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/IRIParserSource.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rdf.api.io;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Optional;
-
-import org.apache.commons.rdf.api.IRI;
-
-final class IRIParserSource implements ParserSource<IRI> {
-       private final IRI iri;
-
-       IRIParserSource(IRI iri) {
-               this.iri = iri;
-       }
-
-       @Override
-       public IRI src() {
-               return iri;
-       }
-
-       @Override
-       public InputStream inputStream() throws IOException {
-               return new URL(iri.getIRIString()).openStream();
-       }
-
-       @Override
-       public Optional<IRI> iri() {
-               return Optional.of(iri);
-       }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/InputParserSource.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/InputParserSource.java
 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/InputParserSource.java
deleted file mode 100644
index 5a99e4b..0000000
--- 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/InputParserSource.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rdf.api.io;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Optional;
-
-final class InputParserSource implements ParserSource {
-       private final InputStream is;
-
-       InputParserSource(InputStream is) {
-               this.is = is;
-       }
-
-       @Override
-       public Object src() {
-               return is;
-       }
-
-       @Override
-       public InputStream inputStream() throws IOException {
-               return is;
-       }
-
-       @Override
-       public Optional iri() {
-               return Optional.empty();
-       }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
index 9adc6ce..457fb6c 100644
--- 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
+++ 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigBuilder.java
@@ -63,18 +63,13 @@ public final class ParserConfigBuilder implements 
NeedTargetOrRDF, NeedTargetOrR
 
        @Override
        public NeedSourceOrBase target(Dataset dataset) {
-               return target(dataset::add);
+               return target(ParserTarget.toDataset(dataset));
 
        }
 
        @Override
        public NeedSourceOrBase<Graph> target(Graph graph) {
-               return target(q -> {
-                       if (q.getGraphName().isPresent()) {
-                               // Only add if q is in default graph
-                               graph.add(q.asTriple());
-                       }
-               });
+               return target(ParserTarget.toGraph(graph));
        }
 
        @Override
@@ -94,11 +89,11 @@ public final class ParserConfigBuilder implements 
NeedTargetOrRDF, NeedTargetOrR
 
        @Override
        public Sync source(final IRI iri) {
-               return source(new IRIParserSource(iri));
+               return source(ParserSource.fromIRI(iri));
        }
 
        public Sync source(Path path) {
-               return source(new PathParserSource(path));
+               return source(ParserSource.fromPath(path));
        }
 
        @Override
@@ -123,7 +118,7 @@ public final class ParserConfigBuilder implements 
NeedTargetOrRDF, NeedTargetOrR
 
        @Override
        public Sync source(InputStream is) {
-               return source(new InputParserSource(is));
+               return source(ParserSource.fromInputStream(is));
        }
 
        @Override

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserSource.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserSource.java 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserSource.java
index 259688e..63f93ac 100644
--- 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserSource.java
+++ 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserSource.java
@@ -19,6 +19,9 @@ package org.apache.commons.rdf.api.io;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Optional;
 
 import org.apache.commons.rdf.api.IRI;
@@ -27,7 +30,92 @@ import org.apache.commons.rdf.api.IRI;
  *
  */
 public interface ParserSource<S> {
-    S src();   
-    InputStream inputStream() throws IOException;
-    Optional<IRI> iri();
+       S src();
+
+       InputStream inputStream() throws IOException;
+
+       Optional<IRI> iri();
+
+       static ParserSource<IRI> fromIRI(IRI iri) {
+               return new IRIParserSource(iri);
+       }
+
+       static ParserSource<Path> fromPath(Path path) {
+               return new PathParserSource(path);
+       }
+
+       static ParserSource<InputStream> fromInputStream(InputStream is) {
+               return new InputParserSource(is);
+       }
+
+}
+
+final class IRIParserSource implements ParserSource<IRI> {
+       private final IRI iri;
+
+       IRIParserSource(IRI iri) {
+               this.iri = iri;
+       }
+
+       @Override
+       public IRI src() {
+               return iri;
+       }
+
+       @Override
+       public InputStream inputStream() throws IOException {
+               return new URL(iri.getIRIString()).openStream();
+       }
+
+       @Override
+       public Optional<IRI> iri() {
+               return Optional.of(iri);
+       }
 }
+
+final class PathParserSource implements ParserSource<Path> {
+       private final Path path;
+
+       PathParserSource(Path path) {
+               this.path = path;
+       }
+
+       @Override
+       public Path src() {
+               return path;
+       }
+
+       @Override
+       public InputStream inputStream() throws IOException {
+               return Files.newInputStream(path);
+       }
+
+       @Override
+       public Optional<IRI> iri() {
+               final String uri = path.toUri().toString();
+               return Optional.of(new IRIImpl(uri));
+       }
+}
+
+final class InputParserSource implements ParserSource<InputStream> {
+       private final InputStream is;
+
+       InputParserSource(InputStream is) {
+               this.is = is;
+       }
+
+       @Override
+       public InputStream src() {
+               return is;
+       }
+
+       @Override
+       public InputStream inputStream() throws IOException {
+               return is;
+       }
+
+       @Override
+       public Optional<IRI> iri() {
+               return Optional.empty();
+       }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserTarget.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserTarget.java 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserTarget.java
index 8898643..4c01dcf 100644
--- 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserTarget.java
+++ 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserTarget.java
@@ -17,16 +17,84 @@
  */
 package org.apache.commons.rdf.api.io;
 
+import java.util.Optional;
 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;
 
-/**
- *
- */
 @FunctionalInterface
 public interface ParserTarget<T> extends Consumer<Quad> {
-    default T dest() {
-        return null;// unknown
-    }
+
+       default T dest() {
+               return null;// unknown
+       }
+
+       static ParserTarget<Dataset> toDataset(final Dataset ds) {
+               return new DatasetParserTarget(ds);
+       }
+
+       static ParserTarget<Graph> toGraph(final Graph graph) {
+               return new GraphParserTarget(graph, null);
+       }
+
+       static ParserTarget<Graph> toGraph(final Graph graph, IRI 
matchGraphName) {
+               return new GraphParserTarget(graph, matchGraphName);
+       }
+
+       static ParserTarget<Graph> toUnionGraph(final Graph graph) {
+               return q -> graph.add(q.asTriple());
+       }
+}
+
+class GraphParserTarget implements ParserTarget<Graph> {
+
+       private final boolean anyGraphName;
+       private final Graph graph;
+       private final IRI matchGraphName;
+
+       GraphParserTarget(Graph graph) {
+               this.graph = graph;
+               this.matchGraphName = null;
+               this.anyGraphName = true;
+       }
+
+       GraphParserTarget(Graph graph, IRI matchGraphName) {
+               this.graph = graph;
+               this.matchGraphName = matchGraphName;
+               this.anyGraphName = false;
+       }
+
+       @Override
+       public Graph dest() {
+               return graph;
+       }
+
+       @Override
+       public void accept(Quad q) {
+               if (anyGraphName || 
q.getGraphName().equals(Optional.ofNullable(matchGraphName))) {
+                       graph.add(q.asTriple());
+               }
+       }
+}
+
+class DatasetParserTarget implements ParserTarget<Dataset> {
+
+       private final Dataset dataset;
+
+       public DatasetParserTarget(Dataset dataset) {
+               this.dataset = dataset;
+       }
+
+       @Override
+       public Dataset dest() {
+               return dataset;
+       }
+
+       @Override
+       public void accept(Quad q) {
+               dataset.add(q);
+       }
 }

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/PathParserSource.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/PathParserSource.java
 
b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/PathParserSource.java
deleted file mode 100644
index 7c6e14c..0000000
--- 
a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/PathParserSource.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rdf.api.io;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Optional;
-
-import org.apache.commons.rdf.api.IRI;
-
-final class PathParserSource implements ParserSource<Path> {
-       private final Path path;
-
-       PathParserSource(Path path) {
-               this.path = path;
-       }
-
-       @Override
-       public Path src() {
-               return path;
-       }
-
-       @Override
-       public InputStream inputStream() throws IOException {
-               return Files.newInputStream(path);
-       }
-
-       @Override
-       public Optional<IRI> iri() {
-               final String uri = path.toUri().toString();
-               return Optional.of(new IRIImpl(uri));
-       }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b08b5bc0/commons-rdf-api/src/test/java/org/apache/commons/rdf/api/fluentparser/TestParseConfig.java
----------------------------------------------------------------------
diff --git 
a/commons-rdf-api/src/test/java/org/apache/commons/rdf/api/fluentparser/TestParseConfig.java
 
b/commons-rdf-api/src/test/java/org/apache/commons/rdf/api/fluentparser/TestParseConfig.java
new file mode 100644
index 0000000..1102633
--- /dev/null
+++ 
b/commons-rdf-api/src/test/java/org/apache/commons/rdf/api/fluentparser/TestParseConfig.java
@@ -0,0 +1,28 @@
+/*
+ * 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.api.fluentparser;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestParseConfig {
+       @Test
+       public void immutable() throws Exception {
+               
+       }
+}

Reply via email to