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.
EMPTY_STRINGBUILDER, nullFieldIndicator, pending, quotechar, quotecharAsString, quoteDoubledAsString, quoteMatcherPattern, separator, separatorAsString, SPECIAL_REGEX_CHARS
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 and Description |
---|
CSVParser()
Constructs CSVParser using default values for everything.
|
Modifier and Type | Method and Description |
---|---|
protected String |
convertToCsvValue(String value,
boolean applyQuotestoAll)
Used when reverse parsing an array of strings to a single string.
|
char |
getEscape() |
boolean |
isIgnoreLeadingWhiteSpace() |
boolean |
isIgnoreQuotations() |
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.
|
boolean |
isStrictQuotes() |
protected String[] |
parseLine(String nextLine,
boolean multi)
Parses an incoming
String and returns an array of elements. |
void |
setErrorLocale(Locale errorLocale)
Sets the locale for all error messages.
|
convertToCsvValue, getPendingText, getQuotechar, getQuotecharAsString, getSeparator, getSeparatorAsString, isPending, isSurroundWithQuotes, nullFieldIndicator, parseLine, parseLineMulti, parseToLine, parseToLine
public CSVParser()
public char getEscape()
public boolean isStrictQuotes()
public boolean isIgnoreLeadingWhiteSpace()
public boolean isIgnoreQuotations()
protected String convertToCsvValue(String value, boolean applyQuotestoAll)
AbstractCSVParser
convertToCsvValue
in class AbstractCSVParser
value
- String to be convertedapplyQuotestoAll
- All values should be surrounded with quotesprotected String[] parseLine(String nextLine, boolean multi) throws IOException
AbstractCSVParser
String
and returns an array of elements.parseLine
in class AbstractCSVParser
nextLine
- The string to parsemulti
- Whether it takes multiple lines to form a single recordnull
if nextLine
is null
IOException
- If bad things happen during the readprotected boolean isNextCharacterEscapable(String nextLine, boolean inQuotes, int i)
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.nextLine
- The current lineinQuotes
- True if the current context is quotedi
- Current index in linepublic void setErrorLocale(Locale errorLocale)
ICSVParser
errorLocale
- Locale for error messages. If null, the default locale
is used.Copyright © 2005–2023. All rights reserved.