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 2023/12/23 12:40:39 UTC

(jena) branch main updated (cca8c38875 -> f8a677f2c3)

This is an automated email from the ASF dual-hosted git repository.

andy pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git


    from cca8c38875 Bump org.apache.shiro:shiro-web from 1.12.0 to 1.13.0
     new d99ad23e92 Fix warnings
     new f8a677f2c3 GH-2083: Convert default graph quads to triples when writing triples syntax.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/jena/riot/out/NodeFmtLib.java  |  6 +++
 .../apache/jena/riot/system/StreamRDFWrapper.java  |  3 +-
 .../apache/jena/riot/system/StreamRDFWriter.java   | 10 +++++
 .../apache/jena/riot/system/StreamTriplesOnly.java | 44 ++++++++++++++++++----
 .../jena/riot/writer/WriterStreamRDFBase.java      |  1 -
 jena-cmds/src/main/java/riotcmd/CmdLangParse.java  | 13 ++++---
 .../arq/querybuilder/AbstractQueryBuilder.java     | 26 ++++++-------
 .../enhancer/impl/TestServiceEnhancerMisc.java     |  2 +-
 8 files changed, 75 insertions(+), 30 deletions(-)


(jena) 01/02: Fix warnings

Posted by an...@apache.org.
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

commit d99ad23e9229540c047c2b774feb1fcb0bcee214
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Fri Dec 22 14:00:19 2023 +0000

    Fix warnings
---
 .../arq/querybuilder/AbstractQueryBuilder.java     | 26 +++++++++++-----------
 .../enhancer/impl/TestServiceEnhancerMisc.java     |  2 +-
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
index 53f48d1cc5..50d0bf038e 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AbstractQueryBuilder.java
@@ -85,7 +85,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * Make a node or path from the object using the query prefix mapping.
-     * 
+     *
      * @param o the object to make the node or path from.
      * @return A node or path.
      * @see Converters#makeNodeOrPath(Object, PrefixMapping)
@@ -108,7 +108,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
      * <li>Will create a literal representation if the parseNode() fails or for any
      * other object type.</li>
      * </ul>
-     * 
+     *
      * @param o the object that should be interpreted as a path or a node.
      * @param pMapping the prefix mapping to resolve path or node with
      * @return the Path or Node
@@ -146,7 +146,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
      * @param p the predicate object
      * @param o the object object.
      * @return a TriplePath
-     * @deprecated Use {@link makeTriplePaths(Object, Object, Object)}
+     * @deprecated Use {@link #makeTriplePaths(Object, Object, Object)}
      */
     @Deprecated(since="5.0.0")
     public TriplePath makeTriplePath(Object s, Object p, Object o) {
@@ -156,7 +156,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
         }
         return new TriplePath(Triple.create(makeNode(s), (Node) po, makeNode(o)));
     }
-    
+
     /**
      * Make a collecton triple path from the objects.
      *
@@ -166,7 +166,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
      * <li>Will return the enclosed Node from a FrontsNode</li>
      * <li>Will return the object if it is a Node.</li>
      * <li>For {@code subject} and {@code object} <em>only</em>, if the object is a collection, will convert each item
-     * in the collection into a node and create an RDF list. All RDF list nodes are included in the collection.</li> 
+     * in the collection into a node and create an RDF list. All RDF list nodes are included in the collection.</li>
      * <li>If the object is a String
      * <ul>
      * <li>For <code>predicate</code> <em>only</em>, will attempt to parse as a path</li>
@@ -201,7 +201,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * A convenience method to quote a string.
-     * 
+     *
      * @param q the string to quote.
      *
      * Will use single quotes if there are no single quotes in the string or if the
@@ -219,7 +219,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * Verify that any Node_Variable nodes are returned as Var nodes.
-     * 
+     *
      * @param n the node to check
      * @return the node n or a new Var if n is an instance of Node_Variable
      * @deprecated use {@link Converters#checkVar(Node)}
@@ -240,7 +240,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
      * <li>Will create a literal representation if the parseNode() fails or for any
      * other object type.</li>
      * </ul>
-     * 
+     *
      * @param o The object to convert (may be null).
      * @param pMapping The prefix mapping to use for prefix resolution.
      * @return The Node value.
@@ -287,7 +287,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * Get the HandlerBlock for this query builder.
-     * 
+     *
      * @return The associated handler block.
      */
     public abstract HandlerBlock getHandlerBlock();
@@ -304,7 +304,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * Gets the where handler used by this QueryBuilder.
-     * 
+     *
      * @return the where handler used by this QueryBuilder.
      */
     public final WhereHandler getWhereHandler() {
@@ -313,7 +313,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * Adds the contents of the whereClause to the where clause of this builder.
-     * 
+     *
      * @param whereClause the where clause to add.
      * @return this builder for chaining.
      */
@@ -410,7 +410,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
 
     /**
      * Creates a collection of nodes from an iterator of Objects.
-     * 
+     *
      * @param iter the iterator of objects, may be null or empty.
      * @param prefixMapping the PrefixMapping to use when nodes are created.
      * @return a Collection of nodes or null if iter is null or empty.
@@ -424,7 +424,7 @@ public abstract class AbstractQueryBuilder<T extends AbstractQueryBuilder<T>>
     /**
      * Creates a collection of nodes from an iterator of Objects. Uses the prefix
      * mapping from the PrologHandler.
-     * 
+     *
      * @param iter the iterator of objects, may be null or empty.
      * @return a Collection of nodes or null if iter is null or empty.
      */
diff --git a/jena-extras/jena-serviceenhancer/src/test/java/org/apache/jena/sparql/service/enhancer/impl/TestServiceEnhancerMisc.java b/jena-extras/jena-serviceenhancer/src/test/java/org/apache/jena/sparql/service/enhancer/impl/TestServiceEnhancerMisc.java
index c907e916df..393c5b31ac 100644
--- a/jena-extras/jena-serviceenhancer/src/test/java/org/apache/jena/sparql/service/enhancer/impl/TestServiceEnhancerMisc.java
+++ b/jena-extras/jena-serviceenhancer/src/test/java/org/apache/jena/sparql/service/enhancer/impl/TestServiceEnhancerMisc.java
@@ -503,7 +503,7 @@ public class TestServiceEnhancerMisc {
             "   SERVICE <loop:cache:bulk+5> { ?s rdfs:label ?l }",
             "}");
 
-        Dataset ds = RDFParser.fromString(dataStr).lang(Lang.TURTLE).toDataset();
+        Dataset ds = RDFParser.fromString(dataStr, Lang.TURTLE).toDataset();
         Query query = QueryFactory.create(queryStr);
 
         int rsSize;


(jena) 02/02: GH-2083: Convert default graph quads to triples when writing triples syntax.

Posted by an...@apache.org.
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

commit f8a677f2c3d10564673e8aad683609a2b79f495e
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Fri Dec 22 14:01:37 2023 +0000

    GH-2083: Convert default graph quads to triples when writing triples syntax.
---
 .../java/org/apache/jena/riot/out/NodeFmtLib.java  |  6 +++
 .../apache/jena/riot/system/StreamRDFWrapper.java  |  3 +-
 .../apache/jena/riot/system/StreamRDFWriter.java   | 10 +++++
 .../apache/jena/riot/system/StreamTriplesOnly.java | 44 ++++++++++++++++++----
 .../jena/riot/writer/WriterStreamRDFBase.java      |  1 -
 jena-cmds/src/main/java/riotcmd/CmdLangParse.java  | 13 ++++---
 6 files changed, 61 insertions(+), 16 deletions(-)

diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFmtLib.java b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFmtLib.java
index 5064fa2e32..60bc9abbdf 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFmtLib.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFmtLib.java
@@ -164,6 +164,12 @@ public class NodeFmtLib
         return displayStrNodes(t.getSubject(), t.getPredicate(), t.getObject());
     }
 
+    public static String displayStr(Quad q) {
+        if ( q == null )
+            return nullStr;
+        return displayStrNodes(q.getGraph(), q.getSubject(), q.getPredicate(), q.getObject());
+    }
+
     public static String displayStr(Node node) {
         if ( node == null )
             return nullStr;
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWrapper.java b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWrapper.java
index ced0853038..e1c68c8984 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWrapper.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWrapper.java
@@ -28,9 +28,10 @@ import org.apache.jena.sparql.core.Quad ;
 public class StreamRDFWrapper implements StreamRDF
 {
     protected final StreamRDF other ;
+    public final StreamRDF get() { return other; }
 
     public StreamRDFWrapper(StreamRDF other) { this.other = other ; }
-    
+
     @Override
     public void start()
     { other.start() ; }
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
index 01d231d59c..094b05d0e4 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
@@ -225,9 +225,19 @@ public class StreamRDFWriter {
         if ( context == null )
             context = RIOT.getContext().copy();
         StreamRDF stream = x.create(output, format, context);
+        if ( ! RDFLanguages.isQuads(format.getLang()) )
+            stream = triplesOnly(stream);
         return stream ;
     }
 
+
+    /* What to do if a quad is seen when writing in a triples-only syntax. */
+    private static StreamRDF triplesOnly(StreamRDF stream) {
+        // StreamTriplesOnly converts default graph quads to triples.
+        // Otherwise, throw exception.
+        return StreamTriplesOnly.exceptionOnQuads(stream);
+    }
+
     public static boolean registered(Lang lang) {
         RDFFormat fmt = registry.defaultSerialization(lang) ;
         return registry.contains(fmt) ;
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamTriplesOnly.java b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamTriplesOnly.java
index b3c1f3dda7..413bcd4fb8 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/StreamTriplesOnly.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/StreamTriplesOnly.java
@@ -18,6 +18,10 @@
 
 package org.apache.jena.riot.system;
 
+import java.util.function.Function;
+
+import org.apache.jena.riot.RiotException;
+import org.apache.jena.riot.out.NodeFmtLib;
 import org.apache.jena.sparql.core.Quad;
 
 /**
@@ -27,27 +31,51 @@ import org.apache.jena.sparql.core.Quad;
  */
 public class StreamTriplesOnly extends StreamRDFWrapper {
 
-    public static StreamRDF actionIfQuads(StreamRDF stream, Runnable action) {
+    public static enum QuadPolicy { CALL, IGNORE }
+
+    /** Exception on non-delfault graph quad */
+    private static Function<Quad, QuadPolicy> actionException = (q) -> {
+        throw new RiotException("Quad in Triple output: "+NodeFmtLib.str(q));
+    };
+
+    /** Replace an existing wrapper with a new policy for quads */
+    public static StreamRDF setActionIfQuads(StreamRDF stream, Function<Quad, QuadPolicy> action) {
+        // Strip existing layers.
+        while ( stream instanceof StreamTriplesOnly stream2 ) {
+            stream = stream2.get();
+        }
+        return addActionIfQuads(stream, action);
+    }
+
+    /** Add a new policy layer for quads */
+    public static StreamRDF addActionIfQuads(StreamRDF stream, Function<Quad, QuadPolicy> action) {
         return new StreamTriplesOnly(stream, action);
     }
 
-    private boolean seenQuads = false;
-    private final Runnable action;
+    /** Throw a {@link RiotException} if a non-default graph quad is seen. */
+    public static StreamRDF exceptionOnQuads(StreamRDF stream) {
+        return new StreamTriplesOnly(stream, actionException);
+    }
+
+    private QuadPolicy quadAction = QuadPolicy.CALL;
+    // The policy when seeing a quad that isn't in the default graph.
+    // Return true for "continue calling"
+    // Return false for "don't call again, ignore from now on"
+    private final Function<Quad, QuadPolicy> action;
 
-    private StreamTriplesOnly(StreamRDF sink, Runnable action) {
+    private StreamTriplesOnly(StreamRDF sink, Function<Quad, QuadPolicy> action) {
         super(sink) ;
         this.action = action;
     }
 
     @Override
     public void quad(Quad quad) {
-        if ( quad.isTriple() || quad.isDefaultGraph() || quad.isUnionGraph() ) {
+        if ( quad.getGraph() == null || quad.isTriple() || quad.isDefaultGraph() ) {
             triple(quad.asTriple()) ;
             return;
         }
-        if ( ! seenQuads ) {
-            action.run();
-            seenQuads = true;
+        if ( quadAction == QuadPolicy.CALL ) {
+            quadAction = action.apply(quad);
         }
     }
 
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 f4aba272a7..cd826bb35d 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
@@ -47,7 +47,6 @@ import org.apache.jena.sparql.util.Context;
  * <p>
  * For N-Quads and N-triples use {@link WriterStreamRDFPlain}.
  */
-
 public abstract class WriterStreamRDFBase implements StreamRDF
 {
     // What did we do last?
diff --git a/jena-cmds/src/main/java/riotcmd/CmdLangParse.java b/jena-cmds/src/main/java/riotcmd/CmdLangParse.java
index 2f9d1a4a66..a682697ce3 100644
--- a/jena-cmds/src/main/java/riotcmd/CmdLangParse.java
+++ b/jena-cmds/src/main/java/riotcmd/CmdLangParse.java
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Function;
 import java.util.zip.GZIPOutputStream;
 
 import arq.cmdline.ModContext;
@@ -82,12 +83,12 @@ public abstract class CmdLangParse extends CmdGeneral {
     }
 
     protected List<ParseRecord> outcomes = new ArrayList<>();
-
     protected OutputStream outputWrite = System.out;
     protected StreamRDF parserOutputStream = null;
     protected String parserBaseIRI = null;
     protected boolean passRelativeURIs = false;
     protected String writerBaseIRI = null;
+
     @Override
     protected void processModulesAndArgs() {
         cmdStrictMode = super.contains(argStrict);
@@ -202,18 +203,18 @@ public abstract class CmdLangParse extends CmdGeneral {
 
         if ( ! modLangParse.mergeQuads() && ! isQuadsOutput() ) {
             // Only pass through triples.
-            final StreamRDF dest = parserOutputStream;
             if ( isStreamingOutput() ) {
-                Runnable action = () -> {
+                Function<Quad, StreamTriplesOnly.QuadPolicy> action = (quad) -> {
                     // dest may be significantly buffered over the top of the output stream.
                     // The log message does not necessarily come out in the right place - it may be early.
+                    // "Best effort" attempt to align log message to output.
                     IO.flush(outputWrite);
                     Log.warn(SysRIOT.getLogger(), "Quads in triples output - quads ignored.");
+                    return StreamTriplesOnly.QuadPolicy.IGNORE;
                 };
-                parserOutputStream = StreamTriplesOnly.actionIfQuads(parserOutputStream, action);
-            } else {
-                // Not streaming - code can issue error before formatting.
+                parserOutputStream = StreamTriplesOnly.setActionIfQuads(parserOutputStream, action);
             }
+            // If not streaming, leave to the writer,
         }
 
         // If QuadsToTriples is added here, then counts will be "triples only"