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/08 21:13:52 UTC
svn commit: r1444211 - in /jena/Experimental/riot-output: ./
src/main/java/dev/ src/main/java/riot/ src/main/java/riot/writer/
src/test/java/riot/writer/
Author: andy
Date: Fri Feb 8 20:13:51 2013
New Revision: 1444211
URL: http://svn.apache.org/r1444211
Log:
Tests; debugging.
Added:
jena/Experimental/riot-output/src/main/java/riot/writer/RDFJSONWriter.java
jena/Experimental/riot-output/src/main/java/riot/writer/SinkEntityOutput.java
jena/Experimental/riot-output/src/test/java/riot/writer/TS_RiotWriter.java
jena/Experimental/riot-output/src/test/java/riot/writer/TestRiotWriter.java
Removed:
jena/Experimental/riot-output/src/test/java/riot/writer/TestRIOTWriter.java
Modified:
jena/Experimental/riot-output/.classpath
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/RDFWriterRegistry.java
jena/Experimental/riot-output/src/main/java/riot/writer/NQuadsWriter.java
jena/Experimental/riot-output/src/main/java/riot/writer/NTriplesWriter.java
jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.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/.classpath
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/.classpath?rev=1444211&r1=1444210&r2=1444211&view=diff
==============================================================================
--- jena/Experimental/riot-output/.classpath (original)
+++ jena/Experimental/riot-output/.classpath Fri Feb 8 20:13:51 2013
@@ -3,7 +3,6 @@
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
<classpathentry including="**/*.java" kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
- <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry combineaccessrules="false" kind="src" path="/jena-arq"/>
<classpathentry combineaccessrules="false" kind="src" path="/jena-core"/>
<classpathentry kind="lib" path="/home/afs/.m2/repo/org/apache/jena/jena-iri/0.9.5-SNAPSHOT/jena-iri-0.9.5-SNAPSHOT.jar"/>
@@ -12,5 +11,6 @@
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-log4j12/1.6.4/slf4j-log4j12-1.6.4.jar" sourcepath="M2_REPO/org/slf4j/slf4j-log4j12/1.6.4/slf4j-log4j12-1.6.4-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16.jar" sourcepath="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/jcl-over-slf4j/1.6.4/jcl-over-slf4j-1.6.4.jar" sourcepath="M2_REPO/org/slf4j/jcl-over-slf4j/1.6.4/jcl-over-slf4j-1.6.4-sources.jar"/>
+ <classpathentry kind="var" path="M2_REPO/junit/junit/4.11/junit-4.11.jar" sourcepath="M2_REPO/junit/junit/4.11/junit-4.11-sources.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>
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=1444211&r1=1444210&r2=1444211&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/dev/MainWriter.java (original)
+++ jena/Experimental/riot-output/src/main/java/dev/MainWriter.java Fri Feb 8 20:13:51 2013
@@ -31,7 +31,7 @@ import org.apache.jena.riot.system.Prefi
import org.apache.jena.riot.system.PrefixMapFactory ;
import org.apache.jena.riot.system.StreamRDF ;
import riot.RiotWriter ;
-import riot.writer.TriGWriter ;
+import riot.writer.TriGWriterBlocks ;
import riot.writer.TurtleWriter ;
import riot.writer.TurtleWriterBlocks ;
import riot.writer.TurtleWriterFlat ;
@@ -50,14 +50,14 @@ public class MainWriter
{
public static void main(String ...args)
{
- writeTTL() ;
- //writeTriG() ;
+ //writeTTL() ;
+ writeTriG() ;
}
public static void writeTriG()
{
Dataset ds = RDFDataMgr.loadDataset("W.trig") ;
- TriGWriter.write(System.out, ds) ;
+ TriGWriterBlocks.write(System.out, ds) ;
}
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=1444211&r1=1444210&r2=1444211&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 Fri Feb 8 20:13:51 2013
@@ -24,8 +24,12 @@ public class PROJECT_RiotWriter
// Prologue
// JSON-LD
// Remove prefixes options on statics
+
+ // Where do the constants go?
+
+ // TriG & empty graphs.
- // RDF/XML adapter. [done]
+ // Test RT
// baseURI.
// ExRIOT_out1 :: write model
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=1444211&r1=1444210&r2=1444211&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/RDFWriterRegistry.java Fri Feb 8 20:13:51 2013
@@ -19,8 +19,6 @@
package riot;
import static org.apache.jena.riot.RDFLanguages.* ;
-import static org.apache.jena.riot.RDFLanguages.TRIG ;
-import static org.apache.jena.riot.RDFLanguages.TURTLE ;
import java.io.OutputStream ;
import java.util.HashMap ;
@@ -29,6 +27,7 @@ import java.util.Map ;
import org.apache.jena.atlas.lib.Lib ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RDFLanguages ;
+import org.apache.jena.riot.RIOT ;
import org.apache.jena.riot.RiotException ;
import riot.writer.* ;
@@ -37,6 +36,7 @@ import com.hp.hpl.jena.sparql.core.Datas
public class RDFWriterRegistry
{
+ static { RIOT.init() ; }
// Registry with the pretty write
static Map<RDFSerialization, WriterRIOTFactory> registry = new HashMap<RDFSerialization, WriterRIOTFactory>() ;
@@ -78,9 +78,10 @@ public class RDFWriterRegistry
if ( Lib.equal(TurtlePretty, serialization) )
return createTurtle(out, graph) ;
if ( Lib.equal(TurtleBlocks, serialization) )
- return null ;
+ return createTurtleStreaming(out, graph) ;
if ( Lib.equal(TurtleFlat, serialization) )
- return null ;
+ return createTurtleFlat(out, graph) ;
+
if ( Lib.equal(NTriples, serialization) )
return createNTriples(out, graph) ;
if ( Lib.equal(RDFJSON, serialization) )
@@ -89,6 +90,7 @@ public class RDFWriterRegistry
return createRDFXMLAbbrev(out, graph) ;
if ( Lib.equal(RDFXMLPlain, serialization) )
return createRDFXMLPlain(out, graph) ;
+
// graphs in quad format.
if ( Lib.equal(TrigPretty, serialization) )
return createTrig(out, graph) ;
@@ -96,14 +98,16 @@ public class RDFWriterRegistry
return createTrigStreaming(out, graph) ;
if ( Lib.equal(TrigFlat, serialization) )
return createTrigFlat(out,graph) ;
+
if ( Lib.equal(NQuads, serialization) )
return createNQuads(out, graph) ;
return null ;
}} ;
- static { init() ; }
+ public static void init() {}
- static void init()
+ static { init$() ; }
+ private static void init$()
{
register(TurtlePretty, wfactory) ;
register(TurtleBlocks, wfactory) ;
@@ -177,6 +181,13 @@ public class RDFWriterRegistry
@Override public Lang getLang() { return TURTLE ; }
} ; }
+ private static RiotSerializer createTurtleFlat(final OutputStream out, final Graph graph)
+ { return new RiotSerializer() {
+ @Override public void write() { TurtleWriterFlat.write(out, graph) ; }
+ @Override public Lang getLang() { return TURTLE ; }
+ } ; }
+
+
private static RiotSerializer createNTriples(final OutputStream out, final Graph graph)
{ return new RiotSerializer() {
@Override public void write() { NTriplesWriter.write(out, graph) ; }
@@ -195,9 +206,11 @@ public class RDFWriterRegistry
@Override public Lang getLang() { return Lang.RDFXML ; }
} ; }
-
- private static RiotSerializer createRDFJSON(OutputStream out, Graph graph)
- { return null ; }
+ private static RiotSerializer createRDFJSON(final OutputStream out, final Graph graph)
+ { return new RiotSerializer() {
+ @Override public void write() { RDFJSONWriter.write(out, graph) ; }
+ @Override public Lang getLang() { return Lang.RDFJSON ; }
+ } ; }
private static RiotSerializer createN3(OutputStream out, Graph graph)
{ return createTurtle(out, graph) ; }
Modified: jena/Experimental/riot-output/src/main/java/riot/writer/NQuadsWriter.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/NQuadsWriter.java?rev=1444211&r1=1444210&r2=1444211&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/NQuadsWriter.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/NQuadsWriter.java Fri Feb 8 20:13:51 2013
@@ -44,6 +44,8 @@ public class NQuadsWriter
public static void write(OutputStream out, Iterator<Quad> iter)
{
StreamRDF s = new WriterStreamRDFTuples(out) ;
+ s.start() ;
StreamRDFLib.quadsToStream(s, iter) ;
+ s.finish();
}
}
Modified: jena/Experimental/riot-output/src/main/java/riot/writer/NTriplesWriter.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/NTriplesWriter.java?rev=1444211&r1=1444210&r2=1444211&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/NTriplesWriter.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/NTriplesWriter.java Fri Feb 8 20:13:51 2013
@@ -44,6 +44,8 @@ public class NTriplesWriter
public static void write(OutputStream out, Iterator<Triple> iter)
{
StreamRDF s = new WriterStreamRDFTuples(out) ;
+ s.start() ;
StreamRDFLib.triplesToStream(s, iter) ;
+ s.finish() ;
}
}
Added: jena/Experimental/riot-output/src/main/java/riot/writer/RDFJSONWriter.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/RDFJSONWriter.java?rev=1444211&view=auto
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/RDFJSONWriter.java (added)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/RDFJSONWriter.java Fri Feb 8 20:13:51 2013
@@ -0,0 +1,86 @@
+/*
+ * 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.io.OutputStream ;
+import java.io.Writer ;
+import java.util.HashMap ;
+import java.util.HashSet ;
+import java.util.Map ;
+import java.util.Set ;
+
+import org.apache.jena.atlas.lib.Pair ;
+import org.apache.jena.atlas.lib.Sink ;
+import org.apache.jena.riot.system.Prologue ;
+import org.apache.jena.riot.system.SyntaxLabels ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.graph.GraphUtil ;
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.util.iterator.ExtendedIterator ;
+
+public class RDFJSONWriter {
+
+ public RDFJSONWriter() {}
+
+ public static void write (OutputStream out, Graph graph) {
+ Prologue prologue = Prologue.create(null, null) ; // (null, graph.getPrefixMapping()) ;
+ Sink<Pair<Node, Map<Node, Set<Node>>>> sink = new SinkEntityOutput(out, prologue, SyntaxLabels.createNodeToLabel()) ;
+ write ( sink, graph ) ;
+ }
+
+ public static void write (Writer out, Graph graph) {
+ Prologue prologue = Prologue.create(null, null) ; // (null, graph.getPrefixMapping()) ;
+ Sink<Pair<Node, Map<Node, Set<Node>>>> sink = new SinkEntityOutput(out, prologue, SyntaxLabels.createNodeToLabel()) ;
+ write ( sink, graph ) ;
+ }
+
+ private static void write (Sink<Pair<Node, Map<Node, Set<Node>>>> sink, Graph graph) {
+ ExtendedIterator<Node> subjects = GraphUtil.listSubjects(graph, Node.ANY, Node.ANY) ;
+ try {
+ Map<Node, Set<Node>> predicates = new HashMap<Node, Set<Node>>() ;
+ while ( subjects.hasNext() ) {
+ Node subject = subjects.next() ;
+ ExtendedIterator<Triple> triples = graph.find(subject, Node.ANY, Node.ANY) ;
+ try {
+ while ( triples.hasNext() ) {
+ Triple triple = triples.next() ;
+ Node p = triple.getPredicate() ;
+ if ( predicates.containsKey(p) ) {
+ predicates.get(p).add(triple.getObject()) ;
+ } else {
+ Set<Node> objects = new HashSet<Node>() ;
+ objects.add(triple.getObject()) ;
+ predicates.put(p, objects) ;
+ }
+ }
+ } finally {
+ if ( triples != null ) triples.close() ;
+ }
+ sink.send(new Pair<Node, Map<Node, Set<Node>>>(subject, predicates)) ;
+ predicates.clear() ;
+ }
+ } finally {
+ if ( subjects != null ) subjects.close() ;
+ sink.close() ;
+ }
+ }
+
+}
Added: jena/Experimental/riot-output/src/main/java/riot/writer/SinkEntityOutput.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/writer/SinkEntityOutput.java?rev=1444211&view=auto
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/SinkEntityOutput.java (added)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/SinkEntityOutput.java Fri Feb 8 20:13:51 2013
@@ -0,0 +1,148 @@
+/*
+ * 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.io.OutputStream ;
+import java.io.Writer ;
+import java.util.Map ;
+import java.util.Set ;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.atlas.json.io.JSWriter ;
+import org.apache.jena.atlas.lib.Pair ;
+import org.apache.jena.atlas.lib.Sink ;
+import org.apache.jena.riot.RiotException ;
+import org.apache.jena.riot.out.NodeToLabel ;
+import org.apache.jena.riot.system.Prologue ;
+import org.apache.jena.riot.system.SyntaxLabels ;
+
+import com.hp.hpl.jena.graph.Node ;
+
+public class SinkEntityOutput implements Sink<Pair<Node, Map<Node, Set<Node>>>> {
+
+ private Prologue prologue = null ;
+ private NodeToLabel labelPolicy = null ;
+ private JSWriter out ;
+
+ public SinkEntityOutput(OutputStream outs)
+ {
+ this(outs, null, SyntaxLabels.createNodeToLabel()) ;
+ }
+
+ public SinkEntityOutput(OutputStream outs, Prologue prologue, NodeToLabel labels)
+ {
+ init(new JSWriter(outs), prologue, labels) ;
+ }
+
+ public SinkEntityOutput(Writer outs)
+ {
+ this(outs, null, SyntaxLabels.createNodeToLabel()) ;
+ }
+
+ public SinkEntityOutput(Writer outs, Prologue prologue, NodeToLabel labels)
+ {
+ init(new JSWriter(new IndentedWriterEx(outs)), prologue, labels) ;
+ }
+
+ private void init (JSWriter out, Prologue prologue, NodeToLabel labels)
+ {
+ this.out = out ;
+ setPrologue(prologue) ;
+ setLabelPolicy(labels) ;
+ out.startOutput() ;
+ out.startObject() ;
+ }
+
+ public void setPrologue(Prologue prologue)
+ {
+ this.prologue = prologue ;
+ }
+
+ public void setLabelPolicy(NodeToLabel labels)
+ {
+ this.labelPolicy = labels ;
+ }
+
+ @Override
+ public void send(Pair<Node, Map<Node, Set<Node>>> item) {
+ Node s = item.getLeft() ;
+ if ( s.isBlank() ) {
+ out.key("_:" + s.getBlankNodeLabel()) ;
+ } else if ( s.isURI() ) {
+ out.key(s.getURI()) ;
+ } else {
+ throw new RiotException ("Only URIs or blank nodes are legal subjects.") ;
+ }
+ out.startObject() ;
+ // out.pair(key, value) ;
+ Map<Node, Set<Node>> predicates = item.getRight() ;
+ for (Node p : predicates.keySet() ) {
+ out.key(p.getURI()) ;
+ out.startArray() ;
+ Set<Node> objects = predicates.get(p) ;
+ int i = 0;
+ for ( Node o : objects ) {
+ out.startObject() ;
+ if ( o.isBlank() ) {
+ out.pair("type", "bnode") ;
+ out.pair("value", "_:" + o.getBlankNodeLabel()) ;
+ } else if ( o.isURI() ) {
+ out.pair("type", "uri") ;
+ out.pair("value", o.getURI()) ;
+ } else if ( o.isLiteral() ) {
+ String dt = o.getLiteralDatatypeURI() ;
+ String lang = o.getLiteralLanguage() ;
+ String lex = o.getLiteralLexicalForm() ;
+ out.pair("type", "literal") ;
+ out.pair("value", lex) ;
+ if ( dt != null )
+ out.pair("datatype", dt) ;
+ if ( ( lang != null ) && ( lang != "" ) )
+ out.pair("lang", lang) ;
+ }
+ out.finishObject() ;
+ if (i < objects.size() - 1)
+ {
+ out.arraySep();
+ }
+ i++;
+ }
+ out.finishArray() ;
+ }
+ out.finishObject() ;
+ }
+
+ @Override
+ public void flush() {
+ out.finishObject() ;
+ out.finishOutput();
+ }
+
+ @Override
+ public void close() {
+ flush() ;
+ }
+
+ private class IndentedWriterEx extends IndentedWriter {
+ public IndentedWriterEx(Writer writer) {
+ super(writer);
+ }
+ }
+
+}
Modified: 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=1444211&r1=1444210&r2=1444211&view=diff
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java (original)
+++ jena/Experimental/riot-output/src/main/java/riot/writer/WriterStreamRDF.java Fri Feb 8 20:13:51 2013
@@ -51,8 +51,8 @@ public abstract class WriterStreamRDF im
// :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 boolean NL_GDFT_END = true ; // Whether the closing } is on a new line
+ protected static final boolean NL_GNMD_END = true ; // 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.
@@ -60,8 +60,8 @@ public abstract class WriterStreamRDF im
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 activeTripleData = false ;
+ protected boolean activeQuadData = false ;
protected boolean lastWasDirective = false ;
protected final PrefixMap pMap ;
@@ -69,7 +69,8 @@ public abstract class WriterStreamRDF im
protected final IndentedWriter out ;
protected Node lastSubject = null ;
- protected Node lastGraph = null ; // null -> unset : Quad.
+ protected Node lastGraph = null ; // null -> unset : Quad.
+ protected boolean firstGraph = true ;
// --- Indent management.
protected int currentGraphIndent = 0;
@@ -169,8 +170,8 @@ public abstract class WriterStreamRDF im
protected void reset()
{
- lastWasTripleData = false ;
- lastWasQuadData = false ;
+ activeTripleData = false ;
+ activeQuadData = false ;
lastWasDirective = false ;
lastSubject = null ;
lastGraph = null ;
@@ -214,7 +215,8 @@ public abstract class WriterStreamRDF im
// End of graph
if ( ! equal(lastGraph, g) )
{
- out.print(" .") ;
+ if ( lastSubject != null )
+ out.print(" .") ;
boolean nl = dftGraph() ? NL_GDFT_END : NL_GNMD_END ;
if ( nl ) {
@@ -223,10 +225,12 @@ public abstract class WriterStreamRDF im
out.println() ;
out.println("}") ;
} else {
- // } on same line as last quad/triple.
- out.println(" }") ; // Trailing NL
+ // Possibly on same line as last quad/triple.
out.decIndent(graphIndent()) ;
- out.println() ;
+ if ( out.atLineStart() )
+ out.println("}") ; // Trailing NL
+ else
+ out.println(" }") ; // Trailing NL
}
lastSubject = null ;
lastGraph = null ;
@@ -238,25 +242,31 @@ public abstract class WriterStreamRDF im
// Start graph
if ( lastGraph == null )
{
+ if ( ! firstGraph )
+ out.println() ;
+ firstGraph = false ;
+
lastSubject = null ;
if ( g == Quad.defaultGraphNodeGenerated )
{
- out.print("{ ") ;
- setGraphIndent(INDENT_GDFT) ;
if ( NL_GDFT_START )
- out.println();
+ out.println("{") ;
+ else
+ out.print("{ ") ;
+
+ setGraphIndent(INDENT_GDFT) ;
}
else
{
outputNode(g) ;
- out.print(" { ") ;
if ( NL_GNMD_START )
{
- out.println() ;
+ out.println(" {") ;
setGraphIndent(INDENT_GNMD) ;
}
else
{
+ out.print(" { ") ;
setGraphIndent(out.getCol()) ;
}
}
@@ -273,9 +283,9 @@ public abstract class WriterStreamRDF im
protected void endData()
{
- if ( lastWasQuadData )
+ if ( activeQuadData )
endGraph(null) ;
- if ( lastWasTripleData )
+ if ( activeTripleData )
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=1444211&r1=1444210&r2=1444211&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 Fri Feb 8 20:13:51 2013
@@ -55,7 +55,7 @@ public class WriterStreamRDFBlocks exten
startGraph(g) ;
print$(s,p,o) ;
lastGraph = g ;
- lastWasQuadData = true ;
+ activeQuadData = true ;
}
@Override
@@ -63,7 +63,7 @@ public class WriterStreamRDFBlocks exten
{
printPre() ;
print$(s, p, o) ;
- lastWasTripleData = true ;
+ activeTripleData = true ;
}
private void print$(Node s, Node p, Node o)
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=1444211&r1=1444210&r2=1444211&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 Fri Feb 8 20:13:51 2013
@@ -52,7 +52,7 @@ public class WriterStreamRDFFlat extends
startGraph(g) ;
print$(s,p,o) ;
lastGraph = g ;
- lastWasQuadData = true ;
+ activeQuadData = true ;
}
@Override
@@ -60,7 +60,7 @@ public class WriterStreamRDFFlat extends
{
printPre() ;
print$(s, p, o) ;
- lastWasTripleData = true ;
+ activeTripleData = false ; // We completed the triple.
}
private void print$(Node s, Node p, Node o)
Added: jena/Experimental/riot-output/src/test/java/riot/writer/TS_RiotWriter.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/test/java/riot/writer/TS_RiotWriter.java?rev=1444211&view=auto
==============================================================================
--- jena/Experimental/riot-output/src/test/java/riot/writer/TS_RiotWriter.java (added)
+++ jena/Experimental/riot-output/src/test/java/riot/writer/TS_RiotWriter.java Fri Feb 8 20:13:51 2013
@@ -0,0 +1,39 @@
+/**
+ * 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 org.apache.jena.riot.RIOT ;
+import org.junit.BeforeClass ;
+import org.junit.runner.RunWith ;
+import org.junit.runners.Suite ;
+import org.junit.runners.Suite.SuiteClasses ;
+
+@RunWith(Suite.class)
+@SuiteClasses(
+{
+ TestRiotWriter.class
+})
+
+public class TS_RiotWriter
+{
+ @BeforeClass public static void beforeClass()
+ {
+ RIOT.init() ;
+ }
+}
Added: jena/Experimental/riot-output/src/test/java/riot/writer/TestRiotWriter.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/test/java/riot/writer/TestRiotWriter.java?rev=1444211&view=auto
==============================================================================
--- jena/Experimental/riot-output/src/test/java/riot/writer/TestRiotWriter.java (added)
+++ jena/Experimental/riot-output/src/test/java/riot/writer/TestRiotWriter.java Fri Feb 8 20:13:51 2013
@@ -0,0 +1,133 @@
+/**
+ * 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.io.ByteArrayInputStream ;
+import java.io.ByteArrayOutputStream ;
+import java.util.Arrays ;
+
+import org.apache.jena.atlas.junit.BaseTest ;
+import org.apache.jena.atlas.lib.StrUtils ;
+import org.apache.jena.riot.Lang ;
+import org.apache.jena.riot.RDFDataMgr ;
+import org.apache.jena.riot.RiotException ;
+import org.junit.Test ;
+import org.junit.runner.RunWith ;
+import org.junit.runners.Parameterized ;
+import org.junit.runners.Parameterized.Parameters ;
+import riot.RDFSerialization ;
+import riot.RDFWriterRegistry ;
+import riot.RiotSerializer ;
+
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+
+@RunWith(Parameterized.class)
+public class TestRiotWriter extends BaseTest
+{
+ @Parameters(name = "{index}: {0}")
+ public static Iterable<Object[]> data() {
+ return Arrays.asList(new Object[][] {
+ { RDFWriterRegistry.NTriples } ,
+ { RDFWriterRegistry.Turtle } ,
+ { RDFWriterRegistry.TurtlePretty } ,
+ { RDFWriterRegistry.TurtleBlocks } ,
+ { RDFWriterRegistry.TurtleFlat } ,
+ { RDFWriterRegistry.RDFXML } ,
+ { RDFWriterRegistry.RDFXMLPretty } ,
+ { RDFWriterRegistry.RDFXMLPlain } ,
+ { RDFWriterRegistry.RDFJSON } ,
+
+ // graph in quad formats.
+ { RDFWriterRegistry.Trig } ,
+ { RDFWriterRegistry.TrigPretty } ,
+ { RDFWriterRegistry.TrigBlocks } ,
+ { RDFWriterRegistry.TrigFlat } ,
+ { RDFWriterRegistry.NQuads} ,
+
+ }) ;
+ }
+
+ private RDFSerialization format ;
+
+ public TestRiotWriter(RDFSerialization format)
+ {
+ this.format = format ;
+ }
+
+ static String DIR = "testing/RIOT/Writer" ;
+
+ @Test public void writer00() { test("writer-rt-00.ttl") ; }
+ @Test public void writer01() { test("writer-rt-01.ttl") ; }
+ @Test public void writer02() { test("writer-rt-02.ttl") ; }
+ @Test public void writer03() { test("writer-rt-03.ttl") ; }
+ @Test public void writer04() { test("writer-rt-04.ttl") ; }
+ @Test public void writer05() { test("writer-rt-05.ttl") ; }
+ @Test public void writer06() { test("writer-rt-06.ttl") ; }
+ @Test public void writer07() { test("writer-rt-07.ttl") ; }
+ @Test public void writer08() { test("writer-rt-08.ttl") ; }
+ @Test public void writer09() { test("writer-rt-09.ttl") ; }
+ @Test public void writer10() { test("writer-rt-10.ttl") ; }
+ @Test public void writer11() { test("writer-rt-11.ttl") ; }
+ @Test public void writer12() { test("writer-rt-12.ttl") ; }
+ @Test public void writer13() { test("writer-rt-13.ttl") ; }
+ @Test public void writer14() { test("writer-rt-14.ttl") ; }
+ @Test public void writer15() { test("writer-rt-15.ttl") ; }
+ @Test public void writer16() { test("writer-rt-16.ttl") ; }
+
+ private void test(String filename)
+ {
+ String displayname = filename.substring(0, filename.lastIndexOf('.')) ;
+ String fn = DIR + "/" + filename ;
+ Model m = RDFDataMgr.loadModel(fn) ;
+ Lang lang = format.getLang() ;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream() ;
+ RiotSerializer rs = RDFWriterRegistry.getWriterFactory(format).create(out, m.getGraph(), format) ;
+ assertEquals(lang, rs.getLang()) ;
+ rs.write() ;
+
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()) ;
+ String s = StrUtils.fromUTF8bytes(out.toByteArray()) ;
+
+ Model m2 = ModelFactory.createDefaultModel() ;
+
+ try {
+ RDFDataMgr.read(m2, in, lang) ;
+ } catch (RiotException ex)
+ {
+ System.out.println(format) ;
+ System.out.println(s) ;
+ throw ex ;
+ }
+
+ boolean b = m.isIsomorphicWith(m2) ;
+ if ( !b )
+ {
+ System.out.println("------["+format+"]---------------------------------------------------") ;
+
+ System.out.println("#### file="+displayname) ;
+ System.out.print(s) ;
+ }
+
+ assertTrue("Did not round-trip file="+filename+" / format="+format, b) ;
+
+ }
+}
+