Search in sources :

Example 1 with NodeToLabelMap

use of org.apache.jena.sparql.util.NodeToLabelMap in project jena by apache.

the class CSVOutput method format.

@Override
public void format(OutputStream out, ResultSet resultSet) {
    try {
        Writer w = FileUtils.asUTF8(out);
        NodeToLabelMap bnodes = new NodeToLabelMap();
        w = new BufferedWriter(w);
        String sep = null;
        List<String> varNames = resultSet.getResultVars();
        List<Var> vars = new ArrayList<>(varNames.size());
        // Convert to Vars and output the header line.
        for (String v : varNames) {
            if (sep != null)
                w.write(sep);
            else
                sep = ",";
            w.write(csvSafe(v));
            vars.add(Var.alloc(v));
        }
        w.write(NL);
        // Data output
        for (; resultSet.hasNext(); ) {
            sep = null;
            Binding b = resultSet.nextBinding();
            for (Var v : vars) {
                if (sep != null)
                    w.write(sep);
                sep = ",";
                Node n = b.get(v);
                if (n != null)
                    output(w, n, bnodes);
            }
            w.write(NL);
        }
        w.flush();
    } catch (IOException ex) {
        throw new ARQException(ex);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NodeToLabelMap(org.apache.jena.sparql.util.NodeToLabelMap) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Node (org.apache.jena.graph.Node)1 ARQException (org.apache.jena.sparql.ARQException)1 Var (org.apache.jena.sparql.core.Var)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 NodeToLabelMap (org.apache.jena.sparql.util.NodeToLabelMap)1