Add CSV countries file and remove the JSON ones.
[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
9 public static List<String> parseLine(String cvsLine) {
10 return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
11 }
12
13 public static List<String> parseLine(String cvsLine, char separators) {
14 return parseLine(cvsLine, separators, DEFAULT_QUOTE);
15 }
16
17 public static List<String> parseLine(String cvsLine, char separators, char customQuote) {
18
19 List<String> result = new ArrayList<>();
20
21 //if empty, return!
22 if (cvsLine == null && cvsLine.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 = cvsLine.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}