Class CSVParser

java.lang.Object
ca.phon.csv.CSVParser

public class CSVParser extends Object
Processes data from a CSVTokenStream into arrays of Strings.

Uses the following grammar:

  csvfile
  :   record (NEWLINE record)* EOF
  ;

  record
  :  field (SEPARATOR field)*
  ;

  field
  :   escaped
  |   unescaped
  ;

  escaped
  :   QUOTE (TEXTDATA | NEWLINE | SEPARATOR | TWO_QUOTE)* QUOTE
  ;

  unescaped
  :   TEXTDATA*
  ;
 

  • Constructor Summary

    Constructors
    Constructor
    Description
    CSVParser(CSVTokenStream tokenStream, CSVQuoteType quoteChar, boolean trimSpaces)
    Constructs a new CSVParser with the specified token stream, quote character and boolean for trimming spaces
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a listener to the CSVParser.
    void
    Parses the tokens for an entire CSV file.
    Parses the tokens for an escaped field (cell surrounded by quotes) from a CSV file.
    Parses the tokens for a single field (cell) from a CSV file.
    Parses the tokens for a single record (row) from a CSV file.
    Parses the tokens for an unescaped field (cell not surrounded by quotes) from a CSV file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CSVParser

      public CSVParser(CSVTokenStream tokenStream, CSVQuoteType quoteChar, boolean trimSpaces)
      Constructs a new CSVParser with the specified token stream, quote character and boolean for trimming spaces
      Parameters:
      tokenStream - An object containing a queue of tokens to be parsed.
      quoteChar - An enum for which type of quote character is being used.
      trimSpaces - If leading and trailing whitespace should be trimmed.
  • Method Details

    • addListener

      public void addListener(CSVParserListener listener)
      Adds a listener to the CSVParser.
    • csvFile

      public void csvFile()
      Parses the tokens for an entire CSV file.
    • record

      public String[] record()
      Parses the tokens for a single record (row) from a CSV file.
      Returns:
      An array of strings containing the contents of each field in the record.
    • field

      public String field()
      Parses the tokens for a single field (cell) from a CSV file.
      Returns:
      The contents of the field as a string.
    • escaped

      public String escaped()
      Parses the tokens for an escaped field (cell surrounded by quotes) from a CSV file.
      Returns:
      The escaped contents of the field as a string.
    • unescaped

      public String unescaped()
      Parses the tokens for an unescaped field (cell not surrounded by quotes) from a CSV file.
      Returns:
      The contents of the field as a string.