Commit 3eb9c8f7 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Fix sax content handler to handle all the things.

parent 7eae3cae
Pipeline #21280 passed with stages
in 4 minutes and 4 seconds
......@@ -20,6 +20,7 @@ package org.memobase.xml
import ch.memobase.reporting.Report
import ch.memobase.reporting.ReportStatus
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import java.io.StringWriter
import org.apache.logging.log4j.LogManager
......@@ -158,15 +159,25 @@ class SAXContentHandler(
when {
currentElementTag == localName -> {
// extract the identifier value based on the given field name from configuration.
// this is used for the message key
if (currentElementTag == identifierFieldName) {
identifier = currentElementContent
}
if (innerElements.isEmpty()) {
if (currentElementContent.isNotEmpty()) {
jsonResult[currentElementTag] = currentElementContent
when (val item = jsonResult[currentElementTag]){
null -> jsonResult[currentElementTag] = currentElementContent
is JsonArray<*> -> item + currentElementContent
else -> jsonResult[currentElementTag] = JsonArray(item, currentElementContent)
}
}
} else {
jsonResult[currentElementTag] = innerElements.toMap()
when (val item = jsonResult[currentElementTag]) {
null -> jsonResult[currentElementTag] = innerElements.toMap()
is JsonArray<*> -> item + innerElements.toMap()
else -> jsonResult[currentElementTag] = JsonArray(item, innerElements.toMap())
}
innerElements.clear()
}
currentElementTag = ""
......
/*
* XML Data Transform
* Copyright (C) 2020 Memoriav
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.memobase.test
import ch.memobase.settings.HeaderMetadata
import com.beust.klaxon.Klaxon
import java.io.File
import java.io.FileOutputStream
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.memobase.models.Message
import org.memobase.xml.XMLTransformer
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Disabled
class LocalTestRun {
private val headerMetadata = HeaderMetadata(
"snp-001",
"1",
"snp",
false,
"record",
"identifierMain",
0, 0, 0, 0
)
@Test
@Disabled
fun `test local folder`() {
val folder = "/home/jonas/memobase/data/snp-001"
val inputFolder = "/home/jonas/memobase/data/test/step-1"
val outputFolder = "/home/jonas/memobase/data/test/step-2"
val transformFile = "/config/transform.xslt"
val xslt = File(folder + transformFile).readBytes()
val transformer = XMLTransformer()
File(inputFolder)
.walk(FileWalkDirection.TOP_DOWN)
.maxDepth(1)
.filter { it.isFile }
.map { Pair(it.name, it) }
.map {
Pair(it.first, Message.fromJson(it.second.readText())[0])
}
.map {
transformer.applyXSLT(
it.first,
headerMetadata,
File(it.second.path).inputStream(),
xslt
)
}
.forEach { result ->
FileOutputStream(File(outputFolder + "/" + result.second.identifier)).use {
it.bufferedWriter().use { writer ->
writer.write(result.second.output.toString())
}
}
}
}
}
Markdown is supported
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