/** * Returns a new instance of {@link NTriplesWriter}. */ public RDFWriter getWriter(Writer writer) { return new NTriplesWriter(writer); }
/** * Writes the N-Triples representation of the given {@link Value}. * * @param value * The value to write. * @throws IOException */ protected void writeValue(Value value) throws IOException { if (value instanceof IRI) { writeIRI((IRI)value); } else if (value instanceof BNode) { writeBNode((BNode)value); } else if (value instanceof Literal) { writeLiteral((Literal)value); } else { throw new IllegalArgumentException("Unknown value type: " + value.getClass()); } }
@Override public void handleStatement(Statement st) throws RDFHandlerException { if (!writingStarted) { throw new RuntimeException("Document writing has not yet been started"); } try { writeValue(st.getSubject()); writer.write(" "); writeIRI(st.getPredicate()); writer.write(" "); writeValue(st.getObject()); writer.write(" .\n"); } catch (IOException e) { throw new RDFHandlerException(e); } }
/** * Write the N-Triples representation of the given {@link Literal}, optionally ignoring the xsd:string * datatype as it is implied for RDF-1.1. * * @param lit * The literal to write. * @throws IOException */ private void writeLiteral(Literal lit) throws IOException { // Do some character escaping on the label: writer.append("\""); writeString(lit.getLabel()); writer.append("\""); if (Literals.isLanguageLiteral(lit)) { // Append the literal's language writer.append("@"); writer.append(lit.getLanguage().get()); } else { // SES-1917 : In RDF-1.1, all literals have a type, and if they are not // language literals we display the type for backwards compatibility IRI datatype = lit.getDatatype(); if (!datatype.equals(XMLSchema.STRING) || !xsdStringToPlainLiteral) { writer.append("^^"); writeIRI(lit.getDatatype()); } } }
@Override public void startRDF() throws RDFHandlerException { if (writingStarted) { throw new RuntimeException("Document writing has already started"); } writingStarted = true; xsdStringToPlainLiteral = getWriterConfig().get(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL); escapeUnicode = getWriterConfig().get(NTriplesWriterSettings.ESCAPE_UNICODE); }
@Override public void handleStatement(Statement st) throws RDFHandlerException { if (!writingStarted) { throw new RuntimeException("Document writing has not yet been started"); } try { writeValue(st.getSubject()); writer.write(" "); writeIRI(st.getPredicate()); writer.write(" "); writeValue(st.getObject()); writer.write(" .\n"); } catch (IOException e) { throw new RDFHandlerException(e); } }
/** * Write the N-Triples representation of the given {@link Literal}, optionally ignoring the xsd:string * datatype as it is implied for RDF-1.1. * * @param lit * The literal to write. * @throws IOException */ private void writeLiteral(Literal lit) throws IOException { // Do some character escaping on the label: writer.append("\""); writeString(lit.getLabel()); writer.append("\""); if (Literals.isLanguageLiteral(lit)) { // Append the literal's language writer.append("@"); writer.append(lit.getLanguage().get()); } else { // SES-1917 : In RDF-1.1, all literals have a type, and if they are not // language literals we display the type for backwards compatibility IRI datatype = lit.getDatatype(); if (!datatype.equals(XMLSchema.STRING) || !xsdStringToPlainLiteral) { writer.append("^^"); writeIRI(lit.getDatatype()); } } }
@Override public void startRDF() throws RDFHandlerException { if (writingStarted) { throw new RuntimeException("Document writing has already started"); } writingStarted = true; xsdStringToPlainLiteral = getWriterConfig().get(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL); escapeUnicode = getWriterConfig().get(NTriplesWriterSettings.ESCAPE_UNICODE); }
/** * Returns a new instance of {@link NTriplesWriter}. */ @Override public RDFWriter getWriter(OutputStream out) { return new NTriplesWriter(out); }
/** * Writes the N-Triples representation of the given {@link Value}. * * @param value * The value to write. * @throws IOException */ protected void writeValue(Value value) throws IOException { if (value instanceof IRI) { writeIRI((IRI)value); } else if (value instanceof BNode) { writeBNode((BNode)value); } else if (value instanceof Literal) { writeLiteral((Literal)value); } else { throw new IllegalArgumentException("Unknown value type: " + value.getClass()); } }
/** * Returns a new instance of {@link NTriplesWriter}. */ @Override public RDFWriter getWriter(Writer writer) { return new NTriplesWriter(writer); }
/** * Returns a new instance of {@link NTriplesWriter}. */ public RDFWriter getWriter(OutputStream out) { return new NTriplesWriter(out); }
public NTriplesWriter(OutputStream out) { super(new org.eclipse.rdf4j.rio.ntriples.NTriplesWriter(out)); }
println("Writing n-triples to: " + outputFile.getCanonicalPath()); try (OutputStream out = new FileOutputStream(outputFile)) { NTriplesWriter ntriplesWriter = new NTriplesWriter(out); conn.export(ntriplesWriter, context);