Top Description Fields Constructors Methods
org.jooq.tools.csv

public Class CSVParser

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.ArrayList, .List

A very simple CSV parser released under a commercial-friendly license. This just implements splitting a single line into fields.
Authors
Glen Smith, Rainer Pruy

Field Summary

Modifier and TypeField and Description
public static final char
DEFAULT_ESCAPE_CHARACTER

The default escape character to use if none is supplied to the constructor.

public static final boolean
DEFAULT_IGNORE_LEADING_WHITESPACE

The default leading whitespace behavior to use if none is supplied to the constructor

public static final char
DEFAULT_QUOTE_CHARACTER

The default quote character to use if none is supplied to the constructor.

public static final char
DEFAULT_SEPARATOR

The default separator to use if none is supplied to the constructor.

public static final boolean
DEFAULT_STRICT_QUOTES

The default strict quote behavior to use if none is supplied to the constructor

private final char
private final boolean
private boolean
public static final int
public static final char
NULL_CHARACTER

This is the "null" character - if a value is set to this then it is ignored.

private String
private final char
private final char
private final boolean

Constructor Summary

AccessConstructor and Description
public
CSVParser()

Constructs CSVParser using a comma for the separator.

public
CSVParser(char
the delimiter to use for separating entries.
separator
)

Constructs CSVParser with supplied separator.

public
CSVParser(char
the delimiter to use for separating entries
separator
,
char
the character to use for quoted elements
quotechar
)

Constructs CSVParser with supplied separator and quote char.

public
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
)

Constructs CSVReader with supplied separator and quote char.

public
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
)

Constructs CSVReader with supplied separator and quote char.

public
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
)

Constructs CSVReader with supplied separator and quote char.

Method Summary

Modifier and TypeMethod and Description
private static boolean
anyCharactersAreTheSame(char separator, char quotechar, char escape)

protected boolean

Returns:

true if every character in the sequence is whitespace
isAllWhiteSpace
(CharSequence
A sequence of characters to examine
sb
)

precondition: sb.length() > 0

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
)

precondition: the current character is an escape

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
)

precondition: the current character is a quote or an escape

public boolean

Returns:

true if something was left over from last call(s)
isPending
()

private static boolean
isSameCharacter(char c1, char c2)

public String[]
parseLine(String nextLine)

private String[]

Returns:

the comma-tokenized list of elements, or null if nextLine is null
parseLine
(String
the string to parse
nextLine
,
boolean multi)

Parses an incoming String and returns an array of elements.

public String[]
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_ESCAPE_CHARACTERback to summary
public static final char DEFAULT_ESCAPE_CHARACTER

The default escape character to use if none is supplied to the constructor.

DEFAULT_IGNORE_LEADING_WHITESPACEback to summary
public static final boolean DEFAULT_IGNORE_LEADING_WHITESPACE

The default leading whitespace behavior to use if none is supplied to the constructor

DEFAULT_QUOTE_CHARACTERback to summary
public static final char DEFAULT_QUOTE_CHARACTER

The default quote character to use if none is supplied to the constructor.

DEFAULT_SEPARATORback to summary
public static final char DEFAULT_SEPARATOR

The default separator to use if none is supplied to the constructor.

DEFAULT_STRICT_QUOTESback to summary
public static final boolean DEFAULT_STRICT_QUOTES

The default strict quote behavior to use if none is supplied to the constructor

escapeback to summary
private final char escape
ignoreLeadingWhiteSpaceback to summary
private final boolean ignoreLeadingWhiteSpace
inFieldback to summary
private boolean inField
INITIAL_READ_SIZEback to summary
public static final int INITIAL_READ_SIZE
NULL_CHARACTERback to summary
public static final char NULL_CHARACTER

This is the "null" character - if a value is set to this then it is ignored. I.E. if the quote character is set to null then there is no quote character.

pendingback to summary
private String pending
quotecharback to summary
private final char quotechar
separatorback to summary
private final char separator
strictQuotesback to summary
private final boolean strictQuotes

Constructor Detail

CSVParserback to summary
public CSVParser()

Constructs CSVParser using a comma for the separator.

CSVParserback to summary
public CSVParser(char separator)

Constructs CSVParser with supplied separator.

Parameters
separator:char

the delimiter to use for separating entries.

CSVParserback to summary
public CSVParser(char separator, char quotechar)

Constructs CSVParser with supplied separator and quote char.

Parameters
separator:char

the delimiter to use for separating entries

quotechar:char

the character to use for quoted elements

CSVParserback to summary
public CSVParser(char separator, char quotechar, char escape)

Constructs CSVReader with supplied separator and quote char.

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

CSVParserback to summary
public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes)

Constructs CSVReader with supplied separator and quote char. Allows setting the "strict quotes" flag

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

CSVParserback to summary
public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace)

Constructs CSVReader with supplied separator and quote char. Allows setting the "strict quotes" and "ignore leading whitespace" flags

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

Method Detail

anyCharactersAreTheSameback to summary
private static boolean anyCharactersAreTheSame(char separator, char quotechar, char escape)
isAllWhiteSpaceback to summary
protected boolean isAllWhiteSpace(CharSequence sb)

precondition: sb.length() > 0

Parameters
sb:CharSequence

A sequence of characters to examine

Returns:boolean

true if every character in the sequence is whitespace

isNextCharacterEscapableback to summary
protected boolean isNextCharacterEscapable(String nextLine, boolean inQuotes, int i)

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)

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

isPendingback to summary
public boolean isPending()
Returns:boolean

true if something was left over from last call(s)

isSameCharacterback to summary
private static boolean isSameCharacter(char c1, char c2)
parseLineback to summary
public String[] parseLine(String nextLine) throws IOException
parseLineback to summary
private String[] parseLine(String nextLine, boolean multi) throws IOException

Parses an incoming String and returns an array of elements.

Parameters
nextLine:String

the string to parse

Returns:String[]

the comma-tokenized list of elements, or null if nextLine is null

Exceptions
IOException:
if bad things happen during the read
parseLineMultiback to summary
public String[] parseLineMulti(String nextLine) throws IOException