]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Allow loading databoard serialized files with type adapting 78/2778/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 15 Mar 2019 13:46:25 +0000 (15:46 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 15 Mar 2019 15:11:25 +0000 (15:11 +0000)
refs #273

Change-Id: I8aeb070809910ae502ead24bcb87eeba3142bf22
(cherry picked from commit 4797ec6414d21a81c5e0b23bf8a48b356b6711c5)

bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java

index cc41e8e9b021254899aa2e942c2f8fc69c81a603..35c8822d558974a8fdfad5662e002eb413106561 100644 (file)
@@ -188,8 +188,7 @@ public class Files {
        public static Datatype readFileType(File file) throws IOException {
                BinaryFile rf = new BinaryFile( file, "r" );
                try {
-                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
-                       return (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+                       return (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
                } finally {
                        rf.close();
                }               
@@ -208,8 +207,7 @@ public class Files {
        public static Object readFile(File file, Binding binding) throws IOException {
                BinaryFile rf = new BinaryFile( file, "r" );
                try {
-                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
-                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
                        
                        if (type.equals(binding.type())) {
                                return Bindings.getSerializerUnchecked( binding ).deserialize(rf);
@@ -229,6 +227,30 @@ public class Files {
                        rf.close();
                }
        }
+       
+       public static Object readFileTypeAdapting(File file, Binding binding) throws IOException {
+        BinaryFile rf = new BinaryFile( file, "r" );
+        try {
+            Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
+            
+            if (type.equals(binding.type())) {
+                return Bindings.getSerializerUnchecked( binding ).deserialize(rf);
+            } else {
+                try {
+                    Binding fileContentBinding = Bindings.getMutableBinding(type);
+                    Adapter adapter = Bindings.getTypeAdapter(fileContentBinding, binding);
+                    Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize(rf);
+                    return adapter.adapt( value );
+                } catch (AdapterConstructionException e) {
+                    throw new IOException(e);
+                } catch (AdaptException e) {
+                    throw new IOException(e);
+                }
+            }
+        } finally {
+            rf.close();
+        }
+    }
 
        /**
         * Read a file to an object.
@@ -241,8 +263,7 @@ public class Files {
        public static void readFile(File file, RecordBinding binding, Object dst) throws IOException {
                BinaryFile rf = new BinaryFile( file, "r" );
                try {
-                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
-                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( rf );
                        
                        if (type.equals(binding.type())) {
                                Serializer s = Bindings.getSerializerUnchecked( binding ); 
@@ -276,8 +297,7 @@ public class Files {
         */
        public static Object readFile(InputStream is, Binding binding) throws IOException {
                BinaryReadable readable = InputStreamReadable.readFully( is );
-               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
-               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
+               Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( readable );
                        
                if (!type.equals(binding.type())) {
                        try {
@@ -308,8 +328,7 @@ public class Files {
         */
        public static Object readFile(InputStream is, long streamLength, Binding binding) throws IOException {
                BinaryReadable readable = new InputStreamReadable( is, streamLength );
-               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
-               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
+               Datatype type = (Datatype) Bindings.getSerializerUnchecked( Bindings.DATATYPE ).deserialize( readable );
                        
                if (!type.equals(binding.type())) {
                        try {