|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.logdistiller.util.csv.CSVLexer
public class CSVLexer
Read files in comma separated value format. The use of this class is no longer recommended. It is now recommended that you use com.Ostermiller.util.CSVParser instead. That class, has a cleaner API, and methods for returning all the values on a line in a String array. CSV is a file format used as a portable representation of a database. Each line is one entry or record and the fields in a record are separated by commas. Commas may be preceded or followed by arbitrary space and/or tab characters which are ignored.
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 ); }
Field Summary | |
---|---|
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 Summary | |
---|---|
CSVLexer(InputStream in)
Creates a new scanner. |
|
CSVLexer(Reader in)
Creates a new scanner There is also a java.io.InputStream version of this constructor. |
Method Summary | |
---|---|
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. |
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(String[] args)
Prints out tokens and line numbers from a file or System.in. |
void |
setCommentStart(String commentDelims)
Set the characters that indicate a comment at the beginning of the line. |
void |
setEscapes(String escapes,
String replacements)
Specify escape sequences and their replacements. |
void |
yybegin(int newState)
Enters a new lexical state |
int |
yychar()
|
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(Reader reader)
Resets the scanner to read from a new input stream. |
int |
yystate()
Returns the current lexical state. |
String |
yytext()
Returns the text matched by the current regular expression. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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
Constructor Detail |
---|
public CSVLexer(Reader in)
in
- the java.io.Reader to read input from.public CSVLexer(InputStream in)
in
- the java.io.Inputstream to read input from.Method Detail |
---|
public static void main(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(String escapes, 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(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 IOException
IOException
public final void yyreset(Reader reader)
reader
- the new input streampublic final int yystate()
public final int yychar()
public final void yybegin(int newState)
newState
- the new lexical statepublic final 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 String getNextToken() throws IOException
IOException
- if any I/O-Error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |