Search in sources :

Example 1 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class UpdateWriter method output.

// -- Convenience static methods -----------------------
public static void output(UpdateRequest request, IndentedWriter out) {
    Prologue prologue = request;
    if (!request.explicitlySetBaseURI())
        prologue = new Prologue(request.getPrefixMapping(), (IRIResolver) null);
    SerializationContext sCxt = new SerializationContext(prologue, new NodeToLabelMapBNode());
    output(request, out, sCxt);
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) Prologue(org.apache.jena.sparql.core.Prologue) NodeToLabelMapBNode(org.apache.jena.sparql.util.NodeToLabelMapBNode)

Example 2 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class QueryExecUtils method outputResultSet.

@SuppressWarnings("deprecation")
public static void outputResultSet(ResultSet results, Prologue prologue, ResultsFormat outputFormat) {
    // Proper ResultSet formats.
    Lang lang = ResultsFormat.convert(outputFormat);
    if (lang != null) {
        ResultSetMgr.write(System.out, results, lang);
        System.out.flush();
        return;
    }
    // Old way.
    boolean done = false;
    if (prologue == null)
        prologue = new Prologue(globalPrefixMap);
    if (outputFormat.equals(ResultsFormat.FMT_UNKNOWN))
        outputFormat = ResultsFormat.FMT_TEXT;
    if (outputFormat.equals(ResultsFormat.FMT_NONE) || outputFormat.equals(ResultsFormat.FMT_COUNT)) {
        int count = ResultSetFormatter.consume(results);
        if (outputFormat.equals(ResultsFormat.FMT_COUNT)) {
            System.out.println("Count = " + count);
        }
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RDF_XML) || outputFormat.equals(ResultsFormat.FMT_RDF_N3) || outputFormat.equals(ResultsFormat.FMT_RDF_TTL)) {
        Model m = RDFOutput.encodeAsModel(results);
        m.setNsPrefixes(prologue.getPrefixMapping());
        m.setNsPrefix("rs", ResultSetGraphVocab.getURI());
        RDFDataMgr.write(System.out, m, Lang.TURTLE);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_XML)) {
        ResultSetFormatter.outputAsXML(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_JSON)) {
        ResultSetFormatter.outputAsJSON(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_SSE)) {
        ResultSetFormatter.outputAsSSE(System.out, results, prologue);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
        ResultSetFormatter.out(System.out, results, prologue);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_TUPLES)) {
        PlainFormat pFmt = new PlainFormat(System.out, prologue);
        ResultSetApply a = new ResultSetApply(results, pFmt);
        a.apply();
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_CSV)) {
        ResultSetFormatter.outputAsCSV(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_TSV)) {
        ResultSetFormatter.outputAsTSV(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_BIO)) {
        ResultSetFormatter.outputAsBIO(System.out, results);
        done = true;
    }
    if (!done)
        System.err.println("Unknown format request: " + outputFormat);
    results = null;
    System.out.flush();
}
Also used : Prologue(org.apache.jena.sparql.core.Prologue) ResultSetApply(org.apache.jena.sparql.resultset.ResultSetApply) Model(org.apache.jena.rdf.model.Model) Lang(org.apache.jena.riot.Lang) PlainFormat(org.apache.jena.sparql.resultset.PlainFormat)

Example 3 with Prologue

use of org.apache.jena.sparql.core.Prologue 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 4 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class MgtFunctions method prefixes.

/** Return prefixes for the datasets, SPARQL syntax. */
public static String prefixes(HttpServletRequest request) {
    String dsName = dataset(request);
    DatasetRef desc = getFromRegistry(dsName);
    if (desc == null)
        return "<not found>";
    DatasetGraph dsg = desc.dataset;
    if (dsg instanceof DatasetGraphTDB) {
        PrefixMapping pmap = ((DatasetGraphTDB) dsg).getPrefixes().getPrefixMapping();
        Prologue prologue = new Prologue(pmap);
        IndentedLineBuffer buff = new IndentedLineBuffer();
        PrologueSerializer.output(buff, prologue);
        buff.append("\n");
        return buff.asString();
    }
    return "";
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) Prologue(org.apache.jena.sparql.core.Prologue) DatasetRef(org.apache.jena.fuseki.server.DatasetRef) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) DatasetGraphTDB(org.apache.jena.tdb.store.DatasetGraphTDB) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer)

Example 5 with Prologue

use of org.apache.jena.sparql.core.Prologue in project jena by apache.

the class ParseHandlerResolver method dump.

private void dump() {
    Iterator<Prologue> iter = state.iterator();
    for (; iter.hasNext(); ) {
        Prologue p = iter.next();
        System.out.println("  Prologue: " + p.getBaseURI());
    }
}
Also used : Prologue(org.apache.jena.sparql.core.Prologue)

Aggregations

Prologue (org.apache.jena.sparql.core.Prologue)8 QueryParseException (org.apache.jena.query.QueryParseException)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 BuilderPath (org.apache.jena.sparql.sse.builders.BuilderPath)2 WriterPath (org.apache.jena.sparql.sse.writers.WriterPath)2 NodeToLabelMapBNode (org.apache.jena.sparql.util.NodeToLabelMapBNode)2 ArgDecl (jena.cmd.ArgDecl)1 CmdException (jena.cmd.CmdException)1 CmdLineArgs (jena.cmd.CmdLineArgs)1 TerminationException (jena.cmd.TerminationException)1 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)1 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 DatasetRef (org.apache.jena.fuseki.server.DatasetRef)1 Node (org.apache.jena.graph.Node)1 Syntax (org.apache.jena.query.Syntax)1 Model (org.apache.jena.rdf.model.Model)1 Lang (org.apache.jena.riot.Lang)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)1 Expr (org.apache.jena.sparql.expr.Expr)1