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;
}
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);
}
}
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;
}
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();
}
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();
}
Aggregations