<commit_before>package org.redundent.kotlin.xml

import org.apache.commons.lang3.StringEscapeUtils
import org.w3c.dom.Document
import org.xml.sax.InputSource
import java.io.File
import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.ArrayList
import java.util.Comparator
import java.util.LinkedHashMap
import java.util.NoSuchElementException
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Node as W3CNode

private fun getLineEnding(prettyFormat: Boolean) = if (prettyFormat) System.lineSeparator() else ""

/**
 * Base interface for all elements. You shouldn't have to interact with this interface directly.
 * @author <NAME>
 */
interface Element {
	/**
	 * This method handles creating the xml. Used internally
	 */
	fun render(builder: StringBuilder, indent: String, prettyFormat: Boolean)
}

/**
 * An element type that has some text in it.
 * For example:
 * <loc>http://blog.redundent.org</loc>
 */
open class TextElement internal constructor(val text: String) : Element {
	internal fun isEmpty() = text.trim('\n', '\r').isBlank()

	override fun render(builder: StringBuilder, indent: String, prettyFormat: Boolean) {
		if (isEmpty()) {
			return
		}

		val lineEnding = getLineEnding(prettyFormat)

		builder.append("$indent${StringEscapeUtils.escapeXml11(text)}$lineEnding")
	}
}

/**
 * Similar to a [TextElement] except that the inner text is wrapped inside a <![CDATA[]]> tag.
 */
class CDATAElement internal constructor(text: String) : TextElement(text) {
	override fun render(builder: StringBuilder, indent: String, prettyFormat: Boolean) {
		if (isEmpty()) {
			return
		}

		val lineEnding = getLineEnding(prettyFormat)
		builder.append("$indent<![CDATA[$lineEnding$text$lineEnding]]>$lineEnding")
	}
}

/**
 * Base type for all elements. This is what handles pretty much all the rendering and building.
 */
open class Node(val nodeName: String) : Element {
	/**
	 * The default xmlns for the document. To add other namespaces, use the [namespace] method
	 */
	var xmlns: String?
		get() = this["xmlns"]
		set(value) {
			this["xmlns"] = value
		}

	/**
	 * Whether to include the xml prolog, i.e. <?xml version="1.0" encoding="UTS-8"?>
	 */
	var includeXmlProlog = false

	/**
	 * Sets the encoding on the document. Setting this value will set [includeXmlProlog] to true
	 */
	var encoding: String = StandardCharsets.UTF_8.name()
		set(value) {
			includeXmlProlog = true
			field = value
		}

	/**
	 * Any attributes that belong to this element. You can either interact with this property directly or use the [get] and [set] operators
	 * @sample [set]
	 */
	val attributes = LinkedHashMap<String, Any?>()

	private val _children = ArrayList<Element>()

	private val childOrderMap: Map<String, Int>? by lazy {
		val xmlTypeAnnotation = this::class.annotations.firstOrNull { it is XmlType } as? XmlType ?: return@lazy null
		val childOrder = xmlTypeAnnotation.childOrder

		childOrder.indices.associateBy { childOrder[it] }
	}

	val children: List<Element>
		get() = _children

	private fun <T : Element> initTag(tag: T, init: (T.() -> Unit)?): T {
		if (init != null) {
			tag.init()
		}<commit_msg>#9 - Use Kotlin's charset.<commit_after>package org.redundent.kotlin.xml

import org.apache.commons.lang3.StringEscapeUtils
import org.w3c.dom.Document
import org.xml.sax.InputSource
import java.io.File
import java.io.InputStream
import java.util.ArrayList
import java.util.Comparator
import java.util.LinkedHashMap
import java.util.NoSuchElementException
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Node as W3CNode

private fun getLineEnding(prettyFormat: Boolean) = if (prettyFormat) System.lineSeparator() else ""

/**
 * Base interface for all elements. You shouldn't have to interact with this interface directly.
 * @author <NAME>
 */
interface Element {
	/**
	 * This method handles creating the xml. Used internally
	 */
	fun render(builder: StringBuilder, indent: String, prettyFormat: Boolean)
}

/**
 * An element type that has some text in it.
 * For example:
 * <loc>http://blog.redundent.org</loc>
 */
open class TextElement internal constructor(val text: String) : Element {
	internal fun isEmpty() = text.trim('\n', '\r').isBlank()

	override fun render(builder: StringBuilder, indent: String, prettyFormat: Boolean) {
		if (isEmpty()) {
			return
		}

		val lineEnding = getLineEnding(prettyFormat)

		builder.append("$indent${StringEscapeUtils.escapeXml11(text)}$lineEnding")
	}
}

/**
 * Similar to a [TextElement] except that the inner text is wrapped inside a <![CDATA[]]> tag.
 */
class CDATAElement internal constructor(text: String) : TextElement(text) {
	override fun render(builder: StringBuilder, indent: String, prettyFormat: Boolean) {
		if (isEmpty()) {
			return
		}

		val lineEnding = getLineEnding(prettyFormat)
		builder.append("$indent<![CDATA[$lineEnding$text$lineEnding]]>$lineEnding")
	}
}

/**
 * Base type for all elements. This is what handles pretty much all the rendering and building.
 */
open class Node(val nodeName: String) : Element {
	/**
	 * The default xmlns for the document. To add other namespaces, use the [namespace] method
	 */
	var xmlns: String?
		get() = this["xmlns"]
		set(value) {
			this["xmlns"] = value
		}

	/**
	 * Whether to include the xml prolog, i.e. <?xml version="1.0" encoding="UTS-8"?>
	 */
	var includeXmlProlog = false

	/**
	 * Sets the encoding on the document. Setting this value will set [includeXmlProlog] to true
	 */
	var encoding: String = Charsets.UTF_8.name()
		set(value) {
			includeXmlProlog = true
			field = value
		}

	/**
	 * Any attributes that belong to this element. You can either interact with this property directly or use the [get] and [set] operators
	 * @sample [set]
	 */
	val attributes = LinkedHashMap<String, Any?>()

	private val _children = ArrayList<Element>()

	private val childOrderMap: Map<String, Int>? by lazy {
		val xmlTypeAnnotation = this::class.annotations.firstOrNull { it is XmlType } as? XmlType ?: return@lazy null
		val childOrder = xmlTypeAnnotation.childOrder

		childOrder.indices.associateBy { childOrder[it] }
	}

	val children: List<Element>
		get() = _children

	private fun <T : Element> initTag(tag: T, init: (T.() -> Unit)?): T {
		if (init != null) {
			tag.init()
		}
{'fraction_non_alphanumeric': 0.12055273189326557, 'fraction_numerical': 0.003335451080050826, 'mean_word_length': 4.282718120805369, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 2, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6543024', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1891, 'n_words': 597}