Make the main window a singleton to permit to query it from other views and update...
[Persons_Comparator.git] / src / CSVUtils.java
CommitLineData
ea1d42c9
JB
1import java.util.ArrayList;
2import java.util.List;
3
ea1d42c9
JB
4public class CSVUtils {
5
6 private static final char DEFAULT_SEPARATOR = ',';
7 private static final char DEFAULT_QUOTE = '"';
8
7add5cb9
JB
9 public static List<String> parseLine(String csvLine) {
10 return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
ea1d42c9
JB
11 }
12
7add5cb9
JB
13 public static List<String> parseLine(String csvLine, char separators) {
14 return parseLine(csvLine, separators, DEFAULT_QUOTE);
ea1d42c9
JB
15 }
16
7add5cb9 17 public static List<String> parseLine(String csvLine, char separators, char customQuote) {
ea1d42c9
JB
18
19 List<String> result = new ArrayList<>();
20
21 //if empty, return!
7add5cb9 22 if (csvLine == null && csvLine.isEmpty()) {
ea1d42c9
JB
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
7add5cb9 39 char[] chars = csvLine.toCharArray();
ea1d42c9
JB
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}