public class CSVLexer
extends java.lang.Object
If field includes a comma or a new line, the whole field must be surrounded with double quotes. When the field is in quotes, any quote literals must be escaped by \" Backslash literals must be escaped by \\. Otherwise a backslash an the character following it will be treated as the following character, ie."\n" is equivelent to "n". Other escape sequences may be set using the setEscapes() method. Text that comes after quotes that have been closed but come before the next comma will be ignored.
Empty fields are returned as as String of length zero: "". The following line has four empty
fields and two non-empty fields in it. There is an empty field on each end, and two in the
middle.
,second,, ,fifth,
Blank lines are always ignored. Other lines will be ignored if they start with a comment character as set by the setCommentStart() method.
An example of how CVSLexer might be used:
CSVLexer shredder = new CSVLexer(System.in); shredder.setCommentStart("#;!"); shredder.setEscapes("nrtf", "\n\r\t\f"); String t; while ((t = shredder.getNextToken()) != null) { System.out.println("" + shredder.getLineNumber() + " " + t); }
Modifier and Type | Field and Description |
---|---|
static int |
AFTER |
static int |
BEFORE
lexical states
|
static int |
COMMENT |
static int |
YYEOF
This character denotes the end of file
|
static int |
YYINITIAL |
Constructor and Description |
---|
CSVLexer(java.io.InputStream in)
Creates a new scanner.
|
CSVLexer(java.io.Reader in)
Creates a new scanner
There is also a java.io.InputStream version of this constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
changeDelimiter(char newDelim)
Change this Lexer so that it uses a new delimiter.
|
void |
changeQuote(char newQuote)
Change this Lexer so that it uses a new character for quoting.
|
int |
getLineNumber()
Get the line number that the last token came from.
|
java.lang.String |
getNextToken()
Resumes scanning until the next regular expression is matched,
the end of input is encountered or an I/O-Error occurs.
|
static void |
main(java.lang.String[] args)
Prints out tokens and line numbers from a file or System.in.
|
void |
setCommentStart(java.lang.String commentDelims)
Set the characters that indicate a comment at the beginning of the line.
|
void |
setEscapes(java.lang.String escapes,
java.lang.String replacements)
Specify escape sequences and their replacements.
|
void |
yybegin(int newState)
Enters a new lexical state
|
char |
yycharat(int pos)
Returns the character at position pos from the
matched text.
|
void |
yyclose()
Closes the input stream.
|
int |
yylength()
Returns the length of the matched text region.
|
void |
yypushback(int number)
Pushes the specified amount of characters back into the input stream.
|
void |
yyreset(java.io.Reader reader)
Resets the scanner to read from a new input stream.
|
int |
yystate()
Returns the current lexical state.
|
java.lang.String |
yytext()
Returns the text matched by the current regular expression.
|
public static final int YYEOF
public static final int BEFORE
public static final int YYINITIAL
public static final int COMMENT
public static final int AFTER
public CSVLexer(java.io.Reader in)
in
- the java.io.Reader to read input from.public CSVLexer(java.io.InputStream in)
in
- the java.io.Inputstream to read input from.public static void main(java.lang.String[] args)
args
- program arguments, of which the first is a filenamepublic void changeDelimiter(char newDelim) throws BadDelimiterException
The initial character is a comma, the delimiter cannot be changed to a quote or other character that has special meaning in CSV.
newDelim
- delimiter to which to switch.BadDelimiterException
- if the character cannot be used as a delimiter.public void changeQuote(char newQuote) throws BadQuoteException
The initial character is a double quote ("), the delimiter cannot be changed to a comma or other character that has special meaning in CSV.
newQuote
- character to use for quoting.BadQuoteException
- if the character cannot be used as a quote.public void setEscapes(java.lang.String escapes, java.lang.String replacements)
setEscapes("nrtf", "\n\r\t\f");
escapes
- a list of characters that will represent escape sequences.replacements
- the list of repacement characters for those escape sequences.public void setCommentStart(java.lang.String commentDelims)
# Comment ; Another Comment ! Yet another commentBy default there are no comments in CVS files. Commas and quotes may not be used to indicate comment lines.
commentDelims
- list of characters a comment line may start with.public int getLineNumber()
New line breaks that occur in the middle of a token are not counted in the line number count.
If no tokens have been returned, the line number is undefined.
public final void yyclose() throws java.io.IOException
java.io.IOException
public final void yyreset(java.io.Reader reader)
reader
- the new input streampublic final int yystate()
public final void yybegin(int newState)
newState
- the new lexical statepublic final java.lang.String yytext()
public final char yycharat(int pos)
pos
- the position of the character to fetch.
A value from 0 to yylength()-1.public final int yylength()
public void yypushback(int number)
number
- the number of characters to be read again.
This number must not be greater than yylength()!public java.lang.String getNextToken() throws java.io.IOException
java.io.IOException
- if any I/O-Error occursCopyright (c) 2001-2020 by Stephen Ostermiller