Package net.sf.basedb.util.export
Class TableWriter
java.lang.Object
java.io.Writer
java.io.PrintWriter
net.sf.basedb.util.export.TableWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
- Direct Known Subclasses:
AnnotationWriter
,BaseFileWriter
,DataWriter
,XlsxTableWriter
Wraps a writer stream and provides methods for easier writing
of tabular data. The writer can be configured with a
setDataSeparator(String)
(default is 'tab'),
setNullValue(String)
(default is an empty string)
and an setEncoder(EncoderDecoder)
(no default).
Use tablePrintData(Object...)
to print a singe line
with data. Other PrintWriter
methods are not affected
by those settings.- Version:
- 2.12
- Author:
- Nicklas
- Last modified
- $Date: 2019-03-27 09:34:35 +0100 (ons, 27 mars 2019) $
-
Field Summary
Fields inherited from class java.io.PrintWriter
out
-
Constructor Summary
ConstructorDescriptionTableWriter
(Writer out) Create a new table writer that is writing it's output to the given writer. -
Method Summary
Modifier and TypeMethodDescriptionFormatter<?>
getColumnFormatter
(int index) The data separator string is written between each data column in the output.Get the current default encoder for this writer.The null value string is written to the output instead of null data values.void
setColumnFormatter
(int index, Formatter<?> formatter) Set a data formatter for a given column.void
setDataSeparator
(String dataSeparator) Change the data separator string.void
setEncoder
(EncoderDecoder encoder) Change the encoder.void
setNullValue
(String nullValue) Change the null value string.Encode the given string with the default encoder.void
tablePrintData
(Object... data) Print a data line to the parent writer using the default encoder and column formatters.void
tablePrintData
(EncoderDecoder encoder, Object... data) Print a data line to the parent writer.void
tablePrintData
(EncoderDecoder encoder, Map<Integer, Formatter<?>> formatters, Object... data) Print a data line to the parent writer.void
tablePrintHeaders
(Object... headers) Print a header line to the parent writer.Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write
Methods inherited from class java.io.Writer
nullWriter
-
Field Details
-
dataSeparator
-
nullValue
-
encoder
-
formatters
-
-
Constructor Details
-
TableWriter
Create a new table writer that is writing it's output to the given writer.- Parameters:
out
- The writer to write the data to
-
-
Method Details
-
getDataSeparator
The data separator string is written between each data column in the output. The default value is 'tab'.- Returns:
- The current value of the data separator string
-
setDataSeparator
Change the data separator string. Note! If this value is modified then a different encoder is probably also needed. SeesetEncoder(EncoderDecoder)
.- Parameters:
dataSeparator
- The new data separator
-
getNullValue
The null value string is written to the output instead of null data values. The default value is an empty string.- Returns:
- The current value of the null value string
-
setNullValue
Change the null value string.- Parameters:
nullValue
- The new null value string
-
getEncoder
Get the current default encoder for this writer.- Returns:
- The current encoder or null
- Since:
- 2.15
-
setEncoder
Change the encoder.- Parameters:
encoder
- The new encoder or null to not encode- Since:
- 2.15
-
tableEncode
Encode the given string with the default encoder.- Parameters:
s
- The string to encode- Returns:
- The encoded string, or the argument if no default encoder has been specified
- Since:
- 2.15
-
setColumnFormatter
Set a data formatter for a given column. From now on, the given formatter will be used instead of the toString() method to convert data values in that column. It is important that the data is of the correct type for the formatter.- Since:
- 3.15
-
getColumnFormatter
- Since:
- 3.15
- See Also:
-
tablePrintHeaders
Print a header line to the parent writer. The only difference between this method and thetablePrintData(Object...)
method is that no column formatters are used (since they operate on data and the headers are typically strings).- Since:
- 3.15
-
tablePrintData
Print a data line to the parent writer using the default encoder and column formatters.- Parameters:
data
- The values to write
-
tablePrintData
Print a data line to the parent writer. Each value is separated by the data separator string ('tab' by default). Null values are replaced with the null value string (empty string by default). Non-null objects are converted to strings values by calling either their toString() method or using the formatter assigned for each column. If an encoder has been specified, the value is finally encoded withEncoderDecoder.encode(String)
.- Parameters:
data
- The values to writeencoder
- The encoder to use, or null to not use any encoder- Since:
- 2.15
-
tablePrintData
public void tablePrintData(EncoderDecoder encoder, Map<Integer, Formatter<?>> formatters, Object... data) Print a data line to the parent writer. Each value is separated by the data separator string ('tab' by default). Null values are replaced with the null value string (empty string by default). Non-null objects are converted to strings values by calling either their toString() method or using the formatter assigned for each column. If an encoder has been specified, the value is finally encoded withEncoderDecoder.encode(String)
.- Parameters:
data
- The values to writeformatters
- Column formatters to use (null to always use the toString() method)encoder
- The encoder to use, or null to not use any encoder- Since:
- 2.15
-