This is an automated email from the ASF dual-hosted git repository.

lmccay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a7767a  KNOX-2307 - CSVKnoxShellTableBuilder must support quoted 
strings and embedded commas (#301)
9a7767a is described below

commit 9a7767a5e5dbe588e48087c24338b5ae98ff90f4
Author: lmccay <lmc...@apache.org>
AuthorDate: Wed Mar 25 20:24:36 2020 -0400

    KNOX-2307 - CSVKnoxShellTableBuilder must support quoted strings and 
embedded commas (#301)
    
    Change-Id: I82e096d204accc0ba6a334b18d6287eb67adf74c
---
 .../shell/table/CSVKnoxShellTableBuilder.java      |  6 +++--
 .../gateway/shell/table/KnoxShellTableTest.java    | 27 ++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git 
a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/CSVKnoxShellTableBuilder.java
 
b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/CSVKnoxShellTableBuilder.java
index d3ceedc..b7c23aa 100644
--- 
a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/CSVKnoxShellTableBuilder.java
+++ 
b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/CSVKnoxShellTableBuilder.java
@@ -51,7 +51,8 @@ public class CSVKnoxShellTableBuilder extends 
KnoxShellTableBuilder {
   }
 
   public KnoxShellTable string(String csvString) throws IOException {
-    try (InputStream is = new 
ByteArrayInputStream(csvString.getBytes(StandardCharsets.UTF_8)); Reader 
stringStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
+    try (InputStream is = new 
ByteArrayInputStream(csvString.getBytes(StandardCharsets.UTF_8));
+        Reader stringStreamReader = new InputStreamReader(is, 
StandardCharsets.UTF_8);
         BufferedReader csvReader = new BufferedReader(stringStreamReader);) {
       buildTableFromCSVReader(csvReader);
     }
@@ -69,7 +70,8 @@ public class CSVKnoxShellTableBuilder extends 
KnoxShellTableBuilder {
       if (!addingHeaders) {
         this.table.row();
       }
-      String[] data = row.split(",", -1);
+      // handle comma's within quoted string values for single col
+      String[] data = row.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
 
       for (String value : data) {
         if (addingHeaders) {
diff --git 
a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
 
b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
index ac2f802..5644924 100644
--- 
a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
+++ 
b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
@@ -191,6 +191,33 @@ public class KnoxShellTableTest {
   }
 
   @Test
+  public void testCSVQuotedEmbeddedCommaStringToTable() throws IOException {
+    KnoxShellTable table = new KnoxShellTable();
+    table.title("From URL");
+
+    table.header("\"Column A, Column A1\"").header("Column B").header("Column 
C");
+    table.row().value("123").value("456").value("344444444");
+    table.row().value("789").value("012").value("844444444");
+
+    String csv = table.toCSV();
+    try {
+      // write file to /tmp to read back in
+      FileUtils.writeStringToFile(new File("/tmp/testtable.csv"), csv, 
StandardCharsets.UTF_8);
+
+      KnoxShellTable urlTable = KnoxShellTable.builder().csv()
+          .withHeaders()
+          .url("file:///tmp/testtable.csv");
+      urlTable.title("From URL");
+      assertEquals(urlTable.toString(), table.toString());
+
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+    assertEquals(table.headers.get(0), "\"Column A, Column A1\"");
+  }
+
+  @Test
   public void testCSVStringToTable() throws IOException {
     String initialString = "colA, colB, colC\nvalue1, value2. value3\nvalue4, 
value5, value6";
 

Reply via email to