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 2022/02/07 16:35:31 UTC
[jena] branch main updated: Tidy up around streaming printing
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/main by this push:
new c33343f Tidy up around streaming printing
new 447c210 Merge pull request #1191 from afs/print-as-is
c33343f is described below
commit c33343fc9a142a24453150bb905b40adf8268a8d
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sat Feb 5 20:01:26 2022 +0000
Tidy up around streaming printing
---
.../org/apache/jena/riot/out/NodeFormatter.java | 40 +++++++++----------
.../apache/jena/riot/system/PrintingStreamRDF.java | 41 +++++++++++++------
.../jena/riot/writer/WriterStreamRDFBase.java | 46 +++++++++++++++-------
.../jena/riot/writer/WriterStreamRDFFlat.java | 11 +++---
.../jena/riot/writer/WriterStreamRDFPlain.java | 23 +++++++----
5 files changed, 100 insertions(+), 61 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatter.java b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatter.java
index abfba8a..e846c31 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatter.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatter.java
@@ -18,36 +18,34 @@
package org.apache.jena.riot.out;
+import org.apache.jena.atlas.io.AWriter;
+import org.apache.jena.graph.Node;
-
-import org.apache.jena.atlas.io.AWriter ;
-import org.apache.jena.graph.Node ;
-
+/** Processor for output of RDF terms. */
public interface NodeFormatter
{
- public void format(AWriter w, Node n) ;
+ public void format(AWriter w, Node n);
/** Node is guaranteed to be a URI node */
- public void formatURI(AWriter w, Node n) ;
- public void formatURI(AWriter w, String uriStr) ;
-
- public void formatVar(AWriter w, Node n) ;
- public void formatVar(AWriter w, String name) ;
-
+ public void formatURI(AWriter w, Node n);
+ public void formatURI(AWriter w, String uriStr);
+
+ public void formatVar(AWriter w, Node n);
+ public void formatVar(AWriter w, String name);
+
/** Node is guaranteed to be a blank node */
- public void formatBNode(AWriter w, Node n) ;
- public void formatBNode(AWriter w, String label) ;
-
+ public void formatBNode(AWriter w, Node n);
+ public void formatBNode(AWriter w, String label);
+
/** Node is guaranteed to be a literal */
- public void formatLiteral(AWriter w, Node n) ;
-
+ public void formatLiteral(AWriter w, Node n);
+
/** Plain string / xsd:string (RDF 1.1) */
- public void formatLitString(AWriter w, String lex) ;
-
+ public void formatLitString(AWriter w, String lex);
+
/** String with language tag */
- public void formatLitLang(AWriter w, String lex, String langTag) ;
+ public void formatLitLang(AWriter w, String lex, String langTag);
/** Literal with datatype, not a simple literal, not an xsd:string (RDF 1.1), no language tag. */
- public void formatLitDT(AWriter w, String lex, String datatypeURI) ;
-
+ public void formatLitDT(AWriter w, String lex, String datatypeURI);
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/PrintingStreamRDF.java b/jena-arq/src/main/java/org/apache/jena/riot/system/PrintingStreamRDF.java
index 3c1bf27..bc369c5 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/PrintingStreamRDF.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/PrintingStreamRDF.java
@@ -20,29 +20,43 @@ package org.apache.jena.riot.system;
import java.io.OutputStream ;
-import org.apache.jena.atlas.io.AWriter ;
-import org.apache.jena.atlas.io.IndentedWriter;
-import org.apache.jena.riot.writer.WriterStreamRDFPlain ;
+import org.apache.jena.atlas.io.IO;
+import org.apache.jena.riot.out.NodeFormatter;
+import org.apache.jena.riot.out.NodeFormatterTTL;
+import org.apache.jena.riot.writer.WriterStreamRDFPlain;
-/** Primarily for debugging */
-public class PrintingStreamRDF extends WriterStreamRDFPlain //implements StreamRDF
+/**
+ * A StreamRDF which displays the items sent to the stream. It is primarily for
+ * development purposes.
+ * <p>
+ * The output is not a legal syntax. Do not consider this
+ * format to be stable. It is "N-Quads with abbreviations".
+ * <p>
+ * Use via
+ * <pre>
+ * StreamRDFLib.print(System.out);
+ * </pre>
+ */
+public class PrintingStreamRDF extends WriterStreamRDFPlain
{
+ private PrefixMap prefixMap = PrefixMapFactory.create();
+ private NodeFormatter pretty = new NodeFormatterTTL(null, prefixMap);
+
public PrintingStreamRDF(OutputStream out) {
- super(init(out)) ;
+ super(IO.wrapUTF8(out));
}
- private static AWriter init(OutputStream out) {
- IndentedWriter output = new IndentedWriter(out);
- output.setFlushOnNewline(true);
- return output;
- }
+ @Override
+ protected NodeFormatter getFmt() { return pretty; }
@Override
public void base(String base) {
out.print("BASE") ;
out.print(" ") ;
- nodeFmt.formatURI(out, base);
+ getFmt().formatURI(out, base);
out.println();
+ // Reset the formatter because of the new base URI.
+ pretty = new NodeFormatterTTL(base, prefixMap);
}
@Override
@@ -51,7 +65,8 @@ public class PrintingStreamRDF extends WriterStreamRDFPlain //implements StreamR
out.print(" ") ;
out.print(prefix) ;
out.print(": ") ;
- nodeFmt.formatURI(out, iri);
+ getFmt().formatURI(out, iri);
out.println();
+ prefixMap.add(prefix, iri);
}
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java
index b30fc9a..44eeacc 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBase.java
@@ -35,11 +35,17 @@ import org.apache.jena.riot.system.StreamRDF ;
import org.apache.jena.sparql.core.Quad ;
import org.apache.jena.sparql.util.Context;
-/** Core engine for output of triples / quads that is streaming.
- * Handles prefixes and base, together with the environment for processing.
- * If fed quads, the output is valid TriG.
- * If fed only triples, the output is valid Turtle.
- * Not for N-Quads and N-triples.
+/**
+ * Core engine for output of triples / quads that is streaming. It covers Turtle and
+ * TriG "{@link WriterStreamRDFFlat flat}" and "{@link WriterStreamRDFBlocks blocks}"
+ * variants.
+ * <p>
+ * It handles prefixes and base and node formats which is controlled by a {@link NodeFormatterTTL}.
+ * <p>
+ * The output is valid TriG.<br/>
+ * If fed only triples, the output is valid Turtle.
+ * <p>
+ * For N-Quads and N-triples use {@link WriterStreamRDFPlain}.
*/
public abstract class WriterStreamRDFBase implements StreamRDF
@@ -56,33 +62,43 @@ public abstract class WriterStreamRDFBase implements StreamRDF
protected NodeFormatterTTL fmt ;
protected final IndentedWriter out ;
protected final DirectiveStyle prefixStyle;
+ // Allows for ability to write RDF without writing the base URI.
protected final boolean printBase;
// Is there an active prefix mapping for the RDF namespace.
protected int countPrefixesForRDF = 0;
- public WriterStreamRDFBase(OutputStream output, Context context) {
+ protected WriterStreamRDFBase(OutputStream output, Context context) {
this(new IndentedWriter(output), context) ;
}
- public WriterStreamRDFBase(Writer output, Context context) {
+ protected WriterStreamRDFBase(Writer output, Context context) {
this(wrap(output), context);
}
- public WriterStreamRDFBase(IndentedWriter output, Context context) {
- out = output ;
- pMap = PrefixMapFactory.create() ;
- nodeToLabel = NodeToLabel.createScopeByDocument() ;
+ protected WriterStreamRDFBase(IndentedWriter output, Context context) {
+ this(output,
+ NodeToLabel.createScopeByDocument(),
+ WriterLib.directiveStyle(context),
+ context.isFalseOrUndef(RIOT.symTurtleOmitBase));
+ }
+ protected WriterStreamRDFBase(IndentedWriter output,
+ NodeToLabel nodeToLabel,
+ DirectiveStyle prefixStyle,
+ boolean printBase) {
// Stream writing does not take an external base URI from the API "write"
// call. The base URI is output if StreamRDF.base() called, which means BASE
// was in the data stream.
- baseURI = null ;
- prefixStyle = WriterLib.directiveStyle(context);
- printBase =
- ( context == null ) ? true : context.isFalseOrUndef(RIOT.symTurtleOmitBase);
+ this.out = output ;
+ this.baseURI = null ;
+ this.pMap = PrefixMapFactory.create() ;
+ this.nodeToLabel = nodeToLabel ;
+ this.prefixStyle = prefixStyle;
+ this.printBase = printBase;
setFormatter() ;
}
+ // Set and reset the formatter. It needs resetting if BASE is encountered.
private void setFormatter() {
fmt = new NodeFormatterTTL(baseURI, pMap, nodeToLabel);
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java
index bf9d647..ed1417e 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFFlat.java
@@ -19,7 +19,7 @@
package org.apache.jena.riot.writer;
import java.io.OutputStream ;
-import java.io.Writer ;
+import java.io.Writer;
import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.graph.Node ;
@@ -27,8 +27,9 @@ import org.apache.jena.graph.Triple ;
import org.apache.jena.sparql.core.Quad ;
import org.apache.jena.sparql.util.Context;
-/** An output of triples / quads that is streaming.
- * It writes one line per triple/quads, with prefixes and literal short forms.
+/**
+ * An output of triples / quads that is streaming.
+ * It writes one line per triple/quads, with prefixes and literal short forms.
*/
public class WriterStreamRDFFlat extends WriterStreamRDFBase
@@ -37,11 +38,11 @@ public class WriterStreamRDFFlat extends WriterStreamRDFBase
super(output, context);
}
- public WriterStreamRDFFlat(IndentedWriter output, Context context) {
+ public WriterStreamRDFFlat(Writer output, Context context) {
super(output, context);
}
- public WriterStreamRDFFlat(Writer output, Context context) {
+ public WriterStreamRDFFlat(IndentedWriter output, Context context) {
super(output, context);
}
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFPlain.java b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFPlain.java
index 7147789..1e5328b 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFPlain.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFPlain.java
@@ -38,7 +38,7 @@ public class WriterStreamRDFPlain implements StreamRDF {
// appearance of the Nodes themselves.
protected final AWriter out ;
- protected final NodeFormatter nodeFmt ;
+ private final NodeFormatter nodeFmt ;
/**
* Output tuples, using UTF8 output See {@link StreamRDFLib#writer} for
@@ -49,14 +49,23 @@ public class WriterStreamRDFPlain implements StreamRDF {
}
/**
- * Output tuples, choosing ASCII or UTF8 See
- * {@link StreamRDFLib#writer} for ways to create a AWriter object.
+ * Output tuples, choosing ASCII or UTF8.
+ * See {@link StreamRDFLib#writer} for ways to create a AWriter object.
*/
public WriterStreamRDFPlain(AWriter w, CharSpace charSpace) {
- out = w ;
- nodeFmt = new NodeFormatterNT(charSpace) ;
+ this(w, new NodeFormatterNT(charSpace)) ;
}
+ /**
+ * Output tuples using a specific {@link NodeFormatter}.
+ */
+ public WriterStreamRDFPlain(AWriter w, NodeFormatter nodeFmt) {
+ this.out = w;
+ this.nodeFmt = nodeFmt;
+ }
+
+ protected NodeFormatter getFmt() { return nodeFmt; }
+
@Override
public void start() {}
@@ -99,8 +108,8 @@ public class WriterStreamRDFPlain implements StreamRDF {
out.print(" .\n") ;
}
- private void format(Node n) {
- nodeFmt.format(out, n) ;
+ protected void format(Node n) {
+ getFmt().format(out, n) ;
}
@Override