Search in sources :

Example 46 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class TestFmtUtils method formatPattern_2_triples.

@Test
public void formatPattern_2_triples() {
    BasicPattern basicPattern = new BasicPattern();
    basicPattern.add(getTriple());
    basicPattern.add(getTriple2());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try (IndentedWriter iw = new IndentedWriter(os)) {
        SerializationContext sc = new SerializationContext();
        FmtUtils.formatPattern(iw, basicPattern, sc);
    }
    assertEquals("<n1> <n2> \"l3\" .\n" + "<nb1> <nb2> \"lb3\" .", new String(os.toByteArray()));
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) BasicPattern(org.apache.jena.sparql.core.BasicPattern) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 47 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class Query method serialize.

/**
 * Output the query
 *
 * @param out     OutputStream
 * @param syntax  Syntax URI
 */
public void serialize(OutputStream out, Syntax syntax) {
    IndentedWriter writer = new IndentedWriter(out);
    serialize(writer, syntax);
    writer.flush();
    try {
        out.flush();
    } catch (Exception ex) {
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 48 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class QueryOutputUtils method printQuad.

public static void printQuad(Query query, boolean printOptimized) {
    IndentedWriter out = IndentedWriter.stdout;
    // Flush done
    printQuad(out, query, printOptimized);
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 49 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class AbstractTestJoin method print.

protected static void print(String msg, JoinKey joinKey, Table left, Table right, ExprList conditions, Table expected, Table actual) {
    System.err.flush();
    System.out.flush();
    IndentedWriter out = IndentedWriter.stderr;
    out.println("Test :    " + msg);
    out.println("Joinkey:  " + joinKey);
    print(out, "Left:", left);
    print(out, "Right:", right);
    if (conditions != null)
        out.println("Conditions: " + conditions);
    print(out, "Expected:", expected);
    print(out, "Actual:", actual);
    out.println();
    out.flush();
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 50 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class ExProg2 method main.

public static void main(String[] args) {
    Model model = createModel();
    Query query = QueryFactory.make();
    query.setQuerySelectType();
    // See also ExProg1
    ElementGroup elg = new ElementGroup();
    Var varTitle = Var.alloc("title");
    Var varX = Var.alloc("x");
    Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
    elg.addTriplePattern(t1);
    // Adds a filter.  Need to wrap variable in a NodeVar.
    Expr expr = new E_Regex(new ExprVar(varTitle), "sparql", "i");
    ElementFilter filter = new ElementFilter(expr);
    elg.addElementFilter(filter);
    // Attach the group to query.
    query.setQueryPattern(elg);
    // Choose what we want - SELECT ?title
    query.addResultVar(varTitle);
    // Print query with line numbers
    // Prefix mapping just helps serialization
    query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
    query.serialize(new IndentedWriter(System.out, true));
    System.out.println();
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        // Assumption: it's a SELECT query.
        ResultSet rs = qexec.execSelect();
        // The order of results is undefined.
        System.out.println("Titles: ");
        for (; rs.hasNext(); ) {
            QuerySolution rb = rs.nextSolution();
            // Get title - variable names do not include the '?' (or '$')
            RDFNode x = rb.get("title");
            // Check the type of the result value
            if (x instanceof Literal) {
                Literal titleStr = (Literal) x;
                System.out.println("    " + titleStr);
            } else
                System.out.println("Strange - not a literal: " + x);
        }
    }
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Triple(org.apache.jena.graph.Triple) E_Regex(org.apache.jena.sparql.expr.E_Regex) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Expr(org.apache.jena.sparql.expr.Expr) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter)

Aggregations

IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)57 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)5 TerminationException (org.apache.jena.cmd.TerminationException)4 Node (org.apache.jena.graph.Node)4 PrefixMapping (org.apache.jena.shared.PrefixMapping)4 CmdException (org.apache.jena.cmd.CmdException)3 Triple (org.apache.jena.graph.Triple)3 Syntax (org.apache.jena.query.Syntax)3 IOException (java.io.IOException)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 Pair (org.apache.jena.atlas.lib.Pair)2 RiotException (org.apache.jena.riot.RiotException)2 RiotNotFoundException (org.apache.jena.riot.RiotNotFoundException)2 NodeFormatter (org.apache.jena.riot.out.NodeFormatter)2 NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)2 JenaException (org.apache.jena.shared.JenaException)2 ARQException (org.apache.jena.sparql.ARQException)2 Var (org.apache.jena.sparql.core.Var)2 Plan (org.apache.jena.sparql.engine.Plan)2 Expr (org.apache.jena.sparql.expr.Expr)2