package seerp.struts.action.gt;import java.util.ArrayList;
import java.util.List;public class splitCSV {


private  List splitCSV1(String src) throws Exception {

//每行的数据
StringBuffer sb = new StringBuffer();

List result = new ArrayList();
char ch;
boolean beginWithQuote = false;
for (int i = 0; i < src.length(); i++) {
ch = src.charAt(i);
switch (ch) {
case ' ':
case ' ':
if (beginWithQuote || sb.length() != 0) {
sb.append(ch);
}
break;
case '\"':
if (beginWithQuote) {
if ((++i) >= src.length()) {
result.add(sb.toString());
sb = new StringBuffer();
beginWithQuote = false;
} else if ((ch = src.charAt(i)) == '\"') {
sb.append(ch);
} else {
while ((ch = src.charAt(i)) != ',') {
if (ch != ' ' && ch != ' ') {
throw new Exception(
"Single double-quote char mustn't exist in filed "
+ (result.size() + 1)
+ " while it is begined with quote\nchar at:"
+ i);
}
i++;
if (i > src.length()) {
break;
}
}
result.add(sb.toString());
sb = new StringBuffer();
beginWithQuote = false;
}
} else if (sb.length() == 0) {
beginWithQuote = true;
} else {
throw new Exception(
"Quote cannot exist in a filed which doesn't begin with quote!\nfield:"
+ (result.size() + 1));
}
break;
case ',':
if (beginWithQuote) {
sb.append(ch);
} else {
//result.add(trimPlus(sb.toString()));
sb = new StringBuffer();
if (i == src.length() - 1) {
result.add(sb.toString());
}
beginWithQuote = false;
}
break;
default:
sb.append(ch);
break;
}
}
if (sb.length() != 0) {
if (beginWithQuote) {
throw new Exception(
"last field is begin with but not end with double quote");
} else {
result.add(sb.toString());
}
}
return result;
}
}