Search in sources :

Example 1 with NodeFormatterTTL

use of org.apache.jena.riot.out.NodeFormatterTTL 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)

Example 2 with NodeFormatterTTL

use of org.apache.jena.riot.out.NodeFormatterTTL in project jena by apache.

the class CompactWriter method print.

/**
 * Write shapes with directives BASE/PREFIX/IMPORTS.
 */
public static void print(IndentedWriter out, Shapes shapes) {
    // Output PrefixMap
    PrefixMap graphPrefixMap = shapes.getPrefixMap();
    // Formatter PrefixMap - with the std prefixes if not overridden.
    PrefixMap pmapWithStd = SHACLC.withStandardPrefixes();
    // Add to copy of standrard so it can override any standard settings.
    pmapWithStd.putAll(graphPrefixMap);
    NodeFormatter nodeFmt = new NodeFormatterTTL(null, pmapWithStd);
    boolean someOutput = false;
    // BASE - output if and only if there is exactly one.
    String baseURI = shapes.getBaseURI();
    if (baseURI != null) {
        if (someOutput)
            out.println();
        RiotLib.writeBase(out, baseURI, true);
        someOutput = true;
    }
    // PREFIX
    if (!graphPrefixMap.isEmpty()) {
        if (someOutput)
            out.println();
        RiotLib.writePrefixes(out, graphPrefixMap, true);
        someOutput = true;
    }
    // IMPORTS
    if (shapes.getImports() != null) {
        if (!shapes.getImports().isEmpty()) {
            if (someOutput)
                out.println();
            shapes.getImports().forEach(n -> {
                out.print("IMPORTS ");
                out.pad(WriterConst.PREFIX_IRI);
                nodeFmt.format(out, n);
                out.println();
            });
        }
    }
    PrefixMapping prefixMappingWithStd = Prefixes.adapt(pmapWithStd);
    ShapeOutputVisitor visitor = new ShapeOutputVisitor(prefixMappingWithStd, nodeFmt, out);
    shapes.iteratorAll().forEachRemaining(sh -> {
        out.println();
        writeOneShapeCompact(out, nodeFmt, visitor, sh);
    // writeOneShapeCompactOrSkip(out, nodeFmt, visitor, sh);
    });
    out.flush();
}
Also used : PrefixMap(org.apache.jena.riot.system.PrefixMap) PrefixMapping(org.apache.jena.shared.PrefixMapping) NodeFormatter(org.apache.jena.riot.out.NodeFormatter) NodeFormatterTTL(org.apache.jena.riot.out.NodeFormatterTTL)

Example 3 with NodeFormatterTTL

use of org.apache.jena.riot.out.NodeFormatterTTL in project jena by apache.

the class ShexPrintable method print.

public default void print() {
    IndentedWriter iOut = IndentedWriter.clone(IndentedWriter.stdout);
    NodeFormatter nFmt = new NodeFormatterTTL(null, PrefixMapFactory.create(SSE.getPrefixMapRead()));
    print(iOut, nFmt);
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) NodeFormatter(org.apache.jena.riot.out.NodeFormatter) NodeFormatterTTL(org.apache.jena.riot.out.NodeFormatterTTL)

Example 4 with NodeFormatterTTL

use of org.apache.jena.riot.out.NodeFormatterTTL in project jena by apache.

the class Shex method printSchema.

private static void printSchema(IndentedWriter iOut, ShexSchema shapes, Set<String> visited) {
    boolean havePrinted = false;
    if (!shapes.getPrefixMap().isEmpty()) {
        RiotLib.writePrefixes(iOut, shapes.getPrefixMap(), true);
        havePrinted = true;
    }
    if (shapes.hasImports()) {
        if (havePrinted)
            iOut.println();
        shapes.getImports().forEach(iriStr -> {
            String pname = shapes.getPrefixMap().abbreviate(iriStr);
            if (pname == null)
                iOut.printf("IMPORT <%s>\n", iriStr);
            else
                iOut.printf("IMPORT %s\n", pname);
        });
        havePrinted = true;
    }
    if (!shapes.getShapes().isEmpty()) {
        boolean shapePrinted = false;
        NodeFormatter nFmt = new NodeFormatterTTL(null, shapes.getPrefixMap());
        for (ShexShape shape : shapes.getShapes()) {
            if (havePrinted)
                iOut.println();
            shape.print(iOut, nFmt);
            havePrinted = true;
        }
    }
    // Print imports.
    if (shapes.hasImports()) {
        if (havePrinted)
            iOut.println();
        shapes.getImports().forEach(iriStr -> {
            if (visited.contains(iriStr))
                return;
            visited.add(iriStr);
            String prefix = iOut.getLinePrefix();
            iOut.println("Import = <" + iriStr + ">");
            iOut.incIndent(4);
            try {
                ShexSchema imports = readSchema(iriStr);
                iOut.setLinePrefix(prefix + "I");
                printSchema(iOut, imports, visited);
            } catch (Exception ex) {
                iOut.println("Failed to read shapes: " + ex.getMessage());
            } finally {
                iOut.setLinePrefix(prefix);
                iOut.decIndent(4);
            }
        });
        havePrinted = true;
    }
    iOut.flush();
}
Also used : NodeFormatter(org.apache.jena.riot.out.NodeFormatter) NodeFormatterTTL(org.apache.jena.riot.out.NodeFormatterTTL)

Example 5 with NodeFormatterTTL

use of org.apache.jena.riot.out.NodeFormatterTTL in project jena by apache.

the class CompactWriter method formatterPrefixMap.

private static NodeFormatter formatterPrefixMap(PrefixMapping prefixMapping) {
    PrefixMap pmap = prefixMapWithStd(prefixMapping);
    NodeFormatter nodeFmt = new NodeFormatterTTL(null, pmap);
    return nodeFmt;
}
Also used : PrefixMap(org.apache.jena.riot.system.PrefixMap) NodeFormatter(org.apache.jena.riot.out.NodeFormatter) NodeFormatterTTL(org.apache.jena.riot.out.NodeFormatterTTL)

Aggregations

NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)6 NodeFormatter (org.apache.jena.riot.out.NodeFormatter)4 PrefixMap (org.apache.jena.riot.system.PrefixMap)2 ArrayList (java.util.ArrayList)1 AWriter (org.apache.jena.atlas.io.AWriter)1 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 Node (org.apache.jena.graph.Node)1 PrefixMapping (org.apache.jena.shared.PrefixMapping)1 Var (org.apache.jena.sparql.core.Var)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1