Top Description Fields Constructors Methods
org.apache.commons.csv

pack-priv final Class Lexer

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Static Imports
org.apache.commons.io.IOUtils.EOF

Lexical analyzer.

Field Summary

Modifier and TypeField and Description
private final int
private static final String
private final char[]
private final char[]
private final int
private final char[]
private String
private final boolean
private final boolean
private boolean
private final boolean
private static final String
private final int
private final ExtendedBufferedReader
reader

The buffered reader.

private final boolean

Constructor Summary

AccessConstructor and Description
pack-priv
Lexer(final CSVFormat format, final ExtendedBufferedReader reader)

Method Summary

Modifier and TypeMethod and Description
private void
appendNextEscapedCharacterToToken(final Token
the current token
token
)

Appends the next escaped character to the token's content.

public void
close()

Implements java.io.Closeable.close.

Closes resources.

pack-priv long

Returns:

the number of bytes read
getBytesRead
()

Gets the number of bytes read

pack-priv long

Returns:

the current character position
getCharacterPosition
()

Returns the current character position

pack-priv long

Returns:

the current line number
getCurrentLineNumber
()

Returns the current line number

pack-priv String
pack-priv boolean
pack-priv boolean
isCommentStart(final int ch)

pack-priv boolean

Returns:

true if the next characters constitute a delimiter.
isDelimiter
(final int
the current character.
ch
)

Determine whether the next characters constitute a delimiter through ExtendedBufferedReader#peek(char[]).

pack-priv boolean

Returns:

true if the given character indicates the end of the file.
isEndOfFile
(final int ch)

Tests if the given character indicates the end of the file.

pack-priv boolean

Returns:

true if the given character is the escape character.
isEscape
(final int ch)

Tests if the given character is the escape character.

pack-priv boolean

Returns:

true if the next characters constitute an escape delimiter.
isEscapeDelimiter
()

Tests if the next characters constitute a escape delimiter through ExtendedBufferedReader#peek(char[]).

private boolean
isMetaChar(final int ch)

pack-priv boolean
isQuoteChar(final int ch)

pack-priv boolean

Returns:

true if the character is at the start of a line.
isStartOfLine
(final int
the character to check
ch
)

Tests if the current character represents the start of a line: a CR, LF, or is at the start of the file.

pack-priv Token

Returns:

the next token found.
nextToken
(final Token
an existing Token object to reuse. The caller is responsible for initializing the Token.
token
)

Returns the next token.

private int
private Token

Returns:

a valid token object
parseEncapsulatedToken
(final Token
the current token
token
)

Parses an encapsulated token.

private Token

Returns:

the filled token
parseSimpleToken
(final Token
the current token
token
,
final int
the current character
ch
)

Parses a simple token.

pack-priv boolean

Returns:

true if the given or next character is a line-terminator
readEndOfLine
(final int ch)

Greedily accepts \n, \r and \r\n This checker consumes silently the second control-character...

pack-priv int

Returns:

the unescaped character (as an int) or IOUtils#EOF if char following the escape is invalid.
readEscape
()

Handle an escape sequence.

pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

commentStartback to summary
private final int commentStart
CR_STRINGback to summary
private static final String CR_STRING
delimiterback to summary
private final char[] delimiter
delimiterBufback to summary
private final char[] delimiterBuf
escapeback to summary
private final int escape
escapeDelimiterBufback to summary
private final char[] escapeDelimiterBuf
firstEolback to summary
private String firstEol
ignoreEmptyLinesback to summary
private final boolean ignoreEmptyLines
ignoreSurroundingSpacesback to summary
private final boolean ignoreSurroundingSpaces
isLastTokenDelimiterback to summary
private boolean isLastTokenDelimiter
lenientEofback to summary
private final boolean lenientEof
LF_STRINGback to summary
private static final String LF_STRING
quoteCharback to summary
private final int quoteChar
readerback to summary
private final ExtendedBufferedReader reader

The buffered reader.

trailingDataback to summary
private final boolean trailingData

Constructor Detail

Lexerback to summary
pack-priv Lexer(final CSVFormat format, final ExtendedBufferedReader reader)

Method Detail

appendNextEscapedCharacterToTokenback to summary
private void appendNextEscapedCharacterToToken(final Token token) throws IOException

Appends the next escaped character to the token's content.

Parameters
token:Token

the current token

Exceptions
IOException:
on stream access error
CSVException:
Thrown on invalid input.
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Closes resources.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getBytesReadback to summary
pack-priv long getBytesRead()

Gets the number of bytes read

Returns:long

the number of bytes read

getCharacterPositionback to summary
pack-priv long getCharacterPosition()

Returns the current character position

Returns:long

the current character position

getCurrentLineNumberback to summary
pack-priv long getCurrentLineNumber()

Returns the current line number

Returns:long

the current line number

getFirstEolback to summary
pack-priv String getFirstEol()
isClosedback to summary
pack-priv boolean isClosed()
isCommentStartback to summary
pack-priv boolean isCommentStart(final int ch)
isDelimiterback to summary
pack-priv boolean isDelimiter(final int ch) throws IOException

Determine whether the next characters constitute a delimiter through ExtendedBufferedReader#peek(char[]).

Parameters
ch:int

the current character.

Returns:boolean

true if the next characters constitute a delimiter.

Exceptions
IOException:
If an I/O error occurs.
isEndOfFileback to summary
pack-priv boolean isEndOfFile(final int ch)

Tests if the given character indicates the end of the file.

Returns:boolean

true if the given character indicates the end of the file.

isEscapeback to summary
pack-priv boolean isEscape(final int ch)

Tests if the given character is the escape character.

Returns:boolean

true if the given character is the escape character.

isEscapeDelimiterback to summary
pack-priv boolean isEscapeDelimiter() throws IOException

Tests if the next characters constitute a escape delimiter through ExtendedBufferedReader#peek(char[]). For example, for delimiter "[|]" and escape '!', return true if the next characters constitute "![!|!]".

Returns:boolean

true if the next characters constitute an escape delimiter.

Exceptions
IOException:
If an I/O error occurs.
isMetaCharback to summary
private boolean isMetaChar(final int ch)
isQuoteCharback to summary
pack-priv boolean isQuoteChar(final int ch)
isStartOfLineback to summary
pack-priv boolean isStartOfLine(final int ch)

Tests if the current character represents the start of a line: a CR, LF, or is at the start of the file.

Parameters
ch:int

the character to check

Returns:boolean

true if the character is at the start of a line.

nextTokenback to summary
pack-priv Token nextToken(final Token token) throws IOException

Returns the next token.

A token corresponds to a term, a record change or an end-of-file indicator.

Parameters
token:Token

an existing Token object to reuse. The caller is responsible for initializing the Token.

Returns:Token

the next token found.

Exceptions
IOException:
on stream access error.
CSVException:
Thrown on invalid input.
nullToDisabledback to summary
private int nullToDisabled(final Character c)
parseEncapsulatedTokenback to summary
private Token parseEncapsulatedToken(final Token token) throws IOException

Parses an encapsulated token.

Encapsulated tokens are surrounded by the given encapsulating string. The encapsulator itself might be included in the token using a doubling syntax (as "", '') or using escaping (as in \", \'). Whitespaces before and after an encapsulated token is ignored. The token is finished when one of the following conditions becomes true:

  • An unescaped encapsulator has been reached and is followed by optional whitespace then:
    • delimiter (TOKEN)
    • end of line (EORECORD)
  • end of stream has been reached (EOF)
Parameters
token:Token

the current token

Returns:Token

a valid token object

Exceptions
IOException:
Thrown when in an invalid state: EOF before closing encapsulator or invalid character before delimiter or EOL.
CSVException:
Thrown on invalid input.
parseSimpleTokenback to summary
private Token parseSimpleToken(final Token token, final int ch) throws IOException

Parses a simple token.

Simple tokens are tokens that are not surrounded by encapsulators. A simple token might contain escaped delimiters (as \, or \;). The token is finished when one of the following conditions becomes true:

  • The end of line has been reached (EORECORD)
  • The end of stream has been reached (EOF)
  • An unescaped delimiter has been reached (TOKEN)
Parameters
token:Token

the current token

ch:int

the current character

Returns:Token

the filled token

Exceptions
IOException:
on stream access error
CSVException:
Thrown on invalid input.
readEndOfLineback to summary
pack-priv boolean readEndOfLine(final int ch) throws IOException

Greedily accepts \n, \r and \r\n This checker consumes silently the second control-character...

Returns:boolean

true if the given or next character is a line-terminator

readEscapeback to summary
pack-priv int readEscape() throws IOException

Handle an escape sequence. The current character must be the escape character. On return, the next character is available by calling ExtendedBufferedReader#getLastChar() on the input stream.

Returns:int

the unescaped character (as an int) or IOUtils#EOF if char following the escape is invalid.

Exceptions
IOException:
if there is a problem reading the stream or the end of stream is detected: the escape character is not allowed at end of stream
CSVException:
Thrown on invalid input.
trimTrailingSpacesback to summary
pack-priv void trimTrailingSpaces(final StringBuilder buffer)