Search in sources :

Example 1 with ElementTriplesBlock

use of org.apache.jena.sparql.syntax.ElementTriplesBlock in project jena by apache.

the class ElementTransformSubst method transform.

@Override
public Element transform(ElementTriplesBlock el) {
    ElementTriplesBlock etb = new ElementTriplesBlock();
    boolean changed = false;
    for (Triple t : el.getPattern()) {
        Triple t2 = transform(t);
        changed = changed || t != t2;
        etb.addTriple(t2);
    }
    if (changed)
        return etb;
    return el;
}
Also used : Triple(org.apache.jena.graph.Triple) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock)

Example 2 with ElementTriplesBlock

use of org.apache.jena.sparql.syntax.ElementTriplesBlock in project jena by apache.

the class TestParameterizedSparqlString method test_param_string_injection_05.

@Test
public void test_param_string_injection_05() {
    // This injection attempt results in a valid query but a failed
    // injection
    String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ?var2 . }";
    ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
    pss.setLiteral("var2", "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 3 with ElementTriplesBlock

use of org.apache.jena.sparql.syntax.ElementTriplesBlock in project jena by apache.

the class WhereClauseTest method testBindExprVar.

@ContractTest
public void testBindExprVar() {
    Var v = Var.alloc("foo");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addBind(new E_Random(), v);
    assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
    builder.setVar(v, NodeFactory.createURI("three"));
    Query q = builder.build();
    ElementGroup eg = (ElementGroup) q.getQueryPattern();
    List<Element> lst = eg.getElements();
    assertEquals("Should only be one element", 1, lst.size());
    assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
    ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
    assertTrue("ElementGroup should be empty", etb.isEmpty());
}
Also used : Query(org.apache.jena.query.Query) E_Random(org.apache.jena.sparql.expr.E_Random) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ContractTest(org.xenei.junit.contract.ContractTest)

Example 4 with ElementTriplesBlock

use of org.apache.jena.sparql.syntax.ElementTriplesBlock in project jena by apache.

the class UpdateEngineWorker method elementFromQuads.

protected Element elementFromQuads(List<Quad> quads) {
    ElementGroup el = new ElementGroup();
    ElementTriplesBlock x = new ElementTriplesBlock();
    // Maybe empty??
    el.addElement(x);
    Node g = Quad.defaultGraphNodeGenerated;
    for (Quad q : quads) {
        if (q.getGraph() != g) {
            g = q.getGraph();
            x = new ElementTriplesBlock();
            if (g == null || g == Quad.defaultGraphNodeGenerated)
                el.addElement(x);
            else {
                ElementNamedGraph eng = new ElementNamedGraph(g, x);
                el.addElement(eng);
            }
        }
        x.addTriple(q.asTriple());
    }
    return el;
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Node(org.apache.jena.graph.Node) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 5 with ElementTriplesBlock

use of org.apache.jena.sparql.syntax.ElementTriplesBlock in project jena by apache.

the class WhereClauseTest method testBindStringVar.

@ContractTest
public void testBindStringVar() throws ParseException {
    Var v = Var.alloc("foo");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addBind("rand()", v);
    assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
    builder.setVar(v, NodeFactory.createURI("three"));
    Query q = builder.build();
    ElementGroup eg = (ElementGroup) q.getQueryPattern();
    List<Element> lst = eg.getElements();
    assertEquals("Should only be one element", 1, lst.size());
    assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
    ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
    assertTrue("ElementGroup should be empty", etb.isEmpty());
}
Also used : Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ContractTest(org.xenei.junit.contract.ContractTest)

Aggregations

ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)7 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)6 Element (org.apache.jena.sparql.syntax.Element)4 Var (org.apache.jena.sparql.core.Var)3 Triple (org.apache.jena.graph.Triple)2 Query (org.apache.jena.query.Query)2 Test (org.junit.Test)2 ContractTest (org.xenei.junit.contract.ContractTest)2 Node (org.apache.jena.graph.Node)1 Op (org.apache.jena.sparql.algebra.Op)1 E_Random (org.apache.jena.sparql.expr.E_Random)1 E_Regex (org.apache.jena.sparql.expr.E_Regex)1 Expr (org.apache.jena.sparql.expr.Expr)1 ExprVar (org.apache.jena.sparql.expr.ExprVar)1 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)1 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)1