From 7ca17b7c42375945af37caa38b36193509f3f0e9 Mon Sep 17 00:00:00 2001 From: vfr44rfv Date: Sat, 4 May 2019 21:18:34 +0300 Subject: [PATCH] hotfix --- pom.xml | 5 ++ .../boxable/datatable/DataTable.java | 3 +- .../boxable/esacpe/StringEscapeTest.java | 81 +++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/test/java/be/quodlibet/boxable/esacpe/StringEscapeTest.java diff --git a/pom.xml b/pom.xml index 82fc2588..cf22ec1a 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,11 @@ Easily creates tables in pdf. + + org.apache.commons + commons-lang3 + 3.4 + org.apache.pdfbox pdfbox diff --git a/src/main/java/be/quodlibet/boxable/datatable/DataTable.java b/src/main/java/be/quodlibet/boxable/datatable/DataTable.java index fa295fc6..da199e51 100644 --- a/src/main/java/be/quodlibet/boxable/datatable/DataTable.java +++ b/src/main/java/be/quodlibet/boxable/datatable/DataTable.java @@ -16,6 +16,7 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.font.PDType1Font; @@ -189,7 +190,7 @@ public void addListToTable(List data, Boolean hasHeader) throws IOExceptio // Convert Map of arbitrary objects to a csv String for (List inputList : data) { for (Object v : inputList) { - String value = v.toString(); + String value = StringEscapeUtils.escapeCsv(v.toString()); if (value.contains("" + separator)) { // surround value with quotes if it contains the escape // character diff --git a/src/test/java/be/quodlibet/boxable/esacpe/StringEscapeTest.java b/src/test/java/be/quodlibet/boxable/esacpe/StringEscapeTest.java new file mode 100644 index 00000000..4bb3d399 --- /dev/null +++ b/src/test/java/be/quodlibet/boxable/esacpe/StringEscapeTest.java @@ -0,0 +1,81 @@ +package be.quodlibet.boxable.esacpe; + +import be.quodlibet.boxable.BaseTable; +import be.quodlibet.boxable.datatable.DataTable; +import com.google.common.io.Files; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.junit.*; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class StringEscapeTest { + + public StringEscapeTest() + { + } + + @BeforeClass + public static void setUpClass() + { + } + + @AfterClass + public static void tearDownClass() + { + } + + @Before + public void setUp() + { + } + + @After + public void tearDown() + { + } + + @Test + public void supportSingleQuotes() throws IOException + { + + + //Initialize Document + PDDocument doc = new PDDocument(); + PDPage page = new PDPage(); + //Create a landscape page + page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth())); + doc.addPage(page); + //Initialize table + float margin = 10; + float tableWidth = page.getMediaBox().getWidth() - (2 * margin); + float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin); + float yStart = yStartNewPage; + float bottomMargin = 0; + + //Create the data + List data = new ArrayList(); + data.add(new ArrayList<>( + Arrays.asList("Column One"))); + for (int i = 1; i <= 3; i++) { + data.add(new ArrayList<>( + Arrays.asList("\"\"California is Farting\"\" Anchor Slip-Up Has Internet Fired Up"))); + } + + BaseTable dataTable = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true, + true); + DataTable t = new DataTable(dataTable, page); + t.addListToTable(data, DataTable.HASHEADER); + dataTable.draw(); + File file = new File("target/supportSingleQuotes.pdf"); + System.out.println("Sample file saved at : " + file.getAbsolutePath()); + Files.createParentDirs(file); + doc.save(file); + doc.close(); + } +}