Search in sources :

Example 26 with ElementGroup

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());
        }
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Test(org.junit.Test)

Example 27 with ElementGroup

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);
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 28 with ElementGroup

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);
}
Also used : ElementMinus(org.apache.jena.sparql.syntax.ElementMinus) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 29 with ElementGroup

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);
}
Also used : ElementRewriter(org.apache.jena.arq.querybuilder.rewriters.ElementRewriter) Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 30 with ElementGroup

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;
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Aggregations

ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)37 Element (org.apache.jena.sparql.syntax.Element)24 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)11 Test (org.junit.Test)11 Triple (org.apache.jena.graph.Triple)9 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)9 Node (org.apache.jena.graph.Node)8 Query (org.apache.jena.query.Query)6 Var (org.apache.jena.sparql.core.Var)6 Quad (org.apache.jena.sparql.core.Quad)5 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)5 Update (org.apache.jena.update.Update)5 Op (org.apache.jena.sparql.algebra.Op)3 TriplePath (org.apache.jena.sparql.core.TriplePath)3 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)3 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)3 HashSet (java.util.HashSet)2 SelectClause (org.apache.jena.arq.querybuilder.clauses.SelectClause)2 ElementRewriter (org.apache.jena.arq.querybuilder.rewriters.ElementRewriter)2 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2