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 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 Details

  • Constructor Details

    • 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 Details

    • 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. See setEncoder(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:
    • tablePrintHeaders

      public void tablePrintHeaders(Object... headers)
      Print a header line to the parent writer. The only difference between this method and the tablePrintData(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 with EncoderDecoder.encode(String).
      Parameters:
      data - The values to write
      encoder - 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 with EncoderDecoder.encode(String).
      Parameters:
      data - The values to write
      formatters - 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