Class CSVParser


  • public class CSVParser
    extends java.lang.Object
    A very simple CSV parser released under a commercial-friendly license. This just implements splitting a single line into fields.
    Author:
    Glen Smith, Rainer Pruy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char DEFAULT_ESCAPE_CHARACTER
      The default escape character to use if none is supplied to the constructor.
      static boolean DEFAULT_IGNORE_LEADING_WHITESPACE
      The default leading whitespace behavior to use if none is supplied to the constructor
      static char DEFAULT_QUOTE_CHARACTER
      The default quote character to use if none is supplied to the constructor.
      static char DEFAULT_SEPARATOR
      The default separator to use if none is supplied to the constructor.
      static boolean DEFAULT_STRICT_QUOTES
      The default strict quote behavior to use if none is supplied to the constructor
      static int INITIAL_READ_SIZE  
      static char NULL_CHARACTER
      This is the "null" character - if a value is set to this then it is ignored.
    • Constructor Summary

      Constructors 
      Constructor Description
      CSVParser()
      Constructs CSVParser using a comma for the separator.
      CSVParser​(char separator)
      Constructs CSVParser with supplied separator.
      CSVParser​(char separator, char quotechar)
      Constructs CSVParser with supplied separator and quote char.
      CSVParser​(char separator, char quotechar, char escape)
      Constructs CSVReader with supplied separator and quote char.
      CSVParser​(char separator, char quotechar, char escape, boolean strictQuotes)
      Constructs CSVReader with supplied separator and quote char.
      CSVParser​(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace)
      Constructs CSVReader with supplied separator and quote char.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean isAllWhiteSpace​(java.lang.CharSequence sb)
      precondition: sb.length() > 0
      protected boolean isNextCharacterEscapable​(java.lang.String nextLine, boolean inQuotes, int i)
      precondition: the current character is an escape
      boolean isPending()  
      java.lang.String[] parseLine​(java.lang.String nextLine)  
      java.lang.String[] parseLineMulti​(java.lang.String nextLine)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_SEPARATOR

        public static final char DEFAULT_SEPARATOR
        The default separator to use if none is supplied to the constructor.
        See Also:
        Constant Field Values
      • DEFAULT_QUOTE_CHARACTER

        public static final char DEFAULT_QUOTE_CHARACTER
        The default quote character to use if none is supplied to the constructor.
        See Also:
        Constant Field Values
      • DEFAULT_ESCAPE_CHARACTER

        public static final char DEFAULT_ESCAPE_CHARACTER
        The default escape character to use if none is supplied to the constructor.
        See Also:
        Constant Field Values
      • DEFAULT_STRICT_QUOTES

        public static final boolean DEFAULT_STRICT_QUOTES
        The default strict quote behavior to use if none is supplied to the constructor
        See Also:
        Constant Field Values
      • DEFAULT_IGNORE_LEADING_WHITESPACE

        public static final boolean DEFAULT_IGNORE_LEADING_WHITESPACE
        The default leading whitespace behavior to use if none is supplied to the constructor
        See Also:
        Constant Field Values
      • NULL_CHARACTER

        public static final char NULL_CHARACTER
        This is the "null" character - if a value is set to this then it is ignored. I.E. if the quote character is set to null then there is no quote character.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CSVParser

        public CSVParser()
        Constructs CSVParser using a comma for the separator.
      • CSVParser

        public CSVParser​(char separator)
        Constructs CSVParser with supplied separator.
        Parameters:
        separator - the delimiter to use for separating entries.
      • CSVParser

        public CSVParser​(char separator,
                         char quotechar)
        Constructs CSVParser with supplied separator and quote char.
        Parameters:
        separator - the delimiter to use for separating entries
        quotechar - the character to use for quoted elements
      • CSVParser

        public CSVParser​(char separator,
                         char quotechar,
                         char escape)
        Constructs CSVReader with supplied separator and quote char.
        Parameters:
        separator - the delimiter to use for separating entries
        quotechar - the character to use for quoted elements
        escape - the character to use for escaping a separator or quote
      • CSVParser

        public CSVParser​(char separator,
                         char quotechar,
                         char escape,
                         boolean strictQuotes)
        Constructs CSVReader with supplied separator and quote char. Allows setting the "strict quotes" flag
        Parameters:
        separator - the delimiter to use for separating entries
        quotechar - the character to use for quoted elements
        escape - the character to use for escaping a separator or quote
        strictQuotes - if true, characters outside the quotes are ignored
      • CSVParser

        public CSVParser​(char separator,
                         char quotechar,
                         char escape,
                         boolean strictQuotes,
                         boolean ignoreLeadingWhiteSpace)
        Constructs CSVReader with supplied separator and quote char. Allows setting the "strict quotes" and "ignore leading whitespace" flags
        Parameters:
        separator - the delimiter to use for separating entries
        quotechar - the character to use for quoted elements
        escape - the character to use for escaping a separator or quote
        strictQuotes - if true, characters outside the quotes are ignored
        ignoreLeadingWhiteSpace - if true, white space in front of a quote in a field is ignored
    • Method Detail

      • isPending

        public boolean isPending()
        Returns:
        true if something was left over from last call(s)
      • parseLineMulti

        public java.lang.String[] parseLineMulti​(java.lang.String nextLine)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • parseLine

        public java.lang.String[] parseLine​(java.lang.String nextLine)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • isNextCharacterEscapable

        protected boolean isNextCharacterEscapable​(java.lang.String nextLine,
                                                   boolean inQuotes,
                                                   int i)
        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
      • isAllWhiteSpace

        protected boolean isAllWhiteSpace​(java.lang.CharSequence sb)
        precondition: sb.length() > 0
        Parameters:
        sb - A sequence of characters to examine
        Returns:
        true if every character in the sequence is whitespace