diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/expr/ExprDigest.java b/jena-arq/src/main/java/org/apache/jena/sparql/expr/ExprDigest.java index d12e73b107f..59a8b273f97 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/expr/ExprDigest.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/expr/ExprDigest.java @@ -25,7 +25,6 @@ import org.apache.jena.atlas.lib.Bytes; import org.apache.jena.atlas.lib.Cache; import org.apache.jena.atlas.lib.CacheFactory; -import org.apache.jena.atlas.lib.Lib; import org.apache.jena.datatypes.xsd.XSDDatatype; import org.apache.jena.graph.Node; import org.apache.jena.sparql.ARQInternalErrorException; @@ -102,6 +101,6 @@ private NodeValue calculate(NodeValue v) { @Override public String getFunctionPrintName(SerializationContext cxt) { - return Lib.uppercase(super.getFunctionPrintName(cxt)); + return printName; } } diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryCheck.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryCheck.java new file mode 100644 index 00000000000..dbbd7a84d89 --- /dev/null +++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryCheck.java @@ -0,0 +1,139 @@ +/* + * 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 org.apache.jena.sparql.util; + +import org.apache.jena.atlas.io.IndentedLineBuffer; +import org.apache.jena.query.Query; +import org.apache.jena.query.QueryException; +import org.apache.jena.query.QueryFactory; +import org.apache.jena.sparql.algebra.Algebra; +import org.apache.jena.sparql.algebra.Op; +import org.apache.jena.sparql.core.QueryCheckException; +import org.apache.jena.sparql.lang.SPARQLParserRegistry; +import org.apache.jena.sparql.sse.SSE; +import org.apache.jena.sparql.sse.SSE_ParseException; +import org.apache.jena.sparql.sse.WriterSSE; +import org.apache.jena.sparql.sse.builders.SSE_BuildException; + +/** + * Checking read-write operations on query objects. + */ +public class QueryCheck { + + /** + * Parse a query string, + * then check the query can be written and read back in again, + * then check the algebra form can be written and read back in again. + * Throw an exception if there are any problems. + * + * @see #checkParse + * @see #checkOp(Query, boolean) + */ + public static void checkQuery(String queryString) { + Query query = QueryFactory.create(queryString); + checkParse(query); + checkOp(query, true); + } + + /** + * Write a query to a string, + * parse that string to get a second query object, + * and check the two query objects are + * the same (blank node isomorphic). + */ + public static void checkParse(Query query) { + if ( !SPARQLParserRegistry.get().containsFactory(query.getSyntax()) ) + return; + + IndentedLineBuffer buff = new IndentedLineBuffer(); + query.serialize(buff, query.getSyntax()); + + String tmp = buff.toString(); + + Query query2 = null; + try { + String baseURI = null; + if ( !query.explicitlySetBaseURI() ) + // Not in query - use the same one (e.g. file read from) . + baseURI = query.getBaseURI(); + + query2 = QueryFactory.create(tmp, baseURI, query.getSyntax()); + + if ( query2 == null ) + return; + } catch (UnsupportedOperationException ex) { + // No parser after all. + return; + } catch (QueryException ex) { + System.err.println(tmp); + throw new QueryCheckException("could not parse output query", ex); + } + + if ( query.hashCode() != query2.hashCode() ) + throw new QueryCheckException("Reparsed query hashCode does not equal parsed input query \n"+ + "Query (hashCode: " + query.hashCode()+ ")=\n" + query + "\n\n"+ + "Query2 (hashCode: " + query2.hashCode() + ")=\n" + query2); + + if ( !query.equals(query2) ) { + if ( false ) { + System.err.println(query); + System.err.println(query2); + } + throw new QueryCheckException("reparsed output does not equal parsed input"); + } + } + + /** + * Convert a query to the SPARQL algebra with optionally algebra optimizations, + * write the algebra to a string, + * parse that string to get second algebra object + * and check the two algebra objects are the same (blank node isomorphic). + */ + public static void checkOp(Query query, boolean optimizeAlgebra) { + IndentedLineBuffer buff = new IndentedLineBuffer(); + Op op = Algebra.compile(query); + if ( optimizeAlgebra ) + op = Algebra.optimize(op); + WriterSSE.out(buff, op, query); + String str = buff.toString(); + + try { + Op op2 = SSE.parseOp(str); + if ( op.hashCode() != op2.hashCode() ) { + op.hashCode(); + op2.hashCode(); + dump(op, op2); + throw new QueryCheckException("reparsed algebra expression hashCode does not equal algebra from query"); + } + if ( !op.equals(op2) ) { + dump(op, op2); + throw new QueryCheckException("reparsed algebra expression does not equal query algebra"); + } + } catch (SSE_ParseException | SSE_BuildException ex) { + throw ex; + } + } + + private static void dump(Op op, Op op2) { + System.err.println("***********"); + System.err.println(op); + System.err.println(op2); + System.err.println("***********"); + } +} diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryUtils.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryUtils.java deleted file mode 100644 index ebe710a07d3..00000000000 --- a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryUtils.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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 org.apache.jena.sparql.util; - -import org.apache.jena.atlas.io.IndentedLineBuffer ; -import org.apache.jena.query.Query ; -import org.apache.jena.query.QueryException ; -import org.apache.jena.query.QueryFactory ; -import org.apache.jena.sparql.algebra.Algebra ; -import org.apache.jena.sparql.algebra.Op ; -import org.apache.jena.sparql.core.QueryCheckException ; -import org.apache.jena.sparql.lang.SPARQLParserRegistry ; -import org.apache.jena.sparql.sse.SSE ; -import org.apache.jena.sparql.sse.SSE_ParseException ; -import org.apache.jena.sparql.sse.WriterSSE ; -import org.apache.jena.sparql.sse.builders.SSE_BuildException ; - -public class QueryUtils -{ - public static void checkQuery(Query query, boolean optimizeAlgebra) - { - checkParse(query) ; - checkOp(query, optimizeAlgebra) ; - } - - public static void checkOp(Query query, boolean optimizeAlgebra) - { - IndentedLineBuffer buff = new IndentedLineBuffer() ; - Op op = Algebra.compile(query) ; - if ( optimizeAlgebra ) - op = Algebra.optimize(op) ; - WriterSSE.out(buff, op, query) ; - String str = buff.toString() ; - - try { - Op op2 = SSE.parseOp(str) ; - if ( op.hashCode() != op2.hashCode() ) - { - op.hashCode() ; - op2.hashCode() ; - dump(op, op2) ; - throw new QueryCheckException("reparsed algebra expression hashCode does not equal algebra from query") ; - } - if ( ! op.equals(op2) ) - { - dump(op, op2) ; - throw new QueryCheckException("reparsed algebra expression does not equal query algebra") ; - } - } catch (SSE_ParseException | SSE_BuildException ex) - { - throw ex ; - } - } - - private static void dump(Op op, Op op2) - { - System.err.println("***********") ; - System.err.println(op) ; - System.err.println(op2) ; - System.err.println("***********") ; - } - - public static void checkParse(Query query) - { - if ( ! SPARQLParserRegistry.get().containsFactory(query.getSyntax()) ) - return ; - - IndentedLineBuffer buff = new IndentedLineBuffer() ; - query.serialize(buff, query.getSyntax()) ; - - String tmp = buff.toString() ; - - Query query2 = null ; - try { - String baseURI = null ; - if ( ! query.explicitlySetBaseURI() ) - // Not in query - use the same one (e.g. file read from) . - baseURI = query.getBaseURI() ; - - query2 = QueryFactory.create(tmp, baseURI, query.getSyntax()) ; - - if ( query2 == null ) - return ; - } catch (UnsupportedOperationException ex) - { - // No parser after all. - return ; - } - catch (QueryException ex) - { - System.err.println(tmp) ; - throw new QueryCheckException("could not parse output query", ex) ; - } - - if ( query.hashCode() != query2.hashCode() ) - throw new QueryCheckException("reparsed query hashCode does not equal parsed input query \nQuery (hashCode: " + query.hashCode() + ")=\n" + query + "\n\nQuery2 (hashCode: " + query2.hashCode() + ")=\n" + query2) ; - - if ( ! query.equals(query2) ) { - if ( false ) { - System.err.println(query); - System.err.println(query2); - } - throw new QueryCheckException("reparsed output does not equal parsed input") ; - } - } -} diff --git a/jena-arq/src/test/java/org/apache/jena/arq/junit/sparql/tests/SerializationTest.java b/jena-arq/src/test/java/org/apache/jena/arq/junit/sparql/tests/SerializationTest.java index 4c5819ee8bd..9512289a7cf 100644 --- a/jena-arq/src/test/java/org/apache/jena/arq/junit/sparql/tests/SerializationTest.java +++ b/jena-arq/src/test/java/org/apache/jena/arq/junit/sparql/tests/SerializationTest.java @@ -24,7 +24,7 @@ import org.apache.jena.query.Query ; import org.apache.jena.query.Syntax ; import org.apache.jena.sparql.sse.SSE_ParseException ; -import org.apache.jena.sparql.util.QueryUtils ; +import org.apache.jena.sparql.util.QueryCheck ; public class SerializationTest implements Runnable { @@ -62,7 +62,7 @@ protected void runTestWorker(Query query, Syntax syntax) // Query syntax and algebra tests. try { - QueryUtils.checkParse(query) ; + QueryCheck.checkParse(query) ; } catch (RuntimeException ex) { @@ -73,7 +73,7 @@ protected void runTestWorker(Query query, Syntax syntax) } try { - QueryUtils.checkOp(query, true) ; + QueryCheck.checkOp(query, true) ; } catch (SSE_ParseException ex) { System.err.println("**** Test: "+testEntry.getName()) ; diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/util/TS_Util.java b/jena-arq/src/test/java/org/apache/jena/sparql/util/TS_Util.java index 1cfcfa003de..248e96e3d92 100644 --- a/jena-arq/src/test/java/org/apache/jena/sparql/util/TS_Util.java +++ b/jena-arq/src/test/java/org/apache/jena/sparql/util/TS_Util.java @@ -29,7 +29,8 @@ TestFmtUtils.class, TestContextUtils.class, TestIsoMatcher.class, - TestGraphUtils.class + TestGraphUtils.class, + TestQueryCheckRW.class }) public class TS_Util { } diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/util/TestQueryCheckRW.java b/jena-arq/src/test/java/org/apache/jena/sparql/util/TestQueryCheckRW.java new file mode 100644 index 00000000000..39eb80cd96f --- /dev/null +++ b/jena-arq/src/test/java/org/apache/jena/sparql/util/TestQueryCheckRW.java @@ -0,0 +1,63 @@ +/* + * 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 org.apache.jena.sparql.util; + +import org.junit.jupiter.api.Test; + +import org.apache.jena.query.Query; +import org.apache.jena.query.QueryFactory; + +/** + * Test query string parsing, conversion to the algebra + */ +public class TestQueryCheckRW { + + @Test public void rtt_abs() { + // ABS is printed 'abs' + test("SELECT * { BIND(ABS(?X) as ?absX) }"); + } + + @Test public void rtt_hashFunction_md5() { + // MD5 is printed 'MD5' and is 'md5' in algebra. + test("SELECT * { FILTER ( MD5('abc') != 0 ) }"); + } + + @Test public void rtt_hashFunction_sha1() { + test("SELECT * { FILTER ( SHA1('abc') != 0 ) }"); + } + + @Test public void rtt_hashFunction_sha256() { + test("SELECT * { FILTER ( SHA256('abc') != 0 ) }"); + } + + @Test public void rtt_hashFunction_sha384() { + test("SELECT * { FILTER ( SHA384('abc') != 0 ) }"); + } + + @Test public void rtt_hashFunction_sha512() { + test("SELECT * { FILTER ( SHA512('abc') != 0 ) }"); + } + + private static void test(String queryString) { + Query query = QueryFactory.create(queryString); + QueryCheck.checkParse(query); + QueryCheck.checkOp(query, false); + QueryCheck.checkOp(query, true); + } +} diff --git a/jena-cmds/src/main/java/arq/qparse.java b/jena-cmds/src/main/java/arq/qparse.java index 986ce4cf6b8..318632c1edc 100644 --- a/jena-cmds/src/main/java/arq/qparse.java +++ b/jena-cmds/src/main/java/arq/qparse.java @@ -20,241 +20,207 @@ import static org.apache.jena.atlas.lib.Lib.lowercase; -import java.io.PrintStream ; -import java.util.Iterator ; - -import arq.cmdline.CmdARQ ; -import arq.cmdline.ModEngine ; -import arq.cmdline.ModQueryIn ; -import arq.cmdline.ModQueryOut ; -import org.apache.jena.atlas.lib.Lib ; -import org.apache.jena.atlas.logging.LogCtl ; +import java.io.PrintStream; +import java.util.Iterator; + +import arq.cmdline.CmdARQ; +import arq.cmdline.ModEngine; +import arq.cmdline.ModQueryIn; +import arq.cmdline.ModQueryOut; +import org.apache.jena.atlas.lib.Lib; +import org.apache.jena.atlas.logging.LogCtl; import org.apache.jena.cmd.ArgDecl; import org.apache.jena.cmd.CmdException; -import org.apache.jena.query.* ; -import org.apache.jena.shared.JenaException ; -import org.apache.jena.sparql.ARQInternalErrorException ; -import org.apache.jena.sparql.core.QueryCheckException ; -import org.apache.jena.sparql.expr.E_Function ; -import org.apache.jena.sparql.lang.QueryParserBase ; -import org.apache.jena.sparql.resultset.ResultSetException ; -import org.apache.jena.sparql.util.QueryOutputUtils ; -import org.apache.jena.sparql.util.QueryUtils ; +import org.apache.jena.query.*; +import org.apache.jena.shared.JenaException; +import org.apache.jena.sparql.ARQInternalErrorException; +import org.apache.jena.sparql.core.QueryCheckException; +import org.apache.jena.sparql.expr.E_Function; +import org.apache.jena.sparql.lang.QueryParserBase; +import org.apache.jena.sparql.resultset.ResultSetException; +import org.apache.jena.sparql.util.QueryOutputUtils; +import org.apache.jena.sparql.util.QueryCheck; /** A program to parse and print a query. */ - -public class qparse extends CmdARQ -{ - protected ModQueryIn modQuery = new ModQueryIn(Syntax.syntaxARQ) ; - protected ModQueryOut modOutput = new ModQueryOut() ; - protected ModEngine modEngine = new ModEngine() ; - protected final ArgDecl argDeclPrint = new ArgDecl(ArgDecl.HasValue, "print") ; - protected final ArgDecl argDeclOpt = new ArgDecl(ArgDecl.NoValue, "opt", "optimize") ; - protected final ArgDecl argDeclExplain = new ArgDecl(ArgDecl.NoValue, "explain") ; - protected final ArgDecl argDeclFixup = new ArgDecl(ArgDecl.NoValue, "fixup") ; - - protected boolean printNone = false ; - protected boolean printQuery = false ; - protected boolean printOp = false ; - protected boolean printOpt = false ; - protected boolean printQuad = false ; - protected boolean printQuadOpt = false ; - protected boolean printPlan = false ; - - public static void main(String... argv) - { - new qparse(argv).mainRun() ; +public class qparse extends CmdARQ { + protected ModQueryIn modQuery = new ModQueryIn(Syntax.syntaxARQ); + protected ModQueryOut modOutput = new ModQueryOut(); + protected ModEngine modEngine = new ModEngine(); + protected final ArgDecl argDeclPrint = new ArgDecl(ArgDecl.HasValue, "print"); + protected final ArgDecl argDeclOpt = new ArgDecl(ArgDecl.NoValue, "opt", "optimize"); + protected final ArgDecl argDeclExplain = new ArgDecl(ArgDecl.NoValue, "explain"); + protected final ArgDecl argDeclFixup = new ArgDecl(ArgDecl.NoValue, "fixup"); + + protected boolean printNone = false; + protected boolean printQuery = false; + protected boolean printOp = false; + protected boolean printOpt = false; + protected boolean printQuad = false; + protected boolean printQuadOpt = false; + protected boolean printPlan = false; + + public static void main(String...argv) { + new qparse(argv).mainRun(); } - public qparse(String[] argv) - { - super(argv) ; - super.addModule(modQuery) ; - super.addModule(modOutput) ; - super.addModule(modEngine) ; - super.getUsage().startCategory(null) ; - super.add(argDeclPrint, "--print", "Print in various forms [query, op, quad, optquad, plan]") ; - super.add(argDeclExplain, "--explain", "Print with algebra-level optimization") ; - super.add(argDeclOpt, "--opt", "[deprecated]") ; - super.add(argDeclFixup, "--fixup", "Convert undeclared prefix names to URIs") ; + public qparse(String[] argv) { + super(argv); + super.addModule(modQuery); + super.addModule(modOutput); + super.addModule(modEngine); + super.getUsage().startCategory(null); + super.add(argDeclPrint, "--print", "Print in various forms [query, op, quad, optquad, plan]"); + super.add(argDeclExplain, "--explain", "Print with algebra-level optimization"); + super.add(argDeclOpt, "--opt", "[deprecated]"); + super.add(argDeclFixup, "--fixup", "Convert undeclared prefix names to URIs"); // Switch off function build warnings. - E_Function.WarnOnUnknownFunction = false ; + E_Function.WarnOnUnknownFunction = false; } @Override - protected void processModulesAndArgs() - { - super.processModulesAndArgs() ; + protected void processModulesAndArgs() { + super.processModulesAndArgs(); if ( contains(argDeclOpt) ) - printOpt = true ; - if ( contains(argDeclExplain) ) - { - printQuery = true ; - printOpt = true ; + printOpt = true; + if ( contains(argDeclExplain) ) { + printQuery = true; + printOpt = true; } if ( contains(argDeclFixup) ) { // Fixup undeclared prefix names. ARQ.set(ARQ.fixupUndefinedPrefixes, true); } - for ( String arg : getValues( argDeclPrint ) ) { - switch(lowercase(arg)) { - case "query": - printQuery = true; - break; - case "op": case "alg": case "algebra": - printOp = true; - break; - case "quad": case "quads": - printQuad = true; - break; - case "plan": - printPlan = true; - break; - case "opt": - printOpt = true; - break; - case "optquad": case "quadopt": - printQuadOpt = true; - break; - case "none": - printNone = true; - break; - default: - throw new CmdException("Not a recognized print form: " + arg + " : Choices are: query, op, quad, opt, optquad, plan" ); + for ( String arg : getValues(argDeclPrint) ) { + switch (lowercase(arg)) { + case "query" -> printQuery = true; + case "op", "alg", "algebra" -> printOp = true; + case "quad", "quads" -> printQuad = true; + case "plan" -> printPlan = true; + case "opt" -> printOpt = true; + case "optquad", "quadopt" -> printQuadOpt = true; + case "none" -> printNone = true; + default -> throw new CmdException("Not a recognized print form: " + arg + + " : Choices are: query, op, quad, opt, optquad, plan"); } } - - if ( ! printQuery && ! printOp && ! printQuad && ! printPlan && ! printOpt && ! printQuadOpt && ! printNone ) - printQuery = true ; + if ( !printQuery && !printOp && !printQuad && !printPlan && !printOpt && !printQuadOpt && !printNone ) + printQuery = true; } - static String usage = qparse.class.getName()+" [--in syntax] [--out syntax] [--print=FORM] [\"query\"] | --query " ; + static String usage = qparse.class.getName() + " [--in syntax] [--out syntax] [--print=FORM] [\"query\"] | --query "; @Override - protected String getSummary() - { - return usage ; + protected String getSummary() { + return usage; } - static final String divider = "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" ; - //static final String divider = "" ; - boolean needDivider = false ; - private void divider() - { - if ( needDivider ) System.out.println(divider) ; - needDivider = true ; + static final String divider = "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"; + // static final String divider = "" ; + boolean needDivider = false; + private void divider() { + if ( needDivider ) + System.out.println(divider); + needDivider = true; } @Override - protected void exec() - { - try{ - Query query = modQuery.getQuery() ; + protected void exec() { + try { + Query query = modQuery.getQuery(); try { - LogCtl.disable(QueryParserBase.ParserLoggerName) ; - QueryUtils.checkQuery(query, true) ; - } catch (QueryCheckException ex) - { - System.err.println() ; - System.err.println("**** Check failure: "+ex.getMessage()) ; + LogCtl.disable(QueryParserBase.ParserLoggerName); + QueryCheck.checkParse(query); + QueryCheck.checkOp(query, true); + } catch (QueryCheckException ex) { + System.err.println(); + System.err.println("**** Check failure: " + ex.getMessage()); if ( ex.getCause() != null ) - ex.getCause().printStackTrace(System.err) ; + ex.getCause().printStackTrace(System.err); + } finally { + LogCtl.setLevel(QueryParserBase.ParserLoggerName, "INFO"); } - finally { LogCtl.setLevel(QueryParserBase.ParserLoggerName, "INFO") ; } - // Print the query out in some syntax - if ( printQuery ) - { divider() ; modOutput.output(query) ; } + if ( printQuery ) { + divider(); + modOutput.output(query); + } // Print internal forms. - if ( printOp ) - { divider() ; modOutput.outputOp(query, false) ; } + if ( printOp ) { + divider(); + modOutput.outputOp(query, false); + } - if ( printQuad ) - { divider() ; modOutput.outputQuad(query, false) ; } + if ( printQuad ) { + divider(); + modOutput.outputQuad(query, false); + } - if ( printOpt ) - { divider() ; modOutput.outputOp(query, true) ; } + if ( printOpt ) { + divider(); + modOutput.outputOp(query, true); + } - if ( printQuadOpt ) - { divider() ; modOutput.outputQuad(query, true) ; } + if ( printQuadOpt ) { + divider(); + modOutput.outputQuad(query, true); + } - if ( printPlan ) - { - divider() ; - // This forces internal query initialization - must be after QueryUtils.checkQuery - QueryExecution qExec = QueryExecutionFactory.create(query, DatasetFactory.createGeneral()) ; - QueryOutputUtils.printPlan(query, qExec) ; + if ( printPlan ) { + divider(); + // This forces internal query initialization - must be after + // QueryUtils.checkQuery + QueryExecution qExec = QueryExecutionFactory.create(query, DatasetFactory.createGeneral()); + QueryOutputUtils.printPlan(query, qExec); } - } - catch (ARQInternalErrorException intEx) - { - System.err.println(intEx.getMessage()) ; - if ( intEx.getCause() != null ) - { - System.err.println("Cause:") ; - intEx.getCause().printStackTrace(System.err) ; - System.err.println() ; + } catch (ARQInternalErrorException intEx) { + System.err.println(intEx.getMessage()); + if ( intEx.getCause() != null ) { + System.err.println("Cause:"); + intEx.getCause().printStackTrace(System.err); + System.err.println(); } - intEx.printStackTrace(System.err) ; - } - catch (ResultSetException ex) - { - System.err.println(ex.getMessage()) ; - ex.printStackTrace(System.err) ; - } - catch (QueryException qEx) - { - //System.err.println(qEx.getMessage()) ; - throw new CmdException("Query Exeception", qEx) ; - } - catch (JenaException ex) { - ex.printStackTrace() ; - throw ex ; } - catch (CmdException ex) { throw ex ; } - catch (Exception ex) - { - throw new CmdException("Exception", ex) ; + intEx.printStackTrace(System.err); + } catch (ResultSetException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(System.err); + } catch (QueryException qEx) { + // System.err.println(qEx.getMessage()) ; + throw new CmdException("Query Exeception", qEx); + } catch (JenaException ex) { + ex.printStackTrace(); + throw ex; + } catch (CmdException ex) { + throw ex; + } catch (Exception ex) { + throw new CmdException("Exception", ex); } } @Override - protected String getCommandName() { return Lib.className(this) ; } - -// static String usage = qparse.class.getName()+ -// " [--in syntax] [--out syntax] [\"query\" | --query \n"+ -// " where syntax is one of ARQ, SPARQL\n" + -// "Other options: \n"+ -// " --num on|off Line number ('on' by default)\n"+ -// " -n Same as --num=off\n"+ -// " --base URI Set the base URI for resolving relative URIs\n"+ -// " --plain No pretty printing\n"+ -// " --parse Parse only - don't print\n"+ -// " ---planning Turn planning on/off\n"+ -// " --show X Show internal structure (X = query or plan)\n" ; + protected String getCommandName() { + return Lib.className(this); + } - static void writeSyntaxes(String msg, PrintStream out) - { + static void writeSyntaxes(String msg, PrintStream out) { if ( msg != null ) - out.println(msg) ; - for ( Iterator iter = Syntax.querySyntaxNames.keys() ; iter.hasNext() ; ) - { - String k = iter.next() ; - Syntax v = Syntax.lookup(k) ; - k = padOut(k,10) ; - out.println(" "+k+" "+v) ; + out.println(msg); + for ( Iterator iter = Syntax.querySyntaxNames.keys() ; iter.hasNext() ; ) { + String k = iter.next(); + Syntax v = Syntax.lookup(k); + k = padOut(k, 10); + out.println(" " + k + " " + v); } } - // printf ... java 1.5 .. mutter,mutter - static String padOut(String x, int len) - { - StringBuilder r = new StringBuilder(x) ; + + static String padOut(String x, int len) { + StringBuilder r = new StringBuilder(x); for ( int i = x.length() ; i <= len ; i++ ) - r.append(" ") ; - return r.toString() ; + r.append(" "); + return r.toString(); } }