Add -o|--overwrite option

Change-Id: Ida203d092a19e7d2fb83c5e2215e7f567431e04a
diff --git a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt
index 0515961..d1cdbc6 100644
--- a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt
+++ b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt
@@ -165,6 +165,12 @@
         System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", threads.toString())
     }
 
+    @Option(
+        names = ["--overwrite", "-o"],
+        description = ["Overwrite existing files"]
+    )
+    var overwrite: Boolean = false
+
     private var taggerName: String? = null
     private var taggerModel: String? = null
     @Option(
@@ -356,6 +362,10 @@
         if (outputFormat == OutputFormat.KORAPXML) {
             morphoZipOutputStream!!.close()
             val outputMorphoZipFileName = zipFilePath.replace(Regex("\\.zip$"), ".".plus(getMorphoFoundry()).plus(".zip"))
+            if (File(outputMorphoZipFileName).exists() && !overwrite) {
+                LOGGER.severe("Output file $outputMorphoZipFileName already exists. Use --overwrite to overwrite.")
+                exitProcess(1)
+            }
             File(outputMorphoZipFileName).writeBytes(byteArrayOutputStream!!.toByteArray())
         }
     }
diff --git a/app/src/test/kotlin/de/ids_mannheim/korapxmltools/KorapXml2ConlluTest.kt b/app/src/test/kotlin/de/ids_mannheim/korapxmltools/KorapXml2ConlluTest.kt
index 9c8fe4d..6794534 100644
--- a/app/src/test/kotlin/de/ids_mannheim/korapxmltools/KorapXml2ConlluTest.kt
+++ b/app/src/test/kotlin/de/ids_mannheim/korapxmltools/KorapXml2ConlluTest.kt
@@ -3,6 +3,7 @@
 import org.junit.After
 import org.junit.Before
 import java.io.ByteArrayOutputStream
+import java.io.File
 import java.io.PrintStream
 import java.net.URL
 import kotlin.test.Test
@@ -250,4 +251,30 @@
             "9\tentzücke\t_\t_\tVVFIN\tnumber=sg|person=3|tense=pres|mood=subj\t_\t_\t_\t_\n"
         )
     }
+
+    @Test
+    fun korapXmlOutputWorks() {
+        val sourceFile = loadResource("wdf19.zip").path
+        val tmpSourceFileName = createTempFile("tmp", ".zip", null).absolutePath
+        File(sourceFile).copyTo(File(tmpSourceFileName), true)
+
+        val args = arrayOf("-o", "-f", "zip", tmpSourceFileName)
+        debug(args)
+
+        val resultFile = tmpSourceFileName.toString().replace(".zip", ".base.zip")
+        assert(File(resultFile).exists())
+    }
+
+    @Test
+    fun overwriteWorks() {
+        val sourceFile = loadResource("wdf19.zip").path
+        val tmpSourceFileName = createTempFile("tmp", ".zip", null).absolutePath
+        File(sourceFile).copyTo(File(tmpSourceFileName), true)
+        val resultFile = tmpSourceFileName.toString().replace(".zip", ".base.zip")
+        File(resultFile).createNewFile()
+        val args = arrayOf("-o", "-f", "zip", tmpSourceFileName)
+        debug(args)
+        assert(File(resultFile).exists())
+        assert(File(resultFile).length() > 0)
+     }
 }