added helper methods for iterating through next/previous nodes or elements on the DOM
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package kotlin.dom
|
package kotlin.dom
|
||||||
|
|
||||||
import kotlin.*
|
import kotlin.*
|
||||||
|
import kotlin.support.*
|
||||||
import kotlin.util.*
|
import kotlin.util.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import org.w3c.dom.*
|
import org.w3c.dom.*
|
||||||
@@ -190,6 +191,45 @@ fun Element.get(selector: String): List<Element> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns an [[Iterator]] over the next siblings of this node */
|
||||||
|
fun Node.nextSiblings() : Iterator<Node> = NextSiblingIterator(this)
|
||||||
|
|
||||||
|
protected class NextSiblingIterator(var node: Node) : AbstractIterator<Node>() {
|
||||||
|
|
||||||
|
override fun computeNext(): Node? {
|
||||||
|
val next = node.getNextSibling()
|
||||||
|
if (next != null) {
|
||||||
|
node = next
|
||||||
|
return next
|
||||||
|
} else {
|
||||||
|
done()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/** Returns an [[Iterator]] over the next siblings of this node */
|
||||||
|
fun Node.previousSiblings() : Iterator<Node> = PreviousSiblingIterator(this)
|
||||||
|
|
||||||
|
protected class PreviousSiblingIterator(var node: Node) : AbstractIterator<Node>() {
|
||||||
|
|
||||||
|
override fun computeNext(): Node? {
|
||||||
|
val next = node.getPreviousSibling()
|
||||||
|
if (next != null) {
|
||||||
|
node = next
|
||||||
|
return next
|
||||||
|
} else {
|
||||||
|
done()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns an [[Iterator]] of all the next [[Element]] siblings */
|
||||||
|
fun Node.nextElements(): Iterator<Element> = nextSiblings().filterIs<Node, Element>()
|
||||||
|
|
||||||
|
/** Returns an [[Iterator]] of all the previous [[Element]] siblings */
|
||||||
|
fun Node.previousElements(): Iterator<Element> = previousSiblings().filterIs<Node, Element>()
|
||||||
|
|
||||||
/** Returns the attribute value or empty string if its not present */
|
/** Returns the attribute value or empty string if its not present */
|
||||||
inline fun Element.attribute(name: String): String {
|
inline fun Element.attribute(name: String): String {
|
||||||
return this.getAttribute(name) ?: ""
|
return this.getAttribute(name) ?: ""
|
||||||
@@ -271,7 +311,7 @@ inline fun NodeList?.toElementList(): List<Element> {
|
|||||||
fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String {
|
fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String {
|
||||||
return if (this == null)
|
return if (this == null)
|
||||||
"" else {
|
"" else {
|
||||||
this.toList().toXmlString(xmlDeclaration)
|
nodesToXmlString(this.toList(), xmlDeclaration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import javax.xml.transform.OutputKeys
|
|||||||
import java.lang.Iterable
|
import java.lang.Iterable
|
||||||
import java.util.List
|
import java.util.List
|
||||||
import java.util.Collection
|
import java.util.Collection
|
||||||
|
import java.io.Writer
|
||||||
|
|
||||||
/** Creates a new document with the given document builder*/
|
/** Creates a new document with the given document builder*/
|
||||||
fun createDocument(builder: DocumentBuilder): Document {
|
fun createDocument(builder: DocumentBuilder): Document {
|
||||||
@@ -39,35 +40,26 @@ fun createTransformer(source: Source? = null, factory: TransformerFactory = Tran
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Converts the node to an XML String */
|
/** Converts the node to an XML String */
|
||||||
fun Node.toXmlString(xmlDeclaration: Boolean = this is Document): String {
|
fun Node.toXmlString(xmlDeclaration: Boolean = false): String {
|
||||||
return nodeToXmlString(this, xmlDeclaration)
|
val writer = StringWriter()
|
||||||
|
writeXmlString(writer, xmlDeclaration)
|
||||||
|
return writer.toString().sure()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Converts the node to an XML String and writes it to the given [[Writer]] */
|
||||||
|
fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit {
|
||||||
|
val transformer = createTransformer()
|
||||||
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, if (xmlDeclaration) "no" else "yes")
|
||||||
|
transformer.transform(DOMSource(this), StreamResult(writer))
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts the collection of nodes to an XML String */
|
/** Converts the collection of nodes to an XML String */
|
||||||
fun java.lang.Iterable<Node>.toXmlString(xmlDeclaration: Boolean = false): String {
|
fun nodesToXmlString(nodes: Iterable<Node>, xmlDeclaration: Boolean = false): String {
|
||||||
// TODO this should work...
|
// TODO this should work...
|
||||||
// return this.map<Node,String>{it.toXmlString()}.join("")
|
// return this.map<Node,String>{it.toXmlString()}.join("")
|
||||||
val builder = StringBuilder()
|
val builder = StringBuilder()
|
||||||
for (n in this) {
|
for (n in nodes) {
|
||||||
builder.append(n.toXmlString(xmlDeclaration))
|
builder.append(n.toXmlString(xmlDeclaration))
|
||||||
}
|
}
|
||||||
return builder.toString().sure()
|
return builder.toString().sure()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
fun Document.toXmlString(xmlDeclaration: Boolean = true): String {
|
|
||||||
return nodeToXmlString(this, xmlDeclaration)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** Converts the node to an XML String */
|
|
||||||
fun nodeToXmlString(node: Node, xmlDeclaration: Boolean): String {
|
|
||||||
val transformer = createTransformer()
|
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, if (xmlDeclaration) "no" else "yes")
|
|
||||||
val buffer = StringWriter()
|
|
||||||
transformer.transform(DOMSource(node), StreamResult(buffer))
|
|
||||||
return buffer.toString().sure()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ class DomBuilderTest() : TestCase() {
|
|||||||
fail("Not an Element $grandChild")
|
fail("Not an Element $grandChild")
|
||||||
}
|
}
|
||||||
val children = doc.rootElement.children()
|
val children = doc.rootElement.children()
|
||||||
val xml = children.toXmlString()
|
val xml = nodesToXmlString(children)
|
||||||
println("root element has children: ${xml}")
|
println("root element has children: ${xml}")
|
||||||
assertEquals(1, children.size())
|
assertEquals(1, children.size())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user