Search in sources :

Example 26 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class ElementData method add.

public void add(Binding binding) {
    Iterator<Var> iter = binding.vars();
    while (iter.hasNext()) {
        Var v = iter.next();
        if (!vars.contains(v))
            throw new ARQException("Variable " + v + " not already declared for ElementData");
    }
    rows.add(binding);
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var)

Example 27 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class QueryValidatorHTML method executeHTML.

// static final String paramSyntaxExtended = "extendedSyntax";
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        String[] args = httpRequest.getParameterValues(paramQuery);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No query parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many query parameters");
            return;
        }
        final String queryString = httpRequest.getParameter(paramQuery).replaceAll("(\r|\n| )*$", "");
        // queryString = queryString.replace("\r\n", "\n");
        // queryString.replaceAll("(\r|\n| )*$", "");
        String querySyntax = httpRequest.getParameter(paramSyntax);
        if (querySyntax == null || querySyntax.equals(""))
            querySyntax = "SPARQL";
        Syntax language = Syntax.lookup(querySyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + querySyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        boolean outputSPARQL = false;
        boolean outputPrefix = false;
        boolean outputAlgebra = false;
        boolean outputQuads = false;
        boolean outputOptimized = false;
        boolean outputOptimizedQuads = false;
        if (a != null) {
            for (String anA : a) {
                if (anA.equals("sparql")) {
                    outputSPARQL = true;
                }
                if (anA.equals("prefix")) {
                    outputPrefix = true;
                }
                if (anA.equals("algebra")) {
                    outputAlgebra = true;
                }
                if (anA.equals("quads")) {
                    outputQuads = true;
                }
                if (anA.equals("opt")) {
                    outputOptimized = true;
                }
                if (anA.equals("optquads")) {
                    outputOptimizedQuads = true;
                }
            }
        }
        // if ( ! outputSPARQL && ! outputPrefix )
        // outputSPARQL = true;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Query Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Query Validator</h1>");
        // Print query as received
        outStream.println("<p>Input:</p>");
        output(outStream, (out) -> out.print(queryString), lineNumbers);
        // Attempt to parse it.
        Query query = null;
        try {
            query = QueryFactory.create(queryString, "http://example/base/", language);
        } catch (ARQException ex) {
            // Over generous exception (should be QueryException)
            // but this makes the code robust.
            outStream.println("<p>Syntax error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        } catch (RuntimeException ex) {
            outStream.println("<p>Internal error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        }
        if (query != null) {
            if (outputSPARQL)
                outputSyntax(outStream, query, lineNumbers);
            if (outputAlgebra)
                outputAlgebra(outStream, query, lineNumbers);
            if (outputQuads)
                outputAlgebraQuads(outStream, query, lineNumbers);
            if (outputOptimized)
                outputAlgebraOpt(outStream, query, lineNumbers);
            if (outputOptimizedQuads)
                outputAlgebraOptQuads(outStream, query, lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : Query(org.apache.jena.query.Query) ServletOutputStream(javax.servlet.ServletOutputStream) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException) IOException(java.io.IOException)

Example 28 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class RDFLinkDataset method doPutPost.

private void doPutPost(Node graphName, String file, boolean replace) {
    Objects.requireNonNull(file);
    Lang lang = RDFLanguages.filenameToLang(file);
    Txn.executeWrite(dataset, () -> {
        if (RDFLanguages.isTriples(lang)) {
            Graph graph = LibRDFLink.isDefault(graphName) ? dataset.getDefaultGraph() : dataset.getGraph(graphName);
            if (replace)
                clear(graph);
            RDFDataMgr.read(graph, file);
        } else if (RDFLanguages.isQuads(lang)) {
            if (replace)
                dataset.clear();
            // Try to POST to the dataset.
            RDFDataMgr.read(dataset, file);
        } else
            throw new ARQException("Not an RDF format: " + file + " (lang=" + lang + ")");
    });
}
Also used : DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) ARQException(org.apache.jena.sparql.ARQException) Lang(org.apache.jena.riot.Lang)

Example 29 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class QueryExecUtils method getOne.

/**
     * Execute, expecting the result to be one row, one column. Return that one
     * RDFNode or null Throw excpetion if more than one.
     */
public static RDFNode getOne(QueryExecution qExec, String varname) {
    try {
        ResultSet rs = qExec.execSelect();
        if (!rs.hasNext())
            return null;
        QuerySolution qs = rs.nextSolution();
        RDFNode r = qs.get(varname);
        if (rs.hasNext()) {
            QuerySolution qs2 = rs.next();
            RDFNode r2 = qs2.get(varname);
            if (rs.hasNext())
                throw new ARQException("More than one: var ?" + varname + " -> " + r + ", " + r2 + ", ...");
            else
                throw new ARQException("Found two matches: var ?" + varname + " -> " + r + ", " + r2);
        }
        return r;
    } finally {
        qExec.close();
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 30 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class RDFConnectionRemote method upload.

/** Send a file to named graph (or "default" or null for the default graph).
     * <p>
     * The Content-Type is inferred from the file extension.
     * <p>
     * "Replace" means overwrite existing data, othewise the date is added to the target.
     */
protected void upload(String graph, String file, boolean replace) {
    // if triples
    Lang lang = RDFLanguages.filenameToLang(file);
    if (RDFLanguages.isQuads(lang))
        throw new ARQException("Can't load quads into a graph");
    if (!RDFLanguages.isTriples(lang))
        throw new ARQException("Not an RDF format: " + file + " (lang=" + lang + ")");
    String url = RDFConn.urlForGraph(svcGraphStore, graph);
    doPutPost(url, file, lang, replace);
}
Also used : ARQException(org.apache.jena.sparql.ARQException)

Aggregations

ARQException (org.apache.jena.sparql.ARQException)48 IOException (java.io.IOException)11 Var (org.apache.jena.sparql.core.Var)7 ArrayList (java.util.ArrayList)5 Lang (org.apache.jena.riot.Lang)5 BufferedReader (java.io.BufferedReader)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 Node (org.apache.jena.graph.Node)4 Syntax (org.apache.jena.query.Syntax)4 RDFNode (org.apache.jena.rdf.model.RDFNode)4 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2 Triple (org.apache.jena.graph.Triple)2 Query (org.apache.jena.query.Query)2 Model (org.apache.jena.rdf.model.Model)2 Resource (org.apache.jena.rdf.model.Resource)2 RiotException (org.apache.jena.riot.RiotException)2 HttpParams (org.apache.jena.sparql.engine.http.HttpParams)2 Item (org.apache.jena.sparql.sse.Item)2 UpdateRequest (org.apache.jena.update.UpdateRequest)2 BufferedWriter (java.io.BufferedWriter)1