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 all standard csv data. It may be less configurable, but that is not needed for data matching the RFC4180 requirements.
| Modifier and Type | Class and Description |
|---|---|
| private static class | CSVParser.
This class serves to optimize |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private Locale | errorLocale
Locale for all translations. |
| private final char | escape
This is the character that the CSVParser will treat as the escape character. |
| private final String | escapeAsString
String of escape character - optimization for replaceAll |
| private final String | escapeDoubleAsString
String escapeAsString+escapeAsString - optimization for replaceAll |
| private final boolean | ignoreLeadingWhiteSpace
Ignore any leading white space at the start of the field. |
| private final boolean | ignoreQuotations
Skip over quotation characters when parsing. |
| private boolean | |
| private final boolean | strictQuotes
Determines if the field is between quotes (true) or between separators (false). |
| private int |
| Access | Constructor and Description |
|---|---|
| public | |
| protected | CSVParser(char
The delimiter to use for separating entries separator, char The character to use for quoted elements quotechar, char The character to use for escaping a separator or quote escape, boolean If true, characters outside the quotes are ignored strictQuotes, boolean If true, white space in front of a quote in a field is ignored ignoreLeadingWhiteSpace, boolean If true, treat quotations like any other character. ignoreQuotations, CSVReaderNullFieldIndicator Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES,
BOTH, NEITHER (default) nullFieldIndicator, Locale Locale for error messages. errorLocale)Constructs CSVParser. |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | Returns: True if any two of the three are the same.The defined separator character separator, char The defined quotation cahracter quotechar, char The defined escape character escape)Checks to see if any two of the three characters are the same. |
| private String | |
| protected String | convertToCsvValue(String value, boolean applyQuotestoAll)
Implements abstract com. Used when reverse parsing an array of strings to a single string. |
| public char | |
| private void | |
| private void | |
| private boolean | Returns: True if we should process as if we are inside quotes.Are we currently in quotes? inQuotes)Determines if we can process as if we were in quotes. |
| private boolean | Returns: True if the character could be escapable.Source character c)Checks to see if the character passed in could be escapable. |
| private boolean | Returns: True if the character is the defined escape characterSource character c)Checks to see if the character is the defined escape character. |
| private boolean | Returns: True if c is the defined quotation characterSource character c)Checks to see if the passed in character is the defined quotation character. |
| private boolean | Returns: True if the character is the defined separatorSource character c)Checks to see if the character is the defined separator. |
| public boolean | |
| public boolean | |
| protected boolean | Returns: True if the following character is a quoteThe current line nextLine, boolean True if the current context is quoted inQuotes, int Current index in line i)Checks to see if the character after the current index in a String is an escapable character. |
| private boolean | Returns: True if the following character is a quoteThe current line nextLine, boolean True if the current context is quoted inQuotes, int Current index in line i)Checks to see if the character after the index is a quotation character. |
| private boolean | Returns: True if both characters are the same and are not the defined NULL_CHARACTERFirst character c1, char Second character c2)Checks that the two characters are the same and are not the defined NULL_CHARACTER. |
| public boolean | |
| protected String[] | parseLine(String nextLine, boolean multi)
Implements abstract com. Parses an incoming |
| public void | setErrorLocale(Locale errorLocale)
Implements com. Sets the locale for all error messages. |
| private boolean |
| BEGINNING_OF_LINE | back to summary |
|---|---|
| private static final int BEGINNING_OF_LINE | |
| errorLocale | back to summary |
|---|---|
| private Locale errorLocale Locale for all translations. | |
| escape | back to summary |
|---|---|
| private final char escape This is the character that the CSVParser will treat as the escape character. | |
| escapeAsString | back to summary |
|---|---|
| private final String escapeAsString String of escape character - optimization for replaceAll | |
| escapeDoubleAsString | back to summary |
|---|---|
| private final String escapeDoubleAsString String escapeAsString+escapeAsString - optimization for replaceAll | |
| ignoreLeadingWhiteSpace | back to summary |
|---|---|
| private final boolean ignoreLeadingWhiteSpace Ignore any leading white space at the start of the field. | |
| ignoreQuotations | back to summary |
|---|---|
| private final boolean ignoreQuotations Skip over quotation characters when parsing. | |
| inField | back to summary |
|---|---|
| private boolean inField | |
| strictQuotes | back to summary |
|---|---|
| private final boolean strictQuotes Determines if the field is between quotes (true) or between separators (false). | |
| tokensOnLastCompleteLine | back to summary |
|---|---|
| private int tokensOnLastCompleteLine | |
| CSVParser | back to summary |
|---|---|
| public CSVParser() Constructs CSVParser using default values for everything. | |
| CSVParser | back to summary |
|---|---|
| protected CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace, boolean ignoreQuotations, CSVReaderNullFieldIndicator nullFieldIndicator, Locale errorLocale) Constructs CSVParser. This constructor sets all necessary parameters for CSVParser, and intentionally has package access so only the builder can use it.
| |
| anyCharactersAreTheSame | back to summary |
|---|---|
| private boolean anyCharactersAreTheSame(char separator, char quotechar, char escape) Checks to see if any two of the three characters are the same. This is because in opencsv the separator, quote, and escape characters must the different.
| |
| convertEmptyToNullIfNeeded | back to summary |
|---|---|
| private String convertEmptyToNullIfNeeded(String s, boolean fromQuotedField) | |
| convertToCsvValue | back to summary |
|---|---|
| protected String convertToCsvValue(String value, boolean applyQuotestoAll) Implements abstract com. Doc from com. 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.
| |
| getEscape | back to summary |
|---|---|
public char getEscape()
| |
| handleEscapeCharacter | back to summary |
|---|---|
| private void handleEscapeCharacter(String nextLine, CSVParser. | |
| handleQuoteCharButNotStrictQuotes | back to summary |
|---|---|
| private void handleQuoteCharButNotStrictQuotes(String nextLine, CSVParser. | |
| inQuotes | back to summary |
|---|---|
| private boolean inQuotes(boolean inQuotes) Determines if we can process as if we were in quotes.
| |
| isCharacterEscapable | back to summary |
|---|---|
| private boolean isCharacterEscapable(char c) Checks to see if the character passed in could be escapable. Escapable characters for opencsv are the quotation character, the escape character, and the separator.
| |
| isCharacterEscapeCharacter | back to summary |
|---|---|
| private boolean isCharacterEscapeCharacter(char c) Checks to see if the character is the defined escape character.
| |
| isCharacterQuoteCharacter | back to summary |
|---|---|
| private boolean isCharacterQuoteCharacter(char c) Checks to see if the passed in character is the defined quotation character.
| |
| isCharacterSeparator | back to summary |
|---|---|
| private boolean isCharacterSeparator(char c) Checks to see if the character is the defined separator.
| |
| isIgnoreLeadingWhiteSpace | back to summary |
|---|---|
public boolean isIgnoreLeadingWhiteSpace()
| |
| isIgnoreQuotations | back to summary |
|---|---|
public boolean isIgnoreQuotations()
| |
| isNextCharacterEscapable | back to summary |
|---|---|
| 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.
| |
| isNextCharacterEscapedQuote | back to summary |
|---|---|
| private boolean isNextCharacterEscapedQuote(String nextLine, boolean inQuotes, int i) Checks to see if the character after the index is a quotation character. Precondition the current character is a quote or an escape.
| |
| isSameCharacter | back to summary |
|---|---|
| private boolean isSameCharacter(char c1, char c2) Checks that the two characters are the same and are not the defined NULL_CHARACTER.
| |
| isStrictQuotes | back to summary |
|---|---|
public boolean isStrictQuotes()
| |
| parseLine | back to summary |
|---|---|
| protected String[] parseLine(String nextLine, boolean multi) throws IOException Implements abstract com. Doc from com. Parses an incoming
| |
| setErrorLocale | back to summary |
|---|---|
| public void setErrorLocale(Locale errorLocale) Implements com. Doc from com. Sets the locale for all error messages.
| |
| shouldConvertEmptyToNull | back to summary |
|---|---|
| private boolean shouldConvertEmptyToNull(boolean fromQuotedField) | |
CSVParser#parseLine(java.lang.String) ,
which is the hot inner loop of opencsv.
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private final String | |
| private int | |
| private int | |
| private StringBuilder |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| public void | clearOutput()
Clears the current output buffer and resets the indices used to track substrings of the input. |
| public boolean | |
| public boolean | Returns: true if the output is empty, false otherwise.Determines whether the current output is empty. |
| private StringBuilder | |
| public String | Returns: The current output. If no output has been accumulated, returns a substring of the input between the indices specified by the internal state or an empty string if no such substring exists.Retrieves the current accumulated output as a string without modifying or clearing the underlying buffers or state. |
| public char | |
| public String | Returns: The current output as a string before it is cleared. If there is no output, an empty string is returned.Retrieves the current output and clears it. |
| i | back to summary |
|---|---|
| private int i | |
| input | back to summary |
|---|---|
| private final String input | |
| pendingSubstrFrom | back to summary |
|---|---|
| private int pendingSubstrFrom | |
| pendingSubstrTo | back to summary |
|---|---|
| private int pendingSubstrTo | |
| sb | back to summary |
|---|---|
| private StringBuilder sb | |
| StringFragmentCopier | back to summary |
|---|---|
| pack-priv StringFragmentCopier(String input) | |
| append | back to summary |
|---|---|
| public void append(String pending) | |
| append | back to summary |
|---|---|
| public void append(char pending) | |
| appendPrev | back to summary |
|---|---|
| public void appendPrev() | |
| clearOutput | back to summary |
|---|---|
| public void clearOutput() Clears the current output buffer and resets the indices used to track substrings of the input. Specifically, this method sets the internal StringBuilder's length to zero, effectively clearing its content. Additionally, it resets the range of pending substring indices (pendingSubstrFrom and pendingSubstrTo) to the current position (i) in the input. | |
| isEmptyInput | back to summary |
|---|---|
| public boolean isEmptyInput() | |
| isEmptyOutput | back to summary |
|---|---|
| public boolean isEmptyOutput() Determines whether the current output is empty. The output is considered empty if the pending substring indices indicate there is no substring to process (pendingSubstrFrom >= pendingSubstrTo), and the StringBuilder object (sb) is either null or has a length of zero.
| |
| materializeBuilder | back to summary |
|---|---|
| private StringBuilder materializeBuilder() | |
| peekOutput | back to summary |
|---|---|
| public String peekOutput() Retrieves the current accumulated output as a string without modifying or clearing the underlying buffers or state.
| |
| takeInput | back to summary |
|---|---|
| public char takeInput() | |
| takeOutput | back to summary |
|---|---|
| public String takeOutput() Retrieves the current output and clears it. This method combines the operations of peeking at the current output and resetting any internal buffers or indexes to prepare for new content while ensuring the current output is returned.
| |