com.gitlab.mvysny.konsumexml.Whitespace.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konsume-xml Show documentation
Show all versions of konsume-xml Show documentation
Konsume-XML: A simple functional XML parser with no annotations
package com.gitlab.mvysny.konsumexml
private val WHITESPACE_REGEX = Regex("\\s")
private val WHITESPACES_REGEX = Regex("\\s+")
/**
* Processes the whitespaces in XML. Please see [XML Whitespaces](https://www.w3schools.com/xml/schema_facets.asp)
* for more details.
* @author mavi
*/
enum class Whitespace {
/**
* Preserves all whitespaces - will not remove any whitespaces.
*/
preserve {
override fun process(text: String): String = text
},
/**
* This will replace all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces,
* however it will not collapse consecutive whitespaces.
*/
replace {
override fun process(text: String): String =
text.replace(WHITESPACE_REGEX, " ")
},
/**
* This will remove all white space characters (line feeds, tabs, spaces,
* carriage returns are replaced with spaces, leading and trailing spaces are removed,
* and multiple spaces are reduced to a single space).
*/
collapse {
override fun process(text: String): String =
text.trim().replace(WHITESPACES_REGEX, " ")
};
abstract fun process(text: String): String
}