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
public class TableWriter extends PrintWriter
Wraps a writer stream and provides methods for easier writing of tabular data. The writer can be configured with asetDataSeparator(String)
(default is 'tab'),setNullValue(String)
(default is an empty string) and ansetEncoder(EncoderDecoder)
(no default). UsetablePrintData(Object...)
to print a singe line with data. OtherPrintWriter
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 Modifier and Type Field Description private String
dataSeparator
private EncoderDecoder
encoder
private Map<Integer,Formatter<?>>
formatters
private String
nullValue
-
Fields inherited from class java.io.PrintWriter
out
-
-
Constructor Summary
Constructors Constructor Description TableWriter(Writer out)
Create a new table writer that is writing it's output to the given writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Formatter<?>
getColumnFormatter(int index)
String
getDataSeparator()
The data separator string is written between each data column in the output.EncoderDecoder
getEncoder()
Get the current default encoder for this writer.String
getNullValue()
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.String
tableEncode(String s)
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
-
-
-
-
Constructor Detail
-
TableWriter
public TableWriter(Writer out)
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 Detail
-
getDataSeparator
public String 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
public void setDataSeparator(String dataSeparator)
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
public String 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
public void setNullValue(String nullValue)
Change the null value string.- Parameters:
nullValue
- The new null value string
-
getEncoder
public EncoderDecoder getEncoder()
Get the current default encoder for this writer.- Returns:
- The current encoder or null
- Since:
- 2.15
-
setEncoder
public void setEncoder(EncoderDecoder encoder)
Change the encoder.- Parameters:
encoder
- The new encoder or null to not encode- Since:
- 2.15
-
tableEncode
public String tableEncode(String s)
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
public void setColumnFormatter(int index, Formatter<?> formatter)
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
public Formatter<?> getColumnFormatter(int index)
- Since:
- 3.15
- See Also:
setColumnFormatter(int, Formatter)
-
tablePrintHeaders
public void tablePrintHeaders(Object... headers)
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
public void tablePrintData(Object... data)
Print a data line to the parent writer using the default encoder and column formatters.- Parameters:
data
- The values to write
-
tablePrintData
public void tablePrintData(EncoderDecoder encoder, 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 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
-
-