From 6c641803996523bbd5d2ac79e018f90207463e00 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Apr 2019 12:47:04 +0100 Subject: [PATCH] Check that required input files exist (in inputs folder) --- .../kotlin/org/idiosapps/ExceptionHelper.kt | 2 + .../kotlin/org/idiosapps/FXMLController.kt | 34 ++++++++--------- src/main/kotlin/org/idiosapps/Filenames.kt | 38 ++++++++++++++----- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/org/idiosapps/ExceptionHelper.kt b/src/main/kotlin/org/idiosapps/ExceptionHelper.kt index 805226e..7ddf00e 100644 --- a/src/main/kotlin/org/idiosapps/ExceptionHelper.kt +++ b/src/main/kotlin/org/idiosapps/ExceptionHelper.kt @@ -16,6 +16,8 @@ class ExceptionHelper { return getExceptionMessage(XETEX) } else if (exception.toString().contains(PDFTEX)) { return getExceptionMessage(PDFTEX) + } else if (exception.toString().contains("Input")) { + return exception.message.toString() // Exception already contains missing input file info } else { // log out start of stacktrace - should help with any Exception exception.printStackTrace() diff --git a/src/main/kotlin/org/idiosapps/FXMLController.kt b/src/main/kotlin/org/idiosapps/FXMLController.kt index 2b4b20a..5b8390d 100644 --- a/src/main/kotlin/org/idiosapps/FXMLController.kt +++ b/src/main/kotlin/org/idiosapps/FXMLController.kt @@ -14,10 +14,12 @@ class FXMLController { @FXML fun buildButtonClicked() { - try { // first check dependencies - have a good idea of what to expect + try { // first check dependencies & inputs - have a good idea of what to expect OSUtils.hasProgram(PDFTEX) OSUtils.hasProgram(XETEX) - buildGradedReader() // our pipeline for building our graded reader! + Filenames.checkInputs() + + buildGradedReader() // use our pipeline for building our graded reader! } catch (exception: Exception) { exception.printStackTrace() @@ -36,8 +38,6 @@ class FXMLController { private fun buildGradedReader() { var languageUsed = "mandarin" - val filenames = Filenames() // load defaults from class - var vocabArray: ArrayList = ArrayList() // This is a list of all the input vocabulary var vocabComponentArray: ArrayList> = ArrayList() // This an [array of [arrays containing input vocab split into parts]] @@ -50,16 +50,16 @@ class FXMLController { OSUtils.tryMakeOutputDir() // Java will only make a new file if the parent folder exists (on Windows anyway) - val outputStoryTeXWriter = PrintWriter(filenames.outputStoryFilename, "UTF-8") + val outputStoryTeXWriter = PrintWriter(Filenames.outputStoryFilename, "UTF-8") - VocabUtils.splitVocabIntoParts(filenames.inputVocabFilename, vocabArray, vocabComponentArray) - VocabUtils.splitVocabIntoParts(filenames.inputKeyNamesFilename, keyNameArray, keyNameComponentArray) + VocabUtils.splitVocabIntoParts(Filenames.inputVocabFilename, vocabArray, vocabComponentArray) + VocabUtils.splitVocabIntoParts(Filenames.inputKeyNamesFilename, keyNameArray, keyNameComponentArray) - TexUtils.copyToTex(outputStoryTeXWriter, filenames.inputHeaderFilename) - TexUtils.copyToTex(outputStoryTeXWriter, filenames.inputTitleFilename) - TexUtils.copyToTex(outputStoryTeXWriter, filenames.inputStoryFilename) + TexUtils.copyToTex(outputStoryTeXWriter, Filenames.inputHeaderFilename) + TexUtils.copyToTex(outputStoryTeXWriter, Filenames.inputTitleFilename) + TexUtils.copyToTex(outputStoryTeXWriter, Filenames.inputStoryFilename) - SummaryPageWriter.writeVocabSection(outputStoryTeXWriter, filenames.inputVocabFilename, vocabComponentArray) + SummaryPageWriter.writeVocabSection(outputStoryTeXWriter, Filenames.inputVocabFilename, vocabComponentArray) // todo WriteSummaryPage.writeTexGrammar outputStoryTeXWriter.append("\\end{document}") @@ -67,21 +67,21 @@ class FXMLController { PDFUtils.xelatexToPDF() - val pdfNumberOfPages = PDFUtils.getNumberOfPDFPages(filenames.outputPDFFilename) - PDFUtils.readPDF(filenames.outputPDFFilename, vocabComponentArray, pdfPageLastSentences, pdfNumberOfPages) + val pdfNumberOfPages = PDFUtils.getNumberOfPDFPages(Filenames.outputPDFFilename) + PDFUtils.readPDF(Filenames.outputPDFFilename, vocabComponentArray, pdfPageLastSentences, pdfNumberOfPages) TexUtils.getTexLineNumbers( - filenames.outputStoryFilename, + Filenames.outputStoryFilename, pdfPageLastSentences, texLinesOfPDFPagesLastSentences, texLineIndexOfPDFPageLastSentence ) - TeXStyling.addStyling(vocabComponentArray, filenames.outputStoryFilename, SUPERSCRIPT_STYLING) - TeXStyling.addStyling(keyNameComponentArray, filenames.outputStoryFilename, UNDERLINE_STYLING) + TeXStyling.addStyling(vocabComponentArray, Filenames.outputStoryFilename, SUPERSCRIPT_STYLING) + TeXStyling.addStyling(keyNameComponentArray, Filenames.outputStoryFilename, UNDERLINE_STYLING) FooterUtils.addVocabFooters( vocabComponentArray, - filenames.outputStoryFilename, + Filenames.outputStoryFilename, texLinesOfPDFPagesLastSentences, languageUsed, pdfNumberOfPages, diff --git a/src/main/kotlin/org/idiosapps/Filenames.kt b/src/main/kotlin/org/idiosapps/Filenames.kt index 7c2af17..84cf9b3 100644 --- a/src/main/kotlin/org/idiosapps/Filenames.kt +++ b/src/main/kotlin/org/idiosapps/Filenames.kt @@ -1,17 +1,37 @@ package org.idiosapps +import java.io.File + class Filenames { - val inputPrefix: String = "./input/" - val outputPrefix: String = "./output/" + companion object { + val inputPrefix: String = "./input/" + val outputPrefix: String = "./output/" + + val inputHeaderFilename: String = inputPrefix + "header" + val inputTitleFilename: String = inputPrefix + "title" + val inputStoryFilename: String = inputPrefix + "story" + val inputVocabFilename: String = inputPrefix + "vocab" + val inputKeyNamesFilename: String = inputPrefix + "names" - val inputHeaderFilename: String = inputPrefix + "header" - val inputTitleFilename: String = inputPrefix + "title" - val inputStoryFilename: String = inputPrefix + "story" + val outputStoryFilename: String = outputPrefix + "outputStory.tex" + val outputPDFFilename: String = outputPrefix + "outputStory.pdf" - val inputVocabFilename: String = inputPrefix + "vocab" - val inputKeyNamesFilename: String = inputPrefix + "names" + fun checkInputs() { + val inputArray = arrayListOf( + inputHeaderFilename, + inputTitleFilename, + inputStoryFilename, + inputVocabFilename, + inputKeyNamesFilename + ) - val outputStoryFilename: String = outputPrefix + "outputStory.tex" - val outputPDFFilename: String = outputPrefix + "outputStory.pdf" + for (input in inputArray) { + val inputFile = File(input) + if (!inputFile.exists()) { // could also check for size > 0 on required inputs + throw Exception("Input does not exist: $input") + } + } + } + } } \ No newline at end of file