From 78853f8fb22b4ae31de7f7c606de87c03ee2e901 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 25 Dec 2019 11:33:03 +0100 Subject: [PATCH 1/2] More robust URI detection in tabular exporter. Closes #2213. --- .../CustomizableTabularExporterUtilities.java | 13 ++++++--- .../refine/exporters/XlsxExporterTests.java | 29 +++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/main/src/com/google/refine/exporters/CustomizableTabularExporterUtilities.java b/main/src/com/google/refine/exporters/CustomizableTabularExporterUtilities.java index f32058cb8..e139d791a 100644 --- a/main/src/com/google/refine/exporters/CustomizableTabularExporterUtilities.java +++ b/main/src/com/google/refine/exporters/CustomizableTabularExporterUtilities.java @@ -34,6 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.refine.exporters; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.OffsetDateTime; @@ -371,10 +373,13 @@ abstract public class CustomizableTabularExporterUtilities { if (value instanceof String) { text = (String) value; - if(text.contains(":")) { - if(urlValidator.isValid(text)) { - link = text; - } + if(text.contains(":") && urlValidator.isValid(text)) { + // Extra check for https://github.com/OpenRefine/OpenRefine/issues/2213 + try { + link = new URI(text).toString(); + } catch(URISyntaxException e) { + ; + } } } else if (value instanceof OffsetDateTime) { text = ((OffsetDateTime) value).format(DateTimeFormatter.ISO_INSTANT); diff --git a/main/tests/server/src/com/google/refine/exporters/XlsxExporterTests.java b/main/tests/server/src/com/google/refine/exporters/XlsxExporterTests.java index b8a260ef2..23ad37489 100644 --- a/main/tests/server/src/com/google/refine/exporters/XlsxExporterTests.java +++ b/main/tests/server/src/com/google/refine/exporters/XlsxExporterTests.java @@ -43,6 +43,7 @@ import static org.mockito.Mockito.mock; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.Serializable; import java.time.OffsetDateTime; import java.util.Properties; @@ -155,6 +156,30 @@ public class XlsxExporterTests extends RefineTest { Assert.fail(); } } + + @Test + public void exportXlsxStringWithURLs() throws IOException{ + String url = "GET /primo-library/,http:%2F%2Fcatalogue.unice.fr HTTP/1.1"; + createDateGrid(2, 2, url); + + try { + SUT.export(project, options, engine, stream); + } catch (IOException e) { + Assert.fail(); + } + + ByteArrayInputStream inStream = new ByteArrayInputStream( stream.toByteArray() ); + try { + XSSFWorkbook wb = new XSSFWorkbook(inStream); + XSSFSheet ws = wb.getSheetAt(0); + XSSFRow row1 = ws.getRow(1); + XSSFCell cell0 = row1.getCell(0); + Assert.assertTrue(cell0.toString().contains("primo-library")); + wb.close(); + } catch (IOException e) { + Assert.fail(); + } + } //helper methods @@ -180,13 +205,13 @@ public class XlsxExporterTests extends RefineTest { } } - private void createDateGrid(int noOfRows, int noOfColumns, OffsetDateTime now){ + private void createDateGrid(int noOfRows, int noOfColumns, Serializable value){ CreateColumns(noOfColumns); for(int i = 0; i < noOfRows; i++){ Row row = new Row(noOfColumns); for(int j = 0; j < noOfColumns; j++){ - row.cells.add(new Cell(now, null)); + row.cells.add(new Cell(value, null)); } project.rows.add(row); } From 7593d5484d1f7d69dc65385b9d6b91c9b0f39a22 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 25 Dec 2019 22:24:58 +0100 Subject: [PATCH 2/2] Add Hyperlink to cell in Excel importer, with fallback to String, for #2213 --- .../com/google/refine/exporters/XlsExporter.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/main/src/com/google/refine/exporters/XlsExporter.java b/main/src/com/google/refine/exporters/XlsExporter.java index 48100b0ae..461cc9f28 100644 --- a/main/src/com/google/refine/exporters/XlsExporter.java +++ b/main/src/com/google/refine/exporters/XlsExporter.java @@ -39,7 +39,7 @@ import java.time.OffsetDateTime; import java.util.List; import java.util.Properties; -import org.apache.poi.common.usermodel.Hyperlink; +import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; @@ -127,9 +127,15 @@ public class XlsExporter implements StreamExporter { } if (cellData.link != null) { - Hyperlink hl = wb.getCreationHelper().createHyperlink(HyperlinkType.URL); - hl.setLabel(cellData.text); - hl.setAddress(cellData.link); + try { + Hyperlink hl = wb.getCreationHelper().createHyperlink(HyperlinkType.URL); + hl.setLabel(cellData.text); + hl.setAddress(cellData.link); + c.setHyperlink(hl); + } catch(IllegalArgumentException e) { + // If we failed to create the hyperlink and add it to the cell, + // we just use the string value as fallback + } } } }