bill.util.csv
Class CSVLineParser

java.lang.Object
  |
  +--bill.util.csv.CSVLineParser
Direct Known Subclasses:
CSVLineParserAssociated

public class CSVLineParser
extends Object

Class for parsing line of CSV data, typically from a file. Breaks the line down into its component parts, or columns, and stores those parts in a Vector. A CSV file is a comma delimited text data file with some special formatting for embedded commas and quotes.


Constructor Summary
CSVLineParser()
          Default constructor, initializes vector for holding line parts.
CSVLineParser(String line)
          Main constructor, parses the specified line to break out it's parts.
CSVLineParser(String line, boolean trim)
          Alternate constructor, parses the specified line to break out it's parts.
 
Method Summary
 void addLinePart(int partNum, String part)
          Inserts the specified line part at the specified index.
 void addLinePart(String part)
          Appends the specified line part to the current list of line parts.
 void dumpParts()
          For debugging purposes, dumps the list of parts to standard output.
 String getLine()
          Returns the CSV line in the format it is persistently stored.
 String getLinePart(int partNum)
          Retrieves a specific line part from the list of parts.
 Vector getLineParts()
          Returns the list of the parts parsed from the line.
 int getNumberOfLineParts()
          Determines the number of parts the parsed line contains.
 String getUnformattedLine()
          Retrieves the CSV line in an unformatted representation.
 boolean isTrimmed()
          Checks to see if line parts are trimmed.
static void main(String[] argv)
           
protected  void parseLine(String line)
          Parses the specified line to break it up into its parts.
protected  void parseLine(String line, boolean trim)
          Parses the specified line to break it up into its parts.
 void removeLinePart(int partNum)
          Deletes the line part at the specified index.
 void setLine(String line)
          Allows the caller to specify the 'raw' line to be parsed.
 void setLinePart(int partNum, String part)
          Sets the value of the given line part to the specified string.
 void setLineParts(Vector parts)
          Sets the list of parts parsed from the line.
 void setTrimmed(boolean trim)
          Sets the trimmed flag to indicate if line part values where trimmed when this line was parsed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVLineParser

public CSVLineParser()
Default constructor, initializes vector for holding line parts. The setLine method should be called when this constructor is used to specify the line to be parsed and perform the parsing.
See Also:
setLine(String)

CSVLineParser

public CSVLineParser(String line)
Main constructor, parses the specified line to break out it's parts.
Parameters:
line - The line to split up into its parts.

CSVLineParser

public CSVLineParser(String line,
                     boolean trim)
Alternate constructor, parses the specified line to break out it's parts. Also allows the user to specify if they want all the parts trimmed of leading and trailing white space.
Parameters:
line - The line to split up into its parts.
trim - When set to true the column value that is parsed out is trimmed of leading and trailing spaces. This is only relevant for quoted values, as non quoted values are always trimmed.
Method Detail

setLine

public void setLine(String line)
Allows the caller to specify the 'raw' line to be parsed. Typically only used when using the default constructor as the other constructor take the line as a parameter.
Parameters:
line - The line to split up into its parts.

parseLine

protected void parseLine(String line)
Parses the specified line to break it up into its parts.
Parameters:
line - The line of data to be parsed

parseLine

protected void parseLine(String line,
                         boolean trim)
Parses the specified line to break it up into its parts.
Parameters:
line - The line of data to be parsed
trim - When set to true the column value that is parsed out is trimmed of leading and trailing spaces. This is only relevant for quoted values, as non quoted values are always trimmed.

getLine

public String getLine()
Returns the CSV line in the format it is persistently stored.
Returns:
The output formatted CSV line.

getUnformattedLine

public String getUnformattedLine()
Retrieves the CSV line in an unformatted representation. Unformatted means that double quotes are not properly escaped by an additional double quote. I.E The value "hello" would normally be formatted as ""hello"" in a CSV representation, but this method will leave it as "hello". Commas are still inserted between values.

Note that if any of the values have commas in them then there may appear to be extra elements in the output if those values do not also include quotes.

Returns:
The CSV line in an unformatted representation.

setLinePart

public void setLinePart(int partNum,
                        String part)
Sets the value of the given line part to the specified string.
Parameters:
partNum - The index of the part to be set, this is a 0 (zero) based value, so 0 = first part.
part - The string value to set the line part to.

getNumberOfLineParts

public int getNumberOfLineParts()
Determines the number of parts the parsed line contains.
Returns:
The number of parts.

isTrimmed

public boolean isTrimmed()
Checks to see if line parts are trimmed.
Returns:
Returns true if line parts are trimmed, returns false otherwise.

setTrimmed

public void setTrimmed(boolean trim)
Sets the trimmed flag to indicate if line part values where trimmed when this line was parsed.
Parameters:
trim - Value to set the flag to.

getLineParts

public Vector getLineParts()
Returns the list of the parts parsed from the line.
Returns:
The parts parsed from the line.

setLineParts

public void setLineParts(Vector parts)
Sets the list of parts parsed from the line.
Parameters:
parts - List of parts to be assigned.

getLinePart

public String getLinePart(int partNum)
Retrieves a specific line part from the list of parts.
Parameters:
partNum - The index of the part to be retrieved, this is a 0 (zero) based value, so 0 = first part.

addLinePart

public void addLinePart(String part)
Appends the specified line part to the current list of line parts.
Parameters:
part - The line part to add.

addLinePart

public void addLinePart(int partNum,
                        String part)
Inserts the specified line part at the specified index. Each line part in this line 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 line parts. (If the index is equal to the current number of line parts, the new line part is appended to the existing line parts.)
Parameters:
partNum - The index to insert the new line part at.
part - The line part to add.

removeLinePart

public void removeLinePart(int partNum)
Deletes the line part at the specified index. Each line part in this line 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 line parts.
Parameters:
partNum - The index of the line part to remove.

dumpParts

public void dumpParts()
For debugging purposes, dumps the list of parts to standard output.

main

public static void main(String[] argv)