Remove unused library.
[Persons_Comparator.git] / src / CSVUtils.java
... / ...
CommitLineData
1import java.util.ArrayList;
2import java.util.List;
3
4public class CSVUtils {
5
6 private static final char DEFAULT_SEPARATOR = ',';
7 private static final char DEFAULT_QUOTE = '"';
8
9 public static List<String> parseLine(String csvLine) {
10 return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
11 }
12
13 public static List<String> parseLine(String csvLine, char separators) {
14 return parseLine(csvLine, separators, DEFAULT_QUOTE);
15 }
16
17 public static List<String> parseLine(String csvLine, char separators, char customQuote) {
18
19 List<String> result = new ArrayList<>();
20
21 //if empty, return!
22 if (csvLine == null && csvLine.isEmpty()) {
23 return result;
24 }
25
26 if (customQuote == ' ') {
27 customQuote = DEFAULT_QUOTE;
28 }
29
30 if (separators == ' ') {
31 separators = DEFAULT_SEPARATOR;
32 }
33
34 StringBuffer curVal = new StringBuffer();
35 boolean inQuotes = false;
36 boolean startCollectChar = false;
37 boolean doubleQuotesInColumn = false;
38
39 char[] chars = csvLine.toCharArray();
40
41 for (char ch : chars) {
42
43 if (inQuotes) {
44 startCollectChar = true;
45 if (ch == customQuote) {
46 inQuotes = false;
47 doubleQuotesInColumn = false;
48 } else {
49
50 //Fixed : allow "" in custom quote enclosed
51 if (ch == '\"') {
52 if (!doubleQuotesInColumn) {
53 curVal.append(ch);
54 doubleQuotesInColumn = true;
55 }
56 } else {
57 curVal.append(ch);
58 }
59
60 }
61 } else {
62 if (ch == customQuote) {
63
64 inQuotes = true;
65
66 //Fixed : allow "" in empty quote enclosed
67 if (chars[0] != '"' && customQuote == '\"') {
68 curVal.append('"');
69 }
70
71 //double quotes in column will hit this!
72 if (startCollectChar) {
73 curVal.append('"');
74 }
75
76 } else if (ch == separators) {
77
78 result.add(curVal.toString());
79
80 curVal = new StringBuffer();
81 startCollectChar = false;
82
83 } else if (ch == '\r') {
84 //ignore LF characters
85 continue;
86 } else if (ch == '\n') {
87 //the end, break!
88 break;
89 } else {
90 curVal.append(ch);
91 }
92 }
93
94 }
95
96 result.add(curVal.toString());
97
98 return result;
99 }
100
101}