gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [taler-libeufin] 24/25: Move XMLManagement.java to Kotlin.


From: gnunet
Subject: [GNUnet-SVN] [taler-libeufin] 24/25: Move XMLManagement.java to Kotlin.
Date: Fri, 20 Sep 2019 19:33:02 +0200

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

marcello pushed a commit to branch master
in repository libeufin.

commit fb7d3ff96812239f925a0d8a088c2bfd0af302c6
Author: Marcello Stanisci <address@hidden>
AuthorDate: Fri Sep 20 16:59:01 2019 +0200

    Move XMLManagement.java to Kotlin.
---
 src/main/java/tech/libeufin/XMLManagement.java     | 201 -------------------
 .../java/tech/libeufin/messages/HEVResponse.java   |   4 +
 src/main/kotlin/Main.kt                            |  10 +-
 src/main/kotlin/tech/libeufin/XMLManagement.kt     | 220 +++++++++++++++++++++
 4 files changed, 231 insertions(+), 204 deletions(-)

diff --git a/src/main/java/tech/libeufin/XMLManagement.java 
b/src/main/java/tech/libeufin/XMLManagement.java
deleted file mode 100644
index 664e0d6..0000000
--- a/src/main/java/tech/libeufin/XMLManagement.java
+++ /dev/null
@@ -1,201 +0,0 @@
-package tech.libeufin;
-
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-
-import java.io.*;
-import javax.xml.XMLConstants;
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.*;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.validation.*; // has SchemaFactory
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import org.w3c.dom.Document;
-import tech.libeufin.messages.HEVResponseDataType;
-
-/**
- * This class takes care of importing XSDs and validate
- * XMLs against those.
- */
-
-public class XMLManagement {
-
-    /**
-     * Bundle of all the XSDs loaded in memory.
-     */
-    Schema bundle;
-    Validator validator;
-
-    /**
-     * Load all the XSDs from disk.
-     */
-    public XMLManagement(){
-        ClassLoader classLoader = this.getClass().getClassLoader();
-        InputStream ebics_hev_path = 
classLoader.getResourceAsStream("ebics_hev.xsd");
-        Source schemas[] = {new StreamSource(ebics_hev_path)
-                // other StreamSources for other schemas here ..
-        };
-
-        try {
-            SchemaFactory sf = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
-            this.bundle = sf.newSchema(schemas);
-            this.validator = this.bundle.newValidator();
-        } catch (SAXException e) {
-            e.printStackTrace();
-        }
-    }
-
-    /**
-     * Parse string into XML DOM.
-     * @param xmlString the string to parse.
-     * @return the DOM representing @a xmlString
-     */
-    static public Document parseStringIntoDom(String xmlString) {
-
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-
-        try {
-
-            InputStream xmlInputStream = new 
ByteArrayInputStream(xmlString.getBytes());
-            // Source xmlSource = new StreamSource(xmlInputStream);
-
-            DocumentBuilder builder = factory.newDocumentBuilder();
-            Document document = builder.parse(new InputSource(xmlInputStream));
-
-            return document;
-
-        } catch (ParserConfigurationException e) {
-            System.out.println("Could not parse string into DOM: " + e);
-        } catch (SAXException e) {
-            System.out.println("Could not parse string into DOM: " + e);
-        } catch (IOException e) {
-            System.out.println("Could not parse string into DOM: " + e);
-        }
-
-        return null;
-    }
-
-    /**
-     *
-     * @param xmlDoc the XML document to validate
-     * @return true when validation passes, false otherwise
-     */
-    public boolean validate(Source xmlDoc){
-        try{
-            this.validator.validate(xmlDoc);
-        } catch (SAXException e) {
-            System.out.println("Validation did not pass " + e);
-            return false;
-        } catch (IOException e) {
-            System.out.println("Could not pass XML to validator.");
-            return false;
-        }
-        return true;
-    }
-
-    /**
-     * Craft object to be passed to the XML validator.
-     * @param xmlString XML body, as read from the POST body.
-     * @return InputStream object, as wanted by the validator.
-     */
-    public boolean validate(String xmlString){
-        InputStream xmlInputStream = new 
ByteArrayInputStream(xmlString.getBytes());
-        Source xmlSource = new StreamSource(xmlInputStream);
-        return this.validate(xmlSource);
-    }
-
-    /**
-     * Return the DOM representation of the Java object, using the JAXB
-     * interface.  FIXME: narrow input type to JAXB type!
-     *
-     * @param object to be transformed into DOM.  Typically, the object
-     *               has already got its setters called.
-     * @return the DOM Document, or null (if errors occur).
-     */
-    static public Document convertJaxbToDom(JAXBElement<?> object) {
-
-        try {
-            JAXBContext jc = JAXBContext.newInstance("tech.libeufin.messages");
-
-            /* Make the target document.  */
-            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-            DocumentBuilder db = dbf.newDocumentBuilder();
-            Document document = db.newDocument();
-
-            /* Marshalling the object into the document.  */
-            Marshaller m = jc.createMarshaller();
-            m.marshal(object, document); // document absorbed XML!
-            return document;
-
-        } catch (JAXBException e) {
-            System.out.println(e);
-        } catch (ParserConfigurationException e) {
-            System.out.println(e);
-        }
-
-        return null;
-    }
-
-    /**
-     * Extract String from DOM.
-     *
-     * @param document the DOM to extract the string from.
-     * @return the final String, or null if errors occur.
-     */
-    static public String getStringFromDocument(Document document){
-
-        try {
-            /* Make Transformer.  */
-            TransformerFactory tf = TransformerFactory.newInstance();
-            Transformer t = tf.newTransformer();
-            // t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-            t.setOutputProperty(OutputKeys.INDENT, "no");
-
-            /* Make string writer.  */
-            StringWriter sw = new StringWriter();
-
-            /* Extract string.  */
-            t.transform(new DOMSource(document), new StreamResult(sw));
-            String output = sw.toString();
-
-            return output;
-
-        } catch (TransformerConfigurationException e) {
-            System.out.println(e);
-        } catch (TransformerException e) {
-            System.out.println(e);
-        }
-
-        return null;
-    }
-
-    /**
-     * Extract String from JAXB.
-     *
-     * @param object the JAXB instance
-     * @return String representation of @a object, or null if errors occur
-     */
-    public static String getStringFromJaxb(JAXBElement<?> object){
-        try {
-            JAXBContext jc = JAXBContext.newInstance("tech.libeufin.messages");
-            StringWriter sw = new StringWriter();
-
-            /* Getting the string.  */
-            Marshaller m = jc.createMarshaller();
-            m.marshal(object, sw);
-            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
-
-            return sw.toString();
-        } catch (JAXBException e) {
-            e.printStackTrace();
-            return null;
-        }
-    }
-};
\ No newline at end of file
diff --git a/src/main/java/tech/libeufin/messages/HEVResponse.java 
b/src/main/java/tech/libeufin/messages/HEVResponse.java
index d37ab98..a1a4a58 100644
--- a/src/main/java/tech/libeufin/messages/HEVResponse.java
+++ b/src/main/java/tech/libeufin/messages/HEVResponse.java
@@ -14,6 +14,10 @@ public class HEVResponse {
         this.value.setSystemReturnCode(srt);
     }
 
+    /**
+     * Instantiate the root element.
+     * @return the JAXB object.
+     */
     public JAXBElement<HEVResponseDataType> makeHEVResponse(){
         ObjectFactory of = new ObjectFactory();
         return of.createEbicsHEVResponse(this.value);
diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
index 2655b14..8eb3475 100644
--- a/src/main/kotlin/Main.kt
+++ b/src/main/kotlin/Main.kt
@@ -27,6 +27,8 @@ import io.ktor.routing.*
 import io.ktor.server.engine.*
 import io.ktor.server.netty.*
 import tech.libeufin.messages.HEVResponse
+import tech.libeufin.messages.HEVResponseDataType
+import javax.xml.bind.JAXBElement
 
 fun main(args: Array<String>) {
     var xmlProcess = XMLManagement();
@@ -51,7 +53,7 @@ fun main(args: Array<String>) {
                     return@post
                 }
 
-                val bodyDocument = XMLManagement.parseStringIntoDom(body)
+                val bodyDocument = xmlProcess.parseStringIntoDom(body)
                 if (null == bodyDocument)
                 {
                     /* Should never happen.  */
@@ -65,10 +67,12 @@ fun main(args: Array<String>) {
                 {
                     /* known type, and already valid here! */
                     val hevResponse: HEVResponse = HEVResponse("rc", "rt")
-                    val responseText: String = 
XMLManagement.getStringFromJaxb(hevResponse.makeHEVResponse())
+                    val jaxbHEV: JAXBElement<HEVResponseDataType> = 
hevResponse.makeHEVResponse()
 
+                    val responseText: String? = 
xmlProcess.getStringFromJaxb(jaxbHEV)
+                    // FIXME: check if String is actually non-NULL!
                     call.respondText(contentType = ContentType.Application.Xml,
-                        status = HttpStatusCode.OK) {responseText};
+                        status = HttpStatusCode.OK) {responseText.toString()};
                     return@post
                 }
 
diff --git a/src/main/kotlin/tech/libeufin/XMLManagement.kt 
b/src/main/kotlin/tech/libeufin/XMLManagement.kt
new file mode 100644
index 0000000..2a119fd
--- /dev/null
+++ b/src/main/kotlin/tech/libeufin/XMLManagement.kt
@@ -0,0 +1,220 @@
+package tech.libeufin;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import java.io.*;
+import javax.xml.XMLConstants;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.*; // has SchemaFactory
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import org.w3c.dom.Document;
+import tech.libeufin.messages.HEVResponseDataType;
+
+/**
+ * This class takes care of importing XSDs and validate
+ * XMLs against those.
+ */
+
+public class XMLManagement() {
+
+    /**
+     * Bundle of all the XSDs loaded in memory, from disk.
+     */
+    private val bundle = {
+        val classLoader = ClassLoader.getSystemClassLoader()
+        val ebicsHevPath = classLoader.getResourceAsStream("ebics_hev.xsd");
+        val schemas = arrayOf(StreamSource(ebicsHevPath)
+            // other StreamSources for other schemas here ..
+        )
+
+        try {
+            val sf = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+            // bundle = sf.newSchema(schemas);
+            // validator = bundle.newValidator();
+            sf.newSchema()
+        } catch (e: SAXException) {
+            e.printStackTrace();
+            null
+        }
+    }()
+    private val validator = bundle?.newValidator()
+
+    /**
+     * Parse string into XML DOM.
+     * @param xmlString the string to parse.
+     * @return the DOM representing @a xmlString
+     */
+    // static public Document parseStringIntoDom(String xmlString) {
+    fun parseStringIntoDom(xmlString: String): Document? {
+
+        // DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
+        val factory = DocumentBuilderFactory.newInstance()
+
+        try {
+
+            // InputStream xmlInputStream = new 
ByteArrayInputStream(xmlString.getBytes());
+            val xmlInputStream = ByteArrayInputStream(xmlString.toByteArray())
+            // Source xmlSource = new StreamSource(xmlInputStream);
+
+            // DocumentBuilder builder = factory.newDocumentBuilder();
+            val builder = factory.newDocumentBuilder();
+            // Document document = builder.parse(new 
InputSource(xmlInputStream));
+            val document = builder.parse(InputSource(xmlInputStream));
+
+            return document;
+
+        } catch (e: ParserConfigurationException) {
+            e.printStackTrace()
+        } catch (e: SAXException) {
+            e.printStackTrace()
+        } catch (e: IOException) {
+            e.printStackTrace()
+        }
+        return null;
+    }
+
+    /**
+     *
+     * @param xmlDoc the XML document to validate
+     * @return true when validation passes, false otherwise
+     */
+    // public boolean validate(Source xmlDoc){
+    fun validate(xmlDoc: Source): Boolean {
+        try{
+            validator?.validate(xmlDoc);
+        } catch (e: SAXException) {
+            e.printStackTrace()
+            return false;
+        } catch (e: IOException) {
+            e.printStackTrace()
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Craft object to be passed to the XML validator.
+     * @param xmlString XML body, as read from the POST body.
+     * @return InputStream object, as wanted by the validator.
+     */
+    fun validate(xmlString: String): Boolean {
+        // InputStream xmlInputStream = new 
ByteArrayInputStream(xmlString.getBytes());
+        val xmlInputStream = ByteArrayInputStream(xmlString.toByteArray())
+        // Source xmlSource = new StreamSource(xmlInputStream);
+        val xmlSource = StreamSource(xmlInputStream)
+        return validate(xmlSource);
+    }
+
+    /**
+     * Return the DOM representation of the Java object, using the JAXB
+     * interface.  FIXME: narrow input type to JAXB type!
+     *
+     * @param object to be transformed into DOM.  Typically, the object
+     *               has already got its setters called.
+     * @return the DOM Document, or null (if errors occur).
+     */
+    // static public Document convertJaxbToDom(JAXBElement<?> object) {
+    fun convertJaxbToDom(obj: JAXBElement<Unit>): Document? {
+
+        try {
+            // JAXBContext jc = 
JAXBContext.newInstance("tech.libeufin.messages");
+            val jc = JAXBContext.newInstance("tech.libeufin.messages");
+
+            /* Make the target document.  */
+            // DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
+            val dbf = DocumentBuilderFactory.newInstance()
+            // DocumentBuilder db = dbf.newDocumentBuilder();
+            val db = dbf.newDocumentBuilder();
+            // Document document = db.newDocument();
+            val document = db.newDocument();
+
+            /* Marshalling the object into the document.  */
+            // Marshaller m = jc.createMarshaller();
+            val m = jc.createMarshaller()
+            m.marshal(obj, document); // document absorbed XML!
+            return document;
+
+        } catch (e: JAXBException) {
+            e.printStackTrace()
+        } catch (e: ParserConfigurationException) {
+            e.printStackTrace()
+        }
+
+        return null;
+    }
+
+    /**
+     * Extract String from DOM.
+     *
+     * @param document the DOM to extract the string from.
+     * @return the final String, or null if errors occur.
+     */
+    // static public String getStringFromDocument(Document document){
+    fun getStringFromDocument(document: Document): String? {
+
+        try {
+            /* Make Transformer.  */
+            // TransformerFactory tf = TransformerFactory.newInstance();
+            val tf = TransformerFactory.newInstance();
+            val t = tf.newTransformer();
+            // Transformer t = tf.newTransformer();
+            // t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+            t.setOutputProperty(OutputKeys.INDENT, "no");
+
+            /* Make string writer.  */
+            val sw = StringWriter();
+            // StringWriter sw = new StringWriter();
+
+            /* Extract string.  */
+            // t.transform(new DOMSource(document), new StreamResult(sw));
+            t.transform(DOMSource(document), StreamResult(sw))
+            // String output = sw.toString();
+            val output = sw.toString()
+
+            return output;
+
+        } catch (e: TransformerConfigurationException) {
+            e.printStackTrace()
+        } catch (e: TransformerException) {
+            e.printStackTrace()
+        }
+        return null;
+    }
+
+    /**
+     * Extract String from JAXB.
+     *
+     * @param object the JAXB instance
+     * @return String representation of @a object, or null if errors occur
+     */
+    // public static String getStringFromJaxb(JAXBElement<?> object){
+    fun <T>getStringFromJaxb(obj: JAXBElement<T>): String? {
+        try {
+            // JAXBContext jc = 
JAXBContext.newInstance("tech.libeufin.messages");
+            val jc = JAXBContext.newInstance("tech.libeufin.messages")
+            // StringWriter sw = new StringWriter();
+            val sw = StringWriter();
+
+            /* Getting the string.  */
+            // Marshaller m = jc.createMarshaller();
+            val m = jc.createMarshaller();
+            m.marshal(obj, sw);
+            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+            return sw.toString();
+
+        } catch (e: JAXBException) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+};
\ No newline at end of file

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



reply via email to

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