Package com.helger.commons.csv
Class CSVParser
java.lang.Object
com.helger.commons.csv.CSVParser
A very simple CSV parser released under a commercial-friendly license. This
just implements splitting a single line into fields.
- Author:
- Glen Smith, Rainer Pruy, Philip Helger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncharcharcharprotected booleanChecks if every element is the character sequence is whitespace.booleanbooleanprotected booleanisNextCharacterEscapable(String sNextLine, boolean bInQuotes, int nIndex) Checks to see if the character after the current index in a String is an escapable character.booleanbooleanParses an incoming String and returns an array of elements.parseLineMulti(String sNextLine) Parses an incoming String and returns an array of elements.setEscapeChar(char cEscapeChar) Sets the character to use for escaping a separator or quote.setIgnoreLeadingWhiteSpace(boolean bIgnoreLeadingWhiteSpace) Sets the ignore leading whitespace setting - if true, white space in front of a quote in a field is ignored.setIgnoreQuotations(boolean bIgnoreQuotations) Sets the ignore quotations mode - iftrue, quotations are ignored.setQuoteChar(char cQuoteChar) Sets the character to use for quoted elements.setSeparatorChar(char cSeparator) Sets the delimiter to use for separating entries.setStrictQuotes(boolean bStrictQuotes) Sets the strict quotes setting - if true, characters outside the quotes are ignored.
-
Constructor Details
-
CSVParser
public CSVParser()Constructs CSVParser using a comma for the separator.
-
-
Method Details
-
getSeparatorChar
public char getSeparatorChar()- Returns:
- The default separator for this parser.
-
setSeparatorChar
Sets the delimiter to use for separating entries.- Parameters:
cSeparator- the delimiter to use for separating entries- Returns:
- this
-
getQuoteChar
public char getQuoteChar()- Returns:
- The default quotation character for this parser.
-
setQuoteChar
Sets the character to use for quoted elements.- Parameters:
cQuoteChar- the character to use for quoted element.- Returns:
- this
-
getEscapeChar
public char getEscapeChar()- Returns:
- The default escape character for this parser.
-
setEscapeChar
Sets the character to use for escaping a separator or quote.- Parameters:
cEscapeChar- the character to use for escaping a separator or quote.- Returns:
- this
-
isStrictQuotes
public boolean isStrictQuotes()- Returns:
- The default strictQuotes setting for this parser.
-
setStrictQuotes
Sets the strict quotes setting - if true, characters outside the quotes are ignored.- Parameters:
bStrictQuotes- iftrue, characters outside the quotes are ignored- Returns:
- this
-
isIgnoreLeadingWhiteSpace
public boolean isIgnoreLeadingWhiteSpace()- Returns:
- The default ignoreLeadingWhiteSpace setting for this parser.
-
setIgnoreLeadingWhiteSpace
Sets the ignore leading whitespace setting - if true, white space in front of a quote in a field is ignored.- Parameters:
bIgnoreLeadingWhiteSpace- iftrue, white space in front of a quote in a field is ignored- Returns:
- this
-
isIgnoreQuotations
public boolean isIgnoreQuotations()- Returns:
- the default ignoreQuotation setting for this parser.
-
setIgnoreQuotations
Sets the ignore quotations mode - iftrue, quotations are ignored.- Parameters:
bIgnoreQuotations- iftrue, quotations are ignored- Returns:
- this
-
isPending
public boolean isPending()- Returns:
- true if something was left over from last call(s)
-
parseLineMulti
Parses an incoming String and returns an array of elements. This method is used when the data spans multiple lines.- Parameters:
sNextLine- current line to be processed- Returns:
- the tokenized list of elements, or
nullif nextLine isnull - Throws:
IOException- if bad things happen during the read
-
parseLine
Parses an incoming String and returns an array of elements. This method is used when all data is contained in a single line.- Parameters:
sNextLine- Line to be parsed.- Returns:
- the tokenized list of elements, or
nullif nextLine isnull - Throws:
IOException- if bad things happen during the read
-
isNextCharacterEscapable
protected boolean isNextCharacterEscapable(@Nonnull String sNextLine, boolean bInQuotes, int nIndex) 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:
sNextLine- the current linebInQuotes- true if the current context is quotednIndex- current index in line- Returns:
trueif the following character is a quote
-
isAllWhiteSpace
Checks if every element is the character sequence is whitespace. precondition: sb.length() is greater than 0- Parameters:
sb- A sequence of characters to examine- Returns:
trueif every character in the sequence is whitespace
-