Top Description Inners Fields Constructors Methods
com.opencsv

public Class CSVParser

extends AbstractCSVParser
Class Inheritance
Imports
com.opencsv.enums.CSVReaderNullFieldIndicator, org.apache.commons.lang3.ArrayUtils, .ObjectUtils, .StringUtils, java.io.IOException, java.util.ArrayList, .List, .Locale, .ResourceBundle

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.

Authors
Glen Smith, Rainer Pruy

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CSVParser.StringFragmentCopier

This class serves to optimize CSVParser#parseLine(java.lang.String), which is the hot inner loop of opencsv.

Field Summary

Modifier and TypeField 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
Inherited from com.opencsv.AbstractCSVParser:
nullFieldIndicatorpendingquotecharseparator

Constructor Summary

AccessConstructor and Description
public
CSVParser()

Constructs CSVParser using default values for everything.

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.

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

True if any two of the three are the same.
anyCharactersAreTheSame
(char
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
convertEmptyToNullIfNeeded(String s, boolean fromQuotedField)

protected String
convertToCsvValue(String value, boolean applyQuotestoAll)

Implements abstract com.opencsv.AbstractCSVParser.convertToCsvValue.

Used when reverse parsing an array of strings to a single string.

public char
getEscape()

Returns: The default escape character for this parser.

private void
private void
private boolean

Returns:

True if we should process as if we are inside quotes.
inQuotes
(boolean
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.
isCharacterEscapable
(char
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 character
isCharacterEscapeCharacter
(char
Source character
c
)

Checks to see if the character is the defined escape character.

private boolean

Returns:

True if c is the defined quotation character
isCharacterQuoteCharacter
(char
Source 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 separator
isCharacterSeparator
(char
Source character
c
)

Checks to see if the character is the defined separator.

public boolean
isIgnoreLeadingWhiteSpace()

Returns: The default ignoreLeadingWhiteSpace setting for this parser.

public boolean
isIgnoreQuotations()

Returns: The default ignoreQuotation setting for this parser.

protected boolean

Returns:

True if the following character is a quote
isNextCharacterEscapable
(String
The 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 quote
isNextCharacterEscapedQuote
(String
The 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_CHARACTER
isSameCharacter
(char
First character
c1
,
char
Second character
c2
)

Checks that the two characters are the same and are not the defined NULL_CHARACTER.

public boolean
isStrictQuotes()

Returns: The default strictQuotes setting for this parser.

protected String[]
parseLine(String nextLine, boolean multi)

Implements abstract com.opencsv.AbstractCSVParser.parseLine.

Parses an incoming java.lang.String and returns an array of elements.

public void
setErrorLocale(Locale errorLocale)

Implements com.opencsv.ICSVParser.setErrorLocale.

Sets the locale for all error messages.

private boolean
shouldConvertEmptyToNull(boolean fromQuotedField)

Inherited from com.opencsv.AbstractCSVParser:
convertToCsvValuegetPendingTextgetQuotechargetQuotecharAsStringgetQuoteDoubledAsStringgetQuoteMatcherPatterngetSeparatorgetSeparatorAsStringisPendingisSurroundWithQuotesnullFieldIndicatorparseLineparseLineMultiparseToLineparseToLine

Field Detail

BEGINNING_OF_LINEback to summary
private static final int BEGINNING_OF_LINE
errorLocaleback to summary
private Locale errorLocale

Locale for all translations.

escapeback to summary
private final char escape

This is the character that the CSVParser will treat as the escape character.

escapeAsStringback to summary
private final String escapeAsString

String of escape character - optimization for replaceAll

escapeDoubleAsStringback to summary
private final String escapeDoubleAsString

String escapeAsString+escapeAsString - optimization for replaceAll

ignoreLeadingWhiteSpaceback to summary
private final boolean ignoreLeadingWhiteSpace

Ignore any leading white space at the start of the field.

ignoreQuotationsback to summary
private final boolean ignoreQuotations

Skip over quotation characters when parsing.

inFieldback to summary
private boolean inField
strictQuotesback to summary
private final boolean strictQuotes

Determines if the field is between quotes (true) or between separators (false).

tokensOnLastCompleteLineback to summary
private int tokensOnLastCompleteLine

Constructor Detail

CSVParserback to summary
public CSVParser()

Constructs CSVParser using default values for everything.

CSVParserback 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.

Parameters
separator:char

The delimiter to use for separating entries

quotechar:char

The character to use for quoted elements

escape:char

The character to use for escaping a separator or quote

strictQuotes:boolean

If true, characters outside the quotes are ignored

ignoreLeadingWhiteSpace:boolean

If true, white space in front of a quote in a field is ignored

ignoreQuotations:boolean

If true, treat quotations like any other character.

nullFieldIndicator:CSVReaderNullFieldIndicator

Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)

errorLocale:Locale

Locale for error messages.

Method Detail

anyCharactersAreTheSameback 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.

Parameters
separator:char

The defined separator character

quotechar:char

The defined quotation cahracter

escape:char

The defined escape character

Returns:boolean

True if any two of the three are the same.

convertEmptyToNullIfNeededback to summary
private String convertEmptyToNullIfNeeded(String s, boolean fromQuotedField)
convertToCsvValueback to summary
protected String convertToCsvValue(String value, boolean applyQuotestoAll)

Implements abstract com.opencsv.AbstractCSVParser.convertToCsvValue.

Doc from com.opencsv.AbstractCSVParser.convertToCsvValue.

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.

Annotations
@Override
getEscapeback to summary
public char getEscape()
Returns:char

The default escape character for this parser.

handleEscapeCharacterback to summary
private void handleEscapeCharacter(String nextLine, CSVParser.StringFragmentCopier sfc, boolean inQuotes)
handleQuoteCharButNotStrictQuotesback to summary
private void handleQuoteCharButNotStrictQuotes(String nextLine, CSVParser.StringFragmentCopier sfc)
inQuotesback to summary
private boolean inQuotes(boolean inQuotes)

Determines if we can process as if we were in quotes.

Parameters
inQuotes:boolean

Are we currently in quotes?

Returns:boolean

True if we should process as if we are inside quotes.

isCharacterEscapableback 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.

Parameters
c:char

Source character

Returns:boolean

True if the character could be escapable.

isCharacterEscapeCharacterback to summary
private boolean isCharacterEscapeCharacter(char c)

Checks to see if the character is the defined escape character.

Parameters
c:char

Source character

Returns:boolean

True if the character is the defined escape character

isCharacterQuoteCharacterback to summary
private boolean isCharacterQuoteCharacter(char c)

Checks to see if the passed in character is the defined quotation character.

Parameters
c:char

Source character

Returns:boolean

True if c is the defined quotation character

isCharacterSeparatorback to summary
private boolean isCharacterSeparator(char c)

Checks to see if the character is the defined separator.

Parameters
c:char

Source character

Returns:boolean

True if the character is the defined separator

isIgnoreLeadingWhiteSpaceback to summary
public boolean isIgnoreLeadingWhiteSpace()
Returns:boolean

The default ignoreLeadingWhiteSpace setting for this parser.

isIgnoreQuotationsback to summary
public boolean isIgnoreQuotations()
Returns:boolean

The default ignoreQuotation setting for this parser.

isNextCharacterEscapableback 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.

Parameters
nextLine:String

The current line

inQuotes:boolean

True if the current context is quoted

i:int

Current index in line

Returns:boolean

True if the following character is a quote

isNextCharacterEscapedQuoteback 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.

Parameters
nextLine:String

The current line

inQuotes:boolean

True if the current context is quoted

i:int

Current index in line

Returns:boolean

True if the following character is a quote

isSameCharacterback to summary
private boolean isSameCharacter(char c1, char c2)

Checks that the two characters are the same and are not the defined NULL_CHARACTER.

Parameters
c1:char

First character

c2:char

Second character

Returns:boolean

True if both characters are the same and are not the defined NULL_CHARACTER

isStrictQuotesback to summary
public boolean isStrictQuotes()
Returns:boolean

The default strictQuotes setting for this parser.

parseLineback to summary
protected String[] parseLine(String nextLine, boolean multi) throws IOException

Implements abstract com.opencsv.AbstractCSVParser.parseLine.

Doc from com.opencsv.AbstractCSVParser.parseLine.

Parses an incoming java.lang.String and returns an array of elements.

Annotations
@Override
Exceptions
IOException:
If bad things happen during the read
setErrorLocaleback to summary
public void setErrorLocale(Locale errorLocale)

Implements com.opencsv.ICSVParser.setErrorLocale.

Doc from com.opencsv.ICSVParser.setErrorLocale.

Sets the locale for all error messages.

Annotations
@Override
shouldConvertEmptyToNullback to summary
private boolean shouldConvertEmptyToNull(boolean fromQuotedField)
com.opencsv back to summary

private Class CSVParser.StringFragmentCopier

extends Object
Class Inheritance

This class serves to optimize CSVParser#parseLine(java.lang.String), which is the hot inner loop of opencsv.

Field Summary

Modifier and TypeField and Description
private int
i

private final String
private int
private int
private StringBuilder

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
append(String pending)

public void
append(char pending)

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.
isEmptyOutput
()

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.
peekOutput
()

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.
takeOutput
()

Retrieves the current output and clears it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iback to summary
private int i
inputback to summary
private final String input
pendingSubstrFromback to summary
private int pendingSubstrFrom
pendingSubstrToback to summary
private int pendingSubstrTo
sbback to summary
private StringBuilder sb

Constructor Detail

StringFragmentCopierback to summary
pack-priv StringFragmentCopier(String input)

Method Detail

appendback to summary
public void append(String pending)
appendback to summary
public void append(char pending)
appendPrevback to summary
public void appendPrev()
clearOutputback 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.

isEmptyInputback to summary
public boolean isEmptyInput()
isEmptyOutputback 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.

Returns:boolean

true if the output is empty, false otherwise.

materializeBuilderback to summary
private StringBuilder materializeBuilder()
peekOutputback to summary
public String peekOutput()

Retrieves the current accumulated output as a string without modifying or clearing the underlying buffers or state.

Returns:String

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.

takeInputback to summary
public char takeInput()
takeOutputback 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.

Returns:String

The current output as a string before it is cleared. If there is no output, an empty string is returned.