[Core] [Convert] [XML] Support writer

This commit is contained in:
qianmoQ 2024-07-02 12:18:35 +08:00
parent 94a97378b0
commit a2fdc3b27a
5 changed files with 106 additions and 1 deletions

View File

@ -22,4 +22,9 @@ class ConvertResponse
{
_columns = value.toMutableList()
}
override fun toString(): String
{
return "ConvertResponse(path=$path, message=$message, successful=$successful)"
}
}

View File

@ -34,6 +34,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -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

View File

@ -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)
}
}
}
}

View File

@ -0,0 +1 @@
<Root><Node name="Test" age="12"/><Node name="Test1" age="121"/></Root>