jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [04/15] jena git commit: JENA-1323: RDFWriter
Date Wed, 26 Apr 2017 09:09:18 GMT
JENA-1323: RDFWriter


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

Branch: refs/heads/master
Commit: ef278645dc3e7883f0d5e4f533890b5365dcf4b1
Parents: b386e4a
Author: Andy Seaborne <andy@apache.org>
Authored: Tue Apr 18 17:19:25 2017 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Tue Apr 18 22:07:10 2017 +0100

----------------------------------------------------------------------
 .../java/org/apache/jena/riot/RDFDataMgr.java   |  14 +-
 .../java/org/apache/jena/riot/RDFWriter.java    | 220 +++++++++++++++++++
 .../org/apache/jena/riot/RDFWriterBuilder.java  | 165 ++++++++++++++
 .../org/apache/jena/riot/TS_RiotGeneral.java    |   5 +-
 .../org/apache/jena/riot/TestRDFWriter.java     |  88 ++++++++
 5 files changed, 482 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/ef278645/jena-arq/src/main/java/org/apache/jena/riot/RDFDataMgr.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/RDFDataMgr.java b/jena-arq/src/main/java/org/apache/jena/riot/RDFDataMgr.java
index f884b10..87f5003 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/RDFDataMgr.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/RDFDataMgr.java
@@ -1218,23 +1218,21 @@ public class RDFDataMgr
     }
 
     private static void write$(OutputStream out, Graph graph, RDFFormat serialization) {
-        WriterGraphRIOT w = createGraphWriter$(serialization);
-        w.write(out, graph, RiotLib.prefixMap(graph), null, RIOT.getContext());
+        RDFWriter.create().format(serialization).source(graph).output(out);
     }
 
+    @SuppressWarnings("deprecation")
     private static void write$(Writer out, Graph graph, RDFFormat serialization) {
-        WriterGraphRIOT w = createGraphWriter$(serialization);
-        w.write(out, graph, RiotLib.prefixMap(graph), null, RIOT.getContext());
+        RDFWriter.create().format(serialization).source(graph).build().output(out);
     }
 
     private static void write$(OutputStream out, DatasetGraph dataset, RDFFormat serialization)
{
-        WriterDatasetRIOT w = createDatasetWriter$(serialization);
-        w.write(out, dataset, RiotLib.prefixMap(dataset), null, RIOT.getContext());
+        RDFWriter.create().format(serialization).source(dataset).output(out);
     }
 
+    @SuppressWarnings("deprecation")
     private static void write$(Writer out, DatasetGraph dataset, RDFFormat serialization)
{
-        WriterDatasetRIOT w = createDatasetWriter$(serialization);
-        w.write(out, dataset, RiotLib.prefixMap(dataset), null, RIOT.getContext());
+        RDFWriter.create().format(serialization).source(dataset).build().output(out);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/jena/blob/ef278645/jena-arq/src/main/java/org/apache/jena/riot/RDFWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/RDFWriter.java b/jena-arq/src/main/java/org/apache/jena/riot/RDFWriter.java
new file mode 100644
index 0000000..f20f8c2
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/RDFWriter.java
@@ -0,0 +1,220 @@
+/*
+ * 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.jena.riot;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Objects;
+
+import org.apache.jena.atlas.io.IO;
+import org.apache.jena.atlas.web.ContentType;
+import org.apache.jena.graph.Graph;
+import org.apache.jena.riot.system.RiotLib;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.util.Context;
+
+/**
+ * An {@link RDFWriter} is a process that will output triples or quads in an RDF syntax.
+ * {@link RDFWriterBuilder} provides the means to setup the writer.
+ * <p>
+ * The process is
+ * 
+ * <pre>
+ *    DatasetGraph dsg = ...
+ *    RDFWriter writer = RDFWriter.create().source(dsg).lang(Lang.TTL).build();
+ *    OutputStream out = 
+ *    writer.output(out);
+ * </pre>
+ * or using abbreviated forms:
+ * <pre>
+ *    RDFWriter.create().source(dsg).lang(Lang.TTL).output(out);
+ * </pre>
+ * @see WriterDatasetRIOT
+ * @see WriterGraphRIOT
+ */
+
+public class RDFWriter {
+    private static int BUF_SIZE = 128*1024;
+    private final DatasetGraph dataset;
+    private final Graph graph;
+    private final RDFFormat format;
+    private final String baseURI;
+    private final Context context;
+    
+    /** Create an {@link RDFWriterBuilder}.
+     * <p>
+     * Often used in a pattern such as:
+     * <pre>
+     *    RDFWriter.create()
+     *        .lang(Lang.TTL)
+     *        .source(graph)
+     *        .output(System.out);
+     * </pre>
+     */
+    public static RDFWriterBuilder create() { return new RDFWriterBuilder(); }
+    
+    /*package*/ RDFWriter(DatasetGraph dataset, Graph graph, RDFFormat format, Lang lang,
String baseURI, Context context) {
+        this.dataset = dataset;
+        this.graph = graph;
+        // format may still be null - output to a file later. 
+        this.format = chooseFormat(format, lang);
+        this.baseURI = baseURI;
+        this.context = context;
+    }
+    
+    private static RDFFormat chooseFormat(RDFFormat format, Lang lang) {
+        if ( format != null )
+            return format;
+        if ( lang == null )
+            //throw new RiotException("No syntax for output") ;
+            return null;
+        format = RDFWriterRegistry.defaultSerialization(lang);
+        return format;
+    }
+    
+    /** Write and return as a string. 
+     * <p>
+     * The {@code Lang} or {@code RDFFormat} must have been set.
+     */
+    public String asString() {
+        if ( format == null )
+            throw new RiotException("Output as a string needs the Lang/Format specificied");
+        try ( StringWriter sw = new StringWriter() ) {
+            output(sw, format);
+            return sw.toString();
+        } catch (IOException ex) { IO.exception(ex); return null; }
+    }
+    
+    /** Write the source to the {@code OutputStream}.
+     * <p>
+     * The {@code Lang} or {@code RDFFormat} must have been set.
+     * @param output
+     */
+    public void output(OutputStream output) {
+        output(output, format);
+    }
+    
+    /** Write the source to the Java {@code Writer}.
+     * <p>
+     * The {@code Lang} or {@code RDFFormat} must have been set.
+     * @param javaWriter
+     * @deprecated Using Java Writers risk corrupting the character set. Only UTF-8 is safe.

+     */
+    @Deprecated
+    public void output(Writer javaWriter) {
+        output(javaWriter, format);
+    }
+    
+
+    /** Write the source to the file. 
+     * <p>
+     * If neither {@code Lang} nor {@code RDFFormat} are set, an attempt to 
+     * guess an RDF Syntax is made from the file extension. 
+     * <p>Output to "-" goes to stdout.
+     * @param filename
+     */
+    public void output(String filename) {
+        Objects.requireNonNull(filename, "Null filename");
+        RDFFormat fmt = format;
+        if ( fmt == null ) {
+            ContentType ct = RDFLanguages.guessContentType(filename);
+            if ( ct == null )
+                throw new RiotException("Lang and RDFformat unset and can't determine syntax
from '"+filename+"'");
+            Lang lang = RDFLanguages.contentTypeToLang(ct);
+            if ( ct == null )
+                throw new RiotException("No syntax registered for '"+ct.getContentType()+"'");

+            fmt = RDFWriterRegistry.defaultSerialization(lang);
+        }
+        if ( filename.equals("-") ) {
+            output(System.out, fmt);
+            return;
+        }
+        Path p = Paths.get(filename);
+        try ( OutputStream out1 = Files.newOutputStream(p);
+              OutputStream out = new BufferedOutputStream(out1, BUF_SIZE)){
+            output(out, fmt);
+        } catch (IOException ex) { IO.exception(ex); }
+    }
+    
+    private void output(OutputStream output, RDFFormat format) {
+        if ( format == null )
+            throw new RiotException("No syntax (Lang or RDFFormat) for output") ;
+        if ( graph != null ) {
+            write$(output, graph, format);
+            return;
+        }
+        if ( dataset != null ) {
+            write$(output, dataset, format);
+            return;
+        }
+        throw new RiotException("No graph or dataset to write") ;
+    }    
+    
+    private void output(Writer javaWriter, RDFFormat format) {
+        if ( format == null )
+            throw new RiotException("No syntax (Lang or RDFFormat) for output") ;
+        if ( graph != null ) {
+            write$(javaWriter, graph, format);
+            return;
+        }
+        if ( dataset != null ) {
+            write$(javaWriter, dataset, format);
+            return;
+        }
+        throw new RiotException("No graph or dataset to write") ;
+    }    
+
+    // Allowing an externally set PrefixMap was (probably) a mistake. 
+    
+    private static WriterGraphRIOT createGraphWriter$(RDFFormat serialization) {
+        WriterGraphRIOTFactory wf = RDFWriterRegistry.getWriterGraphFactory(serialization);
+        if ( wf == null )
+            throw new RiotException("No graph writer for " + serialization);
+        return wf.create(serialization);
+    }
+
+    private static WriterDatasetRIOT createDatasetWriter$(RDFFormat serialization) {
+        WriterDatasetRIOTFactory wf = RDFWriterRegistry.getWriterDatasetFactory(serialization);
+        if ( wf == null )
+            throw new RiotException("No dataset writer for " + serialization);
+        return wf.create(serialization);
+    }
+
+    private void write$(OutputStream out, Graph graph, RDFFormat serialization) {
+        WriterGraphRIOT w = createGraphWriter$(serialization);
+        w.write(out, graph, RiotLib.prefixMap(graph), null, context);
+    }
+
+    private void write$(OutputStream out, DatasetGraph dataset, RDFFormat serialization)
{
+        WriterDatasetRIOT w = createDatasetWriter$(serialization);
+        w.write(out, dataset, RiotLib.prefixMap(dataset), null, context);
+    }
+
+    private void write$(Writer out, Graph graph, RDFFormat serialization) {
+        WriterGraphRIOT w = createGraphWriter$(serialization);
+        w.write(out, graph, RiotLib.prefixMap(graph), null,context);
+    }
+
+    private void write$(Writer out, DatasetGraph dataset, RDFFormat serialization) {
+        WriterDatasetRIOT w = createDatasetWriter$(serialization);
+        w.write(out, dataset, RiotLib.prefixMap(dataset), null, context);
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/ef278645/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterBuilder.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterBuilder.java b/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterBuilder.java
new file mode 100644
index 0000000..1840fcf
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterBuilder.java
@@ -0,0 +1,165 @@
+/*
+ * 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.jena.riot;
+
+import java.io.OutputStream;
+
+import org.apache.jena.graph.Graph ;
+import org.apache.jena.sparql.core.DatasetGraph ;
+import org.apache.jena.sparql.util.Context;
+
+public class RDFWriterBuilder {
+    private DatasetGraph dataset = null;
+    private Graph        graph   = null;
+    private Context      context = null;
+    private Lang         lang    = null;
+    private RDFFormat    format  = null;
+    private String       baseURI = null;
+    
+    /** A new {@code RDFWriterBuilder}.
+     * <p>
+     * See also {@link RDFWriter#create()} 
+     */
+    RDFWriterBuilder () {}
+
+    /** Set the source of wriing to the graph argument.
+     * <p>
+     * Any prrvious source setting is cleared.
+     * @param graph A {@link Graph}.
+     * @return this
+     */
+    public RDFWriterBuilder source(Graph graph) {
+        this.dataset = null;
+        this.graph = graph;
+        return this;
+    }
+
+    /** Set the source of wriing to the {@code DatasetGraph} argument.
+     * <p>
+     * Any prrvious source setting is cleared.
+     * @param dataset A {@link DatasetGraph}.
+     * @return this
+     */
+    public RDFWriterBuilder source(DatasetGraph dataset) {
+        this.graph = null;
+        this.dataset = dataset;
+        return this;
+    }
+
+//    // Not implemented
+//    public RDFWriterBuilder labels(NodeToLabel nodeToLabel) { return this; }
+//    
+//    // Not implemented
+//    public RDFWriterBuilder formatter(NodeFormatter nodeFormatter) { return this; }
+
+    /** Set the context for the writer when built.
+     *  
+     * @param context
+     * @return this
+     */
+    public RDFWriterBuilder context(Context context) { 
+        if ( context != null )
+            context = context.copy();
+        this.context = context;
+        return this; 
+    }
+    
+    /**
+     * Set the output language to a {@link Lang}; this will set the format. 
+     * <p>
+     * If {@code Lang} and {@code RDFFormat} are not set, an attempt is made to guess it
from file name or URI on output.
+     * <p>
+     * If output is to an {@code OutputStream}, {@code Lang} or {@code RDFFormat} must be
set.
+     * <p>
+     * Any previous setting of {@code Lang} or {@code RDFFormat} is cleared.
+     * 
+     * @param lang
+     * @return this
+     */
+    public RDFWriterBuilder lang(Lang lang) {
+        this.format = null;
+        this.lang = lang;
+        return this;
+    }
+
+    /**
+     * Set the output format to a {@link RDFFormat}. 
+     * <p>
+     * If {@code Lang} and {@code RDFFormat} are not set, an attempt is made to guess it
from file name or URI on output.
+     * <p>
+     * If output is to an {@code OutputStream}, {@code Lang} or {@code RDFFormat} must be
set.
+     * <p>
+     * Any previous setting of {@code Lang} or {@code RDFFormat} is cleared.
+     * @param format
+     * @return this
+     */
+    public RDFWriterBuilder format(RDFFormat format) {
+        this.lang = null;
+        this.format = format;
+        return this;
+    }
+    
+    public RDFWriterBuilder base(String baseURI) {
+        this.baseURI = baseURI;
+        return this;
+    }
+    
+    @Override
+    public RDFWriterBuilder clone() {
+        RDFWriterBuilder clone = new RDFWriterBuilder();
+        clone.dataset   = this.dataset;
+        clone.graph     = this.graph;
+        clone.context   = this.context;
+        clone.lang      = this.lang;
+        clone.format    = this.format;
+        clone.baseURI   = this.baseURI;
+        return clone;
+    }
+    
+    public RDFWriter build() { 
+        if ( context == null )
+            context = RIOT.getContext().copy();
+        if ( dataset == null && graph == null )
+            throw new RiotException("No source to be written");
+        return new RDFWriter(dataset, graph, format, lang, baseURI, context);
+    }
+    
+    
+    /** Short form for {@code build().output(outputStream)}.
+     * 
+     * @param outputStream
+     */
+    public void output(OutputStream outputStream) {
+        build().output(outputStream);
+    }
+        
+    /** Short form for {@code build().output(v)}.
+     * 
+     * @param filename
+     */
+    public void output(String filename) {
+        build().output(filename);
+    }
+    
+    /** Short form for {@code build().output(v)}.
+     */
+    public String asString() {
+        return build().asString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/ef278645/jena-arq/src/test/java/org/apache/jena/riot/TS_RiotGeneral.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/TS_RiotGeneral.java b/jena-arq/src/test/java/org/apache/jena/riot/TS_RiotGeneral.java
index 2978443..c33b75f 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/TS_RiotGeneral.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/TS_RiotGeneral.java
@@ -25,14 +25,15 @@ import org.junit.runners.Suite.SuiteClasses ;
 @RunWith(Suite.class)
 @SuiteClasses(
 { 
-    TestLangRIOT.class
+    TestSysRIOT.class
+    , TestLangRIOT.class
     , TestSyntaxDetermination.class
     , TestJenaReaderRIOT.class
     , TestReadData.class
     , TestRiotReader.class
     , TestRDFParser.class
     , TestParserRegistry.class
-    , TestSysRIOT.class
+    , TestRDFWriter.class
 })
 
 public class TS_RiotGeneral

http://git-wip-us.apache.org/repos/asf/jena/blob/ef278645/jena-arq/src/test/java/org/apache/jena/riot/TestRDFWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/TestRDFWriter.java b/jena-arq/src/test/java/org/apache/jena/riot/TestRDFWriter.java
new file mode 100644
index 0000000..73c8ed0
--- /dev/null
+++ b/jena-arq/src/test/java/org/apache/jena/riot/TestRDFWriter.java
@@ -0,0 +1,88 @@
+/*
+ * 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.jena.riot;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.CharArrayWriter;
+import java.io.Writer;
+
+import org.apache.jena.atlas.lib.StrUtils;
+import org.apache.jena.graph.Graph;
+import org.apache.jena.riot.Lang;
+import org.apache.jena.riot.RDFWriter;
+import org.apache.jena.riot.RiotException;
+import org.apache.jena.sparql.sse.SSE;
+import org.junit.Test;
+
+public class TestRDFWriter {
+    private static Graph graph = SSE.parseGraph("(graph (:s :p :o))");
+    
+    @Test public void rdfwriter_1() {
+        RDFWriter.create().source(graph).build();
+    }
+    
+    @Test(expected=RiotException.class)
+    public void rdfwriter_2() {
+        RDFWriter.create().build();
+    }
+
+    @Test public void rdfwriter_3() {
+        String s = 
+            RDFWriter.create()
+                .source(graph)
+                .lang(Lang.NT)
+                .asString();
+        assertTrue(s.contains("example/s"));
+    }
+
+    @Test(expected=RiotException.class)
+    public void rdfwriter_4() {
+        String s = 
+            RDFWriter.create()
+                // No syntax
+                .source(graph)
+                .asString();
+    }
+    
+    @Test public void rdfwriter_5() {
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+        RDFWriter.create()
+            .source(graph)
+            .lang(Lang.NT)
+            .output(bout);
+        String s = StrUtils.fromUTF8bytes(bout.toByteArray());
+        assertTrue(s.contains("example/s"));
+    }
+    
+    @SuppressWarnings("deprecation")
+    @Test public void rdfwriter_6() {
+        Writer w = new CharArrayWriter();
+        RDFWriter.create()
+            .source(graph)
+            .lang(Lang.NT)
+            .build()
+            .output(w);
+        String s = w.toString();
+        assertTrue(s.contains("example/s"));
+    }
+    
+   
+}


Mime
View raw message