com.continuent.tungsten.common.csv
Class CsvWriter

java.lang.Object
  extended by com.continuent.tungsten.common.csv.CsvWriter

public class CsvWriter
extends java.lang.Object

Writes CSV output. This class implements CSV formatting roughly as described in RFC4180 (http://tools.ietf.org/html/rfc4180) with practical alterations to match specify DBMS implementations.

Version:
1.0
Author:
Robert Hodges

Constructor Summary
CsvWriter(java.io.BufferedWriter writer)
          Instantiate a new instance with output to provided buffered writer.
CsvWriter(java.io.Writer writer)
          Instantiate a new instance with output to provided writer.
 
Method Summary
 void addColumnName(java.lang.String name)
          Add a column name.
 void addRowIdName(java.lang.String name)
          Add a row id name.
 CsvWriter flush()
          Forces a write of any pending row(s) and flushes data on writer.
 char getEscapeChar()
          Returns the escape character.
 java.lang.String getEscapedChars()
          Returns a string of characters that must be preceded by escape character.
 java.util.List<java.lang.String> getNames()
          Return names in column order.
 NullPolicy getNullPolicy()
          Returns the policy for handling null values.
 java.lang.String getNullValue()
          Gets the null value identifier string.
 char getQuoteChar()
          Returns the quote character.
 int getRowCount()
          Returns the current count of rows written.
 char getSeparator()
          Returns separator character.
 java.lang.String getSuppressedChars()
          Returns a string of characters that are suppressed in CSV output.
 int getWidth()
          Return the number of columns.
 java.io.Writer getWriter()
          Get the underlying writer.
 boolean isNullAutofill()
          Returns true to fill nulls automatically.
 boolean isQuoted()
          Returns true if values will be enclosed by a quote character.
 boolean isWriteHeaders()
          If true, write headers.
 CsvWriter put(int index, java.lang.String value)
          Writes value to current row.
 CsvWriter put(java.lang.String key, java.lang.String value)
          Writes value to key in current row.
 void setEscapeChar(char quoteEscapeChar)
          Sets character used to escape quotes and other escaped characters.
 void setEscapedChars(java.lang.String escapedChars)
          Defines zero or more characters that must be preceded by escape character.
 void setNullAutofill(boolean nullAutofill)
          Sets the null autofill policy for columns that have no value (partial rows).
 void setNullPolicy(NullPolicy nullPolicy)
          Sets the policy for handling null values.
 void setNullValue(java.lang.String nullValue)
          Sets the null value identifier string.
 void setQuoteChar(char quoteChar)
          Sets the quote character.
 void setQuoted(boolean quoted)
          Set to true to enable quoting.
 void setSeparator(char separator)
          Sets the separator characters.
 void setSuppressedChars(java.lang.String suppressedChars)
          Sets characters to be suppressed in CSV output.
 void setWriteHeaders(boolean writeHeaders)
          Set to true to write headers.
 CsvWriter write()
          Writes current row, including headers if we are on the first row.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CsvWriter

public CsvWriter(java.io.Writer writer)
Instantiate a new instance with output to provided writer.


CsvWriter

public CsvWriter(java.io.BufferedWriter writer)
Instantiate a new instance with output to provided buffered writer. This call allows clients to set buffering parameters themselves.

Method Detail

setSeparator

public void setSeparator(char separator)
Sets the separator characters.


getSeparator

public char getSeparator()
Returns separator character.


isQuoted

public boolean isQuoted()
Returns true if values will be enclosed by a quote character.


setQuoted

public void setQuoted(boolean quoted)
Set to true to enable quoting.


getNullPolicy

public NullPolicy getNullPolicy()
Returns the policy for handling null values.


setNullPolicy

public void setNullPolicy(NullPolicy nullPolicy)
Sets the policy for handling null values.


getNullValue

public java.lang.String getNullValue()
Gets the null value identifier string.


setNullValue

public void setNullValue(java.lang.String nullValue)
Sets the null value identifier string. This applies only when null policy is NullPolicy.nullValue.


isNullAutofill

public boolean isNullAutofill()
Returns true to fill nulls automatically.


setNullAutofill

public void setNullAutofill(boolean nullAutofill)
Sets the null autofill policy for columns that have no value (partial rows). If true, unwritten columns are filled with the prevailing null value. If false, partial rows prompt an exception.


getQuoteChar

public char getQuoteChar()
Returns the quote character.


setQuoteChar

public void setQuoteChar(char quoteChar)
Sets the quote character.


setEscapeChar

public void setEscapeChar(char quoteEscapeChar)
Sets character used to escape quotes and other escaped characters.

See Also:
setQuoteChar(char)

getEscapeChar

public char getEscapeChar()
Returns the escape character.


getEscapedChars

public java.lang.String getEscapedChars()
Returns a string of characters that must be preceded by escape character.


setEscapedChars

public void setEscapedChars(java.lang.String escapedChars)
Defines zero or more characters that must be preceded by escape character.


getSuppressedChars

public java.lang.String getSuppressedChars()
Returns a string of characters that are suppressed in CSV output.


setSuppressedChars

public void setSuppressedChars(java.lang.String suppressedChars)
Sets characters to be suppressed in CSV output.


getRowCount

public int getRowCount()
Returns the current count of rows written.


getWriter

public java.io.Writer getWriter()
Get the underlying writer.


isWriteHeaders

public boolean isWriteHeaders()
If true, write headers.


setWriteHeaders

public void setWriteHeaders(boolean writeHeaders)
Set to true to write headers.


addColumnName

public void addColumnName(java.lang.String name)
                   throws CsvException
Add a column name. Columns are indexed 1,2,3,...,N in the order added. You must add all names before writing the first row.

Parameters:
name - Column name
Throws:
CsvException - Thrown

addRowIdName

public void addRowIdName(java.lang.String name)
                  throws CsvException
Add a row id name. Row IDs are a numeric counter that can be inserted in any column. By defining the row id name, the matching column always has the batch row number automatically added to it.

Parameters:
name - Row ID name
Throws:
CsvException - Thrown if the row ID has already been set.

getNames

public java.util.List<java.lang.String> getNames()
Return names in column order.


getWidth

public int getWidth()
Return the number of columns.


write

public CsvWriter write()
                throws CsvException,
                       java.io.IOException
Writes current row, including headers if we are on the first row.

Throws:
CsvException - Thrown if there is an inconsistency like too many columns
java.io.IOException - Thrown due to a write error

flush

public CsvWriter flush()
                throws java.io.IOException,
                       CsvException
Forces a write of any pending row(s) and flushes data on writer.

Throws:
CsvException - Thrown on an I/O failure
java.io.IOException

put

public CsvWriter put(int index,
                     java.lang.String value)
              throws CsvException
Writes value to current row. This is the base value.

Parameters:
index - Column index where indexes are numbered 1,2,3,...,N with N being the width of the row in columns
value - String value to write, already escaped if necessary
Throws:
CsvException - Thrown if client attempts to write same column value twice or the row is not wide enough

put

public CsvWriter put(java.lang.String key,
                     java.lang.String value)
              throws CsvException
Writes value to key in current row.

Throws:
CsvException