import java.util.ArrayList;
public class CsvParser {
public static final String SEPARATOR = ",";
public static final char QUOTE = '"';
// parse the csv line into an ArrayList of values
public static ArrayList parse(String line) throws Exception {
ArrayList list = null;
if (line == null) {
// return null if input line is null
}
else {
list = new ArrayList();
if (line.length() == 0) {
// return an empty line if the input is an empty line
list.add(line);
}
else {
StringBuffer sb = new StringBuffer();
String[] a = line.split(SEPARATOR);
for (int i=0; i<a.length; i++) {
sb.append(a[i]);
// count the no. of double quote
int count = 0;
for (int m=0; m<sb.length(); m++) {
if (sb.charAt(m) == QUOTE) {
count++;
}
}
if (count > 0) { // has double-quote
if (count % 2 == 0) { // complete token
sb = new StringBuffer(sb.toString().trim());
if (sb.charAt(0) == QUOTE && sb.charAt(sb.length()-1) == QUOTE) {
sb.deleteCharAt(sb.length()-1); // remove trailing double-quote
sb.deleteCharAt(0); // remove leading double-quote
for (int h=0; h<sb.length(); h++) {
if (sb.charAt(h) == QUOTE) {
int nextPos = h+1;
if (nextPos < sb.length() && sb.charAt(nextPos) == QUOTE) { // if escaped by another double-quote
sb.replace(h, nextPos+1, String.valueOf(QUOTE)); // replace escaped double-quotes
}
else {
throw new Exception("a literal double-quote should be escaped by another double-quote");
}
}
}
// add to list
list.add(sb.toString());
// reset buffer
sb.setLength(0);
}
else {
throw new Exception("field containing commas and double-quotes must be quoted");
}
}
else { // incomplete token
if (i < a.length-1) {
// append the removed separator
sb.append(SEPARATOR);
// continue to append the next token
}
else {
// no more token left
throw new Exception("Incomplete token");
}
}
}
else { // no double quote
// add to list
list.add(sb.toString().trim());
// reset buffer
sb.setLength(0);
}
}
}
}
return list;
}
}