Search in sources :

Example 1 with NodeFormatter

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

the class RowSetWriterTSV method output.

private static void output(AWriter out, RowSet rowSet) {
    try {
        NodeFormatter formatter = createNodeFormatter();
        String sep = null;
        List<Var> vars = rowSet.getResultVars();
        // writes the variables on the first line
        for (Var var : vars) {
            if (sep != null)
                out.write(sep);
            else
                sep = SEP;
            out.write("?");
            out.write(var.getVarName());
        }
        out.write(NL);
        // writes one binding by line
        for (; rowSet.hasNext(); ) {
            sep = null;
            Binding b = rowSet.next();
            for (Var v : vars) {
                if (sep != null)
                    out.write(sep);
                sep = SEP;
                Node n = b.get(v);
                if (n != null) {
                    // This will not include a raw tab.
                    formatter.format(out, n);
                }
            }
            out.write(NL);
        }
    } finally {
        out.flush();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) NodeFormatter(org.apache.jena.riot.out.NodeFormatter) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 2 with NodeFormatter

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

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

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

the class AbstractLineBasedNodeTupleWriter method write.

@Override
public void write(TKey key, T value) {
    log.debug("write({}={})", key, value);
    Node[] ns = this.getNodes(value);
    String sep = this.getSeparator();
    NodeFormatter formatter = this.getNodeFormatter();
    for (int i = 0; i < ns.length; i++) {
        formatter.format(this.writer, ns[i]);
        this.writer.print(sep);
    }
    this.writer.println(this.getTerminator());
    this.writer.flush();
}
Also used : NodeFormatter(org.apache.jena.riot.out.NodeFormatter) Node(org.apache.jena.graph.Node)

Example 5 with NodeFormatter

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

Aggregations

NodeFormatter (org.apache.jena.riot.out.NodeFormatter)8 NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)5 PrefixMap (org.apache.jena.riot.system.PrefixMap)3 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2 Node (org.apache.jena.graph.Node)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 Collection (java.util.Collection)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)1 CollectionUtils (org.apache.jena.atlas.lib.CollectionUtils)1 Prefixes (org.apache.jena.riot.system.Prefixes)1 RiotLib (org.apache.jena.riot.system.RiotLib)1 WriterConst (org.apache.jena.riot.writer.WriterConst)1 ShaclException (org.apache.jena.shacl.ShaclException)1 Shapes (org.apache.jena.shacl.Shapes)1 SHACLC (org.apache.jena.shacl.compact.SHACLC)1 Target (org.apache.jena.shacl.engine.Target)1 TargetType (org.apache.jena.shacl.engine.TargetType)1 Constraint (org.apache.jena.shacl.parser.Constraint)1