libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 3f08fc722ac7c3f29ac1e399dd3825be015da3a3
parent 458ce461c4ef2e3889f608f98cb3d965b7eba3bc
Author: Florian Dold <florian.dold@gmail.com>
Date:   Mon, 18 Nov 2019 21:32:25 +0100

xml parsing/destructoring combinators WIP

Diffstat:
M.idea/dictionaries/dold.xml | 1+
Dsandbox/src/main/kotlin/tech/libeufin/sandbox/XmlBinding.kt | 24------------------------
Asandbox/src/main/kotlin/tech/libeufin/sandbox/XmlCombinators.kt | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asandbox/src/test/kotlin/XmlCombinatorsTest.kt | 36++++++++++++++++++++++++++++++++++++
4 files changed, 113 insertions(+), 24 deletions(-)

diff --git a/.idea/dictionaries/dold.xml b/.idea/dictionaries/dold.xml @@ -2,6 +2,7 @@ <dictionary name="dold"> <words> <w>affero</w> + <w>combinators</w> <w>ebics</w> </words> </dictionary> diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlBinding.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlBinding.kt @@ -1,23 +0,0 @@ -package tech.libeufin.sandbox - -@Retention(AnnotationRetention.RUNTIME) -annotation class XmlSchemaContext - -@Retention(AnnotationRetention.RUNTIME) -annotation class XmlElement - -@Retention(AnnotationRetention.RUNTIME) -annotation class XmlAttribute - -@Retention(AnnotationRetention.RUNTIME) -annotation class XmlValue - -@Retention(AnnotationRetention.RUNTIME) -annotation class XmlWrapper - -@Retention(AnnotationRetention.RUNTIME) -annotation class XmlAdapter - -class XmlBinding<T> { - -} -\ No newline at end of file diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlCombinators.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlCombinators.kt @@ -0,0 +1,76 @@ +package tech.libeufin.sandbox + +import com.sun.xml.txw2.output.IndentingXMLStreamWriter +import java.io.StringWriter +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) + w.writeEndElement() + } + + fun attribute(name: String, value: String) { + w.writeAttribute(name, value) + } + + fun text(content: String) { + w.writeCharacters(content) + } +} + +class XmlDocumentBuilder { + + private var maybeWriter: XMLStreamWriter? = null + + internal var writer: XMLStreamWriter + get() { + val w = maybeWriter + return w ?: throw AssertionError("no writer set") + } + set(w: XMLStreamWriter) { + maybeWriter = w + } + + + fun namespace(prefix: String, uri: String) { + writer.setPrefix(prefix, uri) + } + + fun defaultNamespace(uri: String) { + writer.setDefaultNamespace(uri) + } + + fun root(name: String, f: XmlElementBuilder.() -> Unit) { + val elementBuilder = XmlElementBuilder(writer) + writer.writeStartElement(name) + f(elementBuilder) + writer.writeEndElement() + } +} + +fun constructXml(indent: Boolean = false, f: XmlDocumentBuilder.() -> Unit): String { + val b = XmlDocumentBuilder() + val factory = XMLOutputFactory.newFactory() + factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true) + val stream = StringWriter() + var writer = factory.createXMLStreamWriter(stream) + if (indent) { + writer = IndentingXMLStreamWriter(writer) + } + b.writer = writer + writer.writeStartDocument() + f(b) + writer.writeEndDocument() + return stream.buffer.toString() +} + +class XmlDocumentDestructor { +} + +fun <T>destructXml(input: String, f: XmlDocumentDestructor.() -> T): T { + val d = XmlDocumentDestructor() + return f(d) +} diff --git a/sandbox/src/test/kotlin/XmlCombinatorsTest.kt b/sandbox/src/test/kotlin/XmlCombinatorsTest.kt @@ -0,0 +1,36 @@ +/* + * This file is part of LibEuFin. + * Copyright (C) 2019 Stanisci and Dold. + + * LibEuFin is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation; either version 3, or + * (at your option) any later version. + + * LibEuFin is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General + * Public License for more details. + + * You should have received a copy of the GNU Affero General Public + * License along with LibEuFin; see the file COPYING. If not, see + * <http://www.gnu.org/licenses/> + */ + +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") + } + } + println(s) + } +}