-
Notifications
You must be signed in to change notification settings - Fork 0
Jena query serialization
Adrian Wilke edited this page Feb 19, 2018
·
1 revision
Using the QuerySerializer (which implements the QueryVisitor) of ARQ.
import org.apache.jena.atlas.io.IndentedLineBuffer;
import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryVisitor;
import org.apache.jena.query.Syntax;
import org.apache.jena.sparql.serializer.FmtExprSPARQL;
import org.apache.jena.sparql.serializer.FmtTemplate;
import org.apache.jena.sparql.serializer.QuerySerializerFactory;
import org.apache.jena.sparql.serializer.SerializationContext;
import org.apache.jena.sparql.serializer.SerializerRegistry;
import org.apache.jena.sparql.util.NodeToLabelMapBNode;Query query = new Query();
query.toString();
// calls
query.serialize();
// returns buff.toString() of
IndentedLineBuffer buff = new IndentedLineBuffer();
query.serialize(buff);
// query.serialize(buff) calls
Syntax syntax = Syntax.syntaxSPARQL;
query.serialize(buff, syntax);
// where syntax is pre-defined:
/// Syntax syntaxSPARQL_11 = new
/// Syntax("http://jena.hpl.hp.com/2003/07/query/SPARQL_11");
/// Syntax syntaxSPARQL = syntaxSPARQL_11;
// serialize(IndentedWriter writer, Syntax outSyntax) calls
QuerySerializerFactory factory = SerializerRegistry.get().getQuerySerializerFactory(syntax);
IndentedWriter writer = (IndentedWriter) buff;
QueryVisitor serializer = factory.create(syntax, query, writer);
query.visit(serializer);
// QuerySerializerFactory is arqQuerySerializerFactory, generated in
// SerializerRegistry.init();
// In this method, the QueryVisitor is also created.
//
// In init-method, for
// QueryVisitor create(Syntax syntax, Prologue prologue, IndentedWriter writer)
// the syntax is not used. This seems to be handled by the following
// UpdateSerializerFactory arqUpdateSerializerFactory() { ...
// SerializerRegistry.init() code is
SerializationContext cxt1 = new SerializationContext(query, new NodeToLabelMapBNode("b", false));
SerializationContext cxt2 = new SerializationContext(query, new NodeToLabelMapBNode("c", false));
// new QuerySerializer(writer, new FormatterElement(writer, cxt1), new
// FmtExprSPARQL(writer, cxt1), new FmtTemplate(writer, cxt2));
new FmtExprSPARQL(writer, cxt1);
// sets visitor to internal class
// new FmtExprARQVisitor(writer, cxt1) ;
new FmtTemplate(writer, cxt2);
// calls super constructor
// new FormatterBase(writer, cxt2);