use of org.apache.jena.query.Query in project jena by apache.
the class QueryUtils method checkParse.
public static void checkParse(Query query) {
if (!SPARQLParserRegistry.get().containsFactory(query.getSyntax()))
return;
IndentedLineBuffer buff = new IndentedLineBuffer();
query.serialize(buff, query.getSyntax());
String tmp = buff.toString();
Query query2 = null;
try {
String baseURI = null;
if (!query.explicitlySetBaseURI())
// Not in query - use the same one (e.g. file read from) .
baseURI = query.getBaseURI();
query2 = QueryFactory.create(tmp, baseURI, query.getSyntax());
if (query2 == null)
return;
} catch (UnsupportedOperationException ex) {
// No parser after all.
return;
} catch (QueryException ex) {
System.err.println(tmp);
throw new QueryCheckException("could not parse output query", ex);
}
if (query.hashCode() != query2.hashCode())
throw new QueryCheckException("reparsed query hashCode does not equal parsed input query \nQuery (hashCode: " + query.hashCode() + ")=\n" + query + "\n\nQuery2 (hashCode: " + query2.hashCode() + ")=\n" + query2);
if (!query.equals(query2)) {
if (false) {
System.err.println(query);
System.err.println(query2);
}
throw new QueryCheckException("reparsed output does not equal parsed input");
}
}
use of org.apache.jena.query.Query in project jena by apache.
the class ApplyElementTransformVisitor method visit.
@Override
public void visit(ElementSubQuery el) {
Query newQuery = QueryTransformOps.transform(el.getQuery(), transform, exprTransform);
push(new ElementSubQuery(newQuery));
}
use of org.apache.jena.query.Query in project jena by apache.
the class QueryTransformOps method transform.
/**
* Transform a query using {@link ElementTransform} and {@link ExprTransform}.
* It is the responsibility of these transforms to transform to a legal SPARQL query.
*/
public static Query transform(Query query, ElementTransform transform, ExprTransform exprTransform) {
Query q2 = QueryTransformOps.shallowCopy(query);
// Mutate the q2 structures which are already allocated and no other code can access yet.
mutateByQueryType(q2, transform, exprTransform);
mutateVarExprList(q2.getGroupBy(), exprTransform);
mutateExprList(q2.getHavingExprs(), exprTransform);
if (q2.getOrderBy() != null)
mutateSortConditions(q2.getOrderBy(), exprTransform);
mutateQueryPattern(q2, transform, exprTransform);
if (query.isQueryResultStar()) {
// Reset internal to only what now can be seen.
q2.resetResultVars();
}
return q2;
}
use of org.apache.jena.query.Query in project jena by apache.
the class FormatterElement method visit.
@Override
public void visit(ElementSubQuery el) {
out.print("{ ");
out.incIndent(INDENT);
Query q = el.getQuery();
// Serialize with respect to the existing context.
QuerySerializerFactory factory = SerializerRegistry.get().getQuerySerializerFactory(Syntax.syntaxARQ);
QueryVisitor serializer = factory.create(Syntax.syntaxARQ, context, out);
q.visit(serializer);
out.decIndent(INDENT);
out.print("}");
}
use of org.apache.jena.query.Query in project jena by apache.
the class SerializationTest method run.
// A serialization test is:
// Read query in.
// Serialize to string.
// Parse again.
// Are they equal?
@Override
public void run() {
Query query = SparqlTestLib.queryFromEntry(testEntry);
// Whatever was read in.
runTestWorker(query, query.getSyntax());
}
Aggregations