From: Marko Luukkainen Date: Fri, 15 Mar 2019 13:46:25 +0000 (+0200) Subject: Allow loading databoard serialized files with type adapting X-Git-Tag: v1.43.0~136^2~178 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F78%2F2778%2F1;p=simantics%2Fplatform.git Allow loading databoard serialized files with type adapting refs #273 Change-Id: I8aeb070809910ae502ead24bcb87eeba3142bf22 (cherry picked from commit 4797ec6414d21a81c5e0b23bf8a48b356b6711c5) --- diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java index cc41e8e9b..35c8822d5 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java @@ -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 {