moved JVM specific code into DomJVM.kt
This commit is contained in:
@@ -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<Node> {
|
||||
return if (this == null) {
|
||||
Collections.EMPTY_LIST as List<Node>
|
||||
}
|
||||
else {
|
||||
NodeListAsList(this)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun NodeList?.toElementList(): List<Element> {
|
||||
return if (this == null) {
|
||||
Collections.EMPTY_LIST as List<Element>
|
||||
}
|
||||
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<Node>() {
|
||||
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<Element>() {
|
||||
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 {
|
||||
|
||||
@@ -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<Node> {
|
||||
return if (this == null) {
|
||||
Collections.EMPTY_LIST as List<Node>
|
||||
}
|
||||
else {
|
||||
NodeListAsList(this)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun NodeList?.toElementList(): List<Element> {
|
||||
return if (this == null) {
|
||||
Collections.EMPTY_LIST as List<Element>
|
||||
}
|
||||
else {
|
||||
ElementListAsList(this)
|
||||
}
|
||||
}
|
||||
|
||||
class NodeListAsList(val nodeList: NodeList): AbstractList<Node>() {
|
||||
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<Element>() {
|
||||
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<Node>, xmlDeclaration: Boolean = false): String {
|
||||
public fun nodesToXmlString(nodes: java.lang.Iterable<Node>, xmlDeclaration: Boolean = false): String {
|
||||
// TODO this should work...
|
||||
// return this.map<Node,String>{it.toXmlString()}.makeString("")
|
||||
val builder = StringBuilder()
|
||||
|
||||
Reference in New Issue
Block a user