gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated: xml parsing/destructoring combinators


From: gnunet
Subject: [libeufin] branch master updated: xml parsing/destructoring combinators WIP
Date: Mon, 18 Nov 2019 21:32:31 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 3f08fc7  xml parsing/destructoring combinators WIP
3f08fc7 is described below

commit 3f08fc722ac7c3f29ac1e399dd3825be015da3a3
Author: Florian Dold <address@hidden>
AuthorDate: Mon Nov 18 21:32:25 2019 +0100

    xml parsing/destructoring combinators WIP
---
 .idea/dictionaries/dold.xml                        |  1 +
 .../kotlin/tech/libeufin/sandbox/XmlBinding.kt     | 23 -------
 .../kotlin/tech/libeufin/sandbox/XmlCombinators.kt | 76 ++++++++++++++++++++++
 sandbox/src/test/kotlin/XmlCombinatorsTest.kt      | 36 ++++++++++
 4 files changed, 113 insertions(+), 23 deletions(-)

diff --git a/.idea/dictionaries/dold.xml b/.idea/dictionaries/dold.xml
index 8a95d7e..5040b3a 100644
--- 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
deleted file mode 100644
index 79a77ca..0000000
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/XmlBinding.kt
+++ /dev/null
@@ -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
new file mode 100644
index 0000000..054e17e
--- /dev/null
+++ 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
new file mode 100644
index 0000000..721481a
--- /dev/null
+++ 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)
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]