
com.gitlab.mvysny.konsumexml.KonsumerUtils.kt Maven / Gradle / Ivy
package com.gitlab.mvysny.konsumexml
import com.gitlab.mvysny.konsumexml.stax.StaxParser
import com.gitlab.mvysny.konsumexml.stax.StaxParserFactory
import java.io.File
import java.io.InputStream
/**
* Parses this String as XML.
*/
fun String.konsumeXml(): Konsumer = byteInputStream().konsumeXml()
/**
* Parses this input stream as XML. Don't forget to close returned [Konsumer] - that will also close this input stream.
*
* Uses [StaxParser] to read XML contents; see [StaxParserFactory] on details of how the parser is produced.
*/
fun InputStream.konsumeXml(systemId: String? = null): Konsumer {
val parser: StaxParser = StaxParserFactory.create(this, systemId)
return Konsumer(StaxReader(parser, this), null, KonsumerSettings())
}
/**
* Parses this input stream as XML. It's important to close returned [Konsumer]:
* ```kotlin
* File("in.xml").konsumeXml().use { k ->
* k.child("root") {}
* }
* ```
*
* Uses [StaxParser] to read XML contents; see [StaxParserFactory] on details of how the parser is produced.
*/
fun File.konsumeXml(): Konsumer = inputStream().andTry { it.buffered().konsumeXml(absolutePath) }
/**
* Consumes all of the remaining child elements of given [name]; automatically skips other elements. Doesn't skip
* over text contents - the text contents must be consumed by using the [Konsumer.text] function.
*
* WARNING: This function is dangerous in a way that it will silently throw away all elements that are not listed as
* parameters of the function.
* @param block called for every element with given [name]
*/
@KonsumerDsl
fun Konsumer.allChildrenAutoIgnore(name: String, block: Konsumer.() -> Unit) {
allChildrenAutoIgnore(Names.of(name), block)
}
/**
* Consumes all of the remaining child elements of given [names]; automatically skips other elements. Doesn't skip
* over text contents - the text contents must be consumed by using the [Konsumer.text] function.
*
* WARNING: This function is dangerous in a way that it will silently throw away all elements that are not listed as
* parameters of the function.
* @param block called for every element with given [names]
*/
@KonsumerDsl
fun Konsumer.allChildrenAutoIgnore(names: Names, block: Konsumer.() -> Unit) {
children(anyName) {
if (names.accepts(name!!)) block() else skipContents()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy