nrs.util
Class CSVParser

java.lang.Object
  extended bynrs.util.CSVParser
All Implemented Interfaces:
CSVListener

public class CSVParser
extends java.lang.Object
implements CSVListener

Parses a CSV file and informs a listener of new values.

To use, declare the class that wants to parse CSV values as implementing the nrs.util.CSVListener interface along with implementations of the newCSVValue(String value, boolean newLine) and noMoreCSVValues() methods. Then create a new instance of CSVParser and pass in a BufferedReader which wraps the CSV source, field and text delimiters and the listening class. Calling the parse() method will start parsing. If you want to stop halfway through the file then call the stop() method.

This class can also be called directly as java nrs.util.CSVParser -csv file.csv to see how it works.

Author:
Copyright © 2004 Nick Sydenham <nsydenham@yahoo.co.uk>

Constructor Summary
CSVParser()
          Create a new instance of CSVParser using default values for the field and text delimiter.
CSVParser(java.io.BufferedReader reader, java.lang.String fieldDelimiter, java.lang.String textDelimiter, CSVListener listener)
          Creates a new instance of CSVReader
 
Method Summary
 java.lang.String getFieldDelimiter()
          Get the currect field delimiter
 CSVListener getListener()
          Get the currect CSVListener
 java.io.BufferedReader getReader()
          Get the current BufferedReader
 java.lang.String getTextDelimiter()
          Get the current text delimiter
 boolean hasReadAll()
          Check to see if the CSV source has a) finished being parsed, and b) all values were read
 boolean isNoTextDelimiter()
           
static void main(java.lang.String[] args)
          Main method for testing
 void newCSVValue(java.lang.String value, int lineNum)
          A new CSV value has been found
 void noMoreCSVValues()
          Finished parsing the input
 void parse()
          Parse the data from the specified BufferedReader
 void setFieldDelimiter(java.lang.String fDelim)
          Set the field delimiter
 void setListener(CSVListener listener)
          Set the listener of CSV events
 void setNoTextDelimiter(boolean noTextDelimiter)
          If set the parser won't look for text delimiters.
 void setReader(java.io.BufferedReader reader)
          Set the BufferedReader which will read the CSV source
 void setTextDelimiter(java.lang.String tDelim)
          Set the text delimiter
 void stop()
          Tell the CSVReader not to continue parsing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVParser

public CSVParser()
Create a new instance of CSVParser using default values for the field and text delimiter. The user will need to set the reader and listener before calling the parse method.

See Also:
setReader(BufferedReader), setListener(CSVListener)

CSVParser

public CSVParser(java.io.BufferedReader reader,
                 java.lang.String fieldDelimiter,
                 java.lang.String textDelimiter,
                 CSVListener listener)
Creates a new instance of CSVReader

Parameters:
reader - BufferedReader of the CSV source
fieldDelimiter - the field delimiter, e.g. ,
textDelimiter - the text delimiter, e.g. "
listener - the class that wants to be informed of CSV values
Method Detail

setReader

public void setReader(java.io.BufferedReader reader)
Set the BufferedReader which will read the CSV source

Parameters:
reader - the CSV source

getReader

public java.io.BufferedReader getReader()
Get the current BufferedReader

Returns:
BufferedReader

setListener

public void setListener(CSVListener listener)
Set the listener of CSV events

Parameters:
listener - CSVListener

getListener

public CSVListener getListener()
Get the currect CSVListener

Returns:
CSVListener

setFieldDelimiter

public void setFieldDelimiter(java.lang.String fDelim)
Set the field delimiter

Parameters:
fDelim - the field delimiter character

getFieldDelimiter

public java.lang.String getFieldDelimiter()
Get the currect field delimiter

Returns:
field delimiter

setTextDelimiter

public void setTextDelimiter(java.lang.String tDelim)
Set the text delimiter

Parameters:
tDelim - the text delimiter character

getTextDelimiter

public java.lang.String getTextDelimiter()
Get the current text delimiter

Returns:
text delimiter

isNoTextDelimiter

public boolean isNoTextDelimiter()

setNoTextDelimiter

public void setNoTextDelimiter(boolean noTextDelimiter)
If set the parser won't look for text delimiters. In this case the end of the field delimiter marks the start of the value.

Parameters:
noTextDelimiter - set to true to not scan for text delimiter

parse

public void parse()
           throws java.io.IOException
Parse the data from the specified BufferedReader

Throws:
java.io.IOException - an error occured reading the CSV source

stop

public void stop()
Tell the CSVReader not to continue parsing


hasReadAll

public boolean hasReadAll()
Check to see if the CSV source has a) finished being parsed, and b) all values were read

Returns:
true if all values were parsed, false otherwise

newCSVValue

public void newCSVValue(java.lang.String value,
                        int lineNum)
A new CSV value has been found

Specified by:
newCSVValue in interface CSVListener
Parameters:
value - the new value
lineNum - line number, starting from 0

noMoreCSVValues

public void noMoreCSVValues()
Description copied from interface: CSVListener
Finished parsing the input

Specified by:
noMoreCSVValues in interface CSVListener

main

public static void main(java.lang.String[] args)
Main method for testing

Parameters:
args - command line arguments


Copyright © 2004-2005 Nick Sydenham <nsydenham@yahoo.co.uk>