Class TurtleWriter

java.lang.Object
org.eclipse.rdf4j.rio.helpers.AbstractRDFWriter
org.eclipse.rdf4j.rio.turtle.TurtleWriter
All Implemented Interfaces:
CharSink, Sink, RDFHandler, RDFWriter
Direct Known Subclasses:
TriGWriter, TurtleStarWriter

public class TurtleWriter extends AbstractRDFWriter implements CharSink
An implementation of the RDFWriter interface that writes RDF documents in Turtle format. The Turtle format is defined in in this document.
  • Field Details

    • LINE_WRAP

      private static final int LINE_WRAP
      See Also:
    • DEFAULT_BUFFER_SIZE

      private static final long DEFAULT_BUFFER_SIZE
      See Also:
    • FIRST

      private static final IRI FIRST
    • REST

      private static final IRI REST
    • bufferSize

      private long bufferSize
      Size of statement buffer used for pretty printing and blank node inlining. Set to Long.MAX_VALUE to buffer everything until the end (necessary when blank node inlining).
    • bufferedStatements

      protected Model bufferedStatements
    • bufferLock

      private final Object bufferLock
    • baseIRI

      protected ParsedIRI baseIRI
    • writer

      protected IndentingWriter writer
    • statementClosed

      protected boolean statementClosed
      Flag indicating whether the last written statement has been closed.
    • lastWrittenSubject

      protected Resource lastWrittenSubject
    • lastWrittenPredicate

      protected IRI lastWrittenPredicate
    • stack

      private final Deque<Resource> stack
    • path

      private final Deque<IRI> path
    • xsdStringToPlainLiteral

      private Boolean xsdStringToPlainLiteral
    • prettyPrint

      private Boolean prettyPrint
    • inlineBNodes

      private boolean inlineBNodes
    • abbreviateNumbers

      private Boolean abbreviateNumbers
    • modelFactory

      private ModelFactory modelFactory
  • Constructor Details

    • TurtleWriter

      public TurtleWriter(OutputStream out)
      Creates a new TurtleWriter that will write to the supplied OutputStream.
      Parameters:
      out - The OutputStream to write the Turtle document to.
    • TurtleWriter

      public TurtleWriter(OutputStream out, ParsedIRI baseIRI)
      Creates a new TurtleWriter that will write to the supplied OutputStream.
      Parameters:
      out - The OutputStream to write the Turtle document to.
      baseIRI -
    • TurtleWriter

      public TurtleWriter(Writer writer)
      Creates a new TurtleWriter that will write to the supplied Writer.
      Parameters:
      writer - The Writer to write the Turtle document to.
    • TurtleWriter

      public TurtleWriter(Writer writer, ParsedIRI baseIRI)
      Creates a new TurtleWriter that will write to the supplied Writer.
      Parameters:
      writer - The Writer to write the Turtle document to.
      baseIRI -
  • Method Details

    • getWriter

      public Writer getWriter()
      Description copied from interface: CharSink
      get the Writer used by this CharSink.
      Specified by:
      getWriter in interface CharSink
      Returns:
      an Writer
    • getRDFFormat

      public RDFFormat getRDFFormat()
      Description copied from interface: RDFWriter
      Gets the RDF format that this RDFWriter uses.
      Specified by:
      getRDFFormat in interface RDFWriter
    • getSupportedSettings

      public Collection<RioSetting<?>> getSupportedSettings()
      Specified by:
      getSupportedSettings in interface RDFWriter
      Overrides:
      getSupportedSettings in class AbstractRDFWriter
      Returns:
      A collection of RioSettings that are supported by this RDFWriter.
    • startRDF

      public void startRDF() throws RDFHandlerException
      Description copied from interface: RDFHandler
      Signals the start of the RDF data. This method is called before any data is reported.
      Specified by:
      startRDF in interface RDFHandler
      Overrides:
      startRDF in class AbstractRDFWriter
      Throws:
      RDFHandlerException - If the RDF handler has encountered an unrecoverable error.
    • endRDF

      public void endRDF() throws RDFHandlerException
      Description copied from interface: RDFHandler
      Signals the end of the RDF data. This method is called when all data has been reported.
      Specified by:
      endRDF in interface RDFHandler
      Throws:
      RDFHandlerException - If the RDF handler has encountered an unrecoverable error.
    • handleNamespace

      public void handleNamespace(String prefix, String name) throws RDFHandlerException
      Description copied from interface: RDFHandler
      Handles a namespace declaration/definition. A namespace declaration associates a (short) prefix string with the namespace's URI. The prefix for default namespaces, which do not have an associated prefix, are represented as empty strings.
      Specified by:
      handleNamespace in interface RDFHandler
      Overrides:
      handleNamespace in class AbstractRDFWriter
      Parameters:
      prefix - The prefix for the namespace, or an empty string in case of a default namespace.
      name - The URI that the prefix maps to.
      Throws:
      RDFHandlerException - If the RDF handler has encountered an unrecoverable error.
    • setModelFactory

      public void setModelFactory(ModelFactory modelFactory)
      Set a ModelFactory to use for creating internal Models for statement processing/buffering purposes.
      Parameters:
      modelFactory - a ModelFactory to use for internal buffering / statement processing purposes. May not be null.
    • getModelFactory

      protected ModelFactory getModelFactory()
    • consumeStatement

      protected void consumeStatement(Statement st) throws RDFHandlerException
      Description copied from class: AbstractRDFWriter
      Consume a statement.

      Extending classes must override this method instead of overriding AbstractRDFWriter.handleStatement(Statement) in order to benefit from automatic handling of RDF-star conversion or encoding.

      Overrides:
      consumeStatement in class AbstractRDFWriter
      Parameters:
      st - the statement to consume.
      Throws:
      RDFHandlerException
    • handleStatementInternal

      protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubjectBNode, boolean canShortenObjectBNode)
      Internal method that differentiates between the pretty-print and streaming writer cases.
      Parameters:
      st - The next statement to write
      endRDFCalled - True if endRDF has been called before this method is called. This is used to buffer statements for pretty-printing before dumping them when all statements have been delivered to us.
      canShortenSubjectBNode - True if, in the current context, we may be able to shorten the subject of this statement iff it is an instance of BNode.
      canShortenObjectBNode - True if, in the current context, we may be able to shorten the object of this statement iff it is an instance of BNode.
    • isWellFormedCollection

      private boolean isWellFormedCollection(Resource subj)
      Check that the collection started with the supplied subject node is a well-formed RDF Collection.

      It specifically checks that any collection subject blank nodes (the subjects of the rdf:first and rdf:rest statements) are _not_ reused for any other, unrelated statements, and there are no things like multiple rdf:first or rdf:rest statements for the same subject.

      Returns:
      true if the collection is considered well-formed false otherwise.
    • writeStatement

      protected void writeStatement(Resource subj, IRI pred, Value obj, Resource context, boolean canShortenSubjectBNode, boolean canShortenObjectBNode) throws IOException
      Throws:
      IOException
    • handleComment

      public void handleComment(String comment) throws RDFHandlerException
      Description copied from interface: RDFHandler
      Handles a comment.
      Specified by:
      handleComment in interface RDFHandler
      Parameters:
      comment - The comment.
      Throws:
      RDFHandlerException - If the RDF handler has encountered an unrecoverable error.
    • writeCommentLine

      protected void writeCommentLine(String line) throws IOException
      Throws:
      IOException
    • writeBase

      protected void writeBase(String baseURI) throws IOException
      Throws:
      IOException
    • writeNamespace

      protected void writeNamespace(String prefix, String name) throws IOException
      Throws:
      IOException
    • writePredicate

      protected void writePredicate(IRI predicate) throws IOException
      Throws:
      IOException
    • writeValue

      @Deprecated protected void writeValue(Value val) throws IOException
      Deprecated.
      Parameters:
      val - The Value to write.
      Throws:
      IOException
    • writeValue

      protected void writeValue(Value val, boolean canShorten) throws IOException
      Writes a value, optionally shortening it if it is an IRI and has a namespace definition that is suitable for use in this context for shortening or a BNode that has been confirmed to be able to be shortened in this context.
      Parameters:
      val - The Value to write.
      canShorten - True if, in the current context, we can shorten this value if it is an instance of BNode .
      Throws:
      IOException
    • writeResource

      @Deprecated protected void writeResource(Resource res) throws IOException
      Deprecated.
      Parameters:
      res - The Resource to write.
      Throws:
      IOException
    • writeResource

      protected void writeResource(Resource res, boolean canShorten) throws IOException
      Writes a Resource, optionally shortening it if it is an IRI and has a namespace definition that is suitable for use in this context for shortening or a BNode that has been confirmed to be able to be shortened in this context.
      Parameters:
      res - The Resource to write.
      canShorten - True if, in the current context, we can shorten this value if it is an instance of BNode .
      Throws:
      IOException
    • writeURI

      protected void writeURI(IRI uri) throws IOException
      Throws:
      IOException
    • writeBNode

      @Deprecated protected void writeBNode(BNode bNode) throws IOException
      Deprecated.
      Parameters:
      bNode - The BNode to write.
      Throws:
      IOException
    • writeBNode

      protected void writeBNode(BNode bNode, boolean canShorten) throws IOException
      Throws:
      IOException
    • writeTriple

      protected void writeTriple(Triple triple, boolean canShorten) throws IOException
      Throws:
      IOException
    • writeTripleRDFStar

      protected void writeTripleRDFStar(Triple triple, boolean canShorten) throws IOException
      Throws:
      IOException
    • writeLiteral

      protected void writeLiteral(Literal lit) throws IOException
      Throws:
      IOException
    • closePreviousStatement

      protected void closePreviousStatement() throws IOException
      Throws:
      IOException
    • isHanging

      private boolean isHanging()
    • closeHangingResource

      private void closeHangingResource() throws IOException
      Throws:
      IOException
    • closeNestedResources

      private void closeNestedResources(Resource subj) throws IOException
      Throws:
      IOException
    • handleInlineNode

      private void handleInlineNode(Statement st, boolean inlineSubject, boolean inlineObject) throws IOException
      Throws:
      IOException
    • handleList

      private void handleList(Statement st, boolean canInlineObjectBNode) throws IOException
      Throws:
      IOException
    • wrapLine

      private void wrapLine(boolean space) throws IOException
      Throws:
      IOException
    • processBuffer

      private void processBuffer() throws RDFHandlerException
      not synchronized, assumes calling method has obtained a lock on bufferLock.
      Throws:
      RDFHandlerException
    • nextSubject

      private Optional<Resource> nextSubject(Model contextData, Set<Resource> processedSubjects)
    • processSubject

      private void processSubject(Model contextData, Resource subject, Set<Resource> processedSubjects)
    • processPredicate

      private void processPredicate(Model contextData, Resource subject, IRI predicate, Set<Resource> processedSubjects, Set<IRI> processedPredicates)
    • canInlineValue

      private boolean canInlineValue(Model contextData, Value v)
    • isBuffering

      private boolean isBuffering()
      Checks if the writer is configured such that it needs statement buffering.
      Returns:
      true if the writer is buffering, false otherwise.