Sanitize q-based file name for attachement
Change-Id: I17f0d050edfe03ba4916c7de08d38c7d82f8974c
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
index f4f919e..12f625e 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
@@ -32,6 +32,8 @@
import static com.tutego.jrtf.RtfPara.*;
import static com.tutego.jrtf.RtfText.*;
+import static de.ids_mannheim.korap.plkexport.Util.*;
+
@Path("/")
public class IdsExportService {
@@ -149,7 +151,7 @@
// TODO:
// Sanitize file name (i.e. replace extra characters)
builder.header("Content-Disposition",
- "attachment; filename=" + fname);
+ "attachment; filename=" + sanitizeFileName(fname));
Response response = builder.build();
return response;
}
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java
new file mode 100644
index 0000000..53614b2
--- /dev/null
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java
@@ -0,0 +1,14 @@
+package de.ids_mannheim.korap.plkexport;
+
+public class Util {
+ public static String sanitizeFileName (String fname) {
+ return fname
+ .replaceAll("[^\\p{L}0-9\\(\\)\\-\\_]", "-")
+ .replaceAll("--+", "-")
+ .replaceAll("([\\(\\)\\_])-+", "$1")
+ .replaceAll("-+([\\(\\)\\_])", "$1")
+ .replaceFirst("^-+","")
+ .replaceFirst("-+$","")
+ ;
+ }
+}
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/UtilTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/UtilTest.java
new file mode 100644
index 0000000..6b7abc7
--- /dev/null
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/UtilTest.java
@@ -0,0 +1,17 @@
+package de.ids_mannheim.korap.plkexport;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import org.junit.Test;
+
+import static de.ids_mannheim.korap.plkexport.Util.*;
+
+public class UtilTest {
+
+ @Test
+ public void testSanitizeFileName () {
+ assertEquals(sanitizeFileName("[orth='Test']"), "orth-Test");
+ assertEquals(sanitizeFileName("contains(<s name=\"okay\">,[orth='Test'])"), "contains(s-name-okay-orth-Test)");
+ };
+};