bill.util.csv
Class CSVParser

java.lang.Object
  |
  +--bill.util.csv.CSVParser
Direct Known Subclasses:
CSVParserSorted

public class CSVParser
extends Object

Controls the reading of a CSV file. A CSV file is a comma delimited text data file.


Field Summary
protected  Vector _csvLines
          The list of lines read from the CSV file
protected  String _fileName
          Name of the CSV file we are working with
protected  CSVLineParser _header
          The first line of a csv file is a header line that lists what all the columns in the subsequent lines represent.
protected  boolean _modified
          Indicates if the CSV has ben modified since it was read or last saved.
protected  Reader _reader
          Allows an already opened CSV file
protected  int _skipLines
          Number of lines to skip before 'really' reading the file.
protected  OutputStream _writer
          A stream for writing out the CSV data
 
Constructor Summary
CSVParser(OutputStream writer)
          Alternate constructor for skipping lines.
CSVParser(Reader reader)
          Main constructor.
CSVParser(Reader reader, int skipLines)
          Alternate constructor for skipping lines.
CSVParser(Reader reader, OutputStream writer)
          Alternate constructor.
CSVParser(Reader reader, OutputStream writer, int skipLines)
          Alternate constructor for skipping lines.
CSVParser(ResultSet resultSet)
          Constructor for creating a CSV parser using a SQL result set.
CSVParser(String fileName)
          Main constructor.
CSVParser(String fileName, int skipLines)
          Alternate constructor for skipping lines.
 
Method Summary
 void addLine(CSVLineParser line)
          Appends the specified line to the current list of lines.
 void addLine(CSVLineParser line, int lineNum)
          Inserts the specified line at the specified index.
 String getFileName()
          Retrieves the name of the file the CSV information is written to (and possibly read from).
 CSVLineParser getHeader()
          Retrieves the header line for this CSV file.
 CSVLineParser getLine(int lineNum)
          Return a specific data line's contents.
 String getLineLinePart(int lineNum, int partNum)
          Retrieves a specific line part from the specified line.
 String getLineLinePart(int lineNum, String partName)
          Retrieves a specific line part from the specified line.
 Vector getLines()
          Retrieves the list of CSV lines for this parser.
 int getNumberOfLines()
          Retrieves the number of lines read from the CSV file.
 int getPartNumber(String partName)
          Determines the index of a specific line part name.
 boolean hasHeader()
          Test for the presence of a header for this CSV file.
 boolean isModified()
          Checks if the contents of the parser have been modified since they were read or last saved (whichever is more recent).
static void main(String[] argv)
           
 void removeLine(int lineNum)
          Deletes the line at the specified index.
 void save()
          Writes the CSV data out to a file.
 void setFileName(String fileName)
          Set the name of the file the CSV information is written to (and possibly read from).
 void setLineLinePart(int lineNum, int partNum, String value)
          Sets the specified line part from the specified line.
 void setLineLinePart(int lineNum, String partName, String value)
          Sets the specified line part from the specified line.
 void setModified(boolean modified)
          Sets the modified flag.
 void swapLines(int first, int second)
          Swaps the positions of the specified lines.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_skipLines

protected int _skipLines
Number of lines to skip before 'really' reading the file. This is to get past any lines that may be before the header.

_fileName

protected String _fileName
Name of the CSV file we are working with

_writer

protected OutputStream _writer
A stream for writing out the CSV data

_reader

protected Reader _reader
Allows an already opened CSV file

_csvLines

protected Vector _csvLines
The list of lines read from the CSV file

_header

protected CSVLineParser _header
The first line of a csv file is a header line that lists what all the columns in the subsequent lines represent.

_modified

protected boolean _modified
Indicates if the CSV has ben modified since it was read or last saved.
Constructor Detail

CSVParser

public CSVParser(String fileName)
          throws CSVException
Main constructor. Opens up the CSV input file and processes its data lines.
Parameters:
fileName - The name of the CSV input file.
Throws:
CSVException - If the input file cannot be found, opened, or processed.

CSVParser

public CSVParser(Reader reader)
          throws CSVException
Main constructor. Reads specified CSV input stream and processes its data lines. Note that when this constructor is used the save method may not be used unless the setFileName method is first called. This is because we cannot determine a file name from just the stream.
Parameters:
reader - The CSV input stream.
Throws:
CSVException - If the input file cannot be found, opened, or processed.
See Also:
save(), setFileName(String)

CSVParser

public CSVParser(String fileName,
                 int skipLines)
          throws CSVException
Alternate constructor for skipping lines. Opens up the CSV input file and processes its data lines. Use this constructor when there are a set number of lines of data before the CSV header line. The extra lines are ignored.
Parameters:
fileName - The name of the CSV input file.
skipLines - Number of lines to skip before 'really' reading the file. This is to get past any lines that may be before the header.
Throws:
CSVException - If the input file cannot be found, opened, or processed.

CSVParser

public CSVParser(Reader reader,
                 int skipLines)
          throws CSVException
Alternate constructor for skipping lines. Reads specified CSV input stream and processes its data lines. Note that when this constructor is used the save method may not be used unless the setFileName method is first called. This is because we cannot determine a file name from just the stream.
Parameters:
reader - The CSV input stream.
skipLines - Number of lines to skip before 'really' reading the file. This is to get past any lines that may be before the header.
Throws:
CSVException - If the input file cannot be found, opened, or processed.
See Also:
save(), setFileName(String)

CSVParser

public CSVParser(Reader reader,
                 OutputStream writer)
          throws CSVException
Alternate constructor. Reads specified CSV input stream and processes its data lines.
Parameters:
reader - The CSV input stream.
writer - The output stream to write to when saving.
Throws:
CSVException - If the input file cannot be found, opened, or processed.

CSVParser

public CSVParser(Reader reader,
                 OutputStream writer,
                 int skipLines)
          throws CSVException
Alternate constructor for skipping lines. Reads specified CSV input stream and processes its data lines.
Parameters:
reader - The CSV input stream.
writer - The output stream to write to when saving.
skipLines - Number of lines to skip before 'really' reading the file. This is to get past any lines that may be before the header.
Throws:
CSVException - If the input file cannot be found, opened, or processed.

CSVParser

public CSVParser(OutputStream writer)
Alternate constructor for skipping lines. Reads specified CSV input stream and processes its data lines.
Parameters:
writer - The output stream to write to when saving.

CSVParser

public CSVParser(ResultSet resultSet)
          throws CSVException
Constructor for creating a CSV parser using a SQL result set. Note that when this constructor is used the save method may not be used unless the setFileName method is first called. This is because we currently only support saving to a file and we cannot determine a file name from a result set.
Parameters:
resultSet - The SQL result set to use in building the CSV information.
Throws:
CSVException - Thrown when the result set cannot be processed due to a SQLException being thrown.
See Also:
save(), setFileName(String)
Method Detail

getLines

public Vector getLines()
Retrieves the list of CSV lines for this parser.
Returns:
The list of lines contained in this CSV parser. Note that this is the actual list, not a copy, so changes made to the contents of this list are permanent.

isModified

public boolean isModified()
Checks if the contents of the parser have been modified since they were read or last saved (whichever is more recent).
Returns:
Returns true if the parser's contents have been modified, otherwise returns false.

setModified

public void setModified(boolean modified)
Sets the modified flag.
Parameters:
modified - Value to set the modified flag to.

save

public void save()
          throws Exception
Writes the CSV data out to a file.
Throws:
Exception - Thrown when the save file could not be opened or a file name is not defined.

getFileName

public String getFileName()
Retrieves the name of the file the CSV information is written to (and possibly read from).
Returns:
The CSV file name.

setFileName

public void setFileName(String fileName)
Set the name of the file the CSV information is written to (and possibly read from).
Parameters:
fileName - The name of the CSV file.

getLine

public CSVLineParser getLine(int lineNum)
Return a specific data line's contents.
Parameters:
lineNum - The data line to retrieve. Uses a 0 based counter, so the first data line is considered line 0.
Returns:
The data specified line.

getLineLinePart

public String getLineLinePart(int lineNum,
                              int partNum)
Retrieves a specific line part from the specified line.
Parameters:
lineNum - The data line to retrieve line part from. Uses a 0 based counter, so the first data line is considered line 0.
partNum - The index of the part to be retrieved, this is a 0 (zero) based value, so 0 = first part.

getLineLinePart

public String getLineLinePart(int lineNum,
                              String partName)
                       throws CSVException
Retrieves a specific line part from the specified line.
Parameters:
lineNum - The data line to retrieve line part from. Uses a 0 based counter, so the first data line is considered line 0.
partName - The name of the part to be retrieved, based on the part names read from the header line.
Throws:
CSVException - Thrown when the part name is not valid.

setLineLinePart

public void setLineLinePart(int lineNum,
                            int partNum,
                            String value)
Sets the specified line part from the specified line.
Parameters:
lineNum - The data line to retrieve line part from. Uses a 0 based counter, so the first data line is considered line 0.
partName - The name of the part to be retrieved, based on the part names read from the header line.

setLineLinePart

public void setLineLinePart(int lineNum,
                            String partName,
                            String value)
                     throws CSVException
Sets the specified line part from the specified line.
Parameters:
lineNum - The data line to retrieve line part from. Uses a 0 based counter, so the first data line is considered line 0.
partName - The name of the part to be retrieved, based on the part names read from the header line.
Throws:
CSVException - Thrown when the part name is not valid.

getPartNumber

public int getPartNumber(String partName)
                  throws CSVException
Determines the index of a specific line part name.
Parameters:
partName - The name of the part to be retrieved, based on the part names read from the header line.
Throws:
CSVException - Thrown when the part name is not valid.

addLine

public void addLine(CSVLineParser line)
Appends the specified line to the current list of lines.
Parameters:
line - The line parser information to add.

addLine

public void addLine(CSVLineParser line,
                    int lineNum)
Inserts the specified line at the specified index. Each line in this object with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously.
The index must be a value greater than or equal to 0 and less than or equal to the current number of lines. (If the index is equal to the current number of lines, the new line is appended to the existing lines.)
Parameters:
line - The line parser information to add.
lineNum - The index to insert the new line at.

removeLine

public void removeLine(int lineNum)
Deletes the line at the specified index. Each line in this object with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously.
The index must be a value greater than or equal to 0 and less than the current number of lines.
Parameters:
lineNum - The index of the line to remove.

getNumberOfLines

public int getNumberOfLines()
Retrieves the number of lines read from the CSV file.
Returns:
The number of lines read from the CSV file.

getHeader

public CSVLineParser getHeader()
Retrieves the header line for this CSV file. The header line defines what all the columns in the data lines represent. It can also be thought of as a table of contents.
Returns:
The CSV file's header line.

hasHeader

public boolean hasHeader()
Test for the presence of a header for this CSV file.
Returns:
Returns true if a header is set

swapLines

public void swapLines(int first,
                      int second)
Swaps the positions of the specified lines.
Parameters:
first - The line number (0 based) of the first line to swap.
second - The line number (0 based) of the second line to swap.

main

public static void main(String[] argv)