Add a CSVUtils class for future usage.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 12:33:30 +0000 (13:33 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 12:33:30 +0000 (13:33 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/CSVUtils.java [new file with mode: 0644]
src/Main.java

diff --git a/src/CSVUtils.java b/src/CSVUtils.java
new file mode 100644 (file)
index 0000000..01d4e32
--- /dev/null
@@ -0,0 +1,102 @@
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class CSVUtils {
+
+    private static final char DEFAULT_SEPARATOR = ',';
+    private static final char DEFAULT_QUOTE = '"';
+
+    public static List<String> parseLine(String cvsLine) {
+        return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
+    }
+
+    public static List<String> parseLine(String cvsLine, char separators) {
+        return parseLine(cvsLine, separators, DEFAULT_QUOTE);
+    }
+
+    public static List<String> parseLine(String cvsLine, char separators, char customQuote) {
+
+        List<String> result = new ArrayList<>();
+
+        //if empty, return!
+        if (cvsLine == null && cvsLine.isEmpty()) {
+            return result;
+        }
+
+        if (customQuote == ' ') {
+            customQuote = DEFAULT_QUOTE;
+        }
+
+        if (separators == ' ') {
+            separators = DEFAULT_SEPARATOR;
+        }
+
+        StringBuffer curVal = new StringBuffer();
+        boolean inQuotes = false;
+        boolean startCollectChar = false;
+        boolean doubleQuotesInColumn = false;
+
+        char[] chars = cvsLine.toCharArray();
+
+        for (char ch : chars) {
+
+            if (inQuotes) {
+                startCollectChar = true;
+                if (ch == customQuote) {
+                    inQuotes = false;
+                    doubleQuotesInColumn = false;
+                } else {
+
+                    //Fixed : allow "" in custom quote enclosed
+                    if (ch == '\"') {
+                        if (!doubleQuotesInColumn) {
+                            curVal.append(ch);
+                            doubleQuotesInColumn = true;
+                        }
+                    } else {
+                        curVal.append(ch);
+                    }
+
+                }
+            } else {
+                if (ch == customQuote) {
+
+                    inQuotes = true;
+
+                    //Fixed : allow "" in empty quote enclosed
+                    if (chars[0] != '"' && customQuote == '\"') {
+                        curVal.append('"');
+                    }
+
+                    //double quotes in column will hit this!
+                    if (startCollectChar) {
+                        curVal.append('"');
+                    }
+
+                } else if (ch == separators) {
+
+                    result.add(curVal.toString());
+
+                    curVal = new StringBuffer();
+                    startCollectChar = false;
+
+                } else if (ch == '\r') {
+                    //ignore LF characters
+                    continue;
+                } else if (ch == '\n') {
+                    //the end, break!
+                    break;
+                } else {
+                    curVal.append(ch);
+                }
+            }
+
+        }
+
+        result.add(curVal.toString());
+
+        return result;
+    }
+
+}
\ No newline at end of file
index f7714fdedc3d30c35b4b4292de8bb055c0b23c75..a3bc099b9f6dac2aaf98b69ee1b4952140f45dc2 100644 (file)
@@ -32,7 +32,7 @@ public class Main {
         personArrayList.add(person12);
         Person person13 = new Person("Rodolphe", "Germany", 212, 108, "black");
         personArrayList.add(person13);
-        Person person14 = new Person("Rodolphe", "Norway", 212, 108, "blue");
+        Person person14 = new Person("Rodolphe", "Norway", 173, 83, "blue");
         personArrayList.add(person14);
         Person person15 = new Person("Abdel", "Mali", 168, 52, "black");
         personArrayList.add(person15);