All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitlab.mvysny.konsumexml.Whitespace.kt Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy