gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [taler-libeufin] 25/25: Finish translating Java into Kotlin


From: gnunet
Subject: [GNUnet-SVN] [taler-libeufin] 25/25: Finish translating Java into Kotlin.
Date: Fri, 20 Sep 2019 19:33:03 +0200

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

marcello pushed a commit to branch master
in repository libeufin.

commit 62598ecfa3a6722ff614eb9432660bfb38066cc2
Author: Marcello Stanisci <address@hidden>
AuthorDate: Fri Sep 20 19:27:40 2019 +0200

    Finish translating Java into Kotlin.
---
 src/main/kotlin/Main.kt                            | 10 ++--
 src/main/kotlin/tech/libeufin/XMLManagement.kt     | 59 +++++++---------------
 .../tech/libeufin/messages/HEVResponse.kt}         | 20 +++++++-
 src/test/java/XMLManagementTest.java               | 46 -----------------
 src/test/resources/ebics_hev.xml                   |  6 +--
 5 files changed, 45 insertions(+), 96 deletions(-)

diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
index 8eb3475..dba54c0 100644
--- a/src/main/kotlin/Main.kt
+++ b/src/main/kotlin/Main.kt
@@ -28,6 +28,7 @@ import io.ktor.server.engine.*
 import io.ktor.server.netty.*
 import tech.libeufin.messages.HEVResponse
 import tech.libeufin.messages.HEVResponseDataType
+import javax.swing.text.Document
 import javax.xml.bind.JAXBElement
 
 fun main(args: Array<String>) {
@@ -44,7 +45,7 @@ fun main(args: Array<String>) {
                 val body: String = call.receiveText()
                 logger.debug("Body: $body")
 
-                val isValid = xmlProcess.validate(body)
+                val isValid = xmlProcess.validateFromString(body as 
java.lang.String)
 
                 if (!isValid) {
                     logger.error("Invalid request received")
@@ -53,7 +54,7 @@ fun main(args: Array<String>) {
                     return@post
                 }
 
-                val bodyDocument = xmlProcess.parseStringIntoDom(body)
+                val bodyDocument = xmlProcess.parseStringIntoDom(body) as 
org.w3c.dom.Document
                 if (null == bodyDocument)
                 {
                     /* Should never happen.  */
@@ -62,8 +63,9 @@ fun main(args: Array<String>) {
                         status = HttpStatusCode.InternalServerError) 
{"Internal server error"};
                     return@post
                 }
+                logger.info(bodyDocument.documentElement.localName)
 
-                if ("ebicsHEVRequest" == bodyDocument.documentElement.tagName)
+                if ("ebicsHEVRequest" == 
bodyDocument.documentElement.localName)
                 {
                     /* known type, and already valid here! */
                     val hevResponse: HEVResponse = HEVResponse("rc", "rt")
@@ -77,7 +79,7 @@ fun main(args: Array<String>) {
                 }
 
                 /* Log to console and return "unknown type" */
-                // logger.info("Unknown message, just logging it!")
+                logger.info("Unknown message, just logging it!")
                 call.respondText(contentType = ContentType.Application.Xml,
                     status = HttpStatusCode.NotFound) {"Not found"};
                 return@post
diff --git a/src/main/kotlin/tech/libeufin/XMLManagement.kt 
b/src/main/kotlin/tech/libeufin/XMLManagement.kt
index 2a119fd..57b6344 100644
--- a/src/main/kotlin/tech/libeufin/XMLManagement.kt
+++ b/src/main/kotlin/tech/libeufin/XMLManagement.kt
@@ -18,7 +18,9 @@ import javax.xml.validation.*; // has SchemaFactory
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import org.w3c.dom.Document;
+import sun.misc.IOUtils
 import tech.libeufin.messages.HEVResponseDataType;
+import java.util.stream.Collectors
 
 /**
  * This class takes care of importing XSDs and validate
@@ -39,9 +41,7 @@ public class XMLManagement() {
 
         try {
             val sf = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
-            // bundle = sf.newSchema(schemas);
-            // validator = bundle.newValidator();
-            sf.newSchema()
+            sf.newSchema(schemas)
         } catch (e: SAXException) {
             e.printStackTrace();
             null
@@ -54,21 +54,14 @@ public class XMLManagement() {
      * @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()
+        factory.isNamespaceAware = true
 
         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 builder = factory.newDocumentBuilder()
             val document = builder.parse(InputSource(xmlInputStream));
 
             return document;
@@ -89,9 +82,11 @@ public class XMLManagement() {
      * @return true when validation passes, false otherwise
      */
     // public boolean validate(Source xmlDoc){
-    fun validate(xmlDoc: Source): Boolean {
-        try{
-            validator?.validate(xmlDoc);
+    private fun validate(xmlDoc: StreamSource): Boolean {
+        try {
+            validator?.validate(xmlDoc)
+
+
         } catch (e: SAXException) {
             e.printStackTrace()
             return false;
@@ -99,7 +94,7 @@ public class XMLManagement() {
             e.printStackTrace()
             return false;
         }
-        return true;
+        return true
     }
 
     /**
@@ -107,12 +102,10 @@ public class XMLManagement() {
      * @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);
+    fun validateFromString(xmlString: java.lang.String): Boolean {
+        val xmlInputStream: InputStream = ByteArrayInputStream(xmlString.bytes)
+        var xmlSource: StreamSource = StreamSource(xmlInputStream)
+        return this.validate(xmlSource)
     }
 
     /**
@@ -123,23 +116,17 @@ public class XMLManagement() {
      *               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;
@@ -159,29 +146,21 @@ public class XMLManagement() {
      * @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;
+            return sw.toString()
 
         } catch (e: TransformerConfigurationException) {
             e.printStackTrace()
@@ -197,16 +176,12 @@ public class XMLManagement() {
      * @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);
diff --git a/src/main/java/tech/libeufin/messages/HEVResponse.java 
b/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
similarity index 54%
rename from src/main/java/tech/libeufin/messages/HEVResponse.java
rename to src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
index a1a4a58..afe730b 100644
--- a/src/main/java/tech/libeufin/messages/HEVResponse.java
+++ b/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
@@ -3,7 +3,7 @@ package tech.libeufin.messages;
 import javax.xml.bind.JAXBElement;
 
 
-public class HEVResponse {
+/*public class HEVResponse {
     HEVResponseDataType value;
 
     public HEVResponse(String returnCode, String reportText){
@@ -22,4 +22,22 @@ public class HEVResponse {
         ObjectFactory of = new ObjectFactory();
         return of.createEbicsHEVResponse(this.value);
     }
+}*/
+
+class HEVResponse(returnCode: String, reportText: String) {
+
+    val value = {
+        // SystemReturnCodeType srt = new SystemReturnCodeType();
+        val srt = SystemReturnCodeType()
+        srt.setReturnCode(returnCode);
+        srt.setReportText(reportText);
+        val value = HEVResponseDataType();
+        value.setSystemReturnCode(srt);
+        value
+    }()
+
+    fun makeHEVResponse(): JAXBElement<HEVResponseDataType> {
+        val of = ObjectFactory()
+        return of.createEbicsHEVResponse(value)
+    }
 }
diff --git a/src/test/java/XMLManagementTest.java 
b/src/test/java/XMLManagementTest.java
deleted file mode 100644
index 77106a6..0000000
--- a/src/test/java/XMLManagementTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-import org.junit.Test;
-import org.w3c.dom.Element;
-import tech.libeufin.XMLManagement;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.InputStream;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
-import static org.junit.Assert.*;
-import org.w3c.dom.Document;
-import tech.libeufin.messages.HEVResponse;
-import tech.libeufin.messages.HEVResponseDataType;
-
-public class XMLManagementTest {
-
-    @Test
-    public void XMLManagementTest(){
-        XMLManagement xm = new XMLManagement();
-
-        /* Load XML from disk.  */
-        ClassLoader classLoader = this.getClass().getClassLoader();
-        Source ebics_hev_sample = new 
StreamSource(classLoader.getResourceAsStream("ebics_hev.xml"));
-        assertTrue(xm.validate(ebics_hev_sample));
-
-        /* Load XML from string.  */
-        InputStream is = new ByteArrayInputStream("<InvalidXML>".getBytes());
-        Source ebics_from_string = new StreamSource(is);
-        assertFalse(xm.validate(ebics_from_string));
-
-        assertFalse(xm.validate("<moreInvalidXML>"));
-
-        /* Parse XML string into DOM */
-        Document document = xm.parseStringIntoDom("<root></root>");
-        Element documentElement = document.getDocumentElement();
-        assertTrue("root".equals(documentElement.getTagName()));
-
-        /* Make XML DOM from Java object (JAXB) */
-        HEVResponse hr = new HEVResponse("rc", "rt");
-        JAXBElement<HEVResponseDataType> hrObject = hr.makeHEVResponse();
-        Document hevDocument = XMLManagement.convertJaxbToDom(hrObject);
-        
assertTrue("ns2:ebicsHEVResponse".equals(hevDocument.getDocumentElement().getTagName()));
-    }
-}
diff --git a/src/test/resources/ebics_hev.xml b/src/test/resources/ebics_hev.xml
old mode 100644
new mode 100755
index 7645aa7..f9acd04
--- a/src/test/resources/ebics_hev.xml
+++ b/src/test/resources/ebics_hev.xml
@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
-<ebicsHEVRequest xmlns="http://www.ebics.org/H000";>
-  <HostID>bern</HostID>
-</ebicsHEVRequest>
\ No newline at end of file
+<ebics:ebicsHEVRequest xmlns:ebics="http://www.ebics.org/H000";>
+    <ebics:HostID>bern</ebics:HostID>
+</ebics:ebicsHEVRequest>
\ 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]