You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2013/02/07 19:48:50 UTC
svn commit: r1443653 - in /jena/Experimental/riot-output/src/main/java: dev/
riot/ riot/writer/
Author: andy
Date: Thu Feb 7 18:48:49 2013
New Revision: 1443653
URL: http://svn.apache.org/r1443653
Log: (empty)
Removed:
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriterOLD.java
jena/Experimental/riot-output/src/main/java/riot/writer/WriterContext.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/RDFSerialization.java
jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java
jena/Experimental/riot-output/src/main/java/riot/WriterVariant.java
jena/Experimental/riot-output/src/main/java/riot/writer/TW2.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/TurtleShell.java
jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriter.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/WriterStreamBase.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=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/dev/MainWriter.java (original)
+++ jena/Experimental/riot-output/src/main/java/dev/MainWriter.java Thu Feb 7 18:48:49 2013
@@ -50,8 +50,8 @@ public class MainWriter
{
public static void main(String ...args)
{
- //writeTTL() ;
- writeTriG() ;
+ writeTTL() ;
+ //writeTriG() ;
}
public static void writeTriG()
@@ -67,7 +67,7 @@ public class MainWriter
String fn = "D.ttl" ;
Model m = RDFDataMgr.loadModel(fn) ;
- RDFDataMgr.read(m, "W.ttl") ;
+ //RDFDataMgr.read(m, "W.ttl") ;
boolean ALL = false ;
int written = 0 ;
@@ -105,9 +105,9 @@ public class MainWriter
System.out.println("# Write (Pretty)") ;
- RiotWriter.write(System.out, m.getGraph(), Lang.TURTLE) ;
+ RiotWriter.write(System.out, m.getGraph(), Lang.TRIG) ;
System.out.println("----------------------------------") ;
-
+ if ( true ) return ;
// check.
ByteArrayOutputStream out = new ByteArrayOutputStream() ;
RiotWriter.write(out, m.getGraph(), Lang.TURTLE) ;
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=1443653&r1=1443652&r2=1443653&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 Feb 7 18:48:49 2013
@@ -18,8 +18,13 @@
package dev;
+import org.apache.jena.riot.system.Prologue ;
+
public class PROJECT_RiotWriter
{
+
+ Prologue p ;
+ // Prologue
// JSON-LD
// check prefixes from model.
// Remove prefixes options on statics
Modified: 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=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/RDFSerialization.java Thu Feb 7 18:48:49 2013
@@ -69,6 +69,12 @@ public class RDFSerialization
return true ;
}
-
+ @Override
+ public String toString()
+ {
+ if ( variant == null )
+ return lang.getName() ;
+ return lang.getName()+"/"+variant ;
+ }
}
Modified: jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java?rev=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java Thu Feb 7 18:48:49 2013
@@ -18,7 +18,7 @@
package riot;
-import static org.apache.jena.riot.RDFLanguages.TURTLE ;
+import static org.apache.jena.riot.RDFLanguages.* ;
import java.io.OutputStream ;
import java.util.HashMap ;
@@ -29,8 +29,12 @@ import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RDFLanguages ;
import org.apache.jena.riot.RiotException ;
import riot.writer.TurtleWriter ;
+import riot.writer.TurtleWriterBlocks ;
+import riot.writer.TriGWriter ;
+import riot.writer.TriGWriterBlocks ;
import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
public class RDFWriterRegistry
{
@@ -39,7 +43,7 @@ public class RDFWriterRegistry
public static WriterVariant wvPretty = new WriterVariant("pretty" ) ;
public static WriterVariant wvBlocks = new WriterVariant("blocks" ) ;
- public static WriterVariant wvFlat = new WriterVariant("flat" ) ;
+ public static WriterVariant wvFlat = new WriterVariant("flat" ) ;
public static RDFSerialization TurtlePretty = new RDFSerialization(Lang.TURTLE, wvPretty) ;
public static RDFSerialization Turtle = TurtlePretty ;
@@ -66,6 +70,7 @@ public class RDFWriterRegistry
public static RDFSerialization RDFJSON = new RDFSerialization(Lang.RDFJSON) ;
+ // Writing a graph
static WriterRIOTFactory wfactory = new WriterRIOTFactory() {
@Override
public RiotSerializer create(OutputStream out, Graph graph, RDFSerialization serialization)
@@ -89,7 +94,7 @@ public class RDFWriterRegistry
if ( Lib.equal(TrigPretty, serialization) )
return createTrig(out, graph) ;
if ( Lib.equal(TrigBlocks, serialization) )
- return null ;
+ return createTrigStreaming(out, graph) ;
if ( Lib.equal(TrigFlat, serialization) )
return null ;
if ( Lib.equal(NQuads, serialization) )
@@ -165,7 +170,7 @@ public class RDFWriterRegistry
{
// TODO Temporary
return new RiotSerializer() {
- @Override public void write() { TurtleWriter.write(out, graph, graph.getPrefixMapping().getNsPrefixMap()) ; }
+ @Override public void write() { TurtleWriter.write(out, graph) ; }
@Override public Lang getLang() { return TURTLE ; }
} ;
}
@@ -174,7 +179,7 @@ public class RDFWriterRegistry
{
// TODO Temporary
return new RiotSerializer() {
- @Override public void write() { TurtleWriter.write(out, graph, graph.getPrefixMapping().getNsPrefixMap()) ; }
+ @Override public void write() { TurtleWriterBlocks.write(out, graph) ; }
@Override public Lang getLang() { return TURTLE ; }
} ;
}
@@ -195,8 +200,29 @@ public class RDFWriterRegistry
return createTurtle(out, graph) ;
}
- private static RiotSerializer createTrig(OutputStream out, Graph graph)
- { return null ; }
+ private static RiotSerializer createTrig(final OutputStream out, final Graph graph)
+ { return new RiotSerializer() {
+ @Override public void write() { TriGWriter.write(out, graph) ; }
+ @Override public Lang getLang() { return TRIG ; }
+ } ; }
+
+ private static RiotSerializer createTrig(final OutputStream out, final DatasetGraph dsg)
+ { return new RiotSerializer() {
+ @Override public void write() { TriGWriter.write(out, dsg) ; }
+ @Override public Lang getLang() { return TRIG ; }
+ } ; }
+
+ private static RiotSerializer createTrigStreaming(final OutputStream out, final DatasetGraph dsg)
+ { return new RiotSerializer() {
+ @Override public void write() { TriGWriterBlocks.write(out, dsg) ; }
+ @Override public Lang getLang() { return TRIG ; }
+ } ; }
+
+ private static RiotSerializer createTrigStreaming(final OutputStream out, final Graph graph)
+ { return new RiotSerializer() {
+ @Override public void write() { TriGWriterBlocks.write(out, graph) ; }
+ @Override public Lang getLang() { return TRIG ; }
+ } ; }
private static RiotSerializer createNQuads(OutputStream out, Graph graph)
{ return null ; }
Modified: jena/Experimental/riot-output/src/main/java/riot/WriterVariant.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/WriterVariant.java?rev=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/WriterVariant.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/WriterVariant.java Thu Feb 7 18:48:49 2013
@@ -50,4 +50,10 @@ public class WriterVariant
if ( ! Lib.equal(symbol, other.symbol) ) return false ;
return true ;
}
+
+ @Override
+ public String toString()
+ {
+ return symbol.getSymbol() ;
+ }
}
Modified: jena/Experimental/riot-output/src/main/java/riot/writer/TW2.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TW2.java?rev=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/TW2.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/TW2.java Thu Feb 7 18:48:49 2013
@@ -19,225 +19,24 @@
package riot.writer;
import static riot.writer.WriterConst.PREFIX_IRI ;
-import static riot.writer.WriterConst.RDF_First ;
-import static riot.writer.WriterConst.RDF_Nil ;
-import static riot.writer.WriterConst.RDF_Rest ;
-import java.util.* ;
-import java.util.Map.Entry ;
+import java.util.ArrayList ;
+import java.util.Collection ;
+import java.util.List ;
+import java.util.Map ;
import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.atlas.iterator.Iter ;
-import org.apache.jena.atlas.lib.Pair ;
-import org.apache.jena.riot.out.NodeToLabel ;
import org.apache.jena.riot.system.PrefixMap ;
-import org.apache.jena.riot.system.PrefixMapFactory ;
-import riot.out.NodeFormatter ;
-import riot.out.NodeFormatterTTL ;
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.sparql.util.FmtUtils ;
import com.hp.hpl.jena.util.iterator.ExtendedIterator ;
/** Support code for the RIOT TurtleWriter */
-public class TW2
+class TW2
{
-
- static void writePrefixes(WriterContext cxt, Map<String, String> prefixMap)
- {
- writePrefixes(cxt.out, prefixMap) ;
- }
-
- static void writePrefixes(IndentedWriter out, Map<String, String> prefixMap)
- {
- if ( ! prefixMap.isEmpty() )
- {
- // prepare?
- for ( Map.Entry <String, String> e : prefixMap.entrySet() )
- {
- out.print("@prefix ") ;
- out.print(e.getKey()) ;
- out.print(": ") ;
- out.pad(PREFIX_IRI) ;
- out.print("<") ;
- out.print(e.getValue()) ; // Check?
- out.print(">") ;
- out.print(" .") ;
- out.println() ;
- }
- }
- }
-
- // Single, multi-function, pass over the graph
- // It alread scans all objects.
-
- /** Find all list heads and all nodes in well-formed lists.
- * Return a (list head -> Elements map), list elements)
- */
- static void findLists(WriterContext cxt)
- {
- // HashSets for use during parsing.
- Map<Node, List<Node>> lists = new HashMap<Node, List<Node>>() ;
- // Lists that are printed as free standing lists.
- Map<Node, List<Node>> freeLists = new HashMap<Node, List<Node>>() ;
- // Lists that are shared
- Map<Node, List<Node>> nLinkedLists = new HashMap<Node, List<Node>>() ;
-
- Collection<Node> listElts = new HashSet<Node>() ;
- List<Triple> tails = triples(cxt.graph, Node.ANY, RDF_Rest, RDF_Nil) ;
- for ( Triple t : tails )
- {
- // Returns the elements, reversed.
- Collection<Node> listElts2 = new HashSet<Node>() ;
- Pair<Node, List<Node>> p = followTailToHead(cxt, t.getSubject(), listElts2) ;
- if ( p != null )
- {
- Node headElt = p.getLeft() ;
- // Free standing?
- List<Node> elts = p.getRight() ;
- long numLinks = countTriples(cxt.graph, null, null, headElt) ;
- if ( numLinks == 1 )
- lists.put(headElt, elts) ;
- else if ( numLinks == 0 )
- // 0 connected lists
- freeLists.put(headElt, elts) ;
- else
- {
- // Two triples to this list.
- // TODO Can't print the list in abbreviated form as we need to name the head.
- // Take out first element, print rest normally, new head is a one-connected node.
- //OR do in the printing. (N3 has "_:b :-")
- nLinkedLists.put(headElt, elts) ;
- }
- listElts.addAll(listElts2) ;
- }
- }
- cxt.freeLists = freeLists ;
- cxt.lists = lists ;
- cxt.listElts = listElts ;
- cxt.nLinkedLists= nLinkedLists ;
- }
-//
-// // Return is the elements
-// static Pair<Collection<Node>, Collection<Node>> findList1(Node lastElt, WriterContext cxt)
-// {
-//
-// }
-
-
- // return head elt node, list of elements.
- private static Pair<Node, List<Node>> followTailToHead(WriterContext cxt, Node lastListElt, Collection<Node> listElts)
- {
- List<Node> listCells = new ArrayList<Node>() ;
- List<Node> eltsReversed = new ArrayList<Node>() ;
- List<Triple> acc = new ArrayList<Triple>() ;
- Node x = lastListElt ;
- final Graph graph = cxt.graph ;
-
- for ( ; ; )
- {
- if ( ! validListElement(graph, x, acc) )
- {
- if ( listCells.size() == 0 )
- // No earlier valid list.
- return null ;
- // Fix up to previous valid list cell.
- x = listCells.remove(listCells.size()-1) ;
- break ;
- }
-
- Triple t = triple1(graph, x, RDF_First, null) ;
- if ( t == null )
- return null ;
- eltsReversed.add(t.getObject()) ;
- listCells.add(x) ;
-
- // Try to move up the list.
- List<Triple> acc2 = triples(graph, null, null, x) ;
- long numRest = countTriples(graph, null, RDF_Rest, x) ;
- if ( numRest != 1 )
- {
- // Head of well-formed list.
- // Classified by 0,1,more links later.
- listCells.add(x) ;
- break ;
- }
- // numRest == 1
- int numLinks = acc2.size() ;
- if ( numLinks > 1 )
- // Non-list links to x
- break ;
- // Valid.
- Triple tLink = acc2.get(0) ;
- x = tLink.getSubject() ;
- }
- // Success.
- listElts.addAll(listCells) ;
- Collections.reverse(eltsReversed) ;
- return Pair.create(x, eltsReversed);
- }
-
- /* Return the triples of the list element, or null if invalid list */
- private static boolean validListElement(Graph graph, Node x, List<Triple> acc)
- {
- Triple t1 = triple1(graph, x, RDF_Rest, null) ; // Which we came up to get here :-(
- if ( t1 == null )
- return false ;
- Triple t2 = triple1(graph, x, RDF_First, null) ;
- if ( t2 == null )
- return false ;
- long N = countTriples(graph, x, null, null) ;
- if ( N != 2 )
- return false ;
- acc.add(t1) ;
- acc.add(t2) ;
- return true ;
- }
-
- static final boolean recordObjectMisses = true ;
-
- /** Find all embeddable objects */
-
- // TODO Remove.
- @SuppressWarnings("null")
- static void findOneConnectedBNodeObjects(WriterContext cxt)
- {
- Set<Node> bNodesObj1 = new HashSet<Node>() ; // The subject of exactly one triple.
- Set<Node> rejects = recordObjectMisses ? new HashSet<Node>() : null ; // Nodes known not to meet the requirement.
-
- ExtendedIterator<Triple> iter = cxt.graph.find(Node.ANY, Node.ANY, Node.ANY) ;
- try {
- for ( ; iter.hasNext() ; )
- {
- Triple t = iter.next() ;
- Node obj = t.getObject() ;
- if ( ! obj.isBlank() )
- continue ;
- if ( rejects != null && rejects.contains(obj) )
- continue ;
- // No point checking bNodesObj1.
- Node n = connectedOnce(cxt.graph, obj) ;
- if ( n != null )
- bNodesObj1.add(n) ;
- }
- } finally { iter.close() ; }
- cxt.nestedObjects = bNodesObj1 ;
- }
-
- static Node connectedOnce(Graph graph, Node obj)
- {
- ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, obj) ;
- int count = 0 ;
- try {
- if ( ! iter.hasNext() ) return null ;
- iter.next() ;
- if ( ! iter.hasNext() ) return obj ;
- return null ;
- } finally { iter.close() ; }
- }
-
static Collection<Triple> triplesOfSubject(Graph graph, Node subj)
{
return triples(graph, subj, Node.ANY, Node.ANY) ;
@@ -281,78 +80,25 @@ public class TW2
} finally { iter.close() ; }
}
- static WriterContext createContext(IndentedWriter out, Graph graph, Map<String, String> prefixMap)
- {
- WriterContext cxt = new WriterContext() ;
- PrefixMap pmap = PrefixMapFactory.create() ;
- for ( Entry<String, String> e : prefixMap.entrySet() )
- pmap.add(e.getKey(), e.getValue()) ;
-
- cxt.out = out ;
- cxt.prefixMap = prefixMap ;
- cxt.nodeFmt = new NodeFormatterTTL(null, pmap, NodeToLabel.createScopeByDocument()) ;
- cxt.graph = graph ;
- return cxt ;
- }
-
- static void writeNode(WriterContext cxt, Node node)
- {
- writeNode(cxt.out, cxt.nodeFmt, node) ;
- }
-
- static void writeNode(IndentedWriter out, NodeFormatter fmt, Node node)
- {
- fmt.format(out, node) ;
- }
-
- static void X_writeNode(IndentedWriter out, Map<String, String> prefixMap, Node node)
- {
- //TODO Temporary.
- // See RIOT NodeFormatter
- if ( node.isURI() )
- {
- String iri = node.getURI() ;
- // Crude.
- String x = abbreviate(iri, prefixMap) ;
- if ( x != null )
- {
- out.print(x) ;
- return ;
- }
- }
- out.print(FmtUtils.stringForNode(node)) ;
- }
+ static boolean strSafeFor(String str, char ch) { return str.indexOf(ch) == -1 ; }
- /** Abbreviate an IRI or return null */
- private static String abbreviate(String uriStr, Map<String, String> prefixMap)
- {
- for ( Entry<String, String> e : prefixMap.entrySet())
+ public static void writePrefixes(IndentedWriter out, PrefixMap prefixMap)
+ {
+ if ( ! prefixMap.isEmpty() )
{
- String prefix = e.getValue().toString() ;
-
- if ( uriStr.startsWith(prefix) )
+ for ( Map.Entry <String, String> e : prefixMap.getMappingCopyStr().entrySet() )
{
- String ln = uriStr.substring(prefix.length()) ;
- if ( strSafeFor(ln, '/') && strSafeFor(ln, '#') && strSafeFor(ln, ':') )
- return e.getKey()+":"+ln ;
+ out.print("@prefix ") ;
+ out.print(e.getKey()) ;
+ out.print(": ") ;
+ out.pad(PREFIX_IRI) ;
+ out.print("<") ;
+ out.print(e.getValue()) ;
+ out.print(">") ;
+ out.print(" .") ;
+ out.println() ;
}
}
- return null ;
- }
-
- static boolean strSafeFor(String str, char ch) { return str.indexOf(ch) == -1 ; }
-
- private static void flush(IndentedWriter out) { out.flush() ; }
-
- static void print(WriterContext cxt,String string)
- {
- cxt.out.print(string) ;
- flush(cxt.out) ;
- }
-
- static void pad(WriterContext cxt,int col)
- {
- cxt.out.pad(col, true) ;
}
}
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=1443653&r1=1443652&r2=1443653&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 Feb 7 18:48:49 2013
@@ -20,14 +20,17 @@ 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.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.query.Dataset ;
+import com.hp.hpl.jena.rdf.model.Model ;
import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+import com.hp.hpl.jena.sparql.core.DatasetGraphFactory ;
/** TriG pretty writer */
public class TriGWriter extends TurtleShell
@@ -36,26 +39,47 @@ public class TriGWriter extends TurtleSh
public static void write(OutputStream out, Dataset dataset)
{
- write(out, dataset.asDatasetGraph(), dataset.getDefaultModel().getNsPrefixMap()) ;
+ PrefixMap pmap = PrefixMapFactory.create(dataset.getDefaultModel()) ;
+ write(out, dataset.asDatasetGraph(), pmap, null) ;
+ }
+
+ public static void write(OutputStream out, DatasetGraph dsg)
+ {
+ PrefixMap prefixMap = PrefixMapFactory.create(dsg.getDefaultGraph().getPrefixMapping()) ;
+ write(out, dsg, prefixMap, null) ;
}
- public static void write(OutputStream out, DatasetGraph dsg, Map<String, String> prefixMap)
+ public static void write(OutputStream out, DatasetGraph dsg, PrefixMap prefixMap, String baseURI)
{
IndentedWriter iOut = new IndentedWriter(out, false) ;
-
- TriGWriter w = new TriGWriter(iOut, prefixMap) ;
+ TriGWriter w = new TriGWriter(iOut, prefixMap, baseURI) ;
w.write(dsg) ;
iOut.flush() ;
}
- public TriGWriter(IndentedWriter out, Map<String, String> prefixMap)
+ /** Write a model as the default graph of a dataset */
+ public static void write(OutputStream out, Model model)
+ {
+ write(out, model.getGraph()) ;
+ }
+
+ public static void write(OutputStream out, Graph graph)
+ {
+ DatasetGraph dsg = DatasetGraphFactory.createOneGraph(graph) ;
+ write(out, dsg) ;
+ }
+
+
+ public TriGWriter(IndentedWriter out, PrefixMap prefixMap, String baseURI)
{
- super(out, prefixMap) ;
+ super(out, prefixMap, baseURI) ;
}
private void write(DatasetGraph dsg)
{
writePrefixes(prefixMap) ;
+ if ( ! prefixMap.isEmpty() && !dsg.isEmpty() )
+ out.println() ;
Iterator<Node> graphNames = dsg.listGraphNodes() ;
@@ -69,8 +93,6 @@ public class TriGWriter extends TurtleSh
}
}
-
-
private void writeGraph(Node name, Graph graph)
{
if ( name != null )
@@ -81,7 +103,7 @@ public class TriGWriter extends TurtleSh
out.println("{") ;
out.incIndent(GRAPH_INDENT) ;
// Pretty Turtle Writer.
- writeGraph(graph) ;
+ writeGraphTTL(graph) ;
out.decIndent(GRAPH_INDENT) ;
out.ensureStartOfLine() ;
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=1443653&r1=1443652&r2=1443653&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 Feb 7 18:48:49 2013
@@ -18,22 +18,50 @@
package riot.writer;
-import java.util.Map ;
+import java.io.OutputStream ;
import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
import org.apache.jena.riot.system.StreamRDF ;
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+import com.hp.hpl.jena.sparql.core.DatasetGraphFactory ;
/** TriG writer that streams - print in blocks of quads clustered
* by adjacent same graph and same subject
*/
-public class TriGWriterBlocks extends WriterStreamBase
+public class TriGWriterBlocks
{
- public static void write(IndentedWriter out, DatasetGraph datasetGraph, Map<String, String> prefixes)
+ public static void write(OutputStream out, Dataset dataset)
+ {
+ write(out, dataset.asDatasetGraph()) ;
+ }
+
+ public static void write(OutputStream out, DatasetGraph datasetGraph)
+ {
+ IndentedWriter iOut = new IndentedWriter(out) ;
+ write(iOut, datasetGraph) ;
+ }
+
+ public static void write(OutputStream out, Graph graph)
+ {
+ DatasetGraph dsg = DatasetGraphFactory.createOneGraph(graph) ;
+ write(out, dsg) ;
+ }
+
+ public static void write(IndentedWriter out, DatasetGraph datasetGraph)
+ {
+ PrefixMap pmap = PrefixMapFactory.create(datasetGraph.getDefaultGraph().getPrefixMapping()) ;
+ write(out, datasetGraph, pmap, null) ;
+ }
+
+ public static void write(IndentedWriter out, DatasetGraph datasetGraph, PrefixMap prefixes, String baseURI)
{
StreamRDF dest = new WriterStreamRDFBlocks(out) ;
- write(dest, datasetGraph, prefixes) ;
+ WriterStreamBase.write(dest, datasetGraph, prefixes, baseURI) ;
}
}
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=1443653&r1=1443652&r2=1443653&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 Feb 7 18:48:49 2013
@@ -18,23 +18,43 @@
package riot.writer;
-import java.util.Map ;
+import java.io.OutputStream ;
import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
import org.apache.jena.riot.system.StreamRDF ;
+import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.sparql.core.DatasetGraph ;
/** TriG writer that writes quads one per line
* in TriG form with prefixes and short form literals (e.g. integers)
*/
-public class TriGWriterFlat extends WriterStreamBase
+public class TriGWriterFlat
{
- public static void write(IndentedWriter out, DatasetGraph datasetGraph, Map<String, String> prefixes)
+ public static void write(OutputStream out, Dataset dataset)
+ {
+ write(out, dataset.asDatasetGraph()) ;
+ }
+
+ public static void write(OutputStream out, DatasetGraph datasetGraph)
+ {
+ IndentedWriter iOut = new IndentedWriter(out) ;
+ write(iOut, datasetGraph) ;
+ }
+
+ public static void write(IndentedWriter out, DatasetGraph datasetGraph)
+ {
+ PrefixMap pmap = PrefixMapFactory.create(datasetGraph.getDefaultGraph().getPrefixMapping()) ;
+ write(out, datasetGraph, pmap, null) ;
+ }
+
+ public static void write(IndentedWriter out, DatasetGraph datasetGraph, PrefixMap prefixes, String baseURI)
{
StreamRDF dest = new WriterStreamRDFFlat(out) ;
- write(dest, datasetGraph, prefixes) ;
+ WriterStreamBase.write(dest, datasetGraph, prefixes, baseURI) ;
}
}
Modified: jena/Experimental/riot-output/src/main/java/riot/writer/TurtleShell.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TurtleShell.java?rev=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/TurtleShell.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/TurtleShell.java Thu Feb 7 18:48:49 2013
@@ -31,13 +31,11 @@ import static riot.writer.WriterConst.ir
import static riot.writer.WriterConst.rdfNS ;
import java.util.* ;
-import java.util.Map.Entry ;
import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.atlas.lib.Pair ;
import org.apache.jena.riot.out.NodeToLabel ;
import org.apache.jena.riot.system.PrefixMap ;
-import org.apache.jena.riot.system.PrefixMapFactory ;
import riot.other.GLib ;
import riot.out.NodeFormatter ;
import riot.out.NodeFormatterTTL ;
@@ -47,436 +45,419 @@ import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.graph.Triple ;
import com.hp.hpl.jena.util.iterator.ExtendedIterator ;
-/** Base class to support the pretty formsl of Turtle-related languages (Turle, TriG) */
-public class TurtleShell
+/** Base class to support the pretty forms of Turtle-related languages (Turle, TriG) */
+public abstract class TurtleShell
{
- IndentedWriter out ;
- NodeFormatter nodeFmt ;
- Map<String, String> prefixMap ;
-
- // Per graph member variables.
- Graph graph ;
- Set<Node> nestedObjects ; // Blank nodes that have one incoming triple
- Map<Node, List<Node>> lists; // The head node in each well-formed lists -> list elements
- Map<Node, List<Node>> freeLists; // List that do not have any incoming triples
- Map<Node, List<Node>> nLinkedLists; // Lists that have more than one incoming triple
- Collection<Node> listElts ; // All nodes that are part of list structures.
+ protected final IndentedWriter out ;
+ protected final NodeFormatter nodeFmt ;
+ protected final PrefixMap prefixMap ;
+ protected final String baseURI ;
- TurtleShell(IndentedWriter out, Map<String, String> prefixMap)
+ protected TurtleShell(IndentedWriter out, PrefixMap pmap, String baseURI)
{
this.out = out ;
- this.prefixMap = prefixMap ;
-
- //TODO = use only
- PrefixMap pmap = PrefixMapFactory.create() ;
- for ( Entry<String, String> e : prefixMap.entrySet() )
- pmap.add(e.getKey(), e.getValue()) ;
-
- nodeFmt = new NodeFormatterTTL(null, pmap, NodeToLabel.createScopeByDocument()) ;
-
+ this.nodeFmt = new NodeFormatterTTL(baseURI, pmap, NodeToLabel.createScopeByDocument()) ;
+ this.prefixMap = pmap ;
+ this.baseURI = baseURI ;
}
-
- // and a blank line.
- protected void writePrefixes(Map<String, String> prefixMap)
+ protected void writePrefixes(PrefixMap prefixMap)
{
- // TODO
TW2.writePrefixes(out, prefixMap) ;
- if ( ! prefixMap.isEmpty() )
- out.println();
}
-
- private void initGraph(Graph graph)
+
+ protected void writeGraphTTL(Graph graph)
{
- this.graph = graph ;
- findOneConnectedBNodeObjects() ;
- findLists() ;
-
- // TODO Right check?
- // Stop head of lists printed as triples going all the way to the good part.
- nestedObjects.removeAll(listElts) ;
-
+ ShellGraph x = new ShellGraph(graph) ;
+ x.writeGraph() ;
}
- static boolean recordObjectMisses = true ;
-
- private void findOneConnectedBNodeObjects()
- {
- Set<Node> bNodesObj1 = new HashSet<Node>() ; // The subject of exactly one triple.
- Set<Node> rejects = recordObjectMisses ? new HashSet<Node>() : null ; // Nodes known not to meet the requirement.
+ // Write one graph - using an inner object class to isolate
+ // the state variables for writing a single graph.
+ class ShellGraph {
+ // Per graph member variables.
+ private final Graph graph ;
+ private final Set<Node> nestedObjects ; // Blank nodes that have one incoming triple
+ private final Map<Node, List<Node>> lists; // The head node in each well-formed lists -> list elements
+ private final Map<Node, List<Node>> freeLists; // List that do not have any incoming triples
+ private final Map<Node, List<Node>> nLinkedLists; // Lists that have more than one incoming triple
+ private final Collection<Node> listElts ; // All nodes that are part of list structures.
- ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ;
- try {
- for ( ; iter.hasNext() ; )
- {
- Triple t = iter.next() ;
- Node obj = t.getObject() ;
- if ( ! obj.isBlank() )
- continue ;
- if ( rejects != null && rejects.contains(obj) )
- continue ;
- // No point checking bNodesObj1.
- Node n = connectedOnce(obj) ;
- if ( n != null )
- bNodesObj1.add(n) ;
- }
- } finally { iter.close() ; }
- nestedObjects = bNodesObj1 ;
- }
+ ShellGraph(Graph graph)
+ {
+ this.graph = graph ;
+ this.nestedObjects = new HashSet<Node>() ;
+
+ this.lists = new HashMap<Node, List<Node>>() ;
+ this.freeLists = new HashMap<Node, List<Node>>() ;
+ this.nLinkedLists = new HashMap<Node, List<Node>>() ;
+ this.listElts = new HashSet<Node>() ;
- private Node connectedOnce(Node obj)
- {
- ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, obj) ;
- int count = 0 ;
- try {
- if ( ! iter.hasNext() ) return null ;
- iter.next() ;
- if ( ! iter.hasNext() ) return obj ;
- return null ;
- } finally { iter.close() ; }
- }
+
+ findOneConnectedBNodeObjects() ;
+ findLists() ;
+ // Stop head of lists printed as triples going all the way to the good part.
+ nestedObjects.removeAll(listElts) ;
+ }
- // --- Lists setup
- /** Find all list heads and all nodes in well-formed lists.
- * Return a (list head -> Elements map), list elements)
- */
- void findLists()
- {
- // HashSets for use during parsing.
- lists = new HashMap<Node, List<Node>>() ;
- // Lists that are printed as free standing lists.
- freeLists = new HashMap<Node, List<Node>>() ;
- // Lists that are shared
- nLinkedLists = new HashMap<Node, List<Node>>() ;
- // All elements of lists we will print as lists.
- listElts = new HashSet<Node>() ;
- List<Triple> tails = TW2.triples(graph, Node.ANY, RDF_Rest, RDF_Nil) ;
- for ( Triple t : tails )
- {
- // Returns the elements, reversed.
- Collection<Node> listElts2 = new HashSet<Node>() ;
- Pair<Node, List<Node>> p = followTailToHead(t.getSubject(), listElts2) ;
- if ( p != null )
+ private void findOneConnectedBNodeObjects()
+ {
+ Set<Node> rejects = new HashSet<Node>() ; // Nodes known not to meet the requirement.
+
+ ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ;
+ try {
+ for ( ; iter.hasNext() ; )
+ {
+ Triple t = iter.next() ;
+ Node obj = t.getObject() ;
+ if ( ! obj.isBlank() )
+ continue ;
+ if ( rejects.contains(obj) )
+ continue ;
+ // No point checking bNodesObj1.
+ Node n = connectedOnce(obj) ;
+ if ( n != null )
+ nestedObjects.add(n) ;
+ }
+ } finally { iter.close() ; }
+ }
+
+ private Node connectedOnce(Node obj)
+ {
+ ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, obj) ;
+ int count = 0 ;
+ try {
+ if ( ! iter.hasNext() ) return null ;
+ iter.next() ;
+ if ( ! iter.hasNext() ) return obj ;
+ return null ;
+ } finally { iter.close() ; }
+ }
+
+ // --- Lists setup
+ /* Find all list heads and all nodes in well-formed lists.
+ * Return a (list head -> Elements map), list elements)
+ */
+ void findLists()
+ {
+ List<Triple> tails = TW2.triples(graph, Node.ANY, RDF_Rest, RDF_Nil) ;
+ for ( Triple t : tails )
{
- Node headElt = p.getLeft() ;
- // Free standing?private
- List<Node> elts = p.getRight() ;
- long numLinks = TW2.countTriples(graph, null, null, headElt) ;
- if ( numLinks == 1 )
- lists.put(headElt, elts) ;
- else if ( numLinks == 0 )
- // 0 connected lists
- freeLists.put(headElt, elts) ;
- else
+ // Returns the elements, reversed.
+ Collection<Node> listElts2 = new HashSet<Node>() ;
+ Pair<Node, List<Node>> p = followTailToHead(t.getSubject(), listElts2) ;
+ if ( p != null )
{
- // Two triples to this list.
- // TODO Can't print the list in abbreviated form as we need to name the head.
- // Take out first element, print rest normally, new head is a one-connected node.
- //OR do in the printing. (N3 has "_:b :-")
- nLinkedLists.put(headElt, elts) ;
+ Node headElt = p.getLeft() ;
+ // Free standing?private
+ List<Node> elts = p.getRight() ;
+ long numLinks = TW2.countTriples(graph, null, null, headElt) ;
+ if ( numLinks == 1 )
+ lists.put(headElt, elts) ;
+ else if ( numLinks == 0 )
+ // 0 connected lists
+ freeLists.put(headElt, elts) ;
+ else
+ // Two triples to this list.
+ nLinkedLists.put(headElt, elts) ;
+ listElts.addAll(listElts2) ;
}
- listElts.addAll(listElts2) ;
}
}
- }
-
- // return head elt node, list of elements.
- private Pair<Node, List<Node>> followTailToHead(Node lastListElt, Collection<Node> listElts)
- {
- List<Node> listCells = new ArrayList<Node>() ;
- List<Node> eltsReversed = new ArrayList<Node>() ;
- List<Triple> acc = new ArrayList<Triple>() ;
- Node x = lastListElt ;
-
- for ( ; ; )
+
+ // return head elt node, list of elements.
+ private Pair<Node, List<Node>> followTailToHead(Node lastListElt, Collection<Node> listElts)
{
- if ( ! validListElement(x, acc) )
+ List<Node> listCells = new ArrayList<Node>() ;
+ List<Node> eltsReversed = new ArrayList<Node>() ;
+ List<Triple> acc = new ArrayList<Triple>() ;
+ Node x = lastListElt ;
+
+ for ( ; ; )
{
- if ( listCells.size() == 0 )
- // No earlier valid list.
+ if ( ! validListElement(x, acc) )
+ {
+ if ( listCells.size() == 0 )
+ // No earlier valid list.
+ return null ;
+ // Fix up to previous valid list cell.
+ x = listCells.remove(listCells.size()-1) ;
+ break ;
+ }
+
+ Triple t = TW2.triple1(graph, x, RDF_First, null) ;
+ if ( t == null )
return null ;
- // Fix up to previous valid list cell.
- x = listCells.remove(listCells.size()-1) ;
- break ;
- }
-
- Triple t = TW2.triple1(graph, x, RDF_First, null) ;
- if ( t == null )
- return null ;
- eltsReversed.add(t.getObject()) ;
- listCells.add(x) ;
-
- // Try to move up the list.
- List<Triple> acc2 = TW2.triples(graph, null, null, x) ;
- long numRest = TW2.countTriples(graph, null, RDF_Rest, x) ;
- if ( numRest != 1 )
- {
- // Head of well-formed list.
- // Classified by 0,1,more links later.
+ eltsReversed.add(t.getObject()) ;
listCells.add(x) ;
- break ;
+
+ // Try to move up the list.
+ List<Triple> acc2 = TW2.triples(graph, null, null, x) ;
+ long numRest = TW2.countTriples(graph, null, RDF_Rest, x) ;
+ if ( numRest != 1 )
+ {
+ // Head of well-formed list.
+ // Classified by 0,1,more links later.
+ listCells.add(x) ;
+ break ;
+ }
+ // numRest == 1
+ int numLinks = acc2.size() ;
+ if ( numLinks > 1 )
+ // Non-list links to x
+ break ;
+ // Valid.
+ Triple tLink = acc2.get(0) ;
+ x = tLink.getSubject() ;
}
- // numRest == 1
- int numLinks = acc2.size() ;
- if ( numLinks > 1 )
- // Non-list links to x
- break ;
- // Valid.
- Triple tLink = acc2.get(0) ;
- x = tLink.getSubject() ;
- }
- // Success.
- listElts.addAll(listCells) ;
- Collections.reverse(eltsReversed) ;
- return Pair.create(x, eltsReversed);
- }
+ // Success.
+ listElts.addAll(listCells) ;
+ Collections.reverse(eltsReversed) ;
+ return Pair.create(x, eltsReversed);
+ }
- /* Return the triples of the list element, or null if invalid list */
- private boolean validListElement(Node x, List<Triple> acc)
- {
- Triple t1 = TW2.triple1(graph, x, RDF_Rest, null) ; // Which we came up to get here :-(
- if ( t1 == null )
- return false ;
- Triple t2 = TW2.triple1(graph, x, RDF_First, null) ;
- if ( t2 == null )
- return false ;
- long N = TW2.countTriples(graph, x, null, null) ;
- if ( N != 2 )
- return false ;
- acc.add(t1) ;
- acc.add(t2) ;
- return true ;
- }
+ /* Return the triples of the list element, or null if invalid list */
+ private boolean validListElement(Node x, List<Triple> acc)
+ {
+ Triple t1 = TW2.triple1(graph, x, RDF_Rest, null) ; // Which we came up to get here :-(
+ if ( t1 == null )
+ return false ;
+ Triple t2 = TW2.triple1(graph, x, RDF_First, null) ;
+ if ( t2 == null )
+ return false ;
+ long N = TW2.countTriples(graph, x, null, null) ;
+ if ( N != 2 )
+ return false ;
+ acc.add(t1) ;
+ acc.add(t2) ;
+ return true ;
+ }
- // ----
-
- protected void writeGraph(Graph graph)
- {
- initGraph(graph) ;
- // Prefixes
- writePrefixes(prefixMap) ;
- if ( ! prefixMap.isEmpty() )
- out.println();
-
- // Or - listSubjects and sort.
- Iterator<Node> subjects = GLib.listSubjects(graph) ;
- boolean b = writeBySubject(subjects) ;
-
- // Write remainders
- if ( ! nLinkedLists.isEmpty() )
- {
- // Print carefully - need a label for the first cell.
- // So we write out the first element of the list in triples, then put
- // the remainer as a pretty lst.
-
- if ( b ) out.println() ;
- b = false ;
-
- for ( Node n : nLinkedLists.keySet() )
+ // ----
+
+ protected void writeGraph()
+ {
+ Iterator<Node> subjects = GLib.listSubjects(graph) ;
+ boolean b = writeBySubject(subjects) ;
+
+ // Write remainders
+ if ( ! nLinkedLists.isEmpty() )
{
- List<Node> x = nLinkedLists.get(n) ;
-
- writeNode(n) ;
-
- if ( out.getCol() > LONG_SUBJECT )
+ // Print carefully - need a label for the first cell.
+ // So we write out the first element of the list in triples, then put
+ // the remainer as a pretty lst.
+
+ if ( b ) out.println() ;
+ b = false ;
+
+ for ( Node n : nLinkedLists.keySet() )
+ {
+ List<Node> x = nLinkedLists.get(n) ;
+
+ writeNode(n) ;
+
+ if ( out.getCol() > LONG_SUBJECT )
+ println() ;
+ else
+ gap() ;
+ out.incIndent(INDENT_PREDICATE) ;
+ // ----
+ // DRY writeCluster.
+ out.pad() ;
+ writeNode(RDF_First) ;
+ print(" ") ;
+ writeNode(x.get(0)) ;
+ print(" ;") ;
println() ;
- else
- gap() ;
- out.incIndent(INDENT_PREDICATE) ;
- // ----
- // DRY writeCluster.
- out.pad() ;
- writeNode(RDF_First) ;
- print(" ") ;
- writeNode(x.get(0)) ;
- print(" ;") ;
- println() ;
- writeNode(RDF_Rest) ;
- print(" ") ;
- x = x.subList(1, x.size()) ;
- list(x) ;
- print(" .") ;
- println() ;
- }
- }
- //else out.println("# No NNNN") ;
-
- if ( ! freeLists.isEmpty() )
- {
- if ( b ) out.println() ;
- out.println("# >> 0000") ;
- // Write free lists.
- for ( Node n : freeLists.keySet() )
+ writeNode(RDF_Rest) ;
+ print(" ") ;
+ x = x.subList(1, x.size()) ;
+ list(x) ;
+ print(" .") ;
+ out.decIndent(INDENT_PREDICATE) ;
+ println() ;
+ }
+ }
+ //else out.println("# No NNNN") ;
+
+ if ( ! freeLists.isEmpty() )
{
- list(freeLists.get(n)) ;
- out.println(" .") ;
+ if ( b ) out.println() ;
+ out.println("# >> 0000") ;
+ // Write free lists.
+ for ( Node n : freeLists.keySet() )
+ {
+ list(freeLists.get(n)) ;
+ out.println(" .") ;
+ }
+ //out.println() ;
}
- //out.println() ;
- }
- //else out.println("# No 0000") ;
- }
-
- // return true if did write something.
- boolean writeBySubject(Iterator<Node> subjects)
- {
- boolean first = true ;
- for ( ; subjects.hasNext() ; )
- {
- Node subj = subjects.next() ;
- if ( nestedObjects.contains(subj) )
- continue ;
- if ( listElts.contains(subj) )
- continue ;
- if ( ! first )
- out.println() ;
- first = false ;
-
- Collection<Triple> cluster = TW2.triplesOfSubject(graph, subj) ;
- writeCluster(subj, cluster) ;
+ //else out.println("# No 0000") ;
}
- return !first ;
- }
-
-
- // Common subject
- // Used by the blocks writer as well.
- void writeCluster(Node subject, Collection<Triple> cluster)
- {
- if ( cluster.isEmpty() ) return ;
- writeNode(subject) ;
-
- if ( out.getCol() > LONG_SUBJECT )
- out.println() ;
- else
- gap() ;
- out.incIndent(INDENT_PREDICATE) ;
- out.pad() ;
- writePredicateObjectList(cluster) ;
- out.decIndent(INDENT_PREDICATE) ;
- print(" .") ; // Not perfect
- println() ;
- }
-
- // need to skip the triples nested.
-
- private void writePredicateObjectList(Collection<Triple> cluster)
- {
- boolean first = true ;
- // Calc columns
-
- // Sort triples.
- // rdf:type
- // other rdf and rdfs
- // properties together
- // object lists?
- // Find the colject pad column.
-
-
- for ( Triple triple : cluster )
+
+ // return true if did write something.
+ boolean writeBySubject(Iterator<Node> subjects)
{
- if ( first )
- first = false ;
- else
+ boolean first = true ;
+ for ( ; subjects.hasNext() ; )
{
- print(" ;") ;
- println() ;
+ Node subj = subjects.next() ;
+ if ( nestedObjects.contains(subj) )
+ continue ;
+ if ( listElts.contains(subj) )
+ continue ;
+ if ( ! first )
+ out.println() ;
+ first = false ;
+
+ Collection<Triple> cluster = TW2.triplesOfSubject(graph, subj) ;
+ writeCluster(subj, cluster) ;
}
-
- // Write predicate.
- int colPredicateStart = out.getCol() ;
-
- if ( ! prefixMap.containsValue(rdfNS) && triple.getPredicate().getURI().equals(iriType) )
- print("a") ;
- else
- writeNode(triple.getPredicate()) ;
- int colPredicateFinish = out.getCol() ;
- int wPredicate = (colPredicateFinish-colPredicateStart) ;
-
- // Needs to be relative?
- if ( wPredicate > LONG_PREDICATE )
- println() ;
+ return !first ;
+ }
+
+
+ // Common subject
+ // Used by the blocks writer as well.
+ void writeCluster(Node subject, Collection<Triple> cluster)
+ {
+ if ( cluster.isEmpty() ) return ;
+ writeNode(subject) ;
+
+ if ( out.getCol() > LONG_SUBJECT )
+ out.println() ;
else
gap() ;
-
- // Secondary one should be less
- out.incIndent(INDENT_OBJECT) ;
+ out.incIndent(INDENT_PREDICATE) ;
out.pad() ;
- Node obj = triple.getObject() ;
- writeObject(obj) ;
- out.decIndent(INDENT_OBJECT) ;
+ writePredicateObjectList(cluster) ;
+ out.decIndent(INDENT_PREDICATE) ;
+ print(" .") ; // Not perfect
+ println() ;
}
- }
-
- // --> write S or O??
- private void writeObject(Node obj)
- {
- // Order matters? - one connected objects may include list elements.
- if ( lists.containsKey(obj) )
- list(lists.get(obj)) ;
- else if ( nestedObjects.contains(obj) )
- nestedObject(obj) ;
- else
- writeNode(obj) ;
- }
- final void writeNode(Node node)
- {
- nodeFmt.format(out, node) ;
- }
-
- private void nestedObject(Node obj)
- {
- Collection<Triple> x = TW2.triplesOfSubject(graph, obj) ;
-
- if ( x.isEmpty() )
+ // need to skip the triples nested.
+
+ private void writePredicateObjectList(Collection<Triple> cluster)
{
- print("[] ") ;
- return ;
+ boolean first = true ;
+ // Calc columns
+
+ // Sort triples.
+ // rdf:type
+ // other rdf and rdfs
+ // properties together
+ // object lists?
+ // Find the colject pad column.
+
+
+ for ( Triple triple : cluster )
+ {
+ if ( first )
+ first = false ;
+ else
+ {
+ print(" ;") ;
+ println() ;
+ }
+
+ // Write predicate.
+ int colPredicateStart = out.getCol() ;
+
+ if ( ! prefixMap.contains(rdfNS) && triple.getPredicate().getURI().equals(iriType) )
+ print("a") ;
+ else
+ writeNode(triple.getPredicate()) ;
+ int colPredicateFinish = out.getCol() ;
+ int wPredicate = (colPredicateFinish-colPredicateStart) ;
+
+ // Needs to be relative?
+ if ( wPredicate > LONG_PREDICATE )
+ println() ;
+ else
+ gap() ;
+
+ // Secondary one should be less
+ out.incIndent(INDENT_OBJECT) ;
+ out.pad() ;
+ Node obj = triple.getObject() ;
+ writeObject(obj) ;
+ out.decIndent(INDENT_OBJECT) ;
+ }
+
}
- if ( x.size() == 1 )
+ private void nestedObject(Node obj)
{
- print("[ ") ;
- // Includes nested object in triple.
+ Collection<Triple> x = TW2.triplesOfSubject(graph, obj) ;
+
+ if ( x.isEmpty() )
+ {
+ print("[] ") ;
+ return ;
+ }
+
+ if ( x.size() == 1 )
+ {
+ print("[ ") ;
+ // Includes nested object in triple.
+ writePredicateObjectList(x) ;
+ print(" ]") ;
+ return ;
+ }
+
+ // Two or more.
+ int here = out.getCol() ; // before "["
+ print("[") ;
+ int i1 = out.getAbsoluteIndent() ;
+ out.setAbsoluteIndent(here) ;
+ // Inline: println(out) ;
+ out.incIndent(2) ;
writePredicateObjectList(x) ;
- print(" ]") ;
- return ;
+ out.decIndent(2) ;
+ if ( true )
+ {
+ println() ; // Newline for "]"
+ print("]") ;
+ }
+ else
+ { // Compact
+ print(" ]") ;
+ }
+ out.setAbsoluteIndent(i1) ;
}
- // Two or more.
- int here = out.getCol() ; // before "["
- print("[") ;
- int i1 = out.getAbsoluteIndent() ;
- out.setAbsoluteIndent(here) ;
- // Inline: println(out) ;
- out.incIndent(2) ;
- writePredicateObjectList(x) ;
- out.decIndent(2) ;
- if ( true )
- {
- println() ; // Newline for "]"
- print("]") ;
- }
- else
- { // Compact
- print(" ]") ;
+
+ private void list(List<Node> elts)
+ {
+ out.print("(") ;
+ for ( Node n : elts )
+ {
+ out.print(" ") ;
+ writeObject(n) ;
+ }
+
+ out.print(" )") ;
}
- out.setAbsoluteIndent(i1) ;
- }
-
-
- private void list(List<Node> elts)
- {
- out.print("(") ;
- for ( Node n : elts )
+ // --> write S or O??
+ private void writeObject(Node obj)
{
- out.print(" ") ;
- writeObject(n) ;
+ // Order matters? - one connected objects may include list elements.
+ if ( lists.containsKey(obj) )
+ list(lists.get(obj)) ;
+ else if ( nestedObjects.contains(obj) )
+ nestedObject(obj) ;
+ else
+ writeNode(obj) ;
}
+ }
- out.print(" )") ;
+ final void writeNode(Node node)
+ {
+ nodeFmt.format(out, node) ;
}
-
+
+
private void print(String x)
{
out.print(x) ;
@@ -486,7 +467,7 @@ public class TurtleShell
{
out.print(' ', MIN_GAP) ;
}
-
+
// flush aggressively (debugging)
private void println()
{
@@ -494,6 +475,6 @@ public class TurtleShell
//out.flush() ;
}
-
+
}
Modified: jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriter.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriter.java?rev=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriter.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/TurtleWriter.java Thu Feb 7 18:48:49 2013
@@ -22,6 +22,8 @@ import java.io.OutputStream ;
import java.util.Map ;
import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.rdf.model.Model ;
@@ -30,40 +32,65 @@ public class TurtleWriter extends Turtle
{
public static void write(OutputStream cxt, Model model)
{
- write(cxt, model.getGraph(), model.getNsPrefixMap()) ;
+ write(cxt, model, null) ;
}
- public static void write(OutputStream out, Graph graph, Map<String, String> prefixMap)
+ public static void write(OutputStream cxt, Model model, String baseURI)
{
+ write(cxt, model.getGraph()) ;
+ }
+
+ public static void write(OutputStream out, Graph graph)
+ {
+ PrefixMap pmap = PrefixMapFactory.create(graph.getPrefixMapping()) ;
IndentedWriter iOut = new IndentedWriter(out, false) ;
- write(iOut, graph, prefixMap) ;
+ write$(iOut, graph, pmap, null) ;
}
+
+// public static void write(OutputStream out, Graph graph, Map<String, String> prefixMap)
+// {
+// PrefixMap pmap = PrefixMapFactory.create(graph.getPrefixMapping()) ;
+// IndentedWriter iOut = new IndentedWriter(out, false) ;
+// write$(iOut, graph, pmap, null) ;
+// }
+
+// static void write(IndentedWriter out, Graph graph, Map<String, String> prefixMap)
+// {
+// write(out, graph, prefixMap, null) ;
+// }
- // Call from TriG as well.
- static void write(IndentedWriter out, Graph graph, Map<String, String> prefixMap)
+ static void write(IndentedWriter out, Graph graph, Map<String, String> prefixMap, String baseURI)
{
- write$(out, graph, prefixMap) ;
+ write$(out, graph, PrefixMapFactory.create(prefixMap), baseURI) ;
out.flush() ;
}
- static void write$(IndentedWriter out, Graph graph, Map<String, String> prefixMap)
+ static void write$(OutputStream out, Graph graph, PrefixMap prefixMap, String baseURI)
+ {
+ IndentedWriter iOut = new IndentedWriter(out, false) ;
+ write$(iOut, graph, prefixMap, baseURI) ;
+ }
+
+ static void write$(IndentedWriter out, Graph graph, PrefixMap prefixMap, String baseURI)
{
- TurtleWriter w = new TurtleWriter(out, prefixMap) ;
+ TurtleWriter w = new TurtleWriter(out, prefixMap, baseURI) ;
w.write(graph) ;
out.flush() ;
}
- public TurtleWriter(IndentedWriter out, Map<String, String> prefixMap)
+ public TurtleWriter(IndentedWriter out, PrefixMap prefixMap, String baseURI)
{
- super(out, prefixMap) ;
+ super(out, prefixMap, baseURI) ;
}
private void write(Graph graph)
{
writePrefixes(prefixMap) ;
- writeGraph(graph) ;
+ if ( ! prefixMap.isEmpty() && !graph.isEmpty() )
+ out.println() ;
+ writeGraphTTL(graph) ;
}
}
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=1443653&r1=1443652&r2=1443653&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 Feb 7 18:48:49 2013
@@ -19,9 +19,10 @@
package riot.writer;
import java.io.OutputStream ;
-import java.util.Map ;
import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
import org.apache.jena.riot.system.StreamRDF ;
import com.hp.hpl.jena.graph.Graph ;
@@ -30,23 +31,34 @@ import com.hp.hpl.jena.rdf.model.Model ;
/** Turtle writer that streams - print in blocks of triples formatted
* by adjacent same subject.
*/
-public class TurtleWriterBlocks extends WriterStreamBase
+public class TurtleWriterBlocks
{
public static void write(OutputStream out, Model model)
{
- IndentedWriter iOut = new IndentedWriter(out) ;
- write(iOut, model.getGraph(), model.getNsPrefixMap()) ;
+ write(out, model.getGraph()) ;
}
public static void write(IndentedWriter out, Model model)
{
- write(out, model.getGraph(), model.getNsPrefixMap()) ;
+ write(out, model.getGraph()) ;
+ }
+
+ public static void write(OutputStream out, Graph graph)
+ {
+ IndentedWriter iOut = new IndentedWriter(out) ;
+ write(iOut, graph) ;
+ }
+
+ public static void write(IndentedWriter out, Graph graph)
+ {
+ PrefixMap pmap = PrefixMapFactory.create(graph.getPrefixMapping()) ;
+ write(out, graph, pmap, null) ;
}
- public static void write(IndentedWriter out, Graph graph, Map<String, String> prefixes)
+ public static void write(IndentedWriter out, Graph graph, PrefixMap pmap, String baseURI)
{
StreamRDF dest = new WriterStreamRDFBlocks(out) ;
- write(dest, graph, prefixes) ;
+ WriterStreamBase.write(dest, graph, pmap, baseURI) ;
}
}
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=1443653&r1=1443652&r2=1443653&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 Feb 7 18:48:49 2013
@@ -19,31 +19,43 @@
package riot.writer;
import java.io.OutputStream ;
-import java.util.Map ;
import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
import org.apache.jena.riot.system.StreamRDF ;
import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.rdf.model.Model ;
/** Write Turtle as one line of prefixed names, with short form literals (e.g. integers) */
-public class TurtleWriterFlat extends WriterStreamBase
+public class TurtleWriterFlat
{
public static void write(OutputStream out, Model model)
{
- IndentedWriter iOut = new IndentedWriter(out) ;
- write(iOut, model.getGraph(), model.getNsPrefixMap()) ;
+ write(out, model.getGraph()) ;
}
-
+
public static void write(IndentedWriter out, Model model)
{
- write(out, model.getGraph(), model.getNsPrefixMap()) ;
+ write(out, model.getGraph()) ;
+ }
+
+ public static void write(OutputStream out, Graph graph)
+ {
+ IndentedWriter iOut = new IndentedWriter(out) ;
+ write(iOut, graph) ;
+ }
+
+ public static void write(IndentedWriter out, Graph graph)
+ {
+ PrefixMap pmap = PrefixMapFactory.create(graph.getPrefixMapping()) ;
+ write(out, graph, pmap, null) ;
}
- public static void write(IndentedWriter out, Graph graph, Map<String, String> prefixes)
+ public static void write(IndentedWriter out, Graph graph, PrefixMap prefixes, String baseURI)
{
StreamRDF dest = new WriterStreamRDFFlat(out) ;
- write(dest, graph, prefixes) ;
+ WriterStreamBase.write(dest, graph, prefixes, baseURI) ;
}
}
Modified: 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=1443653&r1=1443652&r2=1443653&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamBase.java Thu Feb 7 18:48:49 2013
@@ -19,9 +19,9 @@
package riot.writer;
import java.util.Iterator ;
-import java.util.Map ;
import java.util.Map.Entry ;
+import org.apache.jena.riot.system.PrefixMap ;
import org.apache.jena.riot.system.StreamRDF ;
import com.hp.hpl.jena.graph.Graph ;
@@ -37,9 +37,9 @@ public abstract class WriterStreamBase
protected static void start(StreamRDF dest)
{ dest.start() ; }
- protected static void writePrefixes(StreamRDF dest, Map<String, String> prefixes)
+ protected static void writePrefixes(StreamRDF dest, PrefixMap prefixMap)
{
- for ( Entry<String, String> e : prefixes.entrySet() )
+ for ( Entry<String, String> e : prefixMap.getMappingCopyStr().entrySet())
dest.prefix(e.getKey(), e.getValue()) ;
}
@@ -61,18 +61,20 @@ public abstract class WriterStreamBase
}
}
- protected static void write(StreamRDF dest, DatasetGraph datasetGraph, Map<String, String> prefixes)
+ protected static void write(StreamRDF dest, DatasetGraph datasetGraph, PrefixMap prefixes, String baseURI)
{
start(dest) ;
+ dest.base(baseURI) ;
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)
+ protected static void write(StreamRDF dest, Graph graph, PrefixMap prefixes, String baseURI)
{
start(dest) ;
+ dest.base(baseURI) ;
writePrefixes(dest, prefixes) ;
Iterator<Triple> iter = graph.find(null, null, null) ;
writeTriples(dest, iter) ;