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 2021/08/24 09:06:20 UTC
[jena] branch main updated: JENA-2143: shacl_parse : Read stdin
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 e7c5c4f JENA-2143: shacl_parse : Read stdin
new 31dc0ca Merge pull request #1053 from afs/jena2143-shacl-stdin
e7c5c4f is described below
commit e7c5c4fea4465b45c741e4e8edcdc6b13523b960
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Fri Aug 20 19:20:28 2021 +0100
JENA-2143: shacl_parse : Read stdin
---
.../src/main/java/arq/cmdline/ModLangParse.java | 8 +-
jena-cmds/src/main/java/shacl/shacl_parse.java | 99 +++++++++++++++++-----
2 files changed, 81 insertions(+), 26 deletions(-)
diff --git a/jena-cmds/src/main/java/arq/cmdline/ModLangParse.java b/jena-cmds/src/main/java/arq/cmdline/ModLangParse.java
index af63e37..8092041 100644
--- a/jena-cmds/src/main/java/arq/cmdline/ModLangParse.java
+++ b/jena-cmds/src/main/java/arq/cmdline/ModLangParse.java
@@ -45,11 +45,11 @@ public class ModLangParse extends ModBase
private ArgDecl argRDFS = new ArgDecl(ArgDecl.HasValue, "rdfs");
- private ArgDecl argSyntax = new ArgDecl(ArgDecl.HasValue, "syntax");
+ private ArgDecl argSyntax = new ArgDecl(ArgDecl.HasValue, "syntax");
- private String rdfsVocabFilename = null;
- private Model rdfsVocab = null;
- private String baseIRI = null;
+ private String rdfsVocabFilename = null;
+ private Model rdfsVocab = null;
+ private String baseIRI = null;
private boolean explicitCheck = false;
private boolean explicitNoCheck = false;
diff --git a/jena-cmds/src/main/java/shacl/shacl_parse.java b/jena-cmds/src/main/java/shacl/shacl_parse.java
index 9f7c2fc..f282701 100644
--- a/jena-cmds/src/main/java/shacl/shacl_parse.java
+++ b/jena-cmds/src/main/java/shacl/shacl_parse.java
@@ -25,22 +25,23 @@ import java.util.function.Function;
import java.util.stream.Stream;
import org.apache.jena.atlas.io.IndentedWriter;
-import org.apache.jena.atlas.lib.FileOps;
import org.apache.jena.atlas.lib.StreamOps;
import org.apache.jena.atlas.logging.LogCtl;
import org.apache.jena.cmd.ArgDecl;
import org.apache.jena.cmd.CmdException;
import org.apache.jena.cmd.CmdGeneral;
import org.apache.jena.graph.Graph;
-import org.apache.jena.riot.Lang;
-import org.apache.jena.riot.RDFDataMgr;
-import org.apache.jena.riot.RiotException;
+import org.apache.jena.irix.IRIException;
+import org.apache.jena.irix.IRIs;
+import org.apache.jena.irix.IRIx;
+import org.apache.jena.riot.*;
import org.apache.jena.shacl.ShaclException;
import org.apache.jena.shacl.Shapes;
import org.apache.jena.shacl.compact.ShaclcWriter;
import org.apache.jena.shacl.compact.reader.ShaclcParseException;
import org.apache.jena.shacl.lib.ShLib;
import org.apache.jena.shacl.parser.ShaclParseException;
+import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.sys.JenaSystem;
/** SHACL parsing.
@@ -53,13 +54,18 @@ public class shacl_parse extends CmdGeneral {
JenaSystem.init();
}
- private static ArgDecl argOutput = new ArgDecl(true, "output", "out");
+ private static ArgDecl argOutput = new ArgDecl(ArgDecl.HasValue, "output", "out");
+ private static ArgDecl argBase = new ArgDecl(ArgDecl.HasValue, "base");
+ private static ArgDecl argSyntax = new ArgDecl(ArgDecl.HasValue, "syntax");
- private boolean printCompact = false;
- private boolean printRDF = false;
- private boolean printText = false;
+ private String baseIRI = null;
+ private Lang lang = null;
- private String shapesfile = null;
+ private boolean printCompact = false;
+ private boolean printRDF = false;
+ private boolean printText = false;
+
+ private String shapesfile = null;
public static void main (String... argv) {
new shacl_parse(argv).mainRun() ;
@@ -67,7 +73,9 @@ public class shacl_parse extends CmdGeneral {
public shacl_parse(String[] argv) {
super(argv) ;
- super.add(argOutput, "-output=", "Output formats: RDF, compact, text (default, terse)");
+ super.add(argOutput, "--output=", "Output formats: RDF, compact, text (default, terse)");
+ super.add(argSyntax, "--syntax=NAME", "Set syntax (otherwise syntax guessed from file extension)");
+ super.add(argBase, "--base=URI", "Set the base URI (does not apply to N-triples and N-Quads)");
}
@Override
@@ -78,10 +86,6 @@ public class shacl_parse extends CmdGeneral {
@Override
protected void processModulesAndArgs() {
super.processModulesAndArgs();
- if ( super.positionals.size() == 0 ) {
- System.err.println(getSummary());
- System.exit(0);
- }
if ( super.hasArg(argOutput) ) {
printCompact = false;
@@ -113,6 +117,29 @@ public class shacl_parse extends CmdGeneral {
printRDF = false;
printText = true;
}
+
+ if ( super.contains(argSyntax) ) {
+ String syntax = super.getValue(argSyntax);
+ Lang lang$ = RDFLanguages.nameToLang(syntax);
+ if ( lang$ == null )
+ throw new CmdException("Can not detemine the syntax from '" + syntax + "'");
+ this.lang = lang$;
+ }
+
+ if ( super.contains(argBase) ) {
+ baseIRI = super.getValue(argBase);
+ try {
+ IRIx iri = IRIs.reference(baseIRI);
+ if ( !iri.isAbsolute() )
+ throw new CmdException("Base IRI not suitable for use as a base for RDF: " + baseIRI);
+ } catch (IRIException ex) {
+ throw new CmdException("Bad base IRI: " + baseIRI);
+ }
+ }
+
+ if (positionals.isEmpty() )
+ // stdin
+ positionals.add("-");
}
@Override
@@ -133,13 +160,9 @@ public class shacl_parse extends CmdGeneral {
PrintStream out = System.out;
PrintStream err = System.err;
- if ( ! FileOps.exists(fn) ) {
- err.println(fn+" : File not found");
- return;
- }
-
try {
- shapes = Shapes.parse(fn);
+ Graph g = parseFile(fn);
+ shapes = Shapes.parse(g);
}
catch ( RiotException ex ) { /*ErrorHandler logged this */ return; }
catch (ShaclParseException | ShaclcParseException /*| RiotException*/ ex) {
@@ -158,20 +181,52 @@ public class shacl_parse extends CmdGeneral {
}
if ( printCompact) {
if ( outputByPrev ) {
- out.println("- - - - - - - - ");
+ out.println("- - - - - - - -");
outputByPrev = false;
}
outputByPrev = printCompact(out, err, shapes);
}
if ( printRDF) {
if ( outputByPrev ) {
- out.println("- - - - - - - - ");
+ out.println("- - - - - - - -");
outputByPrev = false;
}
outputByPrev = printRDF(out, err, shapes);
}
}
+ public Graph parseFile(String filename) {
+ String baseParserIRI = this.baseIRI;
+ RDFParserBuilder builder = RDFParser.create();
+ if ( baseParserIRI != null )
+ builder.base(baseParserIRI);
+ if ( this.lang != null )
+ // Always use the command line specified syntax.
+ builder.forceLang(this.lang);
+ else {
+ // Otherwise default to SHACLC (if the parser can't guess).
+ Lang lang = Lang.SHACLC;
+ builder.lang(lang);
+ }
+
+ // Set the source.
+ if ( filename.equals("-") ) {
+ if ( baseParserIRI == null ) {
+ baseParserIRI = "http://base/";
+ builder.base(baseParserIRI);
+ }
+ filename = "stdin";
+ builder.source(System.in);
+ } else {
+ builder.source(filename);
+ }
+
+ Graph graph = GraphFactory.createDefaultGraph();
+ builder.parse(graph);
+ return graph;
+ }
+
+
private boolean printText(PrintStream out, PrintStream err, Shapes shapes) {
IndentedWriter iOut = new IndentedWriter(out);
ShLib.printShapes(iOut, shapes);