You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/11/12 15:26:11 UTC

[isis] branch v2 updated: ISIS-2006: deduplicate code by rather utilizing JaxbUtil

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/v2 by this push:
     new 01c1d49  ISIS-2006: deduplicate code by rather utilizing JaxbUtil
01c1d49 is described below

commit 01c1d4907ea83527ce06dc7b5ae8e410cee30625
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Nov 12 16:26:01 2018 +0100

    ISIS-2006: deduplicate code by rather utilizing JaxbUtil
---
 .../isis/applib/client/RestfulClientConfig.java    | 84 ----------------------
 .../java/org/apache/isis/applib/util/JaxbUtil.java | 77 ++++++++++++++++++++
 2 files changed, 77 insertions(+), 84 deletions(-)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/client/RestfulClientConfig.java b/core/applib/src/main/java/org/apache/isis/applib/client/RestfulClientConfig.java
index 12d8327..a4a94dd 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/client/RestfulClientConfig.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/client/RestfulClientConfig.java
@@ -18,19 +18,10 @@
  */
 package org.apache.isis.applib.client;
 
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.isis.applib.anyio.AnyIn;
-import org.apache.isis.applib.anyio.AnyOut;
-import org.apache.isis.applib.anyio.Try;
 
 /**
  * 
@@ -105,79 +96,4 @@ public class RestfulClientConfig {
         this.useRequestDebugLogging = useRequestDebugLogging;
     }
 
-    // -- MARSHALLING
-    
-    public static Marshaller createMarshaller() throws JAXBException {
-        Marshaller marshaller = JAXBContext.newInstance(RestfulClientConfig.class).createMarshaller();
-        return marshaller;
-    }
-    
-    public static Unmarshaller createUnmarshaller() throws JAXBException {
-        Unmarshaller unmarshaller = JAXBContext.newInstance(RestfulClientConfig.class).createUnmarshaller();
-        return unmarshaller;
-    }
-
-    // -- READ
-    
-    /**
-     * Tries to read the RestfulClientConfig from universal source {@code in}.
-     * @param in - universal source {@link AnyIn}
-     * @return
-     */
-    public static Try<RestfulClientConfig> tryRead(AnyIn in) {
-        
-        return in.tryApplyInputStream(is->{
-            
-            try {
-                StreamSource source = new StreamSource(is);
-                RestfulClientConfig clientConfig = createUnmarshaller()
-                                    .unmarshal(source, RestfulClientConfig.class).getValue();
-                
-                return Try.success(clientConfig);
-                
-            } catch (JAXBException e) {
-                
-                return Try.failure(e);
-            }
-            
-        });
-        
-    }
-    
-    // -- WRITE
-    
-    /**
-     * Tries to write this RestfulClientConfig to universal sink {@code output}.
-     * @param output - universal sink {@link AnyOut}
-     * @return
-     */
-    public Try<Void> tryWrite(AnyOut output) {
-        return output.tryApplyOutputStream(os->{
-    
-            try {
-
-                createMarshaller().marshal(this, os);
-                return Try.success(null);
-                
-            } catch (JAXBException e) {
-                
-                return Try.failure(e);
-            }
-            
-        });
-    }
-    
-    /**
-     * Writes this RestfulClientConfig to universal sink {@code output}.
-     * @param output - universal sink {@link AnyOut}
-     * @throws Exception
-     */
-    public void write(AnyOut output) throws Exception {
-        
-        Try<Void> _try = tryWrite(output);
-        _try.throwIfFailure();
-        
-    }
-    
-
 }
diff --git a/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java b/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java
index 492af28..fbdda51 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/util/JaxbUtil.java
@@ -28,7 +28,11 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.stream.StreamSource;
 
+import org.apache.isis.applib.anyio.AnyIn;
+import org.apache.isis.applib.anyio.AnyOut;
+import org.apache.isis.applib.anyio.Try;
 import org.apache.isis.commons.internal.base._Casts;
 import org.apache.isis.commons.internal.collections._Maps;
 import org.apache.isis.commons.internal.resources._Resources;
@@ -46,6 +50,77 @@ import org.apache.isis.commons.internal.resources._Resources;
 public class JaxbUtil {
 
     private JaxbUtil(){}
+    
+    // -- READ - implemented for AnyIn
+    
+    /**
+     * Tries to read the object from universal source {@code in}.
+     * @param in - universal source {@link AnyIn}
+     * @param dtoClass - object type to be read
+     * @return
+     */
+    public static <T> Try<T> tryReadXml(AnyIn in, final Class<T> dtoClass) {
+        
+        return in.tryApplyInputStream(is->{
+            
+            try {
+                
+                Unmarshaller unmarshaller = jaxbContextFor(dtoClass).createUnmarshaller();
+                
+                StreamSource source = new StreamSource(is);
+                T dto = unmarshaller.unmarshal(source, dtoClass).getValue();
+                
+                return Try.success(dto);
+                
+            } catch (JAXBException e) {
+                
+                return Try.failure(e);
+            }
+            
+        });
+        
+    }
+    
+    // -- WRITE - implemented for AnyOut
+    
+    /**
+     * Tries to write the object to universal sink {@code output}.
+     * @param dto - object to be written
+     * @param output - universal sink {@link AnyOut}
+     * @return
+     */
+    public static <T> Try<Void> tryWriteXml(final T dto, AnyOut output) {
+        return output.tryApplyOutputStream(os->{
+    
+            try {
+
+                final Marshaller marshaller = jaxbContextFor(dto.getClass()).createMarshaller();
+                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+                marshaller.marshal(dto, os);
+                return Try.success(null);
+                
+            } catch (JAXBException e) {
+                
+                return Try.failure(e);
+            }
+            
+        });
+    }
+    
+    /**
+     * Writes the object to universal sink {@code output}.
+     * @param dto - object to be written
+     * @param output - universal sink {@link AnyOut}
+     * @throws Exception
+     */
+    public static <T> void writeXml(final T dto, AnyOut output) throws Exception {
+        
+        Try<Void> _try = tryWriteXml(dto, output);
+        _try.throwIfFailure();
+        
+    }
+    
+    // -- READ
 
     public static <T> T fromXml(
             final Reader reader,
@@ -68,6 +143,8 @@ public class JaxbUtil {
         final String s = _Resources.loadAsString(contextClass, resourceName, charset);
         return fromXml(new StringReader(s), dtoClass);
     }
+    
+    // -- WRITE
 
     public static <T> String toXml(final T dto) {
         final CharArrayWriter caw = new CharArrayWriter();