com.likethesalad.placeholder.utils.XmlUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of string-reference Show documentation
Show all versions of string-reference Show documentation
This is a Gradle plugin for Android applications which resolves XML string references in other XML strings.
package com.likethesalad.placeholder.utils
import com.likethesalad.placeholder.data.Constants.Companion.XML_STRING_TAG
import com.likethesalad.placeholder.models.StringResourceModel
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.Node
class XmlUtils {
companion object {
fun stringResourceModelToElement(document: Document, stringResourceModel: StringResourceModel): Element {
val strElement = document.createElement(XML_STRING_TAG)
strElement.textContent = stringResourceModel.content
for (it in stringResourceModel.attributes) {
strElement.setAttribute(it.key, it.value)
}
return strElement
}
fun nodeToStringResourceModel(node: Node): StringResourceModel {
val attributesMap = mutableMapOf()
val attributesNodes = node.attributes
for (it in 0 until attributesNodes.length) {
val attr = attributesNodes.item(it)
attributesMap[attr.nodeName] = attr.textContent
}
return StringResourceModel(attributesMap, node.textContent)
}
}
}