use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class TestParameterizedSparqlString method test_param_string_positional_injection_05.
@Test
public void test_param_string_positional_injection_05() {
// This injection attempt results in a valid query but a failed
// injection
String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ? . }";
ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
pss.setLiteral(0, "hello\" . ?s ?p ?o");
Query q = pss.asQuery();
Element el = q.getQueryPattern();
if (el instanceof ElementTriplesBlock) {
Assert.assertEquals(1, ((ElementTriplesBlock) q.getQueryPattern()).getPattern().size());
} else if (el instanceof ElementGroup) {
Assert.assertEquals(1, ((ElementGroup) el).getElements().size());
el = ((ElementGroup) el).getElements().get(0);
if (el instanceof ElementTriplesBlock) {
Assert.assertEquals(1, ((ElementTriplesBlock) el).getPattern().size());
}
}
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class WhereHandler method makeSubQuery.
/**
* Convert a subquery into a subquery element.
*
* @param subQuery The sub query to convert
* @return THe converted element.
*/
public ElementSubQuery makeSubQuery(AbstractQueryBuilder<?> subQuery) {
Query q = new Query();
q.setQuerySelectType();
PrologHandler ph = new PrologHandler(query);
ph.addPrefixes(subQuery.getPrologHandler().getPrefixes());
HandlerBlock handlerBlock = new HandlerBlock(q);
handlerBlock.addAll(subQuery.getHandlerBlock());
// remove the prefix mappings from the sub query.
handlerBlock.getPrologHandler().clearPrefixes();
// make sure we have a query pattern before we start building.
if (q.getQueryPattern() == null) {
q.setQueryPattern(new ElementGroup());
}
handlerBlock.build();
return new ElementSubQuery(q);
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class WhereHandler method addMinus.
/**
* Add a minus operation to the where clause. The prolog will be updated with
* the prefixes from the abstract query builder.
*
* @param qb the abstract builder that defines the data to subtract.
*/
public void addMinus(AbstractQueryBuilder<?> qb) {
PrologHandler ph = new PrologHandler(query);
ph.addPrefixes(qb.getPrologHandler().getPrefixes());
ElementGroup clause = getClause();
ElementMinus minus = new ElementMinus(qb.getWhereHandler().getClause());
clause.addElement(minus);
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class WhereHandler method addAll.
/**
* Add all where attributes from the Where Handler argument.
*
* @param whereHandler The Where Handler to copy from.
*/
public void addAll(WhereHandler whereHandler) {
Element e = whereHandler.query.getQueryPattern();
if (e != null) {
// clone the Element
ElementRewriter rewriter = new ElementRewriter(Collections.emptyMap());
e.visit(rewriter);
Element clone = rewriter.getResult();
Element mine = query.getQueryPattern();
if (mine == null) {
query.setQueryPattern(clone);
} else {
ElementGroup eg = null;
if (mine instanceof ElementGroup) {
eg = (ElementGroup) mine;
} else {
eg = new ElementGroup();
eg.addElement(mine);
}
if (clone instanceof ElementGroup) {
for (Element ele : ((ElementGroup) clone).getElements()) {
eg.addElement(ele);
}
} else {
eg.addElement(clone);
}
query.setQueryPattern(eg);
}
}
valuesHandler.addAll(whereHandler.valuesHandler);
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class WhereHandler method getClause.
/**
* Get the element group for the clause. if The element group is not set, create
* and set it.
*
* Public for ExprFactory use.
*
* @return The element group.
*/
public ElementGroup getClause() {
Element e = query.getQueryPattern();
if (e == null) {
e = new ElementGroup();
query.setQueryPattern(e);
}
if (e instanceof ElementGroup) {
return (ElementGroup) e;
}
ElementGroup eg = new ElementGroup();
eg.addElement(e);
query.setQueryPattern(eg);
return eg;
}
Aggregations