org.redundent.kotlin.xml.TextElement.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-xml-builder Show documentation
Show all versions of kotlin-xml-builder Show documentation
A lightweight type safe builder to build xml documents in Kotlin
package org.redundent.kotlin.xml
/**
* An element type that has some text in it.
* For example:
* http://blog.redundent.org
*/
open class TextElement internal constructor(val text: String) : Element {
private fun isEmpty() = text.trim('\n', '\r').isBlank()
override fun render(builder: Appendable, indent: String, printOptions: PrintOptions) {
if (isEmpty()) {
return
}
val lineEnding = getLineEnding(printOptions)
builder.append("$indent${renderedText(printOptions)}$lineEnding")
}
internal fun renderSingleLine(builder: Appendable, printOptions: PrintOptions) {
builder.append(renderedText(printOptions))
}
internal open fun renderedText(printOptions: PrintOptions): String? =
escapeValue(text, printOptions.xmlVersion, printOptions.useCharacterReference)
override fun equals(other: Any?): Boolean = other is TextElement && other.text == text
override fun hashCode(): Int = text.hashCode()
}