CSVpublic class CSV extends Object Parse comma-separated values (CSV), a common Windows file format.
Sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625
Inner logic adapted from a C++ original that was
Copyright (C) 1999 Lucent Technologies
Excerpted from 'The Practice of Programming'
by Brian W. Kernighan and Rob Pike.
Included by permission of the http://tpop.awl.com/ web site,
which says:
"You may use this code for any purpose, as long as you leave
the copyright notice and book citation attached." I have done so. |
Fields Summary |
---|
public static final char | DEFAULT_SEP | protected ArrayList | listThe fields in the current String | protected char | fieldSepthe separator char for this parser |
Constructors Summary |
---|
public CSV()Construct a CSV parser, with the default separator (`,').
this(DEFAULT_SEP);
| public CSV(char sep)Construct a CSV parser with a given separator. Must be
exactly the string that is the separator, not a list of
separator characters!
fieldSep = sep;
|
Methods Summary |
---|
protected int | advPlain(java.lang.String s, java.lang.StringBuffer sb, int i)advPlain: unquoted field; return index of next separator
int j;
j = s.indexOf(fieldSep, i); // look for separator
Debug.println("csv", "i = " + i + ", j = " + j);
if (j == -1) { // none found
sb.append(s.substring(i));
return s.length();
} else {
sb.append(s.substring(i, j));
return j;
}
| protected int | advQuoted(java.lang.String s, java.lang.StringBuffer sb, int i)advQuoted: quoted field; return index of next separator
int j;
int len= s.length();
for (j=i; j<len; j++) {
if (s.charAt(j) == '"" && j+1 < len) {
if (s.charAt(j+1) == '"") {
j++; // skip escape char
} else if (s.charAt(j+1) == fieldSep) { //next delimeter
j++; // skip end quotes
break;
}
} else if (s.charAt(j) == '"" && j+1 == len) { // end quotes at end of line
break; //done
}
sb.append(s.charAt(j)); // regular character.
}
return j;
| public java.util.Iterator | parse(java.lang.String line)parse: break the input String into fields
StringBuffer sb = new StringBuffer();
list.clear(); // discard previous, if any
int i = 0;
if (line.length() == 0) {
list.add(line);
return list.iterator();
}
do {
sb.setLength(0);
if (i < line.length() && line.charAt(i) == '"")
i = advQuoted(line, sb, ++i); // skip quote
else
i = advPlain(line, sb, i);
list.add(sb.toString());
Debug.println("csv", sb.toString());
i++;
} while (i < line.length());
return list.iterator();
|
|