commit e6cc1fc96cd2b431f58abc735cb04f01e9123f8e
parent 609683b878213c5d93d085730b48197e7977305d
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Sat, 7 Dec 2019 00:44:59 +0100
implementing recursive definition of elements
Diffstat:
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlCombinators.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlCombinators.kt
@@ -1,15 +1,31 @@
package tech.libeufin.sandbox
import com.sun.xml.txw2.output.IndentingXMLStreamWriter
+import org.jetbrains.exposed.sql.Op
import java.io.StringWriter
+import java.util.*
import javax.xml.stream.XMLOutputFactory
import javax.xml.stream.XMLStreamWriter
class XmlElementBuilder(val w: XMLStreamWriter) {
- fun element(name: String, f: XmlElementBuilder.() -> Unit = {}) {
- w.writeStartElement(name)
- f(this)
+
+ fun element(path: MutableList<String>, f: XmlElementBuilder.() -> Unit = {}) {
+
+ if (path.isEmpty()) {
+ f(this)
+ return
+ }
+
+ w.writeStartElement(path.removeAt(0))
+ this.element(path, f)
w.writeEndElement()
+
+ }
+
+ fun element(path: String, f: XmlElementBuilder.() -> Unit = {}) {
+
+ val splitPath = path.trim('/').split("/").toMutableList()
+ this.element(splitPath, f)
}
fun attribute(name: String, value: String) {
diff --git a/sandbox/src/test/kotlin/XmlCombinatorsTest.kt b/sandbox/src/test/kotlin/XmlCombinatorsTest.kt
@@ -22,13 +22,20 @@ package tech.libeufin.sandbox
import org.junit.Test
class XmlCombinatorsTest {
+
@Test
fun testBasicXmlBuilding() {
val s = constructXml(indent = true) {
namespace("ebics", "urn:org:ebics:H004")
root("ebics:ebicsRequest") {
attribute("version", "H004")
- element("foo")
+ element("a/b/c") {
+ attribute("attribute-of", "c")
+ element("//d/e/f//") {
+ attribute("nested", "true")
+ element("g/h/")
+ }
+ }
}
}
println(s)