Package com.opencsv

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 is 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 Detail

      • CSVParser

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

      • 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 java.lang.String convertToCsvValue​(java.lang.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 tested
        applyQuotestoAll - All values should be surrounded with quotes
        Returns:
        String that will go into the CSV string
      • parseLine

        protected java.lang.String[] parseLine​(java.lang.String nextLine,
                                               boolean multi)
                                        throws java.io.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:
        java.io.IOException - If bad things happen during the read
      • isNextCharacterEscapable

        protected boolean isNextCharacterEscapable​(java.lang.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 either a quotation character or the escape char and you are inside 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​(java.util.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.