Repository: jena
Updated Branches:
  refs/heads/master abce36d81 -> 2ce86cdd7


JENA-1504: Fix batch handling.

Fix new line output.

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

Branch: refs/heads/master
Commit: f3549cabc95113412aa6d7e2e82066f0d836cd1d
Parents: 46e2f56
Author: Andy Seaborne <a...@apache.org>
Authored: Fri Mar 9 18:33:50 2018 +0000
Committer: Andy Seaborne <a...@apache.org>
Committed: Fri Mar 9 18:35:44 2018 +0000

----------------------------------------------------------------------
 .../riot/writer/WriterStreamRDFBatched.java     | 67 +++++++++-----------
 .../jena/riot/writer/WriterStreamRDFBlocks.java | 30 ++++-----
 2 files changed, 44 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/f3549cab/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java
----------------------------------------------------------------------
diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java
 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java
index 87573d5..3572643 100644
--- 
a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java
+++ 
b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java
@@ -68,75 +68,68 @@ abstract class WriterStreamRDFBatched extends 
WriterStreamRDFBase
         batchQuads = null ;
     }
 
-    @Override
-    protected final void print(Quad quad) {
-        if ( false ) {
-            // Merge to a triple stream.
-            triple(quad.asTriple()) ;
-            return ;
-        }
-
-        Node g = quad.getGraph() ;
-        Node s = quad.getSubject() ;
-
+    private void batch(Node g, Node s, boolean forTriples) {
         if ( !Objects.equals(g, currentGraph) || !Objects.equals(s, 
currentSubject) ) {
-            if ( currentSubject != null ) {
-                if ( currentGraph == null )
-                    finishBatchTriples(currentSubject) ;
-                else
-                    finishBatchQuad(currentGraph, currentSubject) ;
-            }
-            startBatchQuad(g, s) ;
+            finishBatchTriples(currentSubject) ;
+            finishBatchQuad(currentGraph, currentSubject) ;
+            if ( forTriples )
+                startBatchTriple(s);
+            else
+                startBatchQuad(g, s);
             currentGraph = g ;
             currentSubject = s ;
         }
-        processQuad(quad) ;
     }
 
     @Override
     protected final void print(Triple triple) {
         Node s = triple.getSubject() ;
-        if ( !Objects.equals(s, currentSubject) ) {
-            if ( currentSubject != null )
-                finishBatchTriples(currentSubject) ;
-            startBatchTriple(s) ;
-
-            currentGraph = null ;
-            currentSubject = s ;
-        }
+        batch(null, s, true);
         processTriple(triple) ;
     }
 
     private void startBatchTriple(Node subject) {
         batchTriples = new ArrayList<>() ;
-    }
-
-    private void processTriple(Triple triple) {
-        batchTriples.add(triple) ;
+        this.currentGraph = null;
+        this.currentSubject = subject;
     }
 
     private void finishBatchTriples(Node subject) {
-        if ( batchTriples != null && batchTriples.size() > 0 ) {
+        if ( batchTriples != null && !batchTriples.isEmpty() ) {
             printBatchTriples(currentSubject, batchTriples) ;
             batchTriples.clear() ;
         }
     }
 
-    private void startBatchQuad(Node graph, Node subject) {
-        batchQuads = new ArrayList<>() ;
+    private void processTriple(Triple triple) {
+        batchTriples.add(triple) ;
     }
 
-    private void processQuad(Quad Quad) {
-        batchQuads.add(Quad) ;
+    @Override
+    protected final void print(Quad quad) {
+        Node g = quad.getGraph() ;
+        Node s = quad.getSubject() ;
+        batch(g, s, false);
+        processQuad(quad) ;
+    }
+
+    private void startBatchQuad(Node graph, Node subject) {
+        batchQuads = new ArrayList<>() ;
+        this.currentGraph = graph;
+        this.currentSubject = subject;
     }
 
     private void finishBatchQuad(Node graph, Node subject) {
-        if ( batchQuads != null && batchQuads.size() > 0 ) {
+        if ( batchQuads != null && !batchQuads.isEmpty() ) {
             printBatchQuads(currentGraph, currentSubject, batchQuads) ;
             batchQuads.clear() ;
         }
     }
 
+    private void processQuad(Quad quad) {
+        batchQuads.add(quad) ;
+    }
+
     protected abstract void printBatchQuads(Node g, Node s, List<Quad> batch) ;
 
     protected abstract void printBatchTriples(Node s, List<Triple> batch) ;

http://git-wip-us.apache.org/repos/asf/jena/blob/f3549cab/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java
----------------------------------------------------------------------
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 64b4297..97e179f 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
@@ -64,7 +64,6 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
     // Quad output
     protected Node lastGraph            = null ;
     protected Node lastSubject          = null ;
-    protected boolean firstGraph        = true ;
     protected int currentGraphIndent    = 0;
 
     public WriterStreamRDFBlocks(OutputStream output) {
@@ -82,13 +81,13 @@ 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. 
             g = Quad.defaultGraphNodeGenerated ;
         if ( Objects.equals(g, lastGraph) ) {
             // Same graph, different subject.
             out.println(" .") ;
-            out.println() ;
         } else {
-            // Start graph
+            // Different graph
             endGraph(g) ;
             startGraph(g) ;
             lastGraph = g ;
@@ -99,21 +98,24 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
         lastSubject = s ;
     }
 
+    private void startBatch() {
+        // Any output so far? prefixes or a previous graph.
+        if ( out.getRow() > 1 )
+            out.println() ;
+        out.flush();
+    }
+
     private void gap(int gap) {
         out.print(' ', gap) ;
     }
 
     @Override
     protected void printBatchTriples(Node s, List<Triple> triples) {
-        // Blank line?
-        // Not if not prefixes and first batch.
-        if ( out.getRow() > 1 )
-            out.println() ;
-
+        startBatch();
         printBatch(s, triples) ;
         // End of cluster.
-        out.print(" .") ;
-        out.println() ;
+        out.println(" .") ;
+        lastGraph = null;
     }
         
     private void printBatch(Node s, List<Triple> triples) {
@@ -158,14 +160,11 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
     protected boolean dftGraph(Node g)  { return g == 
Quad.defaultGraphNodeGenerated ; }
 
     protected void startGraph(Node g) {
+        startBatch();
         // Start graph
         if ( lastGraph == null ) {
             boolean NL_START = (dftGraph(g) ? NL_GDFT_START : NL_GNMD_START) ;
-
-            if ( !firstGraph )
-                out.println() ;
-            firstGraph = false ;
-
+            
             lastSubject = null ;
             if ( !dftGraph(g) ) {
                 outputNode(g) ;
@@ -218,5 +217,4 @@ public class WriterStreamRDFBlocks extends 
WriterStreamRDFBatched
 
     protected void setGraphIndent(int x)    { currentGraphIndent = x ; }
     protected int graphIndent()             { return currentGraphIndent ; }
-
 }

Reply via email to