gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [libeufin] branch master updated: Coding conventions.


From: gnunet
Subject: [GNUnet-SVN] [libeufin] branch master updated: Coding conventions.
Date: Tue, 24 Sep 2019 14:51:27 +0200

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

marcello pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new e4b2bc7  Coding conventions.
e4b2bc7 is described below

commit e4b2bc79f192b31ab2450a89b4b65ff1c9c5ebb9
Author: Marcello Stanisci <address@hidden>
AuthorDate: Tue Sep 24 14:50:53 2019 +0200

    Coding conventions.
---
 src/main/kotlin/Main.kt                            | 26 +++++------
 src/main/kotlin/tech/libeufin/DB.kt                |  1 +
 src/main/kotlin/tech/libeufin/GetLogger.kt         |  6 +--
 .../libeufin/{XMLManagement.kt => XMLTransform.kt} | 54 +++++++++++-----------
 .../kotlin/tech/libeufin/messages/HEVResponse.kt   | 15 +++---
 .../tech/libeufin/messages/ProtocolAndVersion.kt   |  1 -
 6 files changed, 49 insertions(+), 54 deletions(-)

diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
index cfd0bfd..ed1ed04 100644
--- a/src/main/kotlin/Main.kt
+++ b/src/main/kotlin/Main.kt
@@ -26,15 +26,15 @@ import io.ktor.response.*
 import io.ktor.routing.*
 import io.ktor.server.engine.*
 import io.ktor.server.netty.*
+import org.w3c.dom.Document
 import tech.libeufin.messages.HEVResponse
 import tech.libeufin.messages.HEVResponseDataType
 import tech.libeufin.messages.ProtocolAndVersion
-import tech.libeufin.tech.libeufin.db
 import javax.xml.bind.JAXBElement
 
-fun main(args: Array<String>) {
+fun main( ) {
 
-    var xmlProcess = XMLManagement();
+    var xmlProcess = XMLTransform()
     var logger = getLogger()
 
     val server = embeddedServer(Netty, port = 5000) {
@@ -48,29 +48,27 @@ fun main(args: Array<String>) {
                 val body: String = call.receiveText()
                 logger.debug("Body: $body")
 
-                val isValid = xmlProcess.validateFromString(body as 
java.lang.String)
+                val isValid = xmlProcess.validateFromString(body)
 
                 if (!isValid) {
                     logger.error("Invalid request received")
                     call.respondText(contentType = ContentType.Application.Xml,
-                                     status = HttpStatusCode.BadRequest) {"Bad 
request"};
+                                     status = HttpStatusCode.BadRequest) {"Bad 
request"}
                     return@post
                 }
 
-                val bodyDocument = xmlProcess.parseStringIntoDom(body) as 
org.w3c.dom.Document
-                if (null == bodyDocument)
-                {
+                val bodyDocument: Document? = 
xmlProcess.parseStringIntoDom(body)
+                if (null == bodyDocument) {
                     /* Should never happen.  */
                     logger.error("A valid document failed to parse into DOM!")
                     call.respondText(contentType = ContentType.Application.Xml,
-                        status = HttpStatusCode.InternalServerError) 
{"Internal server error"};
+                        status = HttpStatusCode.InternalServerError) 
{"Internal server error"}
                     return@post
                 }
                 logger.info(bodyDocument.documentElement.localName)
 
-                if ("ebicsHEVRequest" == 
bodyDocument.documentElement.localName)
-                {
-                    val hevResponse: HEVResponse = HEVResponse(
+                if ("ebicsHEVRequest" == 
bodyDocument.documentElement.localName) {
+                    val hevResponse = HEVResponse(
                         "000000",
                         "EBICS_OK",
                         arrayOf(
@@ -83,14 +81,14 @@ fun main(args: Array<String>) {
                     val responseText: String? = 
xmlProcess.getStringFromJaxb(jaxbHEV)
                     // FIXME: check if String is actually non-NULL!
                     call.respondText(contentType = ContentType.Application.Xml,
-                        status = HttpStatusCode.OK) {responseText.toString()};
+                        status = HttpStatusCode.OK) {responseText.toString()}
                     return@post
                 }
 
                 /* Log to console and return "unknown type" */
                 logger.info("Unknown message, just logging it!")
                 call.respondText(contentType = ContentType.Application.Xml,
-                    status = HttpStatusCode.NotFound) {"Not found"};
+                    status = HttpStatusCode.NotFound) {"Not found"}
                 return@post
 
             }
diff --git a/src/main/kotlin/tech/libeufin/DB.kt 
b/src/main/kotlin/tech/libeufin/DB.kt
index 257b233..92193bf 100644
--- a/src/main/kotlin/tech/libeufin/DB.kt
+++ b/src/main/kotlin/tech/libeufin/DB.kt
@@ -10,6 +10,7 @@ object SignKeys: IntIdTable(){
 
 fun db() {
     Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
+
     transaction {
         addLogger(StdOutSqlLogger)
         SchemaUtils.create(SignKeys)
diff --git a/src/main/kotlin/tech/libeufin/GetLogger.kt 
b/src/main/kotlin/tech/libeufin/GetLogger.kt
index bfc35ce..1902a2d 100644
--- a/src/main/kotlin/tech/libeufin/GetLogger.kt
+++ b/src/main/kotlin/tech/libeufin/GetLogger.kt
@@ -15,7 +15,7 @@ fun getLogger(): Logger {
     fa.context = lc
     fa.file = "server.log"
     fa.start()
-    logger.addAppender(fa);
+    logger.addAppender(fa)
     logger.level = Level.DEBUG
-    return logger;
-    }
+    return logger
+}
diff --git a/src/main/kotlin/tech/libeufin/XMLManagement.kt 
b/src/main/kotlin/tech/libeufin/XMLTransform.kt
similarity index 85%
rename from src/main/kotlin/tech/libeufin/XMLManagement.kt
rename to src/main/kotlin/tech/libeufin/XMLTransform.kt
index 8845ebb..bbcee40 100644
--- a/src/main/kotlin/tech/libeufin/XMLManagement.kt
+++ b/src/main/kotlin/tech/libeufin/XMLTransform.kt
@@ -23,23 +23,23 @@ import org.w3c.dom.Document;
  * XMLs against those.
  */
 
-public class XMLManagement() {
+class XMLTransform {
 
     /**
      * 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 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);
+            val sf = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
             sf.newSchema(schemas)
         } catch (e: SAXException) {
-            e.printStackTrace();
+            e.printStackTrace()
             null
         }
     }()
@@ -77,12 +77,9 @@ public class XMLManagement() {
      * @param xmlDoc the XML document to validate
      * @return true when validation passes, false otherwise
      */
-    // public boolean validate(Source xmlDoc){
     private fun validate(xmlDoc: StreamSource): Boolean {
         try {
             validator?.validate(xmlDoc)
-
-
         } catch (e: SAXException) {
             e.printStackTrace()
             return false;
@@ -90,6 +87,7 @@ public class XMLManagement() {
             e.printStackTrace()
             return false;
         }
+
         return true
     }
 
@@ -98,9 +96,9 @@ public class XMLManagement() {
      * @param xmlString XML body, as read from the POST body.
      * @return InputStream object, as wanted by the validator.
      */
-    fun validateFromString(xmlString: java.lang.String): Boolean {
-        val xmlInputStream: InputStream = ByteArrayInputStream(xmlString.bytes)
-        var xmlSource: StreamSource = StreamSource(xmlInputStream)
+    fun validateFromString(xmlString: String): Boolean {
+        val xmlInputStream: InputStream = 
ByteArrayInputStream(xmlString.toByteArray())
+        var xmlSource = StreamSource(xmlInputStream)
         return this.validate(xmlSource)
     }
 
@@ -115,24 +113,23 @@ public class XMLManagement() {
     fun convertJaxbToDom(obj: JAXBElement<Unit>): Document? {
 
         try {
-            val jc = JAXBContext.newInstance("tech.libeufin.messages");
+            val jc = JAXBContext.newInstance("tech.libeufin.messages")
 
             /* Make the target document.  */
             val dbf = DocumentBuilderFactory.newInstance()
-            val db = dbf.newDocumentBuilder();
-            val document = db.newDocument();
+            val db = dbf.newDocumentBuilder()
+            val document = db.newDocument()
 
             /* Marshalling the object into the document.  */
             val m = jc.createMarshaller()
-            m.marshal(obj, document); // document absorbed XML!
-            return document;
+            m.marshal(obj, document) // document absorbed the XML!
+            return document
 
         } catch (e: JAXBException) {
             e.printStackTrace()
         } catch (e: ParserConfigurationException) {
             e.printStackTrace()
         }
-
         return null;
     }
 
@@ -147,12 +144,12 @@ public class XMLManagement() {
         try {
             /* Make Transformer.  */
             val tf = TransformerFactory.newInstance();
-            val t = tf.newTransformer();
+            val t = tf.newTransformer()
 
-            t.setOutputProperty(OutputKeys.INDENT, "no");
+            t.setOutputProperty(OutputKeys.INDENT, "no")
 
             /* Make string writer.  */
-            val sw = StringWriter();
+            val sw = StringWriter()
 
             /* Extract string.  */
             t.transform(DOMSource(document), StreamResult(sw))
@@ -173,19 +170,20 @@ public class XMLManagement() {
      * @return String representation of @a object, or null if errors occur
      */
     fun <T>getStringFromJaxb(obj: JAXBElement<T>): String? {
+        val sw = StringWriter()
+
         try {
             val jc = JAXBContext.newInstance("tech.libeufin.messages")
-            val sw = StringWriter();
-
             /* Getting the string.  */
-            val m = jc.createMarshaller();
-            m.marshal(obj, sw);
-            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-            return sw.toString();
+            val m = jc.createMarshaller()
+            m.marshal(obj, sw)
+            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
 
         } catch (e: JAXBException) {
-            e.printStackTrace();
-            return null;
+            e.printStackTrace()
+            return null
         }
+
+        return sw.toString()
     }
-};
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt 
b/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
index 0a7aa69..c3afc06 100644
--- a/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
+++ b/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
@@ -13,21 +13,20 @@ class HEVResponse(
         reportText: String
     ) : this(returnCode, reportText, null)
 
-    val value: HEVResponseDataType = {
+    private val value: HEVResponseDataType = {
         val srt = SystemReturnCodeType()
         srt.setReturnCode(returnCode);
         srt.setReportText(reportText);
         val value = HEVResponseDataType();
         value.setSystemReturnCode(srt);
 
-        if (null != protocolAndVersion) {
-            protocolAndVersion.forEach {
-                val entry = HEVResponseDataType.VersionNumber()
-                entry.setProtocolVersion(it.protocol)
-                entry.setValue(it.version)
-                value.getVersionNumber().add(entry)
-            }
+        protocolAndVersion?.forEach {
+            val entry = HEVResponseDataType.VersionNumber()
+            entry.setProtocolVersion(it.protocol)
+            entry.setValue(it.version)
+            value.getVersionNumber().add(entry)
         }
+
         value
     }()
 
diff --git a/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt 
b/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt
index 606cd67..7db63ce 100644
--- a/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt
+++ b/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt
@@ -1,7 +1,6 @@
 package tech.libeufin.messages
 
 class ProtocolAndVersion(protocol: String, version: String) {
-
     val protocol = protocol
     val version = version
 }
\ 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]