Class CSVParser
- All Implemented Interfaces:
ICSVParser
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
-
Field Summary
Fields inherited from class AbstractCSVParser
EMPTY_STRINGBUILDER, nullFieldIndicator, pending, quotechar, quotecharAsString, quoteDoubledAsString, quoteMatcherPattern, separator, separatorAsString, SPECIAL_REGEX_CHARSFields inherited from interface ICSVParser
DEFAULT_BUNDLE_NAME, DEFAULT_ESCAPE_CHARACTER, DEFAULT_IGNORE_LEADING_WHITESPACE, DEFAULT_IGNORE_QUOTATIONS, DEFAULT_NULL_FIELD_INDICATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_SEPARATOR, DEFAULT_STRICT_QUOTES, INITIAL_READ_SIZE, MAX_SIZE_FOR_EMPTY_FIELD, NEWLINE, NULL_CHARACTER, READ_BUFFER_SIZE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringconvertToCsvValue(String value, boolean applyQuotestoAll) Used when reverse parsing an array of strings to a single string.charbooleanbooleanprotected booleanisNextCharacterEscapable(String nextLine, boolean inQuotes, int i) Checks to see if the character after the current index in a String is an escapable character.booleanprotected String[]Parses an incomingStringand returns an array of elements.voidsetErrorLocale(Locale errorLocale) Sets the locale for all error messages.Methods inherited from class AbstractCSVParser
convertToCsvValue, getPendingText, getQuotechar, getQuotecharAsString, getSeparator, getSeparatorAsString, isPending, isSurroundWithQuotes, nullFieldIndicator, parseLine, parseLineMulti, parseToLine, parseToLine
-
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
Description copied from class:AbstractCSVParserUsed 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:
convertToCsvValuein classAbstractCSVParser- Parameters:
value- String to be convertedapplyQuotestoAll- All values should be surrounded with quotes- Returns:
- String that will go into the CSV string
-
parseLine
Description copied from class:AbstractCSVParserParses an incomingStringand returns an array of elements.- Specified by:
parseLinein classAbstractCSVParser- Parameters:
nextLine- The string to parsemulti- Whether it takes multiple lines to form a single record- Returns:
- The list of elements, or
nullifnextLineisnull - Throws:
IOException- If bad things happen during the read
-
isNextCharacterEscapable
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 lineinQuotes- True if the current context is quotedi- Current index in line- Returns:
- True if the following character is a quote
-
setErrorLocale
Description copied from interface:ICSVParserSets the locale for all error messages.- Parameters:
errorLocale- Locale for error messages. If null, the default locale is used.
-