Class CSVParser

All Implemented Interfaces:
ICSVParser

public class CSVParser extends AbstractCSVParser

A very simple CSV parser released under a commercial-friendly license. This just implements splitting a single line into fields.

The purpose of the CSVParser is to take a single string and parse it into its elements based on the delimiter, quote and escape characters.

The CSVParser has grown organically based on user requests and does not truly match any current requirements (though it can be configured to match or come close). There are no plans to change this as it will break existing requirements. Consider using the RFC4180Parser for less configurability but closer match to the RFC4180 requirements.

Author:
Glen Smith, Rainer Pruy
  • Constructor Details

    • CSVParser

      public CSVParser()
      Constructs CSVParser using default values for everything.
  • Method Details

    • getEscape

      public char getEscape()
      Returns:
      The default escape character for this parser.
    • isStrictQuotes

      public boolean isStrictQuotes()
      Returns:
      The default strictQuotes setting for this parser.
    • isIgnoreLeadingWhiteSpace

      public boolean isIgnoreLeadingWhiteSpace()
      Returns:
      The default ignoreLeadingWhiteSpace setting for this parser.
    • isIgnoreQuotations

      public boolean isIgnoreQuotations()
      Returns:
      The default ignoreQuotation setting for this parser.
    • convertToCsvValue

      protected String convertToCsvValue(String value, boolean applyQuotestoAll)
      Description copied from class: AbstractCSVParser
      Used when reverse parsing an array of strings to a single string. Handles the application of quotes around the string and handling any quotes within the string.
      Specified by:
      convertToCsvValue in class AbstractCSVParser
      Parameters:
      value - String to be converted
      applyQuotestoAll - All values should be surrounded with quotes
      Returns:
      String that will go into the CSV string
    • parseLine

      protected String[] parseLine(String nextLine, boolean multi) throws IOException
      Description copied from class: AbstractCSVParser
      Parses an incoming String and returns an array of elements.
      Specified by:
      parseLine in class AbstractCSVParser
      Parameters:
      nextLine - The string to parse
      multi - Whether it takes multiple lines to form a single record
      Returns:
      The list of elements, or null if nextLine is null
      Throws:
      IOException - If bad things happen during the read
    • isNextCharacterEscapable

      protected boolean isNextCharacterEscapable(String nextLine, boolean inQuotes, int i)
      Checks to see if the character after the current index in a String is an escapable character.

      Meaning the next character is a quotation character, the escape char, or the separator and you are inside quotes.

      "Inside quotes" in this context is interpreted liberally. For instance, if quotes are not expected but we are inside a field, that still counts for the purposes of this method as being "in quotes".

      Precondition: the current character is an escape.
      Parameters:
      nextLine - The current line
      inQuotes - True if the current context is quoted
      i - Current index in line
      Returns:
      True if the following character is a quote
    • setErrorLocale

      public void setErrorLocale(Locale errorLocale)
      Description copied from interface: ICSVParser
      Sets the locale for all error messages.
      Parameters:
      errorLocale - Locale for error messages. If null, the default locale is used.