package com.kdanmobile.android.lib.material.tokens.category.common


import com.kdanmobile.android.lib.material.tokens.base.BaseXmlHandler
import com.kdanmobile.android.lib.material.tokens.base.XmlParserDataCategory
import org.xml.sax.Attributes

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2023/2/13
 * description: 通用的解析器，获取xml文件已经存在的数据
 * @param dataCategory 要读取的数据类型
 * @see XmlParserDataCategory
 */
class CommonXmlParserHandler(var dataCategory : XmlParserDataCategory) : BaseXmlHandler() {


    override fun startElement(uri: String?, localName: String?, qName: String?, attributes: Attributes?) {
        super.startElement(uri, localName, qName, attributes)
        if (qName.equals(dataCategory.name)) {
            for (i in 0 until attributes!!.length) {
                name = attributes?.getValue(i)
            }
        }
    }


    override fun endElement(uri: String?, localName: String?, qName: String?) {
        super.endElement(uri, localName, qName)
        try {
            if (name.isNullOrEmpty().not() && value.isNullOrEmpty().not() && "\n".equals(value).not()) {
                name = name?.replace("-","_")?.replace(".","_")
                maps[name!!] = value!!
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    override fun characters(ch: CharArray, start: Int, length: Int) {
        super.characters(ch, start, length)
        value = String(ch, start, length)
    }


}