From 629f0e6f6b94d05b671663f615d0c3a8d9961e83 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 29 May 2012 08:48:13 +0100 Subject: [PATCH] moved JVM specific code into DomJVM.kt --- libraries/stdlib/src/kotlin/dom/Dom.kt | 59 ---------------- libraries/stdlib/src/kotlin/dom/DomJVM.kt | 86 ++++++++++++++++++----- 2 files changed, 70 insertions(+), 75 deletions(-) diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 46443d72fde..7e2ce26b8ff 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -2,7 +2,6 @@ package kotlin.dom import kotlin.* import kotlin.support.* -import kotlin.util.* import java.util.* import org.w3c.dom.* @@ -299,64 +298,6 @@ val NodeList?.last : Node? get() = this.tail -inline fun NodeList?.toList(): List { - return if (this == null) { - Collections.EMPTY_LIST as List - } - else { - NodeListAsList(this) - } -} - -inline fun NodeList?.toElementList(): List { - return if (this == null) { - Collections.EMPTY_LIST as List - } - else { - ElementListAsList(this) - } -} - -/** Converts the node list to an XML String */ -fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String { - return if (this == null) - "" else { - nodesToXmlString(this.toList(), xmlDeclaration) - } -} - -class NodeListAsList(val nodeList: NodeList): AbstractList() { - override fun get(index: Int): Node { - val node = nodeList.item(index) - if (node == null) { - throw IndexOutOfBoundsException("NodeList does not contain a node at index: " + index) - } else { - return node - } - } - - override fun size(): Int = nodeList.getLength() -} - -class ElementListAsList(val nodeList: NodeList): AbstractList() { - override fun get(index: Int): Element { - val node = nodeList.item(index) - if (node is Element) { - return node - } else { - if (node == null) { - throw IndexOutOfBoundsException("NodeList does not contain a node at index: " + index) - } else { - throw IllegalArgumentException("Node is not an Element as expected but is $node") - } - } - } - - override fun size(): Int = nodeList.getLength() - -} - - // Syntax sugar inline fun Node.plus(child: Node?): Node { diff --git a/libraries/stdlib/src/kotlin/dom/DomJVM.kt b/libraries/stdlib/src/kotlin/dom/DomJVM.kt index 3cbce87851c..c6e338ee881 100644 --- a/libraries/stdlib/src/kotlin/dom/DomJVM.kt +++ b/libraries/stdlib/src/kotlin/dom/DomJVM.kt @@ -3,25 +3,79 @@ */ package kotlin.dom -import org.w3c.dom.* -import javax.xml.parsers.DocumentBuilder -import javax.xml.parsers.DocumentBuilderFactory -import javax.xml.transform.Transformer -import javax.xml.transform.TransformerFactory -import javax.xml.transform.Source -import javax.xml.transform.dom.DOMSource -import javax.xml.transform.stream.StreamResult - -import java.io.StringWriter -import javax.xml.transform.OutputKeys -import java.lang.Iterable -import java.util.List -import java.util.Collection -import java.io.Writer import java.io.File import java.io.InputStream +import java.io.StringWriter +import java.io.Writer +import java.util.* +import javax.xml.parsers.DocumentBuilder +import javax.xml.parsers.DocumentBuilderFactory +import javax.xml.transform.OutputKeys +import javax.xml.transform.Source +import javax.xml.transform.Transformer +import javax.xml.transform.TransformerFactory +import javax.xml.transform.dom.DOMSource +import javax.xml.transform.stream.StreamResult +import org.w3c.dom.* import org.xml.sax.InputSource +/** Converts the node list to an XML String */ +fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String { + return if (this == null) + "" else { + nodesToXmlString(this.toList(), xmlDeclaration) + } +} + +inline fun NodeList?.toList(): List { + return if (this == null) { + Collections.EMPTY_LIST as List + } + else { + NodeListAsList(this) + } +} + +inline fun NodeList?.toElementList(): List { + return if (this == null) { + Collections.EMPTY_LIST as List + } + else { + ElementListAsList(this) + } +} + +class NodeListAsList(val nodeList: NodeList): AbstractList() { + override fun get(index: Int): Node { + val node = nodeList.item(index) + if (node == null) { + throw IndexOutOfBoundsException("NodeList does not contain a node at index: " + index) + } else { + return node + } + } + + override fun size(): Int = nodeList.getLength() +} + +class ElementListAsList(val nodeList: NodeList): AbstractList() { + override fun get(index: Int): Element { + val node = nodeList.item(index) + if (node is Element) { + return node + } else { + if (node == null) { + throw IndexOutOfBoundsException("NodeList does not contain a node at index: " + index) + } else { + throw IllegalArgumentException("Node is not an Element as expected but is $node") + } + } + } + + override fun size(): Int = nodeList.getLength() + +} + /** Creates a new document with the given document builder*/ public fun createDocument(builder: DocumentBuilder): Document { return builder.newDocument().sure() @@ -100,7 +154,7 @@ public fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit { } /** Converts the collection of nodes to an XML String */ -public fun nodesToXmlString(nodes: Iterable, xmlDeclaration: Boolean = false): String { +public fun nodesToXmlString(nodes: java.lang.Iterable, xmlDeclaration: Boolean = false): String { // TODO this should work... // return this.map{it.toXmlString()}.makeString("") val builder = StringBuilder()