Search in sources :

Example 11 with Syntax

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

the class UpdateValidatorHTML method executeHTML.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        String[] args = httpRequest.getParameterValues(paramUpdate);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No update parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many update parameters");
            return;
        }
        final String updateString = httpRequest.getParameter(paramUpdate).replaceAll("(\r|\n| )*$", "");
        String updateSyntax = httpRequest.getParameter(paramSyntax);
        if (updateSyntax == null || updateSyntax.equals(""))
            updateSyntax = "SPARQL";
        Syntax language = Syntax.lookup(updateSyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + updateSyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        // Currently default.
        boolean outputSPARQL = true;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        // Headers
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Update Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Update Validator</h1>");
        // Print as received
        outStream.println("<p>Input:</p>");
        output(outStream, (out) -> out.print(updateString), lineNumbers);
        // Attempt to parse it.
        UpdateRequest request = null;
        try {
            request = UpdateFactory.create(updateString, "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);
        }
        // Because we pass into anon inner classes
        final UpdateRequest updateRequest = request;
        // OK?  Pretty print
        if (updateRequest != null && outputSPARQL) {
            outStream.println("<p>Formatted, parsed update request:</p>");
            output(outStream, (out) -> updateRequest.output(out), lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) UpdateRequest(org.apache.jena.update.UpdateRequest) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException)

Example 12 with Syntax

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

the class ModQueryIn method processArgs.

@Override
public void processArgs(CmdArgModule cmdline) throws IllegalArgumentException {
    if (cmdline.contains(queryBaseDecl))
        baseURI = cmdline.getValue(queryBaseDecl);
    if (cmdline.contains(queryFileDecl)) {
        queryFilename = cmdline.getValue(queryFileDecl);
        querySyntax = Syntax.guessFileSyntax(queryFilename, defaultQuerySyntax);
    }
    if (cmdline.getNumPositional() == 0 && queryFilename == null)
        cmdline.cmdError("No query string or query file");
    if (cmdline.getNumPositional() > 1)
        cmdline.cmdError("Only one query string allowed");
    if (cmdline.getNumPositional() == 1 && queryFilename != null)
        cmdline.cmdError("Either query string or query file - not both");
    if (queryFilename == null) {
        // One positional argument.
        String qs = cmdline.getPositionalArg(0);
        if (cmdline.matchesIndirect(qs))
            querySyntax = Syntax.guessFileSyntax(qs, defaultQuerySyntax);
        queryString = cmdline.indirect(qs);
    }
    // Set syntax
    if (cmdline.contains(querySyntaxDecl)) {
        // short name
        String s = cmdline.getValue(querySyntaxDecl);
        Syntax syn = Syntax.lookup(s);
        if (syn == null)
            cmdline.cmdError("Unrecognized syntax: " + s);
        querySyntax = syn;
    }
}
Also used : Syntax(org.apache.jena.query.Syntax)

Example 13 with Syntax

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

the class ModQueryOut method processArgs.

@Override
public void processArgs(CmdArgModule cmdline) throws IllegalArgumentException {
    if (cmdline.contains(queryOutputSyntaxDecl)) {
        // short name
        String s = cmdline.getValue(queryOutputSyntaxDecl);
        Syntax syn = Syntax.lookup(s);
        if (syn == null)
            cmdline.cmdError("Unrecognized syntax: " + s);
        outputSyntax = syn;
    }
    lineNumbers = cmdline.contains(queryNumberDecl);
}
Also used : Syntax(org.apache.jena.query.Syntax)

Aggregations

Syntax (org.apache.jena.query.Syntax)13 ServletOutputStream (javax.servlet.ServletOutputStream)4 ARQException (org.apache.jena.sparql.ARQException)4 IOException (java.io.IOException)3 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)3 Query (org.apache.jena.query.Query)3 UpdateRequest (org.apache.jena.update.UpdateRequest)3 TestCase (junit.framework.TestCase)2 JsonBuilder (org.apache.jena.atlas.json.JsonBuilder)2 QueryParseException (org.apache.jena.query.QueryParseException)2 Resource (org.apache.jena.rdf.model.Resource)2 CmdException (jena.cmd.CmdException)1 Prologue (org.apache.jena.sparql.core.Prologue)1 QueryTestException (org.apache.jena.sparql.junit.QueryTestException)1 SurpressedTest (org.apache.jena.sparql.junit.SurpressedTest)1 TestItem (org.apache.jena.sparql.junit.TestItem)1 TestQueryUtils.getQuerySyntax (org.apache.jena.sparql.junit.TestQueryUtils.getQuerySyntax)1 UpdateWriter (org.apache.jena.sparql.modify.request.UpdateWriter)1 NodeToLabelMapBNode (org.apache.jena.sparql.util.NodeToLabelMapBNode)1