Commit da98b5ea authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Validate json files

parent d010e17c
......@@ -23,4 +23,5 @@ object Extensions {
const val xlsx = "xlsx"
const val xls = "xls"
const val xml = "xml"
const val json = "json"
}
\ No newline at end of file
......@@ -20,6 +20,8 @@ package org.memobase
import ch.memobase.reporting.Report
import ch.memobase.reporting.ReportStatus
import com.beust.klaxon.Klaxon
import com.beust.klaxon.KlaxonException
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import com.github.doyaaaaaken.kotlincsv.util.MalformedCSVException
import org.apache.logging.log4j.LogManager
......@@ -32,13 +34,15 @@ import javax.xml.parsers.DocumentBuilderFactory
class FileValidation(private val step: String) {
private val log = LogManager.getLogger(this::class.java)
private val klaxon = Klaxon()
private val supportedExtensions = mapOf(
Pair(Extensions.csv, Formats.csv),
Pair(Extensions.tsv, Formats.tsv),
Pair(Extensions.xlsx, Formats.xlsx),
Pair(Extensions.xls, Formats.xls),
Pair(Extensions.xml, Formats.xml)
Pair(Extensions.xml, Formats.xml),
Pair(Extensions.json, Formats.json)
)
fun validateExtension(file: File): String {
......@@ -142,6 +146,42 @@ class FileValidation(private val step: String) {
}
}
}
Formats.json -> {
inputStream.use { iS ->
try {
val output = klaxon.parseJsonObject(iS.bufferedReader(Charsets.UTF_8))
if (output.isEmpty()) {
Pair(
Message(format, file.path), Report(
id = file.name,
status = ReportStatus.fatal,
message = ReportMessages.formatError(file.path, format, "Parsed object is empty."),
step = step
)
)
} else {
Pair(
Message(format, file.path), Report(
id = file.name,
status = ReportStatus.success,
message = ReportMessages.validatedFile(file.path, format),
step = step
)
)
}
} catch (ex: KlaxonException) {
Pair(
Message(Formats.error, file.path),
Report(
id = file.name,
status = ReportStatus.fatal,
message = ReportMessages.formatError(file.path, format, ex.localizedMessage),
step = step
)
)
}
}
}
else -> Pair(
Message(Formats.error, file.path),
Report(
......
......@@ -23,6 +23,7 @@ object Formats {
const val xlsx = "XLSX"
const val xls = "XLS"
const val xml = "XML"
const val json = "JSON"
const val invalid = "INVALID"
const val error = "ERROR"
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment