Search in sources :

Example 6 with Syntax

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

the class SerializerRegistry method init.

private static synchronized void init() {
    SerializerRegistry reg = new SerializerRegistry();
    // Register standard serializers
    QuerySerializerFactory arqQuerySerializerFactory = new QuerySerializerFactory() {

        @Override
        public QueryVisitor create(Syntax syntax, Prologue prologue, IndentedWriter writer) {
            // For the query pattern
            SerializationContext cxt1 = new SerializationContext(prologue, new NodeToLabelMapBNode("b", false));
            // For the construct pattern
            SerializationContext cxt2 = new SerializationContext(prologue, new NodeToLabelMapBNode("c", false));
            return new QuerySerializer(writer, new FormatterElement(writer, cxt1), new FmtExprSPARQL(writer, cxt1), new FmtTemplate(writer, cxt2));
        }

        @Override
        public QueryVisitor create(Syntax syntax, SerializationContext context, IndentedWriter writer) {
            return new QuerySerializer(writer, new FormatterElement(writer, context), new FmtExprSPARQL(writer, context), new FmtTemplate(writer, context));
        }

        @Override
        public boolean accept(Syntax syntax) {
            // and SPARQL 1.1 can be serialized by the same serializer
            return Syntax.syntaxARQ.equals(syntax) || Syntax.syntaxSPARQL_10.equals(syntax) || Syntax.syntaxSPARQL_11.equals(syntax);
        }
    };
    reg.addQuerySerializer(Syntax.syntaxARQ, arqQuerySerializerFactory);
    reg.addQuerySerializer(Syntax.syntaxSPARQL_10, arqQuerySerializerFactory);
    reg.addQuerySerializer(Syntax.syntaxSPARQL_11, arqQuerySerializerFactory);
    UpdateSerializerFactory arqUpdateSerializerFactory = new UpdateSerializerFactory() {

        @Override
        public UpdateSerializer create(Syntax syntax, Prologue prologue, IndentedWriter writer) {
            if (!prologue.explicitlySetBaseURI())
                prologue = new Prologue(prologue.getPrefixMapping(), (IRIResolver) null);
            SerializationContext context = new SerializationContext(prologue);
            return new UpdateWriter(writer, context);
        }

        @Override
        public boolean accept(Syntax syntax) {
            // and SPARQL 1.1 can be serialized by the same serializer
            return Syntax.syntaxARQ.equals(syntax) || Syntax.syntaxSPARQL_10.equals(syntax) || Syntax.syntaxSPARQL_11.equals(syntax);
        }
    };
    reg.addUpdateSerializer(Syntax.syntaxARQ, arqUpdateSerializerFactory);
    reg.addUpdateSerializer(Syntax.syntaxSPARQL_10, arqUpdateSerializerFactory);
    reg.addUpdateSerializer(Syntax.syntaxSPARQL_11, arqUpdateSerializerFactory);
    registry = reg;
}
Also used : NodeToLabelMapBNode(org.apache.jena.sparql.util.NodeToLabelMapBNode) UpdateWriter(org.apache.jena.sparql.modify.request.UpdateWriter) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Prologue(org.apache.jena.sparql.core.Prologue) Syntax(org.apache.jena.query.Syntax)

Example 7 with Syntax

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

the class uparse method exec.

@Override
protected void exec() {
    for (String filename : requestFiles) {
        Syntax syntax = updateSyntax;
        if (syntax == null)
            syntax = Syntax.guessUpdateFileSyntax(filename);
        String x = oneFile(filename);
        if (x != null)
            execOne(x, syntax);
    }
    for (String x : super.positionals) {
        Syntax syntax = updateSyntax;
        if (matchesIndirect(x)) {
            if (syntax == null)
                syntax = Syntax.guessUpdateFileSyntax(x);
            x = indirect(x);
        }
        if (syntax == null)
            syntax = Syntax.defaultUpdateSyntax;
        execOne(x, syntax);
    }
}
Also used : Syntax(org.apache.jena.query.Syntax)

Example 8 with Syntax

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

the class ScriptTestSuiteFactory method makeTest.

@Override
public Test makeTest(Resource manifest, Resource entry, String testName, Resource action, Resource result) {
    if (action == null) {
        System.out.println("Null action: " + entry);
        return null;
    }
    // Defaults.
    Syntax querySyntax = TestQueryUtils.getQuerySyntax(manifest);
    if (querySyntax != null) {
        if (!querySyntax.equals(Syntax.syntaxARQ) && !querySyntax.equals(Syntax.syntaxSPARQL_10) && !querySyntax.equals(Syntax.syntaxSPARQL_11))
            throw new QueryTestException("Unknown syntax: " + querySyntax);
    }
    TestItem item = TestItem.create(entry, TestManifest.QueryEvaluationTest);
    Resource testType = item.getTestType();
    TestCase test = null;
    if (testType != null) {
        // == Good syntax
        if (testType.equals(TestManifest.PositiveSyntaxTest))
            return new SyntaxTest(testName, results, item);
        if (testType.equals(TestManifest_11.PositiveSyntaxTest11))
            return new SyntaxTest(testName, results, item);
        if (testType.equals(TestManifestX.PositiveSyntaxTestARQ))
            return new SyntaxTest(testName, results, item);
        // == Bad
        if (testType.equals(TestManifest.NegativeSyntaxTest))
            return new SyntaxTest(testName, results, item, false);
        if (testType.equals(TestManifest_11.NegativeSyntaxTest11))
            return new SyntaxTest(testName, results, item, false);
        if (testType.equals(TestManifestX.NegativeSyntaxTestARQ))
            return new SyntaxTest(testName, results, item, false);
        // ---- Update tests
        if (testType.equals(TestManifest_11.PositiveUpdateSyntaxTest11))
            return new SyntaxUpdateTest(testName, results, item, true);
        if (testType.equals(TestManifest_11.NegativeUpdateSyntaxTest11))
            return new SyntaxUpdateTest(testName, results, item, false);
        // Note item is not passed down.
        if (testType.equals(TestManifestUpdate_11.UpdateEvaluationTest))
            return UpdateTest.create(testName, results, entry, action, result);
        if (testType.equals(TestManifest_11.UpdateEvaluationTest))
            return UpdateTest.create(testName, results, entry, action, result);
        if (testType.equals(TestManifestX.TestSerialization))
            return new TestSerialization(testName, results, item);
        if (testType.equals(TestManifest.QueryEvaluationTest) || testType.equals(TestManifestX.TestQuery))
            return new QueryTest(testName, results, item);
        // Reduced is funny.
        if (testType.equals(TestManifest.ReducedCardinalityTest))
            return new QueryTest(testName, results, item);
        if (testType.equals(TestManifestX.TestSurpressed))
            return new SurpressedTest(testName, results, item);
        if (testType.equals(TestManifest_11.CSVResultFormatTest)) {
            Log.warn("Tests", "Skip CSV test: " + testName);
            return null;
        }
        System.err.println("Test type '" + testType + "' not recognized");
    }
    // Default 
    test = new QueryTest(testName, results, item);
    return test;
}
Also used : TestCase(junit.framework.TestCase) Resource(org.apache.jena.rdf.model.Resource) Syntax(org.apache.jena.query.Syntax)

Example 9 with Syntax

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

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

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