Search in sources :

Example 1 with SerializationContext

use of org.apache.jena.sparql.serializer.SerializationContext in project jena by apache.

the class ParameterizedSparqlString method toString.

/**
     * <p>
     * This method is where the actual work happens, the original command text
     * is always preserved and we just generated a temporary command string by
     * prepending the defined Base URI and namespace prefixes at the start of
     * the command and injecting the set parameters into a copy of that base
     * command string and return the resulting command.
     * </p>
     * <p>
     * This class makes no guarantees about the validity of the returned string
     * for use as a SPARQL Query or Update, for example if a variable parameter
     * was injected which was mentioned in the SELECT variables list you'd have
     * a syntax error when you try to parse the query. If you run into issues
     * like this try using a mixture of variable and positional parameters.
     * </p>
     * 
     * @throws ARQException
     *             May be thrown if the code detects a SPARQL Injection
     *             vulnerability because of the interaction of the command
     *             string and the injected variables
     */
@Override
public String toString() {
    String command = this.cmd.toString();
    Pattern p;
    // Go ahead and inject Variable Parameters
    SerializationContext context = new SerializationContext(this.prefixes);
    context.setBaseIRI(this.baseUri);
    for (String var : this.params.keySet()) {
        Node n = this.params.get(var);
        if (n == null) {
            continue;
        }
        this.validateSafeToInject(command, var, n);
        p = Pattern.compile("([?$]" + var + ")([^\\w]|$)");
        command = p.matcher(command).replaceAll(Matcher.quoteReplacement(this.stringForNode(n, context)) + "$2");
    }
    // Then inject Positional Parameters
    // To do this we need to find the ? we will replace
    p = Pattern.compile("(\\?)[\\s;,.]");
    int index = -1;
    int adj = 0;
    Matcher matcher = p.matcher(command);
    while (matcher.find()) {
        index++;
        MatchResult posMatch = matcher.toMatchResult();
        Node n = this.positionalParams.get(index);
        if (n == null)
            continue;
        this.validateSafeToInject(command, index, posMatch.start(1) + adj, n);
        String nodeStr = this.stringForNode(n, context);
        command = command.substring(0, posMatch.start() + adj) + nodeStr + command.substring(posMatch.start() + adj + 1);
        // Because we are using a matcher over the string state prior to
        // starting replacements we need to
        // track the offset adjustments to make
        adj += nodeStr.length() - 1;
    }
    // Build the final command string
    StringBuilder finalCmd = new StringBuilder();
    // Add BASE declaration
    if (this.baseUri != null) {
        finalCmd.append("BASE ");
        finalCmd.append(FmtUtils.stringForURI(this.baseUri, null, null));
        finalCmd.append('\n');
    }
    for (String prefix : this.prefixes.getNsPrefixMap().keySet()) {
        finalCmd.append("PREFIX ");
        finalCmd.append(prefix);
        finalCmd.append(": ");
        finalCmd.append(FmtUtils.stringForURI(this.prefixes.getNsPrefixURI(prefix), null, null));
        finalCmd.append('\n');
    }
    finalCmd.append(command);
    return finalCmd.toString();
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) MatchResult(java.util.regex.MatchResult)

Example 2 with SerializationContext

use of org.apache.jena.sparql.serializer.SerializationContext in project jena by apache.

the class SortCondition method output.

@Override
public void output(IndentedWriter out, SerializationContext sCxt) {
    if (sCxt == null)
        sCxt = new SerializationContext();
    FmtExprSPARQL fmt = new FmtExprSPARQL(out, sCxt);
    format(fmt, out);
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) FmtExprSPARQL(org.apache.jena.sparql.serializer.FmtExprSPARQL)

Example 3 with SerializationContext

use of org.apache.jena.sparql.serializer.SerializationContext in project jena by apache.

the class BasicPattern method toString.

@Override
public String toString() {
    IndentedLineBuffer out = new IndentedLineBuffer();
    SerializationContext sCxt = SSE.sCxt(SSE.getPrefixMapString());
    boolean first = true;
    for (Triple t : triples) {
        if (!first)
            out.print("\n");
        else
            first = false;
        // Adds (triple ...)
        // SSE.write(buff.getIndentedWriter(), t) ;
        out.print("(");
        WriterNode.outputPlain(out, t, sCxt);
        out.print(")");
    }
    out.flush();
    return out.toString();
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) Triple(org.apache.jena.graph.Triple) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer)

Example 4 with SerializationContext

use of org.apache.jena.sparql.serializer.SerializationContext in project jena by apache.

the class QuadPattern method toString.

@Override
public String toString() {
    IndentedLineBuffer out = new IndentedLineBuffer();
    SerializationContext sCxt = SSE.sCxt((SSE.getPrefixMapWrite()));
    boolean first = true;
    for (Quad quad : quads) {
        if (!first)
            out.print(" ");
        else
            first = false;
        // Adds (quad ...)
        // SSE.write(buff.getIndentedWriter(), t) ;
        out.print("(");
        WriterNode.outputPlain(out, quad, sCxt);
        out.print(")");
    }
    out.flush();
    return out.toString();
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer)

Example 5 with SerializationContext

use of org.apache.jena.sparql.serializer.SerializationContext in project jena by apache.

the class QueryOutputUtils method printPlan.

// ModQueryOut
public static void printPlan(Query query, QueryExecution qe) {
    QueryEngineFactory f = QueryEngineRegistry.findFactory(query, qe.getDataset().asDatasetGraph(), ARQ.getContext());
    if (f == null)
        System.err.println("printPlan: Unknown engine type: " + Lib.className(qe));
    Plan plan = f.create(query, qe.getDataset().asDatasetGraph(), BindingRoot.create(), ARQ.getContext());
    SerializationContext sCxt = new SerializationContext(query);
    IndentedWriter out = IndentedWriter.stdout;
    plan.output(out, sCxt);
    out.flush();
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) QueryEngineFactory(org.apache.jena.sparql.engine.QueryEngineFactory) Plan(org.apache.jena.sparql.engine.Plan)

Aggregations

SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)22 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)5 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)5 Triple (org.apache.jena.graph.Triple)3 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 MatchResult (java.util.regex.MatchResult)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 TerminationException (jena.cmd.TerminationException)1 Node (org.apache.jena.graph.Node)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1 Prologue (org.apache.jena.sparql.core.Prologue)1 Quad (org.apache.jena.sparql.core.Quad)1 Plan (org.apache.jena.sparql.engine.Plan)1 QueryEngineFactory (org.apache.jena.sparql.engine.QueryEngineFactory)1 FmtExprSPARQL (org.apache.jena.sparql.serializer.FmtExprSPARQL)1 NodeToLabelMapBNode (org.apache.jena.sparql.util.NodeToLabelMapBNode)1 Test (org.junit.Test)1