Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2862: Command line arguments from a file #2863

Merged
merged 5 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions jena-cmds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
59 changes: 25 additions & 34 deletions jena-cmds/src/main/java/arq/cmdline/ModAlgebra.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,50 @@
import org.apache.jena.cmd.CmdArgModule;
import org.apache.jena.cmd.CmdGeneral;
import org.apache.jena.cmd.ModBase;
import org.apache.jena.sparql.algebra.Op ;
import org.apache.jena.sparql.sse.SSE ;
import org.apache.jena.sparql.algebra.Op;
import org.apache.jena.sparql.sse.SSE;

public class ModAlgebra extends ModBase
{
protected final ArgDecl queryFileDecl = new ArgDecl(ArgDecl.HasValue, "query", "file") ;
public class ModAlgebra extends ModBase {
protected final ArgDecl queryFileDecl = new ArgDecl(ArgDecl.HasValue, "query", "file");

private String queryFilename = null ;
private String queryString = null ;
private Op op = null ;
private String queryFilename = null;
private String queryString = null;
private Op op = null;

@Override
public void registerWith(CmdGeneral cmdLine)
{
cmdLine.getUsage().startCategory("Query") ;
cmdLine.add(queryFileDecl,
"--query, --file",
"File containing an algebra query") ;
public void registerWith(CmdGeneral cmdLine) {
cmdLine.getUsage().startCategory("Query");
cmdLine.add(queryFileDecl, "--query, --file", "File containing an algebra query");
}

@Override
public void processArgs(CmdArgModule cmdLine)
{
if ( cmdLine.contains(queryFileDecl) )
{
queryFilename = cmdLine.getValue(queryFileDecl) ;
queryString = IO.readWholeFileAsUTF8(queryFilename) ;
public void processArgs(CmdArgModule cmdLine) {
if ( cmdLine.contains(queryFileDecl) ) {
queryFilename = cmdLine.getValue(queryFileDecl);
queryString = IO.readWholeFileAsUTF8(queryFilename);
}

if ( cmdLine.getNumPositional() == 0 && queryFilename == null )
cmdLine.cmdError("No query string or query file") ;
cmdLine.cmdError("No query string or query file");

if ( cmdLine.getNumPositional() > 1 )
cmdLine.cmdError("Only one query string allowed") ;
cmdLine.cmdError("Only one query string allowed");

if ( cmdLine.getNumPositional() == 1 && queryFilename != null )
cmdLine.cmdError("Either query string or query file - not both") ;
cmdLine.cmdError("Either query string or query file - not both");


if ( queryFilename == null )
{
String qs = cmdLine.getPositionalArg(0) ;
queryString = cmdLine.indirect(qs) ;
if ( queryFilename == null ) {
String qs = cmdLine.getPositionalArg(0);
queryString = cmdLine.indirect(qs);
}
}

public Op getOp()
{
public Op getOp() {
if ( op != null )
return op ;
op = SSE.parseOp(queryString) ;
return op;
op = SSE.parseOp(queryString);
if ( op == null )
System.err.println("Failed to parse : "+queryString) ;
return op ;
System.err.println("Failed to parse : " + queryString);
return op;
}
}
56 changes: 25 additions & 31 deletions jena-cmds/src/main/java/arq/cmdline/ModItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,51 @@
import org.apache.jena.cmd.CmdArgModule;
import org.apache.jena.cmd.CmdGeneral;
import org.apache.jena.cmd.ModBase;
import org.apache.jena.sparql.sse.Item ;
import org.apache.jena.sparql.sse.SSE ;
import org.apache.jena.sparql.sse.Item;
import org.apache.jena.sparql.sse.SSE;

public class ModItem extends ModBase
{
protected final ArgDecl queryFileDecl = new ArgDecl(ArgDecl.HasValue, "file") ;
public class ModItem extends ModBase {
protected final ArgDecl queryFileDecl = new ArgDecl(ArgDecl.HasValue, "file");

private String filename = null ;
private String parseString = null ;
private Item item = null ;
private String filename = null;
private String parseString = null;
private Item item = null;

@Override
public void registerWith(CmdGeneral cmdLine)
{
cmdLine.getUsage().startCategory("Item") ;
cmdLine.add(queryFileDecl, "--file=", "File") ;
public void registerWith(CmdGeneral cmdLine) {
cmdLine.getUsage().startCategory("Item");
cmdLine.add(queryFileDecl, "--file=", "File");
}

@Override
public void processArgs(CmdArgModule cmdLine)
{
if ( cmdLine.contains(queryFileDecl) )
{
filename = cmdLine.getValue(queryFileDecl) ;
parseString = IO.readWholeFileAsUTF8(filename) ;
return ;
public void processArgs(CmdArgModule cmdLine) {
if ( cmdLine.contains(queryFileDecl) ) {
filename = cmdLine.getValue(queryFileDecl);
parseString = IO.readWholeFileAsUTF8(filename);
return;
}

if ( cmdLine.getNumPositional() == 0 && filename == null )
cmdLine.cmdError("No query string or query file") ;
cmdLine.cmdError("No query string or query file");

if ( cmdLine.getNumPositional() > 1 )
cmdLine.cmdError("Only one query string allowed") ;
cmdLine.cmdError("Only one query string allowed");

if ( cmdLine.getNumPositional() == 1 && filename != null )
cmdLine.cmdError("Either query string or query file - not both") ;
cmdLine.cmdError("Either query string or query file - not both");

if ( filename == null )
{
String qs = cmdLine.getPositionalArg(0) ;
parseString = cmdLine.indirect(qs) ;
if ( filename == null ) {
String qs = cmdLine.getPositionalArg(0);
parseString = cmdLine.indirect(qs);
}
}

public Item getItem()
{
public Item getItem() {
if ( item != null )
return item ;
return item;
// Need to get the (outer) prologue.
item = SSE.parseItem(parseString) ;
return item ;
item = SSE.parseItem(parseString);
return item;
}

}
123 changes: 61 additions & 62 deletions jena-cmds/src/main/java/arq/cmdline/ModQueryIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,140 +16,139 @@
* limitations under the License.
*/

package arq.cmdline ;
package arq.cmdline;

import java.io.IOException ;
import java.io.IOException;

import org.apache.jena.cmd.*;
import org.apache.jena.query.Query ;
import org.apache.jena.query.QueryFactory ;
import org.apache.jena.query.Syntax ;
import org.apache.jena.shared.JenaException ;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.Syntax;
import org.apache.jena.shared.JenaException;
import org.apache.jena.shared.NotFoundException;
import org.apache.jena.sparql.ARQInternalErrorException ;
import org.apache.jena.util.FileUtils ;
import org.apache.jena.sparql.ARQInternalErrorException;
import org.apache.jena.util.FileUtils;

public class ModQueryIn extends ModBase {
protected final ArgDecl queryFileDecl = new ArgDecl(ArgDecl.HasValue, "query", "file") ;
protected final ArgDecl querySyntaxDecl = new ArgDecl(ArgDecl.HasValue, "syntax", "syn", "in") ;
protected final ArgDecl queryBaseDecl = new ArgDecl(ArgDecl.HasValue, "base") ;
protected final ArgDecl queryFileDecl = new ArgDecl(ArgDecl.HasValue, "query", "file");
protected final ArgDecl querySyntaxDecl = new ArgDecl(ArgDecl.HasValue, "syntax", "syn", "in");
protected final ArgDecl queryBaseDecl = new ArgDecl(ArgDecl.HasValue, "base");

private Syntax defaultQuerySyntax = Syntax.syntaxARQ ;
private Syntax querySyntax = null ;
private String queryFilename = null ;
private String queryString = null ;
private Query query = null ;
private String baseURI = null ;
private Syntax defaultQuerySyntax = Syntax.syntaxARQ;
private Syntax querySyntax = null;
private String queryFilename = null;
private String queryString = null;
private Query query = null;
private String baseURI = null;

public ModQueryIn(Syntax defaultSyntax) {
defaultQuerySyntax = defaultSyntax ;
querySyntax = defaultSyntax ;
defaultQuerySyntax = defaultSyntax;
querySyntax = defaultSyntax;
}

@Override
public void registerWith(CmdGeneral cmdLine) {
cmdLine.getUsage().startCategory("Query") ;
cmdLine.add(queryFileDecl, "--query, --file", "File containing a query") ;
cmdLine.add(querySyntaxDecl, "--syntax, --in", "Syntax of the query") ;
cmdLine.add(queryBaseDecl, "--base", "Base URI for the query") ;
cmdLine.getUsage().startCategory("Query");
cmdLine.add(queryFileDecl, "--query, --file", "File containing a query");
cmdLine.add(querySyntaxDecl, "--syntax, --in", "Syntax of the query");
cmdLine.add(queryBaseDecl, "--base", "Base URI for the query");
}

@Override
public void processArgs(CmdArgModule cmdline) throws IllegalArgumentException {
if ( cmdline.contains(queryBaseDecl) )
baseURI = cmdline.getValue(queryBaseDecl) ;
baseURI = cmdline.getValue(queryBaseDecl);

if ( cmdline.contains(queryFileDecl) ) {
queryFilename = cmdline.getValue(queryFileDecl) ;
querySyntax = Syntax.guessQueryFileSyntax(queryFilename, defaultQuerySyntax) ;
queryFilename = cmdline.getValue(queryFileDecl);
querySyntax = Syntax.guessQueryFileSyntax(queryFilename, defaultQuerySyntax);
}

if ( cmdline.getNumPositional() == 0 && queryFilename == null )
cmdline.cmdError("No query string or query file") ;
cmdline.cmdError("No query string or query file");

if ( cmdline.getNumPositional() > 1 )
cmdline.cmdError("Only one query string allowed") ;
cmdline.cmdError("Only one query string allowed");

if ( cmdline.getNumPositional() == 1 && queryFilename != null )
cmdline.cmdError("Either query string or query file - not both") ;
cmdline.cmdError("Either query string or query file - not both");

if ( queryFilename == null ) {
// One positional argument.
String qs = cmdline.getPositionalArg(0) ;
String qs = cmdline.getPositionalArg(0);
if ( cmdline.matchesIndirect(qs) )
querySyntax = Syntax.guessQueryFileSyntax(qs, defaultQuerySyntax) ;
querySyntax = Syntax.guessQueryFileSyntax(qs, defaultQuerySyntax);

queryString = cmdline.indirect(qs) ;
queryString = cmdline.indirect(qs);
}

// Set syntax
if ( cmdline.contains(querySyntaxDecl) ) {
// short name
String s = cmdline.getValue(querySyntaxDecl) ;
Syntax syn = Syntax.lookup(s) ;
String s = cmdline.getValue(querySyntaxDecl);
Syntax syn = Syntax.lookup(s);
if ( syn == null )
cmdline.cmdError("Unrecognized syntax: " + s) ;
querySyntax = syn ;
cmdline.cmdError("Unrecognized syntax: " + s);
querySyntax = syn;
}
}

public Syntax getQuerySyntax() {
return querySyntax ;
return querySyntax;
}

public Query getQuery() {
if ( query != null )
return query ;
return query;

if ( queryFilename != null && queryString != null ) {
System.err.println("Both query string and query file name given") ;
throw new TerminationException(1) ;
System.err.println("Both query string and query file name given");
throw new TerminationException(1);
}

if ( queryFilename == null && queryString == null ) {
System.err.println("No query string and no query file name given") ;
throw new TerminationException(1) ;
System.err.println("No query string and no query file name given");
throw new TerminationException(1);
}

try {
if ( queryFilename != null ) {
if ( queryFilename.equals("-") ) {
try {
// Stderr?
queryString = FileUtils.readWholeFileAsUTF8(System.in) ;
queryString = FileUtils.readWholeFileAsUTF8(System.in);
// And drop into next if
} catch (IOException ex) {
throw new CmdException("Error reading stdin", ex) ;
throw new CmdException("Error reading stdin", ex);
}
} else {
try {
query = QueryFactory.read(queryFilename, baseURI, getQuerySyntax()) ;
return query ;
query = QueryFactory.read(queryFilename, baseURI, getQuerySyntax());
return query;
} catch (NotFoundException ex) {
throw new JenaException("Failed to load Query: "+ex.getMessage());
throw new JenaException("Failed to load Query: " + ex.getMessage());
}
}
}

query = QueryFactory.create(queryString, baseURI, getQuerySyntax()) ;
return query ;
query = QueryFactory.create(queryString, baseURI, getQuerySyntax());
return query;
} catch (ARQInternalErrorException intEx) {
System.err.println(intEx.getMessage()) ;
System.err.println(intEx.getMessage());
if ( intEx.getCause() != null ) {
System.err.println("Cause:") ;
intEx.getCause().printStackTrace(System.err) ;
System.err.println() ;
System.err.println("Cause:");
intEx.getCause().printStackTrace(System.err);
System.err.println();
}
intEx.printStackTrace(System.err) ;
throw new TerminationException(99) ;
}
catch (JenaException ex) {
System.err.println(ex.getMessage()) ;
throw new TerminationException(2) ;
intEx.printStackTrace(System.err);
throw new TerminationException(99);
} catch (JenaException ex) {
System.err.println(ex.getMessage());
throw new TerminationException(2);
} catch (Exception ex) {
System.out.flush() ;
ex.printStackTrace(System.err) ;
throw new TerminationException(98) ;
System.out.flush();
ex.printStackTrace(System.err);
throw new TerminationException(98);
}
}
}
Loading
Loading