liquibase.util.csv.opencsv
Class CSVParser

java.lang.Object
  extended by liquibase.util.csv.opencsv.CSVParser

public class CSVParser
extends Object

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

Field Summary
static char DEFAULT_ESCAPE_CHARACTER
          The default escape character to use if none is supplied to the constructor.
static boolean DEFAULT_IGNORE_QUOTATIONS
          If the quote character is set to null then there is no quote character.
static CSVReaderNullFieldIndicator DEFAULT_NULL_FIELD_INDICATOR
          Denotes what field contents will cause the parser to return null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)
static char DEFAULT_QUOTE_CHARACTER
          The default quote character to use if none is supplied to the constructor.
static char DEFAULT_SEPARATOR
          The default separator to use if none is supplied to the constructor.
static boolean DEFAULT_STRICT_QUOTES
          The default strict quote behavior to use if none is supplied to the constructor.
static int INITIAL_READ_SIZE
          The average size of a line read by openCSV (used for setting the size of StringBuilders).
static char NULL_CHARACTER
          This is the "null" character - if a value is set to this then it is ignored.
static int READ_BUFFER_SIZE
          In most cases we know the size of the line we want to read.
 
Constructor Summary
CSVParser()
          Constructs CSVParser using a comma for the separator.
CSVParser(char separator)
          Constructs CSVParser with supplied separator.
CSVParser(char separator, char quotechar)
          Constructs CSVParser with supplied separator and quote char.
CSVParser(char separator, char quotechar, char escape)
          Constructs CSVReader with supplied separator and quote char.
CSVParser(char separator, char quotechar, char escape, boolean strictQuotes)
          Constructs CSVParser with supplied separator and quote char.
CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace)
          Constructs CSVParser with supplied separator and quote char.
CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace, boolean ignoreQuotations)
          Constructs CSVParser with supplied separator and quote char.
 
Method Summary
 char getEscape()
           
 char getQuotechar()
           
 char getSeparator()
           
protected  boolean isAllWhiteSpace(CharSequence sb)
          Checks if every element is the character sequence is whitespace.
 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 isPending()
           
 boolean isStrictQuotes()
           
 CSVReaderNullFieldIndicator nullFieldIndicator()
           
 String[] parseLine(String nextLine)
          Parses an incoming String and returns an array of elements.
protected  String[] parseLine(String nextLine, boolean multi)
          Parses an incoming String and returns an array of elements.
 String[] parseLineMulti(String nextLine)
          Parses an incoming String and returns an array of elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SEPARATOR

public static final char DEFAULT_SEPARATOR
The default separator to use if none is supplied to the constructor.

See Also:
Constant Field Values

INITIAL_READ_SIZE

public static final int INITIAL_READ_SIZE
The average size of a line read by openCSV (used for setting the size of StringBuilders).

See Also:
Constant Field Values

READ_BUFFER_SIZE

public static final int READ_BUFFER_SIZE
In most cases we know the size of the line we want to read. In that case we will set the initial read to that plus an buffer size.

See Also:
Constant Field Values

DEFAULT_QUOTE_CHARACTER

public static final char DEFAULT_QUOTE_CHARACTER
The default quote character to use if none is supplied to the constructor.

See Also:
Constant Field Values

DEFAULT_ESCAPE_CHARACTER

public static final char DEFAULT_ESCAPE_CHARACTER
The default escape character to use if none is supplied to the constructor.

See Also:
Constant Field Values

DEFAULT_STRICT_QUOTES

public static final boolean DEFAULT_STRICT_QUOTES
The default strict quote behavior to use if none is supplied to the constructor.

See Also:
Constant Field Values

DEFAULT_IGNORE_QUOTATIONS

public static final boolean DEFAULT_IGNORE_QUOTATIONS
If the quote character is set to null then there is no quote character.

See Also:
Constant Field Values

NULL_CHARACTER

public static final char NULL_CHARACTER
This is the "null" character - if a value is set to this then it is ignored.

See Also:
Constant Field Values

DEFAULT_NULL_FIELD_INDICATOR

public static final CSVReaderNullFieldIndicator DEFAULT_NULL_FIELD_INDICATOR
Denotes what field contents will cause the parser to return null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)

Constructor Detail

CSVParser

public CSVParser()
Constructs CSVParser using a comma for the separator.


CSVParser

public CSVParser(char separator)
Constructs CSVParser with supplied separator.

Parameters:
separator - the delimiter to use for separating entries.

CSVParser

public CSVParser(char separator,
                 char quotechar)
Constructs CSVParser with supplied separator and quote char.

Parameters:
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements

CSVParser

public CSVParser(char separator,
                 char quotechar,
                 char escape)
Constructs CSVReader with supplied separator and quote char.

Parameters:
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote

CSVParser

public CSVParser(char separator,
                 char quotechar,
                 char escape,
                 boolean strictQuotes)
Constructs CSVParser with supplied separator and quote char. Allows setting the "strict quotes" flag

Parameters:
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
strictQuotes - if true, characters outside the quotes are ignored

CSVParser

public CSVParser(char separator,
                 char quotechar,
                 char escape,
                 boolean strictQuotes,
                 boolean ignoreLeadingWhiteSpace)
Constructs CSVParser with supplied separator and quote char. Allows setting the "strict quotes" and "ignore leading whitespace" flags

Parameters:
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
strictQuotes - if true, characters outside the quotes are ignored
ignoreLeadingWhiteSpace - if true, white space in front of a quote in a field is ignored

CSVParser

public CSVParser(char separator,
                 char quotechar,
                 char escape,
                 boolean strictQuotes,
                 boolean ignoreLeadingWhiteSpace,
                 boolean ignoreQuotations)
Constructs CSVParser with supplied separator and quote char. Allows setting the "strict quotes" and "ignore leading whitespace" flags

Parameters:
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
strictQuotes - if true, characters outside the quotes are ignored
ignoreLeadingWhiteSpace - if true, white space in front of a quote in a field is ignored
ignoreQuotations - if true, treat quotations like any other character.
Method Detail

getSeparator

public char getSeparator()
Returns:
The default separator for this parser.

getQuotechar

public char getQuotechar()
Returns:
The default quotation character for this parser.

getEscape

public char getEscape()
Returns:
The default escape character for this parser.

isStrictQuotes

public boolean isStrictQuotes()
Returns:
The default strictQuotes setting for this parser.

isIgnoreLeadingWhiteSpace

public boolean isIgnoreLeadingWhiteSpace()
Returns:
The default ignoreLeadingWhiteSpace setting for this parser.

isIgnoreQuotations

public boolean isIgnoreQuotations()
Returns:
the default ignoreQuotation setting for this parser.

isPending

public boolean isPending()
Returns:
true if something was left over from last call(s)

parseLineMulti

public String[] parseLineMulti(String nextLine)
                        throws IOException
Parses an incoming String and returns an array of elements. This method is used when the data spans multiple lines.

Parameters:
nextLine - current line to be processed
Returns:
the comma-tokenized list of elements, or null if nextLine is null
Throws:
IOException - if bad things happen during the read

parseLine

public String[] parseLine(String nextLine)
                   throws IOException
Parses an incoming String and returns an array of elements. This method is used when all data is contained in a single line.

Parameters:
nextLine - Line to be parsed.
Returns:
the comma-tokenized list of elements, or null if nextLine is null
Throws:
IOException - if bad things happen during the read

parseLine

protected String[] parseLine(String nextLine,
                             boolean multi)
                      throws IOException
Parses an incoming String and returns an array of elements.

Parameters:
nextLine - the string to parse
multi - Does it take multiple lines to form a single record.
Returns:
the comma-tokenized list of elements, or null if nextLine is null
Throws:
IOException - if bad things happen during the read

isNextCharacterEscapable

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 either a quotation character or the escape char and you are inside quotes. precondition: the current character is an escape

Parameters:
nextLine - the current line
inQuotes - true if the current context is quoted
i - current index in line
Returns:
true if the following character is a quote

isAllWhiteSpace

protected boolean isAllWhiteSpace(CharSequence sb)
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:
true if every character in the sequence is whitespace

nullFieldIndicator

public CSVReaderNullFieldIndicator nullFieldIndicator()
Returns:
- the null field indicator.


Copyright © 2016 Liquibase.org. All rights reserved.