net.sourceforge.jdbcimporter.parser
Class CSVDelimiterParser

java.lang.Object
  extended bynet.sourceforge.jdbcimporter.parser.CSVDelimiterParser
All Implemented Interfaces:
DelimiterParser

public class CSVDelimiterParser
extends java.lang.Object
implements DelimiterParser

The CSVDelimiterParser class implements the DelimiterParser interface to parse character delimited column values. Each column value is separated from another column value by a character or set of characters (ex. ','). In addition, the CSVDelimiterParser supports the ability to enclose each column value with a character or set of characters (ex. '"' ).

Since:
0.6
Author:
Chris Nagy

Field Summary
protected  java.lang.String columnDelimiter
          The character(s) that separate each column value.
protected  java.lang.String enclosedDelimiter
          The character(s) that enclose each column value.
protected  boolean enclosedOptional
          Flag indicating that all column values are enclosed by the enclosed delimiter.
protected  boolean firstLine
          Flag indicating that the first line has been read from the input reader.
protected  int[] ignoredPositions
          The column indices in the file that should be skipped.
protected  boolean ignoreFirstLine
          Flag indicating that the first line should be ignored.
protected  java.io.BufferedReader inputReader
          The input reader.
protected static org.apache.commons.logging.Log LOG
          The log for debug information.
protected  boolean multiline
          Flag indicating that rows could be multi-line.
 
Constructor Summary
CSVDelimiterParser()
           
 
Method Summary
 java.lang.String getColumnDelimiter()
          Returns the character(s) that separate each column value.
protected  java.lang.String getColumnEnclosedNextRow()
          Returns the next row which may span multiple lines.
 java.lang.String getEnclosedDelimiter()
          Returns the character(s) that enclose each column value.
 java.lang.String getNextRow()
          Returns the next row to be imported as a String.
 ColumnValue[] getValues(java.lang.String nextRow)
          Returns the column values for the row specified.
 boolean isEnclosedOptional()
          Returns whether each column value must be enclosed by the set of characters defined.
protected  ColumnValue[] parseColumnDelimitedLine(java.lang.String line)
          Parse the character delimited line into ColumnValues.
protected  ColumnValue[] parseColumnEnclosedDelimitedLine(java.lang.String line)
          Parse the character delimited line, whose values are also enclosed, into ColumnValues.
 void setColumnDelimiter(java.lang.String delimiter)
          Sets the character(s) that separate each column value.
 void setEnclosedDelimiter(java.lang.String delimiter)
          Sets the character(s) that enclose each column value.
 void setEnclosedOptional(boolean optional)
          Sets that each column value must be enclosed by the set of characters defined.
 void setIgnoredPositions(int[] positions)
          Sets the positions, in the csv, to ignore and not return as values.
 void setIgnoreFirstLine(boolean flag)
          Sets the flag to indicate that the first line should be ignored.
 void setMultiline(boolean flag)
          Sets the flag to indicate that the csv may contain rows that span multiple lines.
 void setReader(java.io.Reader reader)
          Sets the input source for the parser.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

protected static org.apache.commons.logging.Log LOG
The log for debug information.


columnDelimiter

protected java.lang.String columnDelimiter
The character(s) that separate each column value.


enclosedDelimiter

protected java.lang.String enclosedDelimiter
The character(s) that enclose each column value.


enclosedOptional

protected boolean enclosedOptional
Flag indicating that all column values are enclosed by the enclosed delimiter.


ignoredPositions

protected int[] ignoredPositions
The column indices in the file that should be skipped.


inputReader

protected java.io.BufferedReader inputReader
The input reader.


multiline

protected boolean multiline
Flag indicating that rows could be multi-line.


ignoreFirstLine

protected boolean ignoreFirstLine
Flag indicating that the first line should be ignored.


firstLine

protected boolean firstLine
Flag indicating that the first line has been read from the input reader.

Constructor Detail

CSVDelimiterParser

public CSVDelimiterParser()
Method Detail

setColumnDelimiter

public void setColumnDelimiter(java.lang.String delimiter)
Sets the character(s) that separate each column value.

Parameters:
delimiter - the column value delimiter.

getColumnDelimiter

public java.lang.String getColumnDelimiter()
Returns the character(s) that separate each column value.

Returns:
the column value delimiter

setEnclosedDelimiter

public void setEnclosedDelimiter(java.lang.String delimiter)
Sets the character(s) that enclose each column value.

Parameters:
delimiter - the enclose delimiter

getEnclosedDelimiter

public java.lang.String getEnclosedDelimiter()
Returns the character(s) that enclose each column value.

Returns:
the enclose delimiter

setEnclosedOptional

public void setEnclosedOptional(boolean optional)
Sets that each column value must be enclosed by the set of characters defined.

Parameters:
optional - boolean flag indicating that each column value must be enclosed.

isEnclosedOptional

public boolean isEnclosedOptional()
Returns whether each column value must be enclosed by the set of characters defined.

Returns:
boolean flag indicating that each column value must be enclosed.

setIgnoredPositions

public void setIgnoredPositions(int[] positions)
Sets the positions, in the csv, to ignore and not return as values.

Parameters:
positions - a comma-separated list of positions (first position = 1)

setMultiline

public void setMultiline(boolean flag)
Sets the flag to indicate that the csv may contain rows that span multiple lines. The enclosed delimiter must also be set.

Parameters:
flag - flag to indicate that rows may span multiple lines

setIgnoreFirstLine

public void setIgnoreFirstLine(boolean flag)
Sets the flag to indicate that the first line should be ignored.

Parameters:
flag - flag to indicate that the first line should be ignored

setReader

public void setReader(java.io.Reader reader)
Description copied from interface: DelimiterParser
Sets the input source for the parser.

Specified by:
setReader in interface DelimiterParser
Parameters:
reader - the source of the data to import.
See Also:
DelimiterParser.setReader(Reader)

getNextRow

public java.lang.String getNextRow()
                            throws java.io.IOException
Description copied from interface: DelimiterParser
Returns the next row to be imported as a String.

Specified by:
getNextRow in interface DelimiterParser
Returns:
the next row to import
Throws:
java.io.IOException - if an error occurs while retrieving the next row
See Also:
DelimiterParser.getNextRow()

getValues

public ColumnValue[] getValues(java.lang.String nextRow)
                        throws MalformedDataException
Description copied from interface: DelimiterParser
Returns the column values for the row specified.

Specified by:
getValues in interface DelimiterParser
Parameters:
nextRow - the row to be parsed into column values
Returns:
the column values
Throws:
MalformedDataException - if the row is not formatted properly
See Also:
DelimiterParser.getValues(String nextRow)

parseColumnDelimitedLine

protected ColumnValue[] parseColumnDelimitedLine(java.lang.String line)
Parse the character delimited line into ColumnValues.

Parameters:
line - the line
Returns:
column values

parseColumnEnclosedDelimitedLine

protected ColumnValue[] parseColumnEnclosedDelimitedLine(java.lang.String line)
                                                  throws MalformedDataException
Parse the character delimited line, whose values are also enclosed, into ColumnValues.

Parameters:
line - the line
Returns:
column values
Throws:
MalformedDataException

getColumnEnclosedNextRow

protected java.lang.String getColumnEnclosedNextRow()
                                             throws java.io.IOException
Returns the next row which may span multiple lines.

Returns:
the next row
Throws:
java.io.IOException