gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [taler-libeufin] 18/25: method to get (XML) strings from JA


From: gnunet
Subject: [GNUnet-SVN] [taler-libeufin] 18/25: method to get (XML) strings from JAXB objects.
Date: Fri, 20 Sep 2019 19:32:56 +0200

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

marcello pushed a commit to branch master
in repository libeufin.

commit c05891767e61ae2101161d16b14fa405e5192ed2
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed Sep 18 13:43:23 2019 +0200

    method to get (XML) strings from JAXB objects.
---
 src/main/java/tech/libeufin/XMLManagement.java | 34 +++++++++++++++++++++++---
 src/main/kotlin/Main.kt                        |  9 ++++---
 src/test/java/XMLManagementTest.java           | 21 +++++++---------
 3 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/src/main/java/tech/libeufin/XMLManagement.java 
b/src/main/java/tech/libeufin/XMLManagement.java
index e70b05d..1bccda7 100644
--- a/src/main/java/tech/libeufin/XMLManagement.java
+++ b/src/main/java/tech/libeufin/XMLManagement.java
@@ -6,6 +6,7 @@ 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;
@@ -17,6 +18,7 @@ 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
@@ -56,7 +58,7 @@ public class XMLManagement {
      * @param xmlString the string to parse.
      * @return the DOM representing @a xmlString
      */
-    static public Document parseStringIntoDOM(String xmlString) {
+    static public Document parseStringIntoDom(String xmlString) {
 
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
@@ -112,16 +114,16 @@ public class XMLManagement {
 
     /**
      * Return the DOM representation of the Java object, using the JAXB
-     * interface.
+     * 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 parseObjectIntoDocument(Object object) {
+    static public Document convertJaxbToDom(JAXBElement<?> object) {
 
         try {
-            JAXBContext jc = JAXBContext.newInstance(object.getClass());
+            JAXBContext jc = JAXBContext.newInstance("tech.libeufin.messages");
 
             /* Make the target document.  */
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
@@ -144,6 +146,7 @@ public class XMLManagement {
 
     /**
      * Extract String from DOM.
+     *
      * @param document the DOM to extract the string from.
      * @return the final String, or null if errors occur.
      */
@@ -173,4 +176,27 @@ public class XMLManagement {
 
         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/kotlin/Main.kt b/src/main/kotlin/Main.kt
index 4ac446f..7516cdd 100644
--- a/src/main/kotlin/Main.kt
+++ b/src/main/kotlin/Main.kt
@@ -30,8 +30,7 @@ import tech.libeufin.XMLManagement;
 import java.io.ByteArrayInputStream
 import java.io.InputStream
 import org.w3c.dom.Document;
-
-val knownTypes = arrayOf("ebicsHEVRequest")
+import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType
 
 fun main(args: Array<String>) {
     var xmlProcess = XMLManagement();
@@ -50,7 +49,7 @@ fun main(args: Array<String>) {
                     call.respond(HttpStatusCode(400, "Invalid request"));
                 }
 
-                val bodyDocument = XMLManagement.parseStringIntoDOM(body)
+                val bodyDocument = XMLManagement.parseStringIntoDom(body)
                 if (null == bodyDocument)
                 {
                     /* Should never happen.  */
@@ -58,7 +57,8 @@ fun main(args: Array<String>) {
                     call.respond(HttpStatusCode(500, "Internal server error"));
                 }
 
-                if (bodyDocument.documentElement !in knownTypes) {
+                /* FIXME: Check if that's a known type! */
+                if (true) {
                     /* Log to console and return "unknown type" */
                     System.out.println("Unknown message, just logging it!")
                     call.respond(HttpStatusCode(400, "Not found"));
@@ -66,6 +66,7 @@ fun main(args: Array<String>) {
 
                 /* Generate response here.  */
 
+
             }
         }
     }
diff --git a/src/test/java/XMLManagementTest.java 
b/src/test/java/XMLManagementTest.java
index 3bf205c..77106a6 100644
--- a/src/test/java/XMLManagementTest.java
+++ b/src/test/java/XMLManagementTest.java
@@ -5,14 +5,14 @@ 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;
-
-@XmlRootElement(name="SimpleJAXBTest")
-class SimpleJAXBTest {}
+import tech.libeufin.messages.HEVResponse;
+import tech.libeufin.messages.HEVResponseDataType;
 
 public class XMLManagementTest {
 
@@ -33,17 +33,14 @@ public class XMLManagementTest {
         assertFalse(xm.validate("<moreInvalidXML>"));
 
         /* Parse XML string into DOM */
-        Document document = xm.parseStringIntoDOM("<root></root>");
+        Document document = xm.parseStringIntoDom("<root></root>");
         Element documentElement = document.getDocumentElement();
-        assertTrue("root" == documentElement.getTagName());
+        assertTrue("root".equals(documentElement.getTagName()));
 
         /* Make XML DOM from Java object (JAXB) */
-        Document simpleRoot = xm.parseObjectIntoDocument(new SimpleJAXBTest());
-        Element simpleRootDocumentElement = simpleRoot.getDocumentElement();
-        assertTrue("SimpleJAXBTest" == simpleRootDocumentElement.getTagName());
-
-        /* Serialize the DOM into string.  */
-        String simpleRootString = 
XMLManagement.getStringFromDocument(simpleRoot);
-        System.out.println(simpleRootString);
+        HEVResponse hr = new HEVResponse("rc", "rt");
+        JAXBElement<HEVResponseDataType> hrObject = hr.makeHEVResponse();
+        Document hevDocument = XMLManagement.convertJaxbToDom(hrObject);
+        
assertTrue("ns2:ebicsHEVResponse".equals(hevDocument.getDocumentElement().getTagName()));
     }
 }

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



reply via email to

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