1 import java
.util
.ArrayList
;
5 public class CSVUtils
{
7 private static final char DEFAULT_SEPARATOR
= ',';
8 private static final char DEFAULT_QUOTE
= '"';
10 public static List
<String
> parseLine(String cvsLine
) {
11 return parseLine(cvsLine
, DEFAULT_SEPARATOR
, DEFAULT_QUOTE
);
14 public static List
<String
> parseLine(String cvsLine
, char separators
) {
15 return parseLine(cvsLine
, separators
, DEFAULT_QUOTE
);
18 public static List
<String
> parseLine(String cvsLine
, char separators
, char customQuote
) {
20 List
<String
> result
= new ArrayList
<>();
23 if (cvsLine
== null && cvsLine
.isEmpty()) {
27 if (customQuote
== ' ') {
28 customQuote
= DEFAULT_QUOTE
;
31 if (separators
== ' ') {
32 separators
= DEFAULT_SEPARATOR
;
35 StringBuffer curVal
= new StringBuffer();
36 boolean inQuotes
= false;
37 boolean startCollectChar
= false;
38 boolean doubleQuotesInColumn
= false;
40 char[] chars
= cvsLine
.toCharArray();
42 for (char ch
: chars
) {
45 startCollectChar
= true;
46 if (ch
== customQuote
) {
48 doubleQuotesInColumn
= false;
51 //Fixed : allow "" in custom quote enclosed
53 if (!doubleQuotesInColumn
) {
55 doubleQuotesInColumn
= true;
63 if (ch
== customQuote
) {
67 //Fixed : allow "" in empty quote enclosed
68 if (chars
[0] != '"' && customQuote
== '\"') {
72 //double quotes in column will hit this!
73 if (startCollectChar
) {
77 } else if (ch
== separators
) {
79 result
.add(curVal
.toString());
81 curVal
= new StringBuffer();
82 startCollectChar
= false;
84 } else if (ch
== '\r') {
85 //ignore LF characters
87 } else if (ch
== '\n') {
97 result
.add(curVal
.toString());