All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.neko233.config233.reader.xml.XmlConfigHandler.kt Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package com.neko233.config233.reader.json

import com.neko233.config233.reader.ConfigHandler
import com.neko233.config233.reader.convert.ConfigOrmUtils
import com.neko233.config233.reader.dto.FrontEndConfigDto
import com.neko233.config233.reader.exception.ConfigFileNotFoundException
import com.neko233.config233.reader.utils.StringUtilsForConfig
import com.neko233.easyxml.XML
import java.io.File
import java.nio.charset.StandardCharsets

// xml 文件处理类
class XmlConfigHandler : ConfigHandler {
    override fun typeName(): String {
        return "XML"
    }

    override fun readToFrontEndDataList(
        configName: String,
        configFileFullPath: String,
    ): FrontEndConfigDto {


        val file = File(configFileFullPath)
        if (!file.exists()) {
            throw ConfigFileNotFoundException("XML config file not found. path = ${configFileFullPath}")
        }
        val xml = file.readText(StandardCharsets.UTF_8)

        // 
        val rootXml = XML.parseToObject(xml)

        val dataList: List> = rootXml.children.asSequence()
            .map {
                if ("data".equals(it.nodeName, true)) {
                    return@map it.attributes
                }
                return@map null
            }
            .filterNotNull()
            .toList()

        return FrontEndConfigDto().apply {
            this.type = typeName()
            this.suffix = StringUtilsForConfig.getFileExtensionName(configFileFullPath)
            this.configNameSimple = configName
            this.dataList = ArrayList(dataList)
        }
    }

    override fun  readConfigAndOrm(
        clazz: Class,
        configName: String,
        configFileFullPath: String,
    ): List {

        val file = File(configFileFullPath)
        if (!file.exists()) {
            throw ConfigFileNotFoundException("XML config file not found. path = ${configFileFullPath}")
        }
        val xml = file.readText(StandardCharsets.UTF_8)

        // 
        val rootXml = XML.parseToObject(xml)

        return rootXml.children.asSequence()
            .map {
                if ("data".equals(it.nodeName, true)) {
                    val attributes: MutableMap = it.attributes
                    return@map ConfigOrmUtils.getObjByTextKvMap(attributes, clazz)
                }
                return@map null
            }
            .filterNotNull()
            .toList()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy