Class IonTextWriterBuilder
- Direct Known Subclasses:
_Private_IonTextWriterBuilder
IonWriters emitting the Ion text syntax.
WARNING: This class should not be extended by code outside of this library.
Builders may be configured once and reused to construct multiple objects.
Instances of this class are not not safe for use by multiple threads unless they are immutable.
The most general and correct approach is to use the standard()
builder:
IonWriter w = IonTextWriterBuilder.standard().build(outputStream);
The standard configuration gives a direct representation of what's written,
including version markers and local symbol tables. That's good for
diagnostics but it may be more than you want in many situations.
In such cases the minimal() or pretty() builders (or a
combination) may give more satisfying output:
IonWriter w = IonTextWriterBuilder.minimal()
.withPrettyPrinting()
.build(outputStream);
Configuration properties follow the standard JavaBeans idiom in order to be
friendly to dependency injection systems. They also provide alternative
with...() mutation methods that enable a more fluid style.
Auto-flushing
IonWriters created by this builder auto-flush to the
underlying data sink after writing each top-level value in the context of
the writer.
Currently, there is no configuration point available to disable the auto-flushing mechanism. Please vote on issue amazon-ion/ion-java/issues/32 if you require it.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumA strategy for minimizing the output of local symbol tables.static enumRepresents common new-line separators that are valid in Ion.Nested classes/interfaces inherited from class com.amazon.ion.system.IonWriterBuilder
IonWriterBuilder.InitialIvmHandling, IonWriterBuilder.IvmMinimizing -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNOT FOR APPLICATION USE!protectedNOT FOR APPLICATION USE! -
Method Summary
Modifier and TypeMethodDescriptionabstract IonWriterbuild(Appendable out) Creates a new writer that will write text to the given output stream.abstract IonTextWriterBuildercopy()final IonCatalogGets the catalog to use when building anIonWriter.final CharsetGets the charset denoting the output encoding.final SymbolTable[]Gets the imports that will be used to construct the initial local symbol table.Gets the strategy for emitting Ion version markers at the start of the stream.Gets the strategy for eliminating Ion version markers mid-stream.final intGets the length beyond which string and clob content will be rendered as triple-quoted "long strings".Gets the strategy for reducing or eliminating local symbol tables.Gets the character sequence that will be written as a line separator.final booleanGets whether each top level value for standard printing should start on a new line.abstract IonTextWriterBuilderstatic IonTextWriterBuilderjson()Creates a builder preconfigured for JSON compatibility.static IonTextWriterBuilderminimal()Creates a builder configured to minimize system data, eliminating local symbol tables and minimizing version markers.abstract IonTextWriterBuildermutable()protected voidNOT FOR APPLICATION USE!static IonTextWriterBuilderpretty()Creates a builder preconfigured for basic pretty-printing.voidsetCatalog(IonCatalog catalog) Sets the catalog to use when building anIonWriter.voidsetCharset(Charset charset) Sets the charset denoting the output encoding.voidsetImports(SymbolTable... imports) Sets the shared symbol tables that will be used to construct the initial local symbol table.voidSets the strategy for emitting Ion version markers at the start of the stream.voidsetIvmMinimizing(IonWriterBuilder.IvmMinimizing minimizing) Sets the strategy for reducing or eliminating non-initial Ion version markers.voidsetLongStringThreshold(int threshold) Sets the length beyond which string and clob content will be rendered as triple-quoted "long strings".voidsetLstMinimizing(IonTextWriterBuilder.LstMinimizing minimizing) Sets the strategy for reducing or eliminating local symbol tables.voidsetNewLineType(IonTextWriterBuilder.NewLineType newLineType) Sets the character sequence that will be written as a line separator.voidsetWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines) Sets whether each top level value for standard printing should start on a new line.static IonTextWriterBuilderstandard()The standard builder of textIonWriters, with all configuration properties having their default values.final IonTextWriterBuilderwithCatalog(IonCatalog catalog) Declares the catalog to use when building anIonWriter, returning a new mutable builder if this is immutable.final IonTextWriterBuilderwithCharset(Charset charset) Declares the charset denoting the output encoding, returning a new mutable builder if this is immutable.final IonTextWriterBuilderDeclares the output encoding to beUS-ASCII.final IonTextWriterBuilderwithImports(SymbolTable... imports) Declares the imports to use when building anIonWriter, returning a new mutable builder if this is immutable.final IonTextWriterBuilderDeclares the strategy for emitting Ion version markers at the start of the stream, returning a new mutable builder if this is immutable.final IonTextWriterBuilderwithIvmMinimizing(IonWriterBuilder.IvmMinimizing minimizing) Declares the strategy for reducing or eliminating non-initial Ion version markers, returning a new mutable builder if this is immutable.abstract IonTextWriterBuilderDeclares that this builder should downgrade the writers' output to JSON compatibility.final IonTextWriterBuilderwithLongStringThreshold(int threshold) Declares the length beyond which string and clob content will be rendered as triple-quoted "long strings".final IonTextWriterBuilderwithLstMinimizing(IonTextWriterBuilder.LstMinimizing minimizing) Sets the strategy for reducing or eliminating local symbol tables.final IonTextWriterBuilderDeclares that this builder should minimize system-level output (Ion version markers and local symbol tables).final IonTextWriterBuilderwithNewLineType(IonTextWriterBuilder.NewLineType newLineType) Declares the character sequence that will be written as a line separator.abstract IonTextWriterBuilderDeclares that this builder should use basic pretty-printing.final IonTextWriterBuilderwithWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines) Declares whether each top level value for standard printing should start on a new line.Methods inherited from class com.amazon.ion.system.IonWriterBuilder
build
-
Field Details
-
ASCII
The"US-ASCII"charset. -
UTF8
The"UTF-8"charset.
-
-
Constructor Details
-
IonTextWriterBuilder
protected IonTextWriterBuilder()NOT FOR APPLICATION USE! -
IonTextWriterBuilder
NOT FOR APPLICATION USE!
-
-
Method Details
-
standard
The standard builder of textIonWriters, with all configuration properties having their default values. The resulting output is a direct representation of what's written to the writer, including version markers and local symbol tables.- Returns:
- a new, mutable builder instance.
- See Also:
-
minimal
Creates a builder configured to minimize system data, eliminating local symbol tables and minimizing version markers.- Returns:
- a new, mutable builder instance.
- See Also:
-
pretty
Creates a builder preconfigured for basic pretty-printing.The specifics of this configuration may change between releases of this library, so automated processes should not depend on the exact output formatting. In particular, there's currently no promise regarding handling of system data.
- Returns:
- a new, mutable builder instance.
- See Also:
-
json
Creates a builder preconfigured for JSON compatibility.- Returns:
- a new, mutable builder instance.
- See Also:
-
copy
-
immutable
-
mutable
-
withCatalog
Declares the catalog to use when building anIonWriter, returning a new mutable builder if this is immutable.- Parameters:
catalog- the catalog to use in built writers. If null, the writer will be unable to resolve manually-written imports and may throw an exception.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
withImports
Declares the imports to use when building anIonWriter, returning a new mutable builder if this is immutable.If the imports sequence is not null and not empty, the output stream will be bootstrapped with a local symbol table that uses the given
imports.- Parameters:
imports- a sequence of shared symbol tables. The first (and only the first) may be a system table.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
getCharset
Gets the charset denoting the output encoding. Only ASCII and UTF-8 are supported.- Returns:
- may be null, denoting the default of UTF-8.
- See Also:
-
setCharset
Sets the charset denoting the output encoding. Only ASCII and UTF-8 are supported; applications can use the helper constantsASCIIandUTF8.- Parameters:
charset- may be null, denoting the default of UTF-8.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
withCharset
Declares the charset denoting the output encoding, returning a new mutable builder if this is immutable. Only ASCII and UTF-8 are supported; applications can use the helper constantsASCIIandUTF8.- Parameters:
charset- may be null, denoting the default of UTF-8.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
withCharsetAscii
Declares the output encoding to beUS-ASCII.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
-
withMinimalSystemData
Declares that this builder should minimize system-level output (Ion version markers and local symbol tables).This is equivalent to:
- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
-
withPrettyPrinting
Declares that this builder should use basic pretty-printing. Does not alter the handling of system data. Calling this method alters several other configuration properties, so code should call it first, then make any necessary overrides.The specifics of this configuration may change between releases of this library, so automated processes should not depend on the exact output formatting.
- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
-
withJsonDowngrade
Declares that this builder should downgrade the writers' output to JSON compatibility. This format cannot round-trip back to Ion with full fidelity.The specific conversions are as follows:
- System data is suppressed per
withMinimalSystemData(). - All annotations are suppressed.
- Nulls of any type are printed as JSON
null. - Blobs are printed as strings, containing Base64.
- Clobs are printed as strings, containing only Unicode code points U+00 through U+FF.
- Sexps are printed as lists.
- Symbols are printed as strings.
- Timestamps are printed as strings, using Ion timestamp format.
- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- System data is suppressed per
-
getInitialIvmHandling
Gets the strategy for emitting Ion version markers at the start of the stream. By default, IVMs are emitted only when explicitly written or when necessary (for example, before data that's not Ion 1.0, or at the start of Ion binary output).- Specified by:
getInitialIvmHandlingin classIonWriterBuilder- Returns:
- the initial IVM strategy.
The default value (
null) indicates that an initial IVM is emitted if and only if it is received by the writer. - See Also:
-
setInitialIvmHandling
Sets the strategy for emitting Ion version markers at the start of the stream. By default, IVMs are emitted only when explicitly written or when necessary (for example, before data that's not Ion 1.0).- Parameters:
handling- the initial IVM strategy. Null indicates that explicitly-written IVMs will be emitted.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
withInitialIvmHandling
public final IonTextWriterBuilder withInitialIvmHandling(IonWriterBuilder.InitialIvmHandling handling) Declares the strategy for emitting Ion version markers at the start of the stream, returning a new mutable builder if this is immutable. By default, IVMs are emitted only when explicitly written or when necessary (for example, before data that's not Ion 1.0).- Parameters:
handling- the initial IVM strategy. Null indicates that explicitly-written IVMs will be emitted.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
getIvmMinimizing
Gets the strategy for eliminating Ion version markers mid-stream. By default, IVMs are emitted as received or when necessary.This strategy does not affect handling of IVMs at the start of the stream; that's the job of
IonWriterBuilder.InitialIvmHandling.- Specified by:
getIvmMinimizingin classIonWriterBuilder- Returns:
- the IVM minimizing strategy.
The default value (
null) indicates that no minimization occurs and IVMs are emitted as received by the writer. - See Also:
-
setIvmMinimizing
Sets the strategy for reducing or eliminating non-initial Ion version markers. When null, IVMs are emitted as they are written.- Parameters:
minimizing- the IVM minimization strategy. Null indicates that all explicitly-written IVMs will be emitted.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
withIvmMinimizing
Declares the strategy for reducing or eliminating non-initial Ion version markers, returning a new mutable builder if this is immutable. When null, IVMs are emitted as they are written.- Parameters:
minimizing- the IVM minimization strategy. Null indicates that all explicitly-written IVMs will be emitted.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
getLstMinimizing
Gets the strategy for reducing or eliminating local symbol tables. By default, LST data is emitted as received or when necessary (for example, binary data will always collect and emit local symbols).- See Also:
-
setLstMinimizing
Sets the strategy for reducing or eliminating local symbol tables. By default, LST data is emitted as received or when necessary (for example, binary data will always collect and emit local symbols).- Parameters:
minimizing- the LST minimization strategy. Null indicates that LSTs will be emitted as received.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
withLstMinimizing
Sets the strategy for reducing or eliminating local symbol tables. By default, LST data is emitted as received or when necessary (for example, binary data will always collect and emit local symbols).- Parameters:
minimizing- the LST minimization strategy. Null indicates that LSTs will be emitted as received.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
getLongStringThreshold
public final int getLongStringThreshold()Gets the length beyond which string and clob content will be rendered as triple-quoted "long strings". At present, such content will only line-break on extant newlines.- Returns:
- the threshold for printing triple-quoted strings and clobs. Zero means no limit.
- See Also:
-
setLongStringThreshold
public void setLongStringThreshold(int threshold) Sets the length beyond which string and clob content will be rendered as triple-quoted "long strings". At present, such content will only line-break on extant newlines.- Parameters:
threshold- the new threshold; zero means none.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
withLongStringThreshold
Declares the length beyond which string and clob content will be rendered as triple-quoted "long strings". At present, such content will only line-break on extant newlines.- Parameters:
threshold- the new threshold; zero means none.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
getNewLineType
Gets the character sequence that will be written as a line separator. The default isIonTextWriterBuilder.NewLineType.PLATFORM_DEPENDENT- Returns:
- the character sequence to be written between top-level values; null means the default should be used.
- See Also:
-
setNewLineType
Sets the character sequence that will be written as a line separator. The default isIonTextWriterBuilder.NewLineType.PLATFORM_DEPENDENT- Parameters:
newLineType- the character sequence to be written between top-level values; null means the default should be used.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
withNewLineType
Declares the character sequence that will be written as a line separator. The default isIonTextWriterBuilder.NewLineType.PLATFORM_DEPENDENT- Parameters:
newLineType- the character sequence to be written between top-level values; null means the default should be used.- Returns:
- this instance, if mutable; otherwise a mutable copy of this instance.
- See Also:
-
getWriteTopLevelValuesOnNewLines
public final boolean getWriteTopLevelValuesOnNewLines()Gets whether each top level value for standard printing should start on a new line. The default value isfalse. When false, the IonTextWriter will insert a single space character (U+0020) between top-level values. When pretty-printing, this setting is ignored; the pretty printer will always start top-level values on a new line.- Returns:
- value indicating whether standard printing will insert a newline between top-level values
- See Also:
-
setWriteTopLevelValuesOnNewLines
public void setWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines) Sets whether each top level value for standard printing should start on a new line. The default value isfalse. When false, the IonTextWriter will insert a single space character (U+0020) between top-level values. When pretty-printing, this setting is ignored; the pretty printer will always start top-level values on a new line.- Parameters:
writeTopLevelValuesOnNewLines- value indicating whether standard printing will insert a newline between top-level values- See Also:
-
withWriteTopLevelValuesOnNewLines
public final IonTextWriterBuilder withWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines) Declares whether each top level value for standard printing should start on a new line. The default value isfalse. When false, the IonTextWriter will insert a single space character (U+0020) between top-level values. When pretty-printing, this setting is ignored; the pretty printer will always start top-level values on a new line.- Parameters:
writeTopLevelValuesOnNewLines- value indicating whether standard printing will insert a newline between top-level values- See Also:
-
build
Creates a new writer that will write text to the given output stream.If you have an
OutputStream, you'll get better performance usingIonWriterBuilder.build(OutputStream)as opposed to wrapping your stream in anAppendableand calling this method.- Parameters:
out- the stream that will receive Ion text data. Must not be null.- Returns:
- a new
IonWriterinstance; notnull.
-
mutationCheck
protected void mutationCheck()NOT FOR APPLICATION USE! -
getCatalog
Gets the catalog to use when building anIonWriter. The catalog is needed to resolve manually-written imports (not common). By default, this property is null.- See Also:
-
setCatalog
Sets the catalog to use when building anIonWriter.- Parameters:
catalog- the catalog to use in built writers. If null, the writer will be unable to resolve manually-written imports and may throw an exception.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-
getImports
Gets the imports that will be used to construct the initial local symbol table.- Returns:
- may be null or empty.
- See Also:
-
setImports
Sets the shared symbol tables that will be used to construct the initial local symbol table.If the imports sequence is not null and not empty, the output stream will be bootstrapped with a local symbol table that uses the given
imports.- Parameters:
imports- a sequence of shared symbol tables. The first (and only the first) may be a system table.- Throws:
UnsupportedOperationException- if this is immutable.- See Also:
-