Search in sources :

Example 1 with AWriter

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

the class EscapeStr method stringEsc.

/*
     * Escape characters in a string according to Turtle rules. 
     */
public static String stringEsc(String s) {
    AWriter w = new StringWriterI();
    stringEsc(w, s, Chars.CH_QUOTE2, true, CharSpace.UTF8);
    return w.toString();
}
Also used : StringWriterI(org.apache.jena.atlas.io.StringWriterI) AWriter(org.apache.jena.atlas.io.AWriter)

Example 2 with AWriter

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

the class TSVOutput method format.

@Override
public void format(OutputStream out, ResultSet resultSet) {
    // Use a Turtle formatter to format terms
    NodeFormatterTTL formatter = new NodeFormatterTTL(null, null);
    AWriter w = IO.wrapUTF8(out);
    String sep = null;
    List<String> varNames = resultSet.getResultVars();
    List<Var> vars = new ArrayList<>(varNames.size());
    // writes the variables on the first line
    for (String v : varNames) {
        if (sep != null)
            w.write(sep);
        else
            sep = SEP;
        Var var = Var.alloc(v);
        w.write(var.toString());
        vars.add(var);
    }
    w.write(NL);
    // writes one binding by line
    for (; resultSet.hasNext(); ) {
        sep = null;
        Binding b = resultSet.nextBinding();
        for (Var v : vars) {
            if (sep != null)
                w.write(sep);
            sep = SEP;
            Node n = b.get(v);
            if (n != null) {
                // This will not include a raw tab.
                formatter.format(w, n);
            }
        }
        w.write(NL);
    }
    w.flush();
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) AWriter(org.apache.jena.atlas.io.AWriter) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) NodeFormatterTTL(org.apache.jena.riot.out.NodeFormatterTTL)

Aggregations

AWriter (org.apache.jena.atlas.io.AWriter)2 ArrayList (java.util.ArrayList)1 StringWriterI (org.apache.jena.atlas.io.StringWriterI)1 Node (org.apache.jena.graph.Node)1 NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)1 Var (org.apache.jena.sparql.core.Var)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1