From a2fdc3b27a293103e7972e306289dfa87cf94484 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 2 Jul 2024 12:18:35 +0800 Subject: [PATCH] [Core] [Convert] [XML] Support writer --- .../datacap/convert/model/ConvertResponse.kt | 5 ++ convert/datacap-convert-xml/pom.xml | 5 ++ .../edurt/datacap/convert/xml/XmlConvert.kt | 50 ++++++++++++++++++- .../datacap/convert/xml/XmlConvertTest.kt | 46 +++++++++++++++++ convert/datacap-convert-xml/test.xml | 1 + 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 convert/datacap-convert-xml/src/test/kotlin/io/edurt/datacap/convert/xml/XmlConvertTest.kt create mode 100644 convert/datacap-convert-xml/test.xml diff --git a/convert/datacap-convert-spi/src/main/kotlin/io/edurt/datacap/convert/model/ConvertResponse.kt b/convert/datacap-convert-spi/src/main/kotlin/io/edurt/datacap/convert/model/ConvertResponse.kt index fc059515..46f582d2 100644 --- a/convert/datacap-convert-spi/src/main/kotlin/io/edurt/datacap/convert/model/ConvertResponse.kt +++ b/convert/datacap-convert-spi/src/main/kotlin/io/edurt/datacap/convert/model/ConvertResponse.kt @@ -22,4 +22,9 @@ class ConvertResponse { _columns = value.toMutableList() } + + override fun toString(): String + { + return "ConvertResponse(path=$path, message=$message, successful=$successful)" + } } diff --git a/convert/datacap-convert-xml/pom.xml b/convert/datacap-convert-xml/pom.xml index 9b217712..e7b53a53 100644 --- a/convert/datacap-convert-xml/pom.xml +++ b/convert/datacap-convert-xml/pom.xml @@ -34,6 +34,11 @@ com.fasterxml.jackson.core jackson-core + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + diff --git a/convert/datacap-convert-xml/src/main/kotlin/io/edurt/datacap/convert/xml/XmlConvert.kt b/convert/datacap-convert-xml/src/main/kotlin/io/edurt/datacap/convert/xml/XmlConvert.kt index 6c518b1b..d16362c9 100644 --- a/convert/datacap-convert-xml/src/main/kotlin/io/edurt/datacap/convert/xml/XmlConvert.kt +++ b/convert/datacap-convert-xml/src/main/kotlin/io/edurt/datacap/convert/xml/XmlConvert.kt @@ -1,11 +1,21 @@ package io.edurt.datacap.convert.xml +import com.fasterxml.jackson.core.JsonEncoding +import com.fasterxml.jackson.dataformat.xml.XmlFactory +import com.fasterxml.jackson.dataformat.xml.XmlMapper +import io.edurt.datacap.common.utils.DateUtils import io.edurt.datacap.convert.Convert +import io.edurt.datacap.convert.FileConvert.formatFile import io.edurt.datacap.convert.model.ConvertRequest import io.edurt.datacap.convert.model.ConvertResponse +import org.slf4j.LoggerFactory.getLogger class XmlConvert : Convert { + private val log = getLogger(this::class.java) + val root = "Root" + val node = "Node" + override fun format(request: ConvertRequest): ConvertResponse { TODO("Not yet implemented") @@ -18,7 +28,45 @@ class XmlConvert : Convert override fun writer(request: ConvertRequest): ConvertResponse { - TODO("Not yet implemented") + val response = ConvertResponse() + try + { + log.info("${name()} format start time [ ${DateUtils.now()} ]") + val file = formatFile(request, name()) + log.info("${name()} writer file absolute path [ ${file.absolutePath} ]") + + val factory = XmlFactory() + factory.createGenerator(file, JsonEncoding.UTF8) + .use { generator -> + generator.codec = XmlMapper() + val staxWriter = generator.staxWriter + staxWriter.writeStartElement(root) + request.columns + .forEach { column -> + staxWriter.writeStartElement(node) + for (headerIndex in request.headers.indices) + { + when (column) + { + is List<*> -> staxWriter.writeAttribute(request.headers[headerIndex].toString(), column[headerIndex].toString()) + else -> throw UnsupportedOperationException("Unsupported column type") + } + } + staxWriter.writeEndElement() + } + staxWriter.writeEndElement() + } + log.info("${name()} format end time [ ${DateUtils.now()} ]") + response.path = file.absolutePath + response.successful = true + } + catch (e: Exception) + { + e.printStackTrace() + response.successful = false + response.message = e.message + } + return response } override fun reader(request: ConvertRequest): ConvertResponse diff --git a/convert/datacap-convert-xml/src/test/kotlin/io/edurt/datacap/convert/xml/XmlConvertTest.kt b/convert/datacap-convert-xml/src/test/kotlin/io/edurt/datacap/convert/xml/XmlConvertTest.kt new file mode 100644 index 00000000..906953e6 --- /dev/null +++ b/convert/datacap-convert-xml/src/test/kotlin/io/edurt/datacap/convert/xml/XmlConvertTest.kt @@ -0,0 +1,46 @@ +package io.edurt.datacap.convert.xml + +import com.google.inject.Guice.createInjector +import com.google.inject.Injector +import io.edurt.datacap.convert.ConvertFilter +import io.edurt.datacap.convert.ConvertManager +import io.edurt.datacap.convert.model.ConvertRequest +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.slf4j.LoggerFactory.getLogger + +class XmlConvertTest +{ + private val log = getLogger(this::class.java) + private val name = "Xml" + private var injector: Injector? = null + private val request: ConvertRequest = ConvertRequest() + + @Before + fun before() + { + injector = createInjector(ConvertManager()) + + request.name = "test" + request.path = System.getProperty("user.dir") + request.headers = listOf("name", "age") + + val l1 = listOf("Test", 12) + val l2 = listOf("Test1", 121) + request.columns = listOf(l1, l2) + } + + @Test + fun testWriter() + { + injector?.let { injector -> + ConvertFilter.filter(injector, name) + .ifPresent { file -> + val response = file.writer(request) + log.info("Response: {}", response.toString()) + assertTrue(response.successful == true) + } + } + } +} diff --git a/convert/datacap-convert-xml/test.xml b/convert/datacap-convert-xml/test.xml new file mode 100644 index 00000000..afeef37e --- /dev/null +++ b/convert/datacap-convert-xml/test.xml @@ -0,0 +1 @@ + \ No newline at end of file