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