This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/master by this push:
     new 74edeab  JENA-2001: Print rdf:type if RDF prefix is available
     new 79256b3  Merge pull request #887 from afs/jena2001-write-type
74edeab is described below

commit 74edeab4a6635d2e42de07ebf982ccf9f283cb6a
Author: Andy Seaborne <[email protected]>
AuthorDate: Wed Dec 16 12:24:29 2020 +0000

    JENA-2001: Print rdf:type if RDF prefix is available
---
 .../org/apache/jena/riot/RDFParserRegistry.java    |   3 -
 .../org/apache/jena/riot/RDFWriterRegistry.java    | 112 ++++-----------------
 .../apache/jena/riot/adapters/RDFWriterRIOT.java   |   5 +-
 .../apache/jena/riot/system/StreamRDFWriter.java   |  24 +++--
 .../org/apache/jena/riot/writer/TurtleShell.java   |   7 +-
 .../java/org/apache/jena/riot/writer/Widths.java   |  80 +++++++++++++++
 .../jena/riot/writer/WriterDatasetRIOTBase.java    |   8 +-
 .../jena/riot/writer/WriterStreamRDFBase.java      |  34 ++++++-
 .../jena/riot/writer/WriterStreamRDFBlocks.java    |  44 +++++---
 .../jena/riot/writer/WriterStreamRDFFlat.java      |   8 +-
 .../org/apache/jena/riot/writer/TS_RiotWriter.java |   1 -
 .../apache/jena/riot/writer/TestJenaWriters.java   |  78 --------------
 12 files changed, 186 insertions(+), 218 deletions(-)

diff --git a/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java 
b/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
index 4244b1e..80427ac 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
@@ -18,7 +18,6 @@
 
 package org.apache.jena.riot;
 
-//import static org.apache.jena.riot.RDFLanguages.*;
 import static org.apache.jena.riot.Lang.*;
 
 import java.io.InputStream ;
@@ -208,9 +207,7 @@ public class RDFParserRegistry
     }
 
     private static class ReaderRDFThrift implements ReaderRIOT {
-
         static ReaderRIOTFactory factory = (Lang language, ParserProfile 
profile) -> new ReaderRDFThrift(profile);
-
         private final ParserProfile profile;
         public ReaderRDFThrift(ParserProfile profile) { this.profile = 
profile; }
 
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java 
b/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
index 737fdb0..9d96ae8 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
@@ -22,14 +22,23 @@ import java.util.* ;
 
 import org.apache.jena.atlas.lib.CharSpace ;
 import org.apache.jena.riot.system.RiotLib ;
+import org.apache.jena.riot.system.StreamRDFWriter;
 import org.apache.jena.riot.thrift.WriterDatasetThrift ;
 import org.apache.jena.riot.thrift.WriterGraphThrift ;
 import org.apache.jena.riot.writer.* ;
 import org.apache.jena.sys.JenaSystem ;
 
+/**
+ * Writer registry. This is for writers presenting the functionality to write 
graphs and datasets, not streams.
+ * See {@link StreamRDFWriter} for registration of streaming writers.
+ *
+ * To register the language: see {@link RDFLanguages}.
+ *
+ * @see StreamRDFWriter for streaming writers.
+ */
 public class RDFWriterRegistry
 {
-    // All mention and use of Writers is purely for compatibility.
+    // All mention and use of java.io.Writer is purely for compatibility.
     // They are not a good idea (StringWriter is an exception).
     // Let the serializer deal with the character issues.
     // UTF-8 is universal - but UTF-8 is not the default in Java ("platform 
encoding" is).
@@ -39,7 +48,6 @@ public class RDFWriterRegistry
     private static Map<RDFFormat, WriterGraphRIOTFactory> registryGraph     = 
new HashMap<>() ;
     private static Map<RDFFormat, WriterDatasetRIOTFactory> registryDataset = 
new HashMap<>() ;
     private static Map<Lang, RDFFormat> langToFormat                        = 
new HashMap<>() ;
-    private static Map<String, RDFFormat> mapJenaNameToFormat               = 
new HashMap<>() ;
 
     // Writing a graph
     static WriterGraphRIOTFactory wgfactory = new WriterGraphRIOTFactory() {
@@ -93,51 +101,12 @@ public class RDFWriterRegistry
             return null ;
     }} ;
 
-    static WriterDatasetRIOTFactory wdsJsonldfactory = new 
WriterDatasetRIOTFactory() {
-    //private static class WriterDatasetJSONLDFactory implements 
WriterDatasetRIOTFactory {
-        @Override
-        public WriterDatasetRIOT create(RDFFormat syntaxForm) {
-            return new JsonLDWriter(syntaxForm) ;
-        }
-    } ;
-
-    static WriterGraphRIOTFactory wgJsonldfactory = new 
WriterGraphRIOTFactory() {
-    //private static class WriterGraphJSONLDFactory implements 
WriterGraphRIOTFactory {
-        @Override
-        public WriterGraphRIOT create(RDFFormat syntaxForm) {
-            return RiotLib.adapter(new JsonLDWriter(syntaxForm)) ;
-        }
-    } ;
-
-    static WriterGraphRIOTFactory wgThriftFactory = new 
WriterGraphRIOTFactory(){
-        @Override
-        public WriterGraphRIOT create(RDFFormat syntaxForm) {
-            return new WriterGraphThrift(syntaxForm) ;
-        }
-    } ;
-
-    static WriterDatasetRIOTFactory wdsThriftFactory = new 
WriterDatasetRIOTFactory(){
-        @Override
-        public WriterDatasetRIOT create(RDFFormat syntaxForm) {
-            return new WriterDatasetThrift(syntaxForm) ;
-        }
-    } ;
-
-    static WriterGraphRIOTFactory wgTriXFactory = new WriterGraphRIOTFactory() 
{
-
-        @Override
-        public WriterGraphRIOT create(RDFFormat syntaxForm) {
-            return new WriterTriX() ;
-        }
-    } ;
-
-    static WriterDatasetRIOTFactory wdsTriXFactory = new 
WriterDatasetRIOTFactory() {
-
-        @Override
-        public WriterDatasetRIOT create(RDFFormat syntaxForm) {
-            return new WriterTriX() ;
-        }
-    } ;
+    private static WriterDatasetRIOTFactory wdsJsonldfactory   = syntaxForm -> 
new JsonLDWriter(syntaxForm);
+    private static WriterGraphRIOTFactory wgJsonldfactory      = syntaxForm -> 
RiotLib.adapter(new JsonLDWriter(syntaxForm));
+    private static WriterGraphRIOTFactory wgThriftFactory      = syntaxForm -> 
new WriterGraphThrift(syntaxForm);
+    private static WriterDatasetRIOTFactory wdsThriftFactory   = syntaxForm -> 
new WriterDatasetThrift(syntaxForm);
+    private static WriterGraphRIOTFactory wgTriXFactory        = syntaxForm -> 
new WriterTriX();
+    private static WriterDatasetRIOTFactory wdsTriXFactory     = syntaxForm -> 
new WriterTriX() ;
 
      public static void init() {}
      static { init$() ; }
@@ -229,48 +198,6 @@ public class RDFWriterRegistry
 
      // ---- Compatibility
 
-     /** return the RDFFormat for the existing Jena writer name, or null */
-     public static RDFFormat getFormatForJenaWriter(String jenaName) {
-         return mapJenaNameToFormat.get(jenaName);
-     }
-
-     /** Register an RDFFormat for a Jena writer name */
-     private /*public*/ static void setFormatForJenaWriter(String jenaName, 
RDFFormat format) {
-         mapJenaNameToFormat.put(jenaName, format);
-     }
-
-     /** Return a collection of Jena writer names */
-     public static Collection<String> getJenaWriterNames() {
-         return mapJenaNameToFormat.keySet();
-     }
-
-     // Unused; not called from init$()
-     // These settings are used by RDFWriterRIOT to override 
RDFWriterRegistrations.
-
-     // The only difference is "RDF/XML" being plain here but pretty
-     // as registered.
-     // 2017-03:
-     // The number of user questions on format of RDF/XML not being pretty
-     // has dropped off since Jena2.
-
-//     private static void setupJenaNames() {
-//         setFormatForJenaWriter("RDF/XML",                           
RDFFormat.RDFXML_PLAIN) ;
-//         setFormatForJenaWriter("RDF/XML-ABBREV",                    
RDFFormat.RDFXML_ABBREV) ;
-//         setFormatForJenaWriter("N-TRIPLE",                          
RDFFormat.NTRIPLES) ;
-//         setFormatForJenaWriter("NT",                                
RDFFormat.NTRIPLES) ;
-//         setFormatForJenaWriter("N-TRIPLES",                         
RDFFormat.NTRIPLES) ;
-//         setFormatForJenaWriter("N-Triples",                         
RDFFormat.NTRIPLES) ;
-//         setFormatForJenaWriter("N3",                                
RDFFormat.TURTLE) ;
-//         setFormatForJenaWriter(N3JenaWriter.n3WriterPrettyPrinter,  
RDFFormat.TURTLE_PRETTY) ;
-//         setFormatForJenaWriter(N3JenaWriter.n3WriterPlain,          
RDFFormat.TURTLE_BLOCKS) ;
-//         setFormatForJenaWriter(N3JenaWriter.n3WriterTriples,        
RDFFormat.TURTLE_FLAT) ;
-//         setFormatForJenaWriter(N3JenaWriter.n3WriterTriplesAlt,     
RDFFormat.TURTLE_FLAT) ;
-//         setFormatForJenaWriter(N3JenaWriter.turtleWriter,           
RDFFormat.TURTLE) ;
-//         setFormatForJenaWriter(N3JenaWriter.turtleWriterAlt1,       
RDFFormat.TURTLE) ;
-//         setFormatForJenaWriter(N3JenaWriter.turtleWriterAlt2,       
RDFFormat.TURTLE) ;
-//     }
-
-
     /** Register the serialization for graphs and it's associated factory
      * @param serialization         RDFFormat for the output format.
      * @param graphWriterFactory    Source of writer engines
@@ -284,14 +211,13 @@ public class RDFWriterRegistry
      * @param serialization         RDFFormat for the output format.
      * @param datasetWriterFactory    Source of writer engines
      */
-    public static void register(RDFFormat serialization, 
WriterDatasetRIOTFactory datasetWriterFactory)
-    {
-        registryDataset.put(serialization, datasetWriterFactory) ;
+    public static void register(RDFFormat serialization, 
WriterDatasetRIOTFactory datasetWriterFactory) {
+        registryDataset.put(serialization, datasetWriterFactory);
     }
 
     /** Register an RDFFormat */
     private static void register(RDFFormat serialization)
-    { }
+    { /*no-op*/ }
 
     /**
      * Register the default serialization for the language (replace any 
existing
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/adapters/RDFWriterRIOT.java 
b/jena-arq/src/main/java/org/apache/jena/riot/adapters/RDFWriterRIOT.java
index f2c2a46..e6641b2 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/adapters/RDFWriterRIOT.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/adapters/RDFWriterRIOT.java
@@ -60,11 +60,8 @@ public class RDFWriterRIOT implements RDFWriter
     protected RDFWriterBuilder writer() {
         if ( jenaName == null )
             throw new IllegalArgumentException("Jena writer name is null");
-        // For writing via model.write(), use any old names for jena writers. 
(As of 2107-03 - there are none)
-        RDFFormat format = RDFWriterRegistry.getFormatForJenaWriter(jenaName) ;
+        // For writing via model.write(), use any old names for jena writers.
         RDFWriterBuilder builder = org.apache.jena.riot.RDFWriter.create();
-        if ( format != null )
-            return builder.format(format);
         Lang lang = RDFLanguages.nameToLang(jenaName);
         if ( lang != null )
             return builder.lang(lang);
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java 
b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
index 0b3ab74..489a8cb 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
@@ -185,6 +185,18 @@ public class StreamRDFWriter {
      *  and is guaranteed to do so in a scaling, streaming fashion.
      * @param output   OutputStream
      * @param format   The syntax (as an {@link RDFFormat})
+     * @return         StreamRDF, or null if format is not registered for 
streaming.
+     * @see StreamRDFOps#graphToStream
+     * @see StreamRDFOps#datasetToStream
+     */
+    public static StreamRDF getWriterStream(OutputStream output, RDFFormat 
format) {
+        return getWriterStream(output, format, null) ;
+    }
+
+    /** Get a StreamRDF destination that will output in syntax {@link 
RDFFormat}
+     *  and is guaranteed to do so in a scaling, streaming fashion.
+     * @param output   OutputStream
+     * @param format   The syntax (as an {@link RDFFormat})
      * @param context  Context
      * @return         StreamRDF, or null if format is not registered for 
streaming.
      * @see StreamRDFOps#graphToStream
@@ -232,21 +244,21 @@ public class StreamRDFWriter {
      *
      * @param output OutputStream
      * @param graph  Graph to write
-     * @param lang   Syntax
+     * @param format Syntax
      */
-    public static void write(OutputStream output, Graph graph, RDFFormat lang) 
{
-        write(output, graph, lang, null);
+    public static void write(OutputStream output, Graph graph, RDFFormat 
format) {
+        write(output, graph, format, null);
     }
 
     /** Write a Graph in streaming fashion
      *
      * @param output OutputStream
      * @param graph  Graph to write
-     * @param lang   Syntax
+     * @param format Syntax
      * @param context Context
      */
-    public static void write(OutputStream output, Graph graph, RDFFormat lang, 
Context context) {
-        StreamRDF stream = getWriterStream(output, lang, context) ;
+    public static void write(OutputStream output, Graph graph, RDFFormat 
format, Context context) {
+        StreamRDF stream = getWriterStream(output, format, context) ;
         StreamRDFOps.graphToStream(graph, stream) ;
     }
     /** Write a DatasetGraph in streaming fashion
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/TurtleShell.java 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/TurtleShell.java
index 2c6dd33..7c3a095 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/writer/TurtleShell.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/TurtleShell.java
@@ -139,6 +139,7 @@ public abstract class TurtleShell {
         // This is true for the main pretty printing then
         // false when we are clearing up unwritten triples.
         private boolean allowDeepPretty = true ;
+        private final boolean printTypeKeyword;
 
         private ShellGraph(Graph graph, Node graphName, DatasetGraph dsg, 
Set<Node> graphNames) {
             this.dsg = dsg ;
@@ -164,7 +165,7 @@ public abstract class TurtleShell {
             // Stop head of lists printed as triples going all the way to the
             // good part.
             nestedObjects.removeAll(listElts) ;
-
+            this.printTypeKeyword = ! 
prefixMap.getMapping().values().contains(rdfNS);
             //printDetails() ;
         }
 
@@ -655,7 +656,7 @@ public abstract class TurtleShell {
             Collection<Node> predicates = pGroups.keySet() ;
 
             // Find longest predicate URI
-            int predicateMaxWidth = RiotLib.calcWidth(prefixMap, baseURI, 
predicates, MIN_PREDICATE, LONG_PREDICATE) ;
+            int predicateMaxWidth = Widths.calcWidth(prefixMap, baseURI, 
predicates, MIN_PREDICATE, LONG_PREDICATE, printTypeKeyword) ;
 
             boolean first = true ;
 
@@ -744,7 +745,7 @@ public abstract class TurtleShell {
             }
             int colPredicateStart = out.getAbsoluteIndent() ;
 
-            if ( !prefixMap.containsPrefix(rdfNS) && RDF_type.equals(p) )
+            if ( printTypeKeyword && RDF_type.equals(p) )
                 print("a") ;
             else
                 writeNode(p) ;
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/Widths.java 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/Widths.java
new file mode 100644
index 0000000..4763f2c
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/Widths.java
@@ -0,0 +1,80 @@
+/*
+ * 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.writer;
+
+import java.util.Collection;
+
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.Triple;
+import org.apache.jena.riot.system.PrefixMap;
+import static org.apache.jena.riot.writer.WriterConst.RDF_type;
+
+/**
+ * Calculate widths to print in.
+ * <p>
+ * Flag {@code hasPrefixForRDF} is to avoid a reverse lookup in a prefix map.
+ */
+/*package*/ class Widths {
+
+    /*package*/ static int calcWidth(PrefixMap prefixMap, String baseURI, Node 
p, boolean printTypeKeyword) {
+        String x = prefixMap.abbreviate(p.getURI());
+        if ( x == null )
+            return p.getURI().length() + 2;
+        if ( printTypeKeyword && RDF_type.equals(p) ) {
+            // Use "a".
+            return 1;
+        }
+        return x.length();
+    }
+
+    /*package*/ static int calcWidth(PrefixMap prefixMap, String baseURI, 
Collection<Node> nodes, int minWidth, int maxWidth, boolean printTypeKeyword) {
+        Node prev = null;
+        int nodeMaxWidth = minWidth;
+
+        for ( Node n : nodes ) {
+            if ( prev != null && prev.equals(n) )
+                continue;
+            int len = calcWidth(prefixMap, baseURI, n, printTypeKeyword);
+            if ( len > maxWidth )
+                continue;
+            if ( nodeMaxWidth < len )
+                nodeMaxWidth = len;
+            prev = n;
+        }
+        return nodeMaxWidth;
+    }
+ 
+    /*package*/ static int calcWidthTriples(PrefixMap prefixMap, String 
baseURI, Collection<Triple> triples, int minWidth, int maxWidth, boolean 
printTypeKeyword) {
+        Node prev = null;
+        int nodeMaxWidth = minWidth;
+
+        for ( Triple triple : triples ) {
+            Node n = triple.getPredicate();
+            if ( prev != null && prev.equals(n) )
+                continue;
+            int len = calcWidth(prefixMap, baseURI, n, printTypeKeyword);
+            if ( len > maxWidth )
+                continue;
+            if ( nodeMaxWidth < len )
+                nodeMaxWidth = len;
+            prev = n;
+        }
+        return nodeMaxWidth;
+    }
+}
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterDatasetRIOTBase.java 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterDatasetRIOTBase.java
index e21e49a..4101968 100644
--- 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterDatasetRIOTBase.java
+++ 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterDatasetRIOTBase.java
@@ -23,20 +23,14 @@ import java.io.Writer ;
 
 import org.apache.jena.riot.WriterDatasetRIOT ;
 import org.apache.jena.riot.system.PrefixMap ;
-import org.apache.jena.riot.system.PrefixMapFactory ;
 import org.apache.jena.sparql.core.DatasetGraph ;
 import org.apache.jena.sparql.util.Context ;
 
 public abstract class WriterDatasetRIOTBase implements WriterDatasetRIOT
 {
-    private static PrefixMap prefixMap(DatasetGraph dataset)
-    {
-        return 
PrefixMapFactory.create(dataset.getDefaultGraph().getPrefixMapping()) ;
-    }
-    
     @Override
     public abstract void write(Writer out, DatasetGraph dataset, PrefixMap 
prefixMap, String baseURI, Context context) ;
-    
+
     @Override
     public abstract void write(OutputStream out, DatasetGraph dataset, 
PrefixMap prefixMap, String baseURI, Context context) ;
 }
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java
index 7556c10..b30fc9a 100644
--- 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java
+++ 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java
@@ -17,7 +17,7 @@
  */
 
 package org.apache.jena.riot.writer;
-
+import static org.apache.jena.riot.writer.WriterConst.rdfNS;
 import java.io.BufferedWriter ;
 import java.io.OutputStream ;
 import java.io.Writer ;
@@ -57,6 +57,8 @@ public abstract class WriterStreamRDFBase implements StreamRDF
     protected final IndentedWriter out ;
     protected final DirectiveStyle prefixStyle;
     protected final boolean printBase;
+    // Is there an active prefix mapping for the RDF namespace.
+    protected int countPrefixesForRDF = 0;
 
     public WriterStreamRDFBase(OutputStream output, Context context) {
         this(new IndentedWriter(output), context) ;
@@ -70,13 +72,13 @@ public abstract class WriterStreamRDFBase implements 
StreamRDF
         out = output ;
         pMap = PrefixMapFactory.create() ;
         nodeToLabel = NodeToLabel.createScopeByDocument() ;
-        
+
         // Stream writing does not take an external base URI from the API 
"write"
         // call. The base URI is output if StreamRDF.base() called, which 
means BASE
         // was in the data stream.
         baseURI = null ;
         prefixStyle = WriterLib.directiveStyle(context);
-        printBase = 
+        printBase =
             ( context == null ) ? true : 
context.isFalseOrUndef(RIOT.symTurtleOmitBase);
         setFormatter() ;
     }
@@ -133,15 +135,41 @@ public abstract class WriterStreamRDFBase implements 
StreamRDF
     @Override
     public final void prefix(String prefix, String iri) {
         endData();
+        prefixSetup(prefix, iri);
         lastWasDirective = true;
+        if ( pMap.containsPrefix(prefix) ) {
+            // Overwrite?
+            // Update for PrefixMap.get
+            String old = pMap.getMapping().get(prefix);
+            if ( rdfNS.equals(old) ) {
+                countPrefixesForRDF--;
+            }
+        }
+        if ( rdfNS.equals(iri) ) {
+            countPrefixesForRDF++;
+        }
+
         pMap.add(prefix, iri);
         RiotLib.writePrefix(out, prefix, iri, prefixStyle == 
DirectiveStyle.SPARQL);
     }
 
+    protected void prefixSetup(String prefix, String iri) {}
+
     protected void outputNode(Node n) {
         fmt.format(out, n);
     }
 
+    /**
+     * Helper for formats that wish to print 'a' or 'rdf:type' based
+     * on whether a prefix definition is in-scope.
+     */
+    protected void printProperty(Node p) {
+        if ( countPrefixesForRDF <= 0 && WriterConst.RDF_type.equals(p) )
+            out.print('a');
+        else
+            outputNode(p) ;
+    }
+
     // Subclass contract
 
     protected abstract void startData();
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java
index 9cd740f..d854d91 100644
--- 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java
+++ 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java
@@ -28,17 +28,16 @@ import org.apache.jena.atlas.io.IndentedWriter ;
 import org.apache.jena.graph.Node ;
 import org.apache.jena.graph.Triple ;
 import org.apache.jena.riot.other.G;
-import org.apache.jena.riot.system.RiotLib ;
 import org.apache.jena.sparql.core.Quad ;
 import org.apache.jena.sparql.util.Context;
 
-/** An output of triples / quads that print batches of same subject / same 
graph, same subject. 
+/** An output of triples / quads that print batches of same subject / same 
graph, same subject.
  *  It writes something that is easier to read than
  *  N-triples, N-quads but it's not full pretty printing
  *  which usually requires analysing the data before any output.
- *  
- *  If fed only quads, the output is valid TriG. 
- *  If fed only triples, the output is valid Turtle. 
+ *
+ *  If fed quads and triples, the output is valid TriG.
+ *  If fed only triples, the output is valid Turtle.
  */
 
 public class WriterStreamRDFBlocks extends WriterStreamRDFBatched
@@ -53,19 +52,21 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
     protected static final int MIN_PREDICATE        = 6 ; 
//WriterConst.MIN_PREDICATE ;
     protected static final int LONG_PREDICATE       = 
WriterConst.LONG_PREDICATE ;
     protected static final int LONG_SUBJECT         = WriterConst.LONG_SUBJECT 
;
-    
+
     protected static final int INDENT_MIN_S         = 6 ;         // Range of 
subject indent
     protected static final int INDENT_MAX_S         = 14 ;
-    protected static final int GAP_S_P              = 2 ;         
-    protected static final int GAP_P_O              = 2 ;         
-    
+    protected static final int GAP_S_P              = 2 ;
+    protected static final int GAP_P_O              = 2 ;
+
     protected static final int INDENT_GDFT          = 2 ;           // Default 
graph indent
     protected static final int INDENT_GNMD          = 4 ;           // Named 
graph indent
-    
+
     // Quad output
     protected Node lastGraph            = null ;
     protected Node lastSubject          = null ;
     protected int currentGraphIndent    = 0;
+    // Set true if we see a prefix setting for the RDF namespace.
+    protected boolean hasRDFprefix      = false;
 
     public WriterStreamRDFBlocks(OutputStream output, Context context) {
         super(output, context) ;
@@ -82,7 +83,7 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
     @Override
     protected void printBatchQuads(Node g, Node s, List<Quad> quads) {
         if ( g == null )
-            // print as Triples has currentGraph == null. 
+            // print as Triples has currentGraph == null.
             g = Quad.defaultGraphNodeGenerated ;
         if ( Objects.equals(g, lastGraph) ) {
             // Same graph, different subject.
@@ -111,6 +112,15 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
     }
 
     @Override
+    public void prefixSetup(String prefix, String iri) {
+        // Newline between blocks.
+        // After flush, before writing PREFIX
+        boolean addNL = ( activeTripleData || activeQuadData );
+        if ( addNL )
+            out.println();
+    }
+
+    @Override
     protected void printBatchTriples(Node s, List<Triple> triples) {
         startBatch();
         printBatch(s, triples) ;
@@ -118,7 +128,7 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
         out.println(" .") ;
         lastGraph = null;
     }
-        
+
     private void printBatch(Node s, List<Triple> triples) {
         outputNode(s) ;
         if ( out.getCol() > LONG_SUBJECT )
@@ -130,10 +140,12 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
         writePredicateObjectList(triples) ;
         out.decIndent(INDENT_PREDICATE) ;
     }
-        
+
     private void writePredicateObjectList(Collection<Triple> triples) {
         // Find width
-        int predicateMaxWidth = RiotLib.calcWidthTriples(pMap, baseURI, 
triples, MIN_PREDICATE, LONG_PREDICATE) ;
+        // We may have a prefix for RDF otherwise we use the 'a' abbreviation 
for rdf:type.
+        boolean writeKeyWordType = countPrefixesForRDF <= 0 ;
+        int predicateMaxWidth = Widths.calcWidthTriples(pMap, baseURI, 
triples, MIN_PREDICATE, LONG_PREDICATE, writeKeyWordType) ;
         boolean first = true ;
         for ( Triple triple : triples ) {
             if ( !first )
@@ -142,7 +154,7 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
                 first = false ;
 
             Node p = triple.getPredicate() ;
-            outputNode(p) ;
+            printProperty(p);
             out.pad(predicateMaxWidth) ;
             out.print(' ', GAP_P_O) ;
             Node o = triple.getObject() ;
@@ -165,7 +177,7 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
         // Start graph
         if ( lastGraph == null ) {
             boolean NL_START = (dftGraph(g) ? NL_GDFT_START : NL_GNMD_START) ;
-            
+
             lastSubject = null ;
             if ( !dftGraph(g) ) {
                 outputNode(g) ;
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java
index 7999c77..bf9d647 100644
--- 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java
+++ 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java
@@ -63,10 +63,10 @@ public class WriterStreamRDFFlat extends WriterStreamRDFBase
         Node s = triple.getSubject() ;
         Node p = triple.getPredicate() ;
         Node o = triple.getObject() ;
-        
+
         outputNode(s) ;
         out.print(' ') ;
-        outputNode(p) ;
+        printProperty(p);
         out.print(' ') ;
         outputNode(o) ;
         out.println(" .") ;
@@ -79,7 +79,7 @@ public class WriterStreamRDFFlat extends WriterStreamRDFBase
         Node s = quad.getSubject() ;
         Node p = quad.getPredicate() ;
         Node o = quad.getObject() ;
-        
+
         if ( g != null && ! Quad.isDefaultGraph(g) )
         {
             outputNode(g) ;
@@ -89,7 +89,7 @@ public class WriterStreamRDFFlat extends WriterStreamRDFBase
             out.print("{ ") ;
         outputNode(s) ;
         out.print(' ') ;
-        outputNode(p) ;
+        printProperty(p);
         out.print(' ') ;
         outputNode(o) ;
         out.println(" }") ;
diff --git 
a/jena-arq/src/test/java/org/apache/jena/riot/writer/TS_RiotWriter.java 
b/jena-arq/src/test/java/org/apache/jena/riot/writer/TS_RiotWriter.java
index f8a81f7..3a39a0e 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/writer/TS_RiotWriter.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/writer/TS_RiotWriter.java
@@ -28,7 +28,6 @@ import org.junit.runners.Suite.SuiteClasses ;
     TestWriterRegistration.class
     , TestRiotWriterGraph.class
     , TestRiotWriterDataset.class
-    , TestJenaWriters.class
     , TestRDFJSON.class
     , TestTurtleWriter.class
     , TestTriXWriter.class
diff --git 
a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJenaWriters.java 
b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJenaWriters.java
deleted file mode 100644
index c7dfff0..0000000
--- a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJenaWriters.java
+++ /dev/null
@@ -1,78 +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.jena.riot.writer;
-
-import static org.junit.Assert.assertArrayEquals;
-
-import java.io.ByteArrayOutputStream ;
-import java.util.ArrayList ;
-import java.util.List ;
-
-import org.apache.jena.atlas.lib.Bytes ;
-import org.apache.jena.rdf.model.Model ;
-import org.apache.jena.riot.RDFDataMgr ;
-import org.apache.jena.riot.RDFWriterRegistry ;
-import org.junit.Test ;
-import org.junit.runner.RunWith ;
-import org.junit.runners.Parameterized ;
-import org.junit.runners.Parameterized.Parameters ;
-
-@RunWith(Parameterized.class)
-public class TestJenaWriters extends AbstractWriterTest
-{
-    @Parameters(name = "{index}: {0}")
-    public static Iterable<Object[]> data() {
-        List<Object[]> x = new ArrayList<>() ;
-        for ( String wname :  RDFWriterRegistry.getJenaWriterNames())
-            x.add(new Object[]{wname}) ;
-        return x ; 
-    }
-
-    private String jenaFormatName ;
-    
-    public TestJenaWriters(String jenaFormatName)
-    {
-        this.jenaFormatName = jenaFormatName ;
-    }
-    
-    @Test public void jwrite_00() { test("writer-jena.ttl") ; }
-    
-    // More test don't really add anything.
-    // We are not testing the correctness of the writers,
-    // only the wiring up of the writers to model.write.
-
-    private void test(String filename) {
-        Model m = readModel(filename) ;
-        ByteArrayOutputStream out2 = new ByteArrayOutputStream() ;
-        RDFDataMgr.write(out2, m, 
RDFWriterRegistry.getFormatForJenaWriter(jenaFormatName)) ;
-
-        ByteArrayOutputStream out1 = new ByteArrayOutputStream() ;
-        m.write(out1, jenaFormatName) ;
-        
-        try {
-            assertArrayEquals("Format: "+jenaFormatName, out2.toByteArray(), 
out1.toByteArray()) ;
-        } catch (AssertionError ex) {
-            String s1 = Bytes.bytes2string(out1.toByteArray()) ;
-            String s2 = Bytes.bytes2string(out2.toByteArray()) ;
-            System.out.print(s1) ;
-            System.out.print(s2) ;
-            throw ex ;
-        }
-    }
-}

Reply via email to