Author: andy
Date: Thu Jan 31 22:11:09 2013
New Revision: 1441221

URL: http://svn.apache.org/viewvc?rev=1441221&view=rev
Log:
Organise stream writing

Added:
    jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java
    
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java
    jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java
Modified:
    jena/Experimental/riot-output/src/main/java/dev/MainWriter.java
    jena/Experimental/riot-output/src/main/java/dev/PROJECT_RiotWriter.java
    jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriter.java
    
jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterBlocks.java
    jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterFlat.java
    
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterBlocks.java
    
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterFlat.java
    
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFBlocks.java
    
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFFlat.java

Modified: jena/Experimental/riot-output/src/main/java/dev/MainWriter.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/dev/MainWriter.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/dev/MainWriter.java (original)
+++ jena/Experimental/riot-output/src/main/java/dev/MainWriter.java Thu Jan 31 
22:11:09 2013
@@ -55,6 +55,8 @@ public class MainWriter
     {
         Dataset ds = RDFDataMgr.loadDataset("W.trig") ;
         TriGWriter.write(System.out, ds) ;
+        
+        
     }
     
     public static void writeTTL()
@@ -63,25 +65,27 @@ public class MainWriter
         
         Model m = RDFDataMgr.loadModel(fn) ;
         
-        if ( false )
+        boolean ALL = false ; 
+        
+        if ( ALL || true )
         {
             System.out.println("# Write new (flat)") ;
             TurtleWriterFlat.write(System.out, m) ;
             System.out.println("----------------------------------") ;
         }
-        if ( false )
+        if ( ALL || false )
         {
             System.out.println("# Write new (Pretty)") ;
             TurtleWriter2.write(System.out, m) ;
             System.out.println("----------------------------------") ;
         }
-        if ( false )
+        if ( ALL || false )
         {
             System.out.println("# Write new (blocks)") ;
             TurtleWriterBlocks.write(System.out, m) ;
             System.out.println("----------------------------------") ;
         }
-        if ( false )
+        if ( ALL || false )
         {
             System.out.println("# Write old") ;
             m.write(System.out, "TTL") ;

Modified: 
jena/Experimental/riot-output/src/main/java/dev/PROJECT_RiotWriter.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/dev/PROJECT_RiotWriter.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/dev/PROJECT_RiotWriter.java 
(original)
+++ jena/Experimental/riot-output/src/main/java/dev/PROJECT_RiotWriter.java Thu 
Jan 31 22:11:09 2013
@@ -23,9 +23,17 @@ public class PROJECT_RiotWriter
     // Prefix map should generate Turtle legal abbrevs.
     //  Variations.
     
+    // Sort ouf WriteStreamRDF
+    //  Pull down Blocks-only material to *Blocks.
+    //  Widths for flat writing?
+    //  Start graph end graph
+    
     // Common super class for WriterStreamRDFBlocks/triples - it's the 
print(..) that is abstract.
     // Do numbers.s
     
+    // RDFSerialization - relationship to Lang.
+    // Lang+Style?
+    
     // NT and NQ writers? Or are RIOT current OK?
     
     // Interface and organisation.
@@ -37,7 +45,7 @@ public class PROJECT_RiotWriter
     //    TurtleWriter2
     //    TW2
 
-    // TODO - check.
+    // TODO - check for TODO
 
     // PredicateObjectLists
     //   type to front.

Added: jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java?rev=1441221&view=auto
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java 
(added)
+++ jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java Thu 
Jan 31 22:11:09 2013
@@ -0,0 +1,26 @@
+/**
+ * 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 riot;
+
+/** Constants for writable formats */  
+public class RDFSerialization
+{
+
+}
+

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriter.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriter.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriter.java 
(original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriter.java Thu 
Jan 31 22:11:09 2013
@@ -31,7 +31,6 @@ import com.hp.hpl.jena.sparql.core.Datas
 
 public class TriGWriter
 {
-    // UNFINISHED
     public static final int GRAPH_INDENT = 4 ;
     
     public static void write(OutputStream out, Dataset dataset)
@@ -55,7 +54,6 @@ public class TriGWriter
         iOut.flush() ;
     }
 
-    
     private static void writeGraph(IndentedWriter out, Node name, Graph graph, 
 Map<String, String> prefixMap)
     {
         if ( name != null )

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterBlocks.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterBlocks.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterBlocks.java 
(original)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterBlocks.java 
Thu Jan 31 22:11:09 2013
@@ -18,32 +18,19 @@
 
 package riot.writer;
 
-import java.util.Iterator ;
 import java.util.Map ;
-import java.util.Map.Entry ;
 
 import org.apache.jena.atlas.io.IndentedWriter ;
 import org.apache.jena.riot.system.StreamRDF ;
 
 import com.hp.hpl.jena.sparql.core.DatasetGraph ;
-import com.hp.hpl.jena.sparql.core.Quad ;
 
-public class TriGWriterBlocks
+public class TriGWriterBlocks extends WriterStreamBase
 {
-    public static void write(IndentedWriter out, DatasetGraph graph, 
Map<String, String> prefixes) 
+    public static void write(IndentedWriter out, DatasetGraph datasetGraph, 
Map<String, String> prefixes) 
     {
         StreamRDF dest = new WriterStreamRDFBlocks(out) ;
-        dest.start() ;
-        for ( Entry<String, String> e : prefixes.entrySet() )
-            dest.prefix(e.getKey(), e.getValue()) ;
-        Iterator<Quad> iter = graph.find(null, null, null, null) ;
-        for ( ; iter.hasNext() ; )
-        {
-            Quad q = iter.next() ;
-            dest.quad(q) ;
-        }
-        dest.finish() ;
+        write(dest, datasetGraph, prefixes) ;
     }
-
 }
 

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterFlat.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterFlat.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterFlat.java 
(original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/TriGWriterFlat.java 
Thu Jan 31 22:11:09 2013
@@ -18,8 +18,19 @@
 
 package riot.writer;
 
-public class TriGWriterFlat
-{
+import java.util.Map ;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.StreamRDF ;
 
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+
+public class TriGWriterFlat extends WriterStreamBase
+{
+    public static void write(IndentedWriter out, DatasetGraph datasetGraph, 
Map<String, String> prefixes) 
+    {
+        StreamRDF dest = new WriterStreamRDFFlat(out) ;
+        write(dest, datasetGraph, prefixes) ;
+    }
 }
 

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterBlocks.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterBlocks.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterBlocks.java 
(original)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterBlocks.java 
Thu Jan 31 22:11:09 2013
@@ -19,18 +19,15 @@
 package riot.writer;
 
 import java.io.OutputStream ;
-import java.util.Iterator ;
 import java.util.Map ;
-import java.util.Map.Entry ;
 
 import org.apache.jena.atlas.io.IndentedWriter ;
 import org.apache.jena.riot.system.StreamRDF ;
 
 import com.hp.hpl.jena.graph.Graph ;
-import com.hp.hpl.jena.graph.Triple ;
 import com.hp.hpl.jena.rdf.model.Model ;
 
-public class TurtleWriterBlocks
+public class TurtleWriterBlocks extends WriterStreamBase
 {
     public static void write(OutputStream out, Model model)
     {
@@ -46,18 +43,7 @@ public class TurtleWriterBlocks
     public static void write(IndentedWriter out, Graph graph, Map<String, 
String> prefixes) 
     {
         StreamRDF dest = new WriterStreamRDFBlocks(out) ;
-        dest.start() ;
-        for ( Entry<String, String> e : prefixes.entrySet() )
-            dest.prefix(e.getKey(), e.getValue()) ;
-        Iterator<Triple> iter = graph.find(null, null, null) ;
-        for ( ; iter.hasNext() ; )
-        {
-            Triple t = iter.next() ;
-            dest.triple(t) ;
-        }
-        dest.finish() ;
+        write(dest, graph, prefixes) ;
     }
-    
-    
 }
 

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterFlat.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterFlat.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterFlat.java 
(original)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterFlat.java 
Thu Jan 31 22:11:09 2013
@@ -19,68 +19,31 @@
 package riot.writer;
 
 import java.io.OutputStream ;
-import java.util.Iterator ;
 import java.util.Map ;
 
 import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.StreamRDF ;
 
 import com.hp.hpl.jena.graph.Graph ;
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
 import com.hp.hpl.jena.rdf.model.Model ;
 
 /** Write Turtle as one line of prefixed names */   
-public class TurtleWriterFlat
+public class TurtleWriterFlat extends WriterStreamBase
 {
-    static public final int colWidth = 8 ; 
-    static public final int predCol = 8 ;
-    static public final int objCol = 8+predCol ;
-    
     public static void write(OutputStream out, Model model)
     {
         IndentedWriter iOut = new IndentedWriter(out) ;
         write(iOut, model.getGraph(), model.getNsPrefixMap()) ;
     }
-    
+
     public static void write(IndentedWriter out, Model model)
     {
-        write(out, model.getGraph(), model.getNsPrefixMap()) ;
+        write(out, model.getGraph(),  model.getNsPrefixMap()) ;
     }
-    
-    public static void write(OutputStream out, Graph graph, Map<String, 
String> prefixes)
-    {
-        IndentedWriter iOut = new IndentedWriter(out) ;
-        write(iOut, graph, prefixes) ;
-    }
-    
+
     public static void write(IndentedWriter out, Graph graph, Map<String, 
String> prefixes) 
     {
-        TW2.writePrefixes(out, prefixes) ;
-        if ( ! prefixes.isEmpty() )
-            out.println();
-        
-        Iterator<Triple> iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ;
-        
-        for ( ; iter.hasNext() ; )
-        {
-            Triple triple = iter.next() ;
-            
-            Node s = triple.getSubject() ;
-            TW2.writeNode(out, prefixes, s) ;
-            
-            Node p = triple.getPredicate() ;
-            //out.pad(predCol-1) ;
-            out.print(" ") ;
-            TW2.writeNode(out, prefixes, p) ;
-            
-            Node o = triple.getObject() ;
-            //out.pad(objCol-1) ;
-            out.print(" ") ;
-            TW2.writeNode(out, prefixes, o) ;
-
-            out.println(" .") ; 
-        }
-        
-        out.flush() ;
+        StreamRDF dest = new WriterStreamRDFFlat(out) ;
+        write(dest, graph, prefixes) ;
     }
 }

Added: 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java?rev=1441221&view=auto
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java 
(added)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java 
Thu Jan 31 22:11:09 2013
@@ -0,0 +1,83 @@
+/**
+ * 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 riot.writer;
+
+import java.util.Iterator ;
+import java.util.Map ;
+import java.util.Map.Entry ;
+
+import org.apache.jena.riot.system.StreamRDF ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+
+public abstract class WriterStreamBase
+{
+    protected static void finish(StreamRDF dest)
+    { dest.finish() ; }
+
+    protected static void start(StreamRDF dest)
+    { dest.start() ; }
+
+    protected static void writePrefixes(StreamRDF dest, Map<String, String> 
prefixes)
+    {
+        for ( Entry<String, String> e : prefixes.entrySet() )
+            dest.prefix(e.getKey(), e.getValue()) ;
+    }
+
+    protected static void writeTriples(StreamRDF dest, Iterator<Triple> iter)
+    {
+        for ( ; iter.hasNext() ; )
+        {
+            Triple t = iter.next() ;
+            dest.triple(t) ;
+        }
+    }
+
+    protected static void writeQuads(StreamRDF dest, Iterator<Quad> iter)
+    {
+        for ( ; iter.hasNext() ; )
+        {
+            Quad q = iter.next() ;
+            dest.quad(q) ;
+        }
+    }
+
+    protected static void write(StreamRDF dest, DatasetGraph datasetGraph, 
Map<String, String> prefixes) 
+    {
+        start(dest) ;
+        writePrefixes(dest, prefixes) ;
+        Iterator<Quad> iter = datasetGraph.find(null, null, null, null) ;
+        writeQuads(dest, iter) ;
+        finish(dest) ;
+    }
+
+    protected static void write(StreamRDF dest, Graph graph, Map<String, 
String> prefixes) 
+    {
+        start(dest) ;
+        writePrefixes(dest, prefixes) ;
+        Iterator<Triple> iter = graph.find(null, null, null) ;
+        writeTriples(dest, iter) ;
+        finish(dest) ;
+    }
+    
+}
+

Added: 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java?rev=1441221&view=auto
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java 
(added)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java 
Thu Jan 31 22:11:09 2013
@@ -0,0 +1,283 @@
+/**
+ * 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 riot.writer;
+
+import static org.apache.jena.atlas.lib.Lib.equal ;
+
+import java.io.OutputStream ;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.atlas.lib.Tuple ;
+import org.apache.jena.riot.out.NodeToLabel ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
+import org.apache.jena.riot.system.StreamRDF ;
+import riot.out.NodeFormatterTTL ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+
+/** Core engine for output of triples / quads that is streaming.
+ * 
+ *  If fed only quads, the output is valid TriG. 
+ *  If fed only triples, the output is valid Turtle. 
+ */
+
+public abstract class WriterStreamRDF implements StreamRDF
+{
+    // TODO
+    // Min width for properties?
+    // better layering and state machine
+    
+    // When false, gets 
+    // :g1 { ... }
+    // :g2 { ... }
+    protected static final boolean NL_GDFT_START  =  false ;      // Newline 
after { of dft graph
+    protected static final boolean NL_GNMD_START  =  true ;       // same, but 
for named graphs
+    protected static final boolean NL_GDFT_END    =  false ;      // Whether 
the closing } is on a new line
+    protected static final boolean NL_GNMD_END    =  false ;      // Whether 
the closing } is on a new line
+    
+    protected static final int INDENT_MIN_S       = 4 ;           // Range of 
subject indent
+    protected static final int INDENT_MAX_S       = 10 ;          //   i.e. 
offset of predicate.
+
+    protected static final int INDENT_GDFT        = 2 ;           // Default 
graph indent
+    protected static final int INDENT_GNMD        = 4 ;           // Named 
graph indent
+    
+    protected boolean lastWasTripleData = false ;
+    protected boolean lastWasQuadData = false ;
+    protected boolean lastWasDirective = false ;
+
+    protected final PrefixMap pMap ;
+    protected final NodeFormatterTTL fmt ;
+    protected final IndentedWriter out ;
+
+    protected Node lastSubject = null ;
+    protected Node lastGraph = null ;     // null -> unset : Quad. 
+    
+    // --- Indent management.
+    protected int currentGraphIndent = 0;
+    protected int currentSubjectIndent = 0 ;
+
+    protected void setGraphIndent(int x)
+    {
+        currentGraphIndent = x ;
+    }
+
+    protected void setSubjectIndent(int x)
+    {
+        if ( x < INDENT_MIN_S ) x = INDENT_MIN_S ;
+        if ( x > INDENT_MAX_S ) x = INDENT_MAX_S ;
+        currentSubjectIndent = x ;
+    }
+    
+    protected int graphIndent() { return currentGraphIndent ; }
+    protected int subjectIndent() { return currentSubjectIndent ; }
+    // ----
+    public WriterStreamRDF(OutputStream output)
+    { 
+        this(new IndentedWriter(output)) ;
+
+    }
+    public WriterStreamRDF(IndentedWriter output)
+    { 
+        out = output ;
+        pMap = PrefixMapFactory.create() ;
+        fmt = new NodeFormatterTTL(null, pMap, 
NodeToLabel.createScopeByDocument()) ; 
+    }
+
+
+    @Override
+    public void start()
+    {
+        reset() ;
+    }
+
+    @Override
+    public void finish()
+    {
+        endData() ;
+        out.flush() ;
+    }
+
+    @Override
+    public void triple(Triple triple)
+    {
+        Node s = triple.getSubject() ;
+        Node p = triple.getPredicate() ;
+        Node o = triple.getObject() ;
+        print(s,p,o) ;
+    }
+
+    @Override
+    public void quad(Quad quad)
+    {
+        Node g = quad.getGraph() ;
+        Node s = quad.getSubject() ;
+        Node p = quad.getPredicate() ;
+        Node o = quad.getObject() ;
+        if ( g == null )
+            // Don't have g as null for a triple.
+            g = Quad.defaultGraphNodeGenerated ;
+        print(g, s, p, o) ;
+    }
+
+    @Override
+    public void tuple(Tuple<Node> tuple)
+    {}
+
+    @Override
+    public void base(String base)
+    {
+        
+    }
+
+    @Override
+    public void prefix(String prefix, String iri)
+    {
+        endData() ;
+        lastWasDirective = true ;
+    
+        out.print("@prefix ") ;
+        out.print(prefix) ;
+        out.print(":  <") ;
+        out.print(iri) ;        // Don't let it be abbreviated!
+        out.println("> .") ; 
+        pMap.add(prefix, iri) ;
+    }
+
+    protected void outputNode(Node n)
+    {
+        fmt.format(out, n) ;
+    }
+    
+    protected void reset() 
+    {
+        lastWasTripleData = false ;
+        lastWasQuadData = false ;
+        lastWasDirective = false ;
+        lastSubject = null ;
+        lastGraph = null ;
+        // Shouldn't be necessary - if it is some other code is not managing 
inc/dec indents.
+        //out.setAbsoluteIndent(0) ;
+    }
+    
+    protected void printPre()
+    {
+        // Directive -> data transition.
+        if ( lastWasDirective )
+        {
+            reset() ;
+            out.print('\n') ;
+        }
+    }
+    
+    protected void DEBUG(String fmt, Object...args)
+    {
+        int loc = out.getCol() ;            // Absolute
+        int off = out.getAbsoluteIndent() ;
+        out.ensureStartOfLine();
+        out.setAbsoluteIndent(0) ;
+        out.println(String.format(fmt, args)) ;
+        out.setAbsoluteIndent(off) ;
+        out.ensureStartOfLine();
+        out.pad(loc, true) ;
+    }
+    
+    protected abstract void print(Node g, Node s, Node p, Node o) ;
+    
+    protected abstract void print(Node s, Node p, Node o) ;
+    
+    protected boolean dftGraph() { return lastGraph == 
Quad.defaultGraphNodeGenerated ; }
+
+    protected void endGraph(Node g)
+    {
+        if ( lastGraph == null )
+            return ;
+            
+        // End of graph
+        if ( ! equal(lastGraph, g) )
+        {
+            out.print(" .") ;
+            boolean nl = dftGraph() ? NL_GDFT_END : NL_GNMD_END ;
+            
+            if ( nl ) {
+                // } on a new line.
+                out.decIndent(graphIndent()) ;
+                out.println() ;
+                out.println("}") ;
+            } else {
+                // } on same line as last quad/triple.
+                out.println(" }") ;     // Trailing NL  
+                out.decIndent(graphIndent()) ;
+                out.println() ;
+            }
+            lastSubject = null ;
+            lastGraph = null ;
+        }
+    }
+
+    protected void startGraph(Node g)
+    {
+        // Start graph
+        if ( lastGraph == null )
+        {
+            lastSubject = null ;
+            if ( g == Quad.defaultGraphNodeGenerated )
+            {
+                out.print("{ ") ;
+                setGraphIndent(INDENT_GDFT) ;
+                if ( NL_GDFT_START )
+                    out.println();
+            }
+            else
+            {
+                outputNode(g) ;
+                out.print(" { ") ;
+                if ( NL_GNMD_START )
+                {
+                    out.println() ;
+                    setGraphIndent(INDENT_GNMD) ;
+                }
+                else
+                {
+                    setGraphIndent(out.getCol()) ;
+                }
+            }
+            out.incIndent(graphIndent()) ;
+        }
+        lastGraph = g ;
+    }
+
+    protected void endTriples()
+    {
+        out.println(" .") ;
+        lastSubject = null ;
+    }
+    
+    protected void endData()
+    {
+        if ( lastWasQuadData )
+            endGraph(null) ;
+        if ( lastWasTripleData )
+            endTriples() ;
+        reset() ;
+    }
+
+}

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFBlocks.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFBlocks.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFBlocks.java
 (original)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFBlocks.java
 Thu Jan 31 22:11:09 2013
@@ -23,16 +23,8 @@ import static org.apache.jena.atlas.lib.
 import java.io.OutputStream ;
 
 import org.apache.jena.atlas.io.IndentedWriter ;
-import org.apache.jena.atlas.lib.Tuple ;
-import org.apache.jena.riot.out.NodeToLabel ;
-import org.apache.jena.riot.system.PrefixMap ;
-import org.apache.jena.riot.system.PrefixMapFactory ;
-import org.apache.jena.riot.system.StreamRDF ;
-import riot.out.NodeFormatterTTL ;
 
 import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.sparql.core.Quad ;
 
 /** An output of triples / quads that is streaming.
  *  It writes something that is easier to read than
@@ -44,167 +36,19 @@ import com.hp.hpl.jena.sparql.core.Quad 
  *  If fed only triples, the output is valid Turtle. 
  */
 
-public class WriterStreamRDFBlocks implements StreamRDF
+public class WriterStreamRDFBlocks extends WriterStreamRDF
 {
-    // TODO
-    // Min width for properties?
-    // better layering and state machine
-    
-    // When false, gets 
-    // :g1 { ... }
-    // :g2 { ... }
-    private static final boolean NL_GDFT_START  =  false ;      // Newline 
after { of dft graph
-    private static final boolean NL_GNMD_START  =  true ;       // same, but 
for named graphs
-    private static final boolean NL_GDFT_END    =  false ;      // Whether the 
closing } is on a new line
-    private static final boolean NL_GNMD_END    =  false ;      // Whether the 
closing } is on a new line
-    
-    private static final int INDENT_MIN_S       = 4 ;           // Range of 
subject indent
-    private static final int INDENT_MAX_S       = 10 ;          //   i.e. 
offset of predicate.
-
-    private static final int INDENT_GDFT        = 2 ;           // Default 
graph indent
-    private static final int INDENT_GNMD        = 4 ;           // Named graph 
indent
-    
-    private boolean lastWasTripleData = false ;
-    private boolean lastWasQuadData = false ;
-    private boolean lastWasDirective = false ;
-
-    private final PrefixMap pMap ;
-    private final NodeFormatterTTL fmt ;
-    private final IndentedWriter out ;
-
-    private Node lastSubject = null ;
-    private Node lastGraph = null ;     // null -> unset : Quad. 
-    
-    // --- Indent management.
-    private int currentGraphIndent = 0;
-    private int currentSubjectIndent = 0 ;
-
-    private void setGraphIndent(int x)
-    {
-        currentGraphIndent = x ;
-    }
-
-    private void setSubjectIndent(int x)
-    {
-        if ( x < INDENT_MIN_S ) x = INDENT_MIN_S ;
-        if ( x > INDENT_MAX_S ) x = INDENT_MAX_S ;
-        currentSubjectIndent = x ;
-    }
-    
-    private int graphIndent() { return currentGraphIndent ; }
-    private int subjectIndent() { return currentSubjectIndent ; }
-    // ----
     public WriterStreamRDFBlocks(OutputStream output)
     { 
-        this(new IndentedWriter(output)) ;
-
+        super(output) ;
     }
     public WriterStreamRDFBlocks(IndentedWriter output)
-    { 
-        out = output ;
-        pMap = PrefixMapFactory.create() ;
-        fmt = new NodeFormatterTTL(null, pMap, 
NodeToLabel.createScopeByDocument()) ; 
-    }
-
-
-    @Override
-    public void start()
-    {
-        reset() ;
-    }
-
-    @Override
-    public void finish()
-    {
-        endData() ;
-        out.flush() ;
-    }
-
-    @Override
-    public void triple(Triple triple)
-    {
-        Node s = triple.getSubject() ;
-        Node p = triple.getPredicate() ;
-        Node o = triple.getObject() ;
-        print(s,p,o) ;
-    }
-
-    @Override
-    public void quad(Quad quad)
-    {
-        Node g = quad.getGraph() ;
-        Node s = quad.getSubject() ;
-        Node p = quad.getPredicate() ;
-        Node o = quad.getObject() ;
-        if ( g == null )
-            // Don't have g as null for a triple.
-            g = Quad.defaultGraphNodeGenerated ;
-        print(g, s, p, o) ;
-    }
-
-    @Override
-    public void tuple(Tuple<Node> tuple)
-    {}
-
-    @Override
-    public void base(String base)
     {
-        
+        super(output) ;
     }
 
     @Override
-    public void prefix(String prefix, String iri)
-    {
-        endData() ;
-        lastWasDirective = true ;
-    
-        out.print("@prefix ") ;
-        out.print(prefix) ;
-        out.print(":  <") ;
-        out.print(iri) ;        // Don't let it be abbreviated!
-        out.println("> .") ; 
-        pMap.add(prefix, iri) ;
-    }
-
-    private void outputNode(Node n)
-    {
-        fmt.format(out, n) ;
-    }
-    
-    private void reset() 
-    {
-        lastWasTripleData = false ;
-        lastWasQuadData = false ;
-        lastWasDirective = false ;
-        lastSubject = null ;
-        lastGraph = null ;
-        // Shouldn't be necessary - if it is some other code is not managing 
inc/dec indents.
-        //out.setAbsoluteIndent(0) ;
-    }
-    
-    private void printPre()
-    {
-        // Directive -> data transition.
-        if ( lastWasDirective )
-        {
-            reset() ;
-            out.print('\n') ;
-        }
-    }
-    
-    private void DEBUG(String fmt, Object...args)
-    {
-        int loc = out.getCol() ;            // Absolute
-        int off = out.getAbsoluteIndent() ;
-        out.ensureStartOfLine();
-        out.setAbsoluteIndent(0) ;
-        out.println(String.format(fmt, args)) ;
-        out.setAbsoluteIndent(off) ;
-        out.ensureStartOfLine();
-        out.pad(loc, true) ;
-    }
-    
-    private void print(Node g, Node s, Node p, Node o)
+    protected void print(Node g, Node s, Node p, Node o)
     {
         printPre() ;
         endGraph(g) ;
@@ -214,7 +58,8 @@ public class WriterStreamRDFBlocks imple
         lastWasQuadData = true ;
     }
     
-    private void print(Node s, Node p, Node o)
+    @Override
+    protected void print(Node s, Node p, Node o)
     {
         printPre() ;
         print$(s, p, o) ;
@@ -240,81 +85,4 @@ public class WriterStreamRDFBlocks imple
         outputNode(o) ;
         lastSubject = s ;
     }
-    
-    private boolean dftGraph() { return lastGraph == 
Quad.defaultGraphNodeGenerated ; }
-
-    private void endGraph(Node g)
-    {
-        if ( lastGraph == null )
-            return ;
-            
-        // End of graph
-        if ( ! equal(lastGraph, g) )
-        {
-            out.print(" .") ;
-            boolean nl = dftGraph() ? NL_GDFT_END : NL_GNMD_END ;
-            
-            if ( nl ) {
-                // } on a new line.
-                out.decIndent(graphIndent()) ;
-                out.println() ;
-                out.println("}") ;
-            } else {
-                // } on same line as last quad/triple.
-                out.println(" }") ;     // Trailing NL  
-                out.decIndent(graphIndent()) ;
-                out.println() ;
-            }
-            lastSubject = null ;
-            lastGraph = null ;
-        }
-    }
-
-    private void startGraph(Node g)
-    {
-        // Start graph
-        if ( lastGraph == null )
-        {
-            lastSubject = null ;
-            if ( g == Quad.defaultGraphNodeGenerated )
-            {
-                out.print("{ ") ;
-                setGraphIndent(INDENT_GDFT) ;
-                if ( NL_GDFT_START )
-                    out.println();
-            }
-            else
-            {
-                outputNode(g) ;
-                out.print(" { ") ;
-                if ( NL_GNMD_START )
-                {
-                    out.println() ;
-                    setGraphIndent(INDENT_GNMD) ;
-                }
-                else
-                {
-                    setGraphIndent(out.getCol()) ;
-                }
-            }
-            out.incIndent(graphIndent()) ;
-        }
-        lastGraph = g ;
-    }
-
-    private void endTriples()
-    {
-        out.println(" .") ;
-        lastSubject = null ;
-    }
-    
-    private void endData()
-    {
-        if ( lastWasQuadData )
-            endGraph(null) ;
-        if ( lastWasTripleData )
-            endTriples() ;
-        reset() ;
-    }
-
 }

Modified: 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFFlat.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFFlat.java?rev=1441221&r1=1441220&r2=1441221&view=diff
==============================================================================
--- 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFFlat.java
 (original)
+++ 
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDFFlat.java
 Thu Jan 31 22:11:09 2013
@@ -18,12 +18,11 @@
 
 package riot.writer;
 
-import org.apache.jena.atlas.lib.Tuple ;
-import org.apache.jena.riot.system.StreamRDF ;
+import java.io.OutputStream ;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
 
 import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.graph.Triple ;
-import com.hp.hpl.jena.sparql.core.Quad ;
 
 /** An output of triples / quads that is streaming.
  *  It writes N-triples/N-quads + prefixes.
@@ -32,34 +31,45 @@ import com.hp.hpl.jena.sparql.core.Quad 
  *  If fed only triples, the output is valid Turtle. 
  */
 
-public class WriterStreamRDFFlat implements StreamRDF
+public class WriterStreamRDFFlat extends WriterStreamRDF
 {
-
-    @Override
-    public void start()
-    {}
-
-    @Override
-    public void triple(Triple triple)
-    {}
-
-    @Override
-    public void quad(Quad quad)
-    {}
-
-    @Override
-    public void tuple(Tuple<Node> tuple)
-    {}
-
-    @Override
-    public void base(String base)
-    {}
-
-    @Override
-    public void prefix(String prefix, String iri)
-    {}
-
-    @Override
-    public void finish()
-    {}
+    public WriterStreamRDFFlat(OutputStream output)
+    { 
+        super(output) ;
+    }
+    
+    public WriterStreamRDFFlat(IndentedWriter output)
+    {
+        super(output) ;
+        
+    }
+
+    @Override
+    protected void print(Node g, Node s, Node p, Node o)
+    {
+        printPre() ;
+        endGraph(g) ;
+        startGraph(g) ;
+        print$(s,p,o) ;
+        lastGraph = g ;
+        lastWasQuadData = true ;
+    }
+    
+    @Override
+    protected void print(Node s, Node p, Node o)
+    {
+        printPre() ;
+        print$(s, p, o) ;
+        lastWasTripleData = true ;
+    }
+    
+    private void print$(Node s, Node p, Node o)
+    {
+        outputNode(s) ;
+        out.print("  ") ;
+        outputNode(p) ;
+        out.print("  ") ;
+        outputNode(o) ;
+        out.println(" .") ;
+    }
 }


Reply via email to