Search in sources :

Example 11 with QueryParseException

use of org.apache.jena.query.QueryParseException in project jena by apache.

the class QueryValidatorJSON method execute.

public static JsonObject execute(ValidationAction action) {
    JsonBuilder obj = new JsonBuilder();
    obj.startObject();
    final String queryString = getArg(action, paramQuery);
    String querySyntax = getArgOrNull(action, paramSyntax);
    if (querySyntax == null || querySyntax.equals(""))
        querySyntax = "SPARQL";
    Syntax language = Syntax.lookup(querySyntax);
    if (language == null) {
        ServletOps.errorBadRequest("Unknown syntax: " + querySyntax);
        return null;
    }
    boolean outputSPARQL = true;
    boolean outputAlgebra = true;
    boolean outputQuads = true;
    boolean outputOptimized = true;
    boolean outputOptimizedQuads = true;
    obj.key(jInput).value(queryString);
    // Attempt to parse it.
    Query query = null;
    try {
        query = QueryFactory.create(queryString, "http://example/base/", language);
    } catch (QueryParseException ex) {
        obj.key(jErrors);
        // Errors array
        obj.startArray();
        obj.startObject();
        obj.key(jParseError).value(ex.getMessage());
        obj.key(jParseErrorLine).value(ex.getLine());
        obj.key(jParseErrorCol).value(ex.getColumn());
        obj.finishObject();
        obj.finishArray();
        // Outer object
        obj.finishObject();
        return obj.build().getAsObject();
    }
    if (query != null) {
        if (outputSPARQL)
            formatted(obj, query);
        if (outputAlgebra)
            algebra(obj, query);
        if (outputQuads)
            algebraQuads(obj, query);
        if (outputOptimized)
            algebraOpt(obj, query);
        if (outputOptimizedQuads)
            algebraOptQuads(obj, query);
    }
    obj.finishObject();
    return obj.build().getAsObject();
}
Also used : JsonBuilder(org.apache.jena.atlas.json.JsonBuilder) Query(org.apache.jena.query.Query) Syntax(org.apache.jena.query.Syntax) QueryParseException(org.apache.jena.query.QueryParseException)

Example 12 with QueryParseException

use of org.apache.jena.query.QueryParseException in project jena by apache.

the class UpdateValidatorJSON method execute.

public static JsonObject execute(ValidationAction action) {
    JsonBuilder obj = new JsonBuilder();
    obj.startObject();
    final String updateString = getArg(action, paramUpdate);
    String updateSyntax = getArgOrNull(action, paramSyntax);
    if (updateSyntax == null || updateSyntax.equals(""))
        updateSyntax = "SPARQL";
    Syntax language = Syntax.lookup(updateSyntax);
    if (language == null) {
        ServletOps.errorBadRequest("Unknown syntax: " + updateSyntax);
        return null;
    }
    obj.key(jInput).value(updateString);
    UpdateRequest request = null;
    try {
        request = UpdateFactory.create(updateString, "http://example/base/", language);
    } catch (QueryParseException ex) {
        obj.key(jErrors);
        // Errors array
        obj.startArray();
        obj.startObject();
        obj.key(jParseError).value(ex.getMessage());
        obj.key(jParseErrorLine).value(ex.getLine());
        obj.key(jParseErrorCol).value(ex.getColumn());
        obj.finishObject();
        obj.finishArray();
        // Outer object
        obj.finishObject();
        return obj.build().getAsObject();
    }
    formatted(obj, request);
    obj.finishObject();
    return obj.build().getAsObject();
}
Also used : JsonBuilder(org.apache.jena.atlas.json.JsonBuilder) UpdateRequest(org.apache.jena.update.UpdateRequest) Syntax(org.apache.jena.query.Syntax) QueryParseException(org.apache.jena.query.QueryParseException)

Example 13 with QueryParseException

use of org.apache.jena.query.QueryParseException in project jena by apache.

the class qexpr method main2.

public static void main2(String... argv) {
    CmdLineArgs cl = new CmdLineArgs(argv);
    ArgDecl helpDecl = new ArgDecl(ArgDecl.NoValue, "h", "help");
    cl.add(helpDecl);
    ArgDecl verboseDecl = new ArgDecl(ArgDecl.NoValue, "v", "verbose");
    cl.add(verboseDecl);
    ArgDecl versionDecl = new ArgDecl(ArgDecl.NoValue, "ver", "version", "V");
    cl.add(versionDecl);
    ArgDecl quietDecl = new ArgDecl(ArgDecl.NoValue, "q", "quiet");
    cl.add(quietDecl);
    ArgDecl reduceDecl = new ArgDecl(ArgDecl.NoValue, "reduce", "fold", "simplify");
    cl.add(reduceDecl);
    ArgDecl strictDecl = new ArgDecl(ArgDecl.NoValue, "strict");
    cl.add(strictDecl);
    ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print");
    cl.add(printDecl);
    try {
        cl.process();
    } catch (IllegalArgumentException ex) {
        System.err.println(ex.getMessage());
        usage(System.err);
        throw new CmdException();
    }
    if (cl.contains(helpDecl)) {
        usage();
        throw new TerminationException(0);
    }
    if (cl.contains(versionDecl)) {
        System.out.println("ARQ Version: " + ARQ.VERSION + " (Jena: " + Jena.VERSION + ")");
        throw new TerminationException(0);
    }
    // ==== General things
    boolean verbose = cl.contains(verboseDecl);
    boolean quiet = cl.contains(quietDecl);
    if (cl.contains(strictDecl))
        ARQ.setStrictMode();
    boolean actionCopySubstitute = cl.contains(reduceDecl);
    boolean actionPrintPrefix = false;
    boolean actionPrintSPARQL = false;
    boolean actionPrint = cl.contains(printDecl);
    for (String v : cl.getValues(printDecl)) {
        if (v.equalsIgnoreCase("prefix") || v.equalsIgnoreCase("op")) {
            actionPrintPrefix = true;
        } else if (v.equalsIgnoreCase("expr")) {
            actionPrintSPARQL = true;
        } else {
            System.err.println("Unknown print form: " + v);
            throw new TerminationException(0);
        }
    }
    for (int i = 0; i < cl.getNumPositional(); i++) {
        String exprStr = cl.getPositionalArg(i);
        exprStr = cl.indirect(exprStr);
        try {
            PrefixMapping pmap = PrefixMapping.Factory.create();
            pmap.setNsPrefixes(ARQConstants.getGlobalPrefixMap());
            pmap.setNsPrefix("", "http://example/");
            pmap.setNsPrefix("ex", "http://example/ns#");
            //              Node n = asNode() ;
            //              return makeNode(n) ;
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (verbose)
                System.out.print(expr.toString() + " => ");
            if (actionPrint) {
                if (actionPrintSPARQL)
                    System.out.println(ExprUtils.fmtSPARQL(expr));
                if (actionPrintPrefix)
                    WriterSSE.out(IndentedWriter.stdout, expr, new Prologue(pmap));
                continue;
            }
            try {
                if (actionCopySubstitute) {
                    Expr e = ExprLib.foldConstants(expr);
                    System.out.println(e);
                } else {
                    // Default action
                    ARQ.getContext().set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
                    FunctionEnv env = new ExecutionContext(ARQ.getContext(), null, null, null);
                    NodeValue r = expr.eval(null, env);
                    //System.out.println(r.asQuotedString()) ;
                    Node n = r.asNode();
                    String s = NodeFmtLib.displayStr(n);
                    System.out.println(s);
                }
            } catch (ExprEvalException ex) {
                System.out.println("Exception: " + ex.getMessage());
                throw new TerminationException(2);
            }
        } catch (QueryParseException ex) {
            System.err.println("Parse error: " + ex.getMessage());
            throw new TerminationException(2);
        }
    }
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) CmdException(jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(jena.cmd.ArgDecl) CmdLineArgs(jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(jena.cmd.TerminationException) Prologue(org.apache.jena.sparql.core.Prologue) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Expr(org.apache.jena.sparql.expr.Expr) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 14 with QueryParseException

use of org.apache.jena.query.QueryParseException in project jena by apache.

the class SelectHandler method parseExpr.

/**
	 * Parse an expression string into an expression.
	 * 
	 * This must be able to be parsed as though it were written "SELECT "+s
	 * 
	 * @param s
	 *            the select string to parse.
	 * @return the epxression
	 * @throws QueryParseException
	 *             on error
	 */
private Expr parseExpr(String s) throws QueryParseException {
    try {
        ARQParser parser = new ARQParser(new StringReader("SELECT " + s));
        parser.setQuery(new Query());
        parser.getQuery().setPrefixMapping(query.getPrefixMapping());
        parser.SelectClause();
        Query q = parser.getQuery();
        VarExprList vel = q.getProject();
        return vel.getExprs().values().iterator().next();
    } catch (ParseException ex) {
        throw new QueryParseException(ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginLine);
    } catch (TokenMgrError tErr) {
        throw new QueryParseException(tErr.getMessage(), -1, -1);
    } catch (Error err) {
        // The token stream can throw java.lang.Error's
        String tmp = err.getMessage();
        if (tmp == null)
            throw new QueryParseException(err, -1, -1);
        throw new QueryParseException(tmp, -1, -1);
    }
}
Also used : Query(org.apache.jena.query.Query) ARQParser(org.apache.jena.sparql.lang.arq.ARQParser) StringReader(java.io.StringReader) TokenMgrError(org.apache.jena.sparql.lang.arq.TokenMgrError) TokenMgrError(org.apache.jena.sparql.lang.arq.TokenMgrError) ParseException(org.apache.jena.sparql.lang.arq.ParseException) QueryParseException(org.apache.jena.query.QueryParseException) VarExprList(org.apache.jena.sparql.core.VarExprList) QueryParseException(org.apache.jena.query.QueryParseException)

Example 15 with QueryParseException

use of org.apache.jena.query.QueryParseException in project jena by apache.

the class PathParser method parse.

public static Path parse(String str, Prologue prologue) {
    Query query = new Query(prologue);
    Reader in = new StringReader(str);
    ARQParser parser = new ARQParser(in);
    try {
        query.setStrict(true);
        parser.setQuery(query);
        return parser.PathUnit();
    } catch (org.apache.jena.sparql.lang.arq.ParseException ex) {
        throw new QueryParseException(ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginColumn);
    } catch (org.apache.jena.sparql.lang.arq.TokenMgrError tErr) {
        // Last valid token : not the same as token error message - but this should not happen
        int col = parser.token.endColumn;
        int line = parser.token.endLine;
        throw new QueryParseException(tErr.getMessage(), line, col);
    } catch (QueryException ex) {
        throw ex;
    } catch (JenaException ex) {
        throw new QueryException(ex.getMessage(), ex);
    } catch (Error err) {
        // The token stream can throw errors.
        throw new QueryParseException(err.getMessage(), err, -1, -1);
    } catch (Throwable th) {
        Log.warn(PathParser.class, "Unexpected throwable: ", th);
        throw new QueryException(th.getMessage(), th);
    }
}
Also used : Query(org.apache.jena.query.Query) StringReader(java.io.StringReader) Reader(java.io.Reader) QueryParseException(org.apache.jena.query.QueryParseException) JenaException(org.apache.jena.shared.JenaException) QueryException(org.apache.jena.query.QueryException) ARQParser(org.apache.jena.sparql.lang.arq.ARQParser) StringReader(java.io.StringReader)

Aggregations

QueryParseException (org.apache.jena.query.QueryParseException)16 StringReader (java.io.StringReader)6 QueryException (org.apache.jena.query.QueryException)6 JenaException (org.apache.jena.shared.JenaException)6 Reader (java.io.Reader)5 ARQParser (org.apache.jena.sparql.lang.arq.ARQParser)4 Query (org.apache.jena.query.Query)3 VarExprList (org.apache.jena.sparql.core.VarExprList)3 UpdateRequest (org.apache.jena.update.UpdateRequest)3 JsonBuilder (org.apache.jena.atlas.json.JsonBuilder)2 Syntax (org.apache.jena.query.Syntax)2 Prologue (org.apache.jena.sparql.core.Prologue)2 Expr (org.apache.jena.sparql.expr.Expr)2 SPARQLParser11 (org.apache.jena.sparql.lang.sparql_11.SPARQLParser11)2 UpdateException (org.apache.jena.update.UpdateException)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 ArgDecl (jena.cmd.ArgDecl)1 CmdException (jena.cmd.CmdException)1 CmdLineArgs (jena.cmd.CmdLineArgs)1