com.glaivestone.toolbox.text.csv
Class CSVFormatter

java.lang.Object
  |
  +--com.glaivestone.toolbox.text.csv.CSVFormatter

public class CSVFormatter
extends java.lang.Object

Formatter for processing text values into CSV (comma-separated values) format.

A formatter is configured with the separator string which separates field values in the text, along with a delimiter string which may be used to enclose field values. The default configuration uses comma as the field value separator and double-quote as the delimiter string.

The formatter will automatically delimit field values if the value contains an embedded separator. Embedded delimiters in an delimited value are doubled-up when the value is wrapped. The client is responsible for wrapping values in the delimiter if the value contains significant leading or trailing white space that should be preserved.

A null value results in an empty field entry in the CSV string (no characters). An empty string value may result in either an empty field entry, as from a null field value, or it may result in an empty string literal in the CSV string (""), depending on whether the formatter is configured to reduce empty values to null or retain empty values. By default, empty strings are reduced to null to produce empty field entries in the CSV text. Note that an empty string is a zero-length string; a string containing only white space is not empty. CSVFormatter

The standard usage pattern is to instantiate a CSVParser configured with the desired options for the field separator character and the field value delimiter, then use it to format lists of field values into CSV formatted text lines.

Author:
Deb Lewis

Constructor Summary
CSVFormatter()
          Construct a new CSVFormatter which formats text valuess separated by the default comma separator string.
CSVFormatter(java.lang.String aSeparator)
          Construct a new CSVFormatter which formats text valuess separated by the specified separator string.
CSVFormatter(java.lang.String aSeparator, java.lang.String aDelimiter)
          Construct a new CSVFormatter which formats text valuess separated by the specified separator string.
 
Method Summary
 java.lang.String asCSVString(java.lang.String aString)
           
 java.lang.String format(java.util.List values)
          Format the list of string values into a CSV string.
 java.lang.String format(java.lang.String[] values)
          Format the list of string values into a CSV string.
 void setReduceEmptyValues(boolean reduceEmptyValues)
          Specify whether to reduce empty string values to empty field entries in the CSV string (same result as a null value).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVFormatter

public CSVFormatter()
Construct a new CSVFormatter which formats text valuess separated by the default comma separator string.


CSVFormatter

public CSVFormatter(java.lang.String aSeparator)
Construct a new CSVFormatter which formats text valuess separated by the specified separator string.


CSVFormatter

public CSVFormatter(java.lang.String aSeparator,
                    java.lang.String aDelimiter)
Construct a new CSVFormatter which formats text valuess separated by the specified separator string.

Method Detail

asCSVString

public java.lang.String asCSVString(java.lang.String aString)

format

public java.lang.String format(java.util.List values)
Format the list of string values into a CSV string.


format

public java.lang.String format(java.lang.String[] values)
Format the list of string values into a CSV string.


setReduceEmptyValues

public void setReduceEmptyValues(boolean reduceEmptyValues)
Specify whether to reduce empty string values to empty field entries in the CSV string (same result as a null value). Turn this option off to have empty strings emitted as string literals in the CSV.


GFL 1.1 API