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();
}
}
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();
}
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);
}
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();
}
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();
}
Aggregations