Search in sources :

Example 1 with Syntax

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

the class UpdateValidator method execute.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("validation request") ;
        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>");
            // Not Java's finest hour.
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    out.print(updateString);
                }
            };
            output(outStream, c, 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>");
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    updateRequest.output(out);
                }
            };
            output(outStream, c, lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) 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) IOException(java.io.IOException)

Example 2 with Syntax

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

the class QueryValidator method execute.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("validation request") ;
        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");
        // Headers
        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>");
            // Not Java's finest hour.
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    out.print(queryString);
                }
            };
            output(outStream, c, 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 : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) 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 3 with Syntax

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

the class QueryTestSDBFactory method makeTest.

@Override
public Test makeTest(Resource manifest, Resource entry, String testName, Resource action, Resource result) {
    // Defaults.
    Syntax querySyntax = getQuerySyntax(manifest);
    if (testRootName != null)
        testName = testRootName + testName;
    if (querySyntax != null) {
        if (!querySyntax.equals(Syntax.syntaxARQ) && !querySyntax.equals(Syntax.syntaxSPARQL))
            throw new QueryTestException("Unknown syntax: " + querySyntax);
    }
    TestItem testItem = TestItem.create(entry, TestManifestX.TestQuery);
    TestCase test = null;
    if (testItem.getTestType().equals(TestManifestX.TestQuery))
        test = new QueryTestSDB(storeDesc, testName, results, testItem);
    if (testItem.getTestType().equals(TestManifestX.TestSurpressed))
        test = new SurpressedTest(testName, results, testItem);
    if (test == null)
        System.err.println("Unrecognized test type: " + testItem.getTestType());
    Resource action2 = testItem.getAction();
    if (action2.hasProperty(TestManifestX.option))
        System.out.println("OPTION");
    return test;
}
Also used : TestCase(junit.framework.TestCase) Resource(org.apache.jena.rdf.model.Resource) SurpressedTest(org.apache.jena.sparql.junit.SurpressedTest) Syntax(org.apache.jena.query.Syntax) TestQueryUtils.getQuerySyntax(org.apache.jena.sparql.junit.TestQueryUtils.getQuerySyntax) QueryTestException(org.apache.jena.sparql.junit.QueryTestException) TestItem(org.apache.jena.sparql.junit.TestItem)

Example 4 with Syntax

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

the class uparse method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    requestFiles = getValues(fileArg);
    super.processModulesAndArgs();
    if (super.cmdStrictMode)
        updateSyntax = Syntax.syntaxSPARQL_11;
    // Set syntax
    if (super.contains(syntaxArg)) {
        // short name
        String s = super.getValue(syntaxArg);
        Syntax syn = Syntax.lookup(s);
        if (syn == null)
            super.cmdError("Unrecognized syntax: " + s);
        updateSyntax = syn;
    }
    for (String arg : getValues(argDeclPrint)) {
        if (arg.equalsIgnoreCase("query"))
            printUpdate = true;
        else if (arg.equalsIgnoreCase("none"))
            printNone = true;
        else
            throw new CmdException("Not a recognized print form: " + arg + " : Choices are: update, none");
    }
    if (!printUpdate && !printNone)
        printUpdate = true;
}
Also used : CmdException(jena.cmd.CmdException) Syntax(org.apache.jena.query.Syntax)

Example 5 with Syntax

use of org.apache.jena.query.Syntax 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)

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