Package org.eclipse.rdf4j.rio.turtle
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 Summary
Fields Modifier and Type Field Description private java.lang.BooleanabbreviateNumbersprotected ParsedIRIbaseIRIprotected ModelbufferedStatementsprivate java.lang.ObjectbufferLockprivate longbufferSizeSize of statement buffer used for pretty printing and blank node inlining.private static longDEFAULT_BUFFER_SIZEprivate static IRIFIRSTprivate booleaninlineBNodesprotected IRIlastWrittenPredicateprotected ResourcelastWrittenSubjectprivate static intLINE_WRAPprivate ModelFactorymodelFactoryprivate java.util.Deque<IRI>pathprivate java.lang.BooleanprettyPrintprivate static IRIRESTprivate java.util.Deque<Resource>stackprotected booleanstatementClosedFlag indicating whether the last written statement has been closed.protected IndentingWriterwriterprivate java.lang.BooleanxsdStringToPlainLiteral-
Fields inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFWriter
namespaceTable, statementConsumer
-
-
Constructor Summary
Constructors Constructor Description TurtleWriter(java.io.OutputStream out)Creates a new TurtleWriter that will write to the supplied OutputStream.TurtleWriter(java.io.OutputStream out, ParsedIRI baseIRI)Creates a new TurtleWriter that will write to the supplied OutputStream.TurtleWriter(java.io.Writer writer)Creates a new TurtleWriter that will write to the supplied Writer.TurtleWriter(java.io.Writer writer, ParsedIRI baseIRI)Creates a new TurtleWriter that will write to the supplied Writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private booleancanInlineValue(Model contextData, Value v)private voidcloseHangingResource()private voidcloseNestedResources(Resource subj)protected voidclosePreviousStatement()protected voidconsumeStatement(Statement st)Consume a statement.voidendRDF()Signals the end of the RDF data.protected ModelFactorygetModelFactory()RDFFormatgetRDFFormat()Gets the RDF format that this RDFWriter uses.java.util.Collection<RioSetting<?>>getSupportedSettings()java.io.WritergetWriter()get theWriterused by thisCharSink.voidhandleComment(java.lang.String comment)Handles a comment.private voidhandleInlineNode(Statement st, boolean inlineSubject, boolean inlineObject)private voidhandleList(Statement st, boolean canInlineObjectBNode)voidhandleNamespace(java.lang.String prefix, java.lang.String name)Handles a namespace declaration/definition.protected voidhandleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubjectBNode, boolean canShortenObjectBNode)Internal method that differentiates between the pretty-print and streaming writer cases.private booleanisBuffering()Checks if the writer is configured such that it needs statement buffering.private booleanisHanging()private booleanisWellFormedCollection(Resource subj)Check that the collection started with the supplied subject node is a well-formed RDF Collection.private java.util.Optional<Resource>nextSubject(Model contextData, java.util.Set<Resource> processedSubjects)private voidprocessBuffer()not synchronized, assumes calling method has obtained a lock onbufferLock.private voidprocessPredicate(Model contextData, Resource subject, IRI predicate, java.util.Set<Resource> processedSubjects, java.util.Set<IRI> processedPredicates)private voidprocessSubject(Model contextData, Resource subject, java.util.Set<Resource> processedSubjects)voidsetModelFactory(ModelFactory modelFactory)Set aModelFactoryto use for creating internal Models for statement processing/buffering purposes.voidstartRDF()Signals the start of the RDF data.private voidwrapLine(boolean space)protected voidwriteBase(java.lang.String baseURI)protected voidwriteBNode(BNode bNode)Deprecated.UsewriteBNode(BNode, boolean)instead.protected voidwriteBNode(BNode bNode, boolean canShorten)protected voidwriteCommentLine(java.lang.String line)protected voidwriteLiteral(Literal lit)protected voidwriteNamespace(java.lang.String prefix, java.lang.String name)protected voidwritePredicate(IRI predicate)protected voidwriteResource(Resource res)Deprecated.UsewriteResource(Resource, boolean)instead.protected voidwriteResource(Resource res, boolean canShorten)protected voidwriteStatement(Resource subj, IRI pred, Value obj, Resource context, boolean canShortenSubjectBNode, boolean canShortenObjectBNode)protected voidwriteTriple(Triple triple, boolean canShorten)protected voidwriteTripleRDFStar(Triple triple, boolean canShorten)protected voidwriteURI(IRI uri)protected voidwriteValue(Value val)Deprecated.UsewriteValue(Value, boolean)instead.protected voidwriteValue(Value val, boolean canShorten)-
Methods inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFWriter
checkWritingStarted, getFileFormat, getWriterConfig, handleStatement, isWritingStarted, set, setWriterConfig
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.eclipse.rdf4j.common.io.Sink
acceptsFileFormat, getFileFormat
-
-
-
-
Field Detail
-
LINE_WRAP
private static final int LINE_WRAP
- See Also:
- Constant Field Values
-
DEFAULT_BUFFER_SIZE
private static final long DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
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 java.lang.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 java.util.Deque<Resource> stack
-
path
private final java.util.Deque<IRI> path
-
xsdStringToPlainLiteral
private java.lang.Boolean xsdStringToPlainLiteral
-
prettyPrint
private java.lang.Boolean prettyPrint
-
inlineBNodes
private boolean inlineBNodes
-
abbreviateNumbers
private java.lang.Boolean abbreviateNumbers
-
modelFactory
private ModelFactory modelFactory
-
-
Constructor Detail
-
TurtleWriter
public TurtleWriter(java.io.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(java.io.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(java.io.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(java.io.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 Detail
-
getWriter
public java.io.Writer getWriter()
Description copied from interface:CharSinkget theWriterused by thisCharSink.
-
getRDFFormat
public RDFFormat getRDFFormat()
Description copied from interface:RDFWriterGets the RDF format that this RDFWriter uses.- Specified by:
getRDFFormatin interfaceRDFWriter
-
getSupportedSettings
public java.util.Collection<RioSetting<?>> getSupportedSettings()
- Specified by:
getSupportedSettingsin interfaceRDFWriter- Overrides:
getSupportedSettingsin classAbstractRDFWriter- Returns:
- A collection of
RioSettings that are supported by this RDFWriter.
-
startRDF
public void startRDF() throws RDFHandlerExceptionDescription copied from interface:RDFHandlerSignals the start of the RDF data. This method is called before any data is reported.- Specified by:
startRDFin interfaceRDFHandler- Overrides:
startRDFin classAbstractRDFWriter- Throws:
RDFHandlerException- If the RDF handler has encountered an unrecoverable error.
-
endRDF
public void endRDF() throws RDFHandlerExceptionDescription copied from interface:RDFHandlerSignals the end of the RDF data. This method is called when all data has been reported.- Specified by:
endRDFin interfaceRDFHandler- Throws:
RDFHandlerException- If the RDF handler has encountered an unrecoverable error.
-
handleNamespace
public void handleNamespace(java.lang.String prefix, java.lang.String name) throws RDFHandlerExceptionDescription copied from interface:RDFHandlerHandles 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:
handleNamespacein interfaceRDFHandler- Overrides:
handleNamespacein classAbstractRDFWriter- 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 aModelFactoryto use for creating internal Models for statement processing/buffering purposes.- Parameters:
modelFactory- aModelFactoryto 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:AbstractRDFWriterConsume 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:
consumeStatementin classAbstractRDFWriter- 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 writeendRDFCalled- 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 ofBNode.canShortenObjectBNode- True if, in the current context, we may be able to shorten the object of this statement iff it is an instance ofBNode.
-
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 java.io.IOException
- Throws:
java.io.IOException
-
handleComment
public void handleComment(java.lang.String comment) throws RDFHandlerExceptionDescription copied from interface:RDFHandlerHandles a comment.- Specified by:
handleCommentin interfaceRDFHandler- Parameters:
comment- The comment.- Throws:
RDFHandlerException- If the RDF handler has encountered an unrecoverable error.
-
writeCommentLine
protected void writeCommentLine(java.lang.String line) throws java.io.IOException- Throws:
java.io.IOException
-
writeBase
protected void writeBase(java.lang.String baseURI) throws java.io.IOException- Throws:
java.io.IOException
-
writeNamespace
protected void writeNamespace(java.lang.String prefix, java.lang.String name) throws java.io.IOException- Throws:
java.io.IOException
-
writePredicate
protected void writePredicate(IRI predicate) throws java.io.IOException
- Throws:
java.io.IOException
-
writeValue
@Deprecated protected void writeValue(Value val) throws java.io.IOException
Deprecated.UsewriteValue(Value, boolean)instead.- Parameters:
val- TheValueto write.- Throws:
java.io.IOException
-
writeValue
protected void writeValue(Value val, boolean canShorten) throws java.io.IOException
-
writeResource
@Deprecated protected void writeResource(Resource res) throws java.io.IOException
Deprecated.UsewriteResource(Resource, boolean)instead.- Parameters:
res- TheResourceto write.- Throws:
java.io.IOException
-
writeResource
protected void writeResource(Resource res, boolean canShorten) throws java.io.IOException
-
writeURI
protected void writeURI(IRI uri) throws java.io.IOException
- Throws:
java.io.IOException
-
writeBNode
@Deprecated protected void writeBNode(BNode bNode) throws java.io.IOException
Deprecated.UsewriteBNode(BNode, boolean)instead.- Parameters:
bNode- TheBNodeto write.- Throws:
java.io.IOException
-
writeBNode
protected void writeBNode(BNode bNode, boolean canShorten) throws java.io.IOException
- Throws:
java.io.IOException
-
writeTriple
protected void writeTriple(Triple triple, boolean canShorten) throws java.io.IOException
- Throws:
java.io.IOException
-
writeTripleRDFStar
protected void writeTripleRDFStar(Triple triple, boolean canShorten) throws java.io.IOException
- Throws:
java.io.IOException
-
writeLiteral
protected void writeLiteral(Literal lit) throws java.io.IOException
- Throws:
java.io.IOException
-
closePreviousStatement
protected void closePreviousStatement() throws java.io.IOException- Throws:
java.io.IOException
-
isHanging
private boolean isHanging()
-
closeHangingResource
private void closeHangingResource() throws java.io.IOException- Throws:
java.io.IOException
-
closeNestedResources
private void closeNestedResources(Resource subj) throws java.io.IOException
- Throws:
java.io.IOException
-
handleInlineNode
private void handleInlineNode(Statement st, boolean inlineSubject, boolean inlineObject) throws java.io.IOException
- Throws:
java.io.IOException
-
handleList
private void handleList(Statement st, boolean canInlineObjectBNode) throws java.io.IOException
- Throws:
java.io.IOException
-
wrapLine
private void wrapLine(boolean space) throws java.io.IOException- Throws:
java.io.IOException
-
processBuffer
private void processBuffer() throws RDFHandlerExceptionnot synchronized, assumes calling method has obtained a lock onbufferLock.- Throws:
RDFHandlerException
-
nextSubject
private java.util.Optional<Resource> nextSubject(Model contextData, java.util.Set<Resource> processedSubjects)
-
processSubject
private void processSubject(Model contextData, Resource subject, java.util.Set<Resource> processedSubjects)
-
processPredicate
private void processPredicate(Model contextData, Resource subject, IRI predicate, java.util.Set<Resource> processedSubjects, java.util.Set<IRI> processedPredicates)
-
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.
-
-