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