]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / Files.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java
new file mode 100644 (file)
index 0000000..16dbd1e
--- /dev/null
@@ -0,0 +1,382 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010- Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ * \r
+ * Contributors:\r
+ *    VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard;\r
+\r
+import java.io.DataInput;\r
+import java.io.DataOutput;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.io.OutputStreamWriter;\r
+import java.nio.ByteBuffer;\r
+\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.adapter.Adapter;\r
+import org.simantics.databoard.adapter.AdapterConstructionException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.RecordBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.MutableVariant;\r
+import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
+import org.simantics.databoard.parser.repository.DataValueRepository;\r
+import org.simantics.databoard.serialization.RuntimeSerializerConstructionException;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.databoard.serialization.SerializerConstructionException;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.StreamUtil;\r
+import org.simantics.databoard.util.binary.BinaryFile;\r
+import org.simantics.databoard.util.binary.BinaryReadable;\r
+import org.simantics.databoard.util.binary.ByteBufferReadable;\r
+import org.simantics.databoard.util.binary.ByteBufferWriteable;\r
+import org.simantics.databoard.util.binary.InputStreamReadable;\r
+import org.simantics.databoard.util.binary.OutputStreamWriteable;\r
+import org.simantics.databoard.util.binary.UTF8;\r
+\r
+public class Files {\r
+       \r
+       /**\r
+        * Create a text file (.dbv) with a value. The file is UTF-8 encoded.  \r
+        * If old file exists, it is deleted.\r
+        * \r
+        * @param file\r
+        * @param binding\r
+        * @param value\r
+        * @throws IOException \r
+        */\r
+       public static void createTextFile(File file, Binding binding, Object value) \r
+       throws IOException\r
+       {\r
+               try {\r
+                       if (file.exists()) file.delete();\r
+                       file.createNewFile();\r
+                       \r
+                       String txt = binding.printValueDefinition(value, false);\r
+                       FileOutputStream fos = new FileOutputStream(file, false);\r
+                       try {\r
+                               OutputStreamWriter os = new OutputStreamWriter(fos, UTF8.CHARSET);\r
+                               os.append(txt);\r
+                               os.flush();\r
+                               os.close();\r
+                       } finally {\r
+                               fos.close();\r
+                       }\r
+               } catch(BindingException e) {\r
+                       throw new IOException(e);\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Create a binary file (.dbb) with a initial value. Binary file is a variant, \r
+        * there is a filetype in the header of the file.\r
+        * If old file exists, it is deleted.\r
+        * \r
+        * @param file\r
+        * @param binding\r
+        * @param value\r
+        * @throws IOException \r
+        */\r
+       public static void createFile(File file, Binding binding, Object value) \r
+       throws IOException \r
+       {\r
+               if (file.exists()) file.delete();\r
+               file.createNewFile();\r
+               MutableVariant v = new MutableVariant(binding, value);\r
+               Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );\r
+               s.serialize(v, file);\r
+       }\r
+\r
+       /**\r
+        * Create a new binary file (.dbb) with an empty value. Binary file is a variant, \r
+        * there is a filetype in the header of the file.\r
+        * If old file exists, it is deleted.\r
+        * \r
+        * @since 0.5\r
+        * @param file\r
+        * @throws IOException \r
+        * @throws RuntimeSerializerConstructionException\r
+        */\r
+       public static void createFile(File file) throws IOException, RuntimeSerializerConstructionException  \r
+       {\r
+               if (file.exists()) file.delete();\r
+               file.createNewFile();\r
+               BinaryFile bf = new BinaryFile(file);\r
+               try {\r
+                       Binding binding = Bindings.MUTABLE_VARIANT;\r
+                       Object value = new MutableVariant(); \r
+                       Serializer s = Bindings.getSerializerUnchecked( binding );\r
+                       s.serialize(bf, value);\r
+               } finally {\r
+                       bf.close();\r
+               }\r
+       }       \r
+\r
+       /**\r
+        * Create a binary file (.dbb) with empty value of given type. Binary file is a variant, \r
+        * there is a filetype in the header of the file.\r
+        * If old file exists, it is deleted.\r
+        * \r
+        * @param file\r
+        * @param type\r
+        * @throws IOException \r
+        * @throws RuntimeSerializerConstructionException \r
+        */\r
+       public static void createFile(File file, Datatype type) throws IOException, RuntimeSerializerConstructionException \r
+       {\r
+               if (file.exists()) file.delete();\r
+               file.createNewFile();\r
+               BinaryFile bf = new BinaryFile(file);\r
+               try {\r
+                       Binding binding = Bindings.MUTABLE_VARIANT;\r
+                       Object value = binding.createDefault();\r
+                       Serializer s = Bindings.getSerializer( binding );\r
+                       s.serialize(bf, value);\r
+               } catch (BindingException e) {\r
+                       throw new IOException(e);\r
+               } catch (SerializerConstructionException e) {\r
+                       throw new IOException(e);\r
+               } finally {\r
+                       bf.close();\r
+               }\r
+       }\r
+       \r
+       \r
+       /**\r
+        * Read a text file (.dbv).   \r
+        * \r
+        * @param file\r
+        * @param binding\r
+        * @return value\r
+        * @throws IOException \r
+        * @throws BindingException \r
+        * @throws DataTypeSyntaxError \r
+        */\r
+       public static Object readTextFile(File file, Binding binding) \r
+       throws IOException, DataTypeSyntaxError, BindingException \r
+       {\r
+               FileInputStream fis = new FileInputStream(file);\r
+               try {\r
+                       byte[] data = StreamUtil.readFully(fis);\r
+                       String txt = new String(data, UTF8.CHARSET);\r
+                       DataValueRepository repo = new DataValueRepository();\r
+                       repo.setTypeRepository( Datatypes.datatypeRepository );\r
+                       return binding.parseValue(txt, repo);\r
+               } finally {\r
+                       fis.close();\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Read file type of a binary file. \r
+        * \r
+        * @param file\r
+        * @return datatype\r
+        * @throws IOException \r
+        */\r
+       public static Datatype readFileType(File file) throws IOException {\r
+               BinaryFile rf = new BinaryFile( file );\r
+               try {\r
+                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
+                       return (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );\r
+               } finally {\r
+                       rf.close();\r
+               }               \r
+       }\r
+       \r
+       /**\r
+        * Read a binary file into a java instance. Binary file is a variant, \r
+        * there is a filetype in the header of the file. \r
+        * If requested binding is not the exact binding of the file, an adapter is tried.\r
+        * \r
+        * @param file file\r
+        * @param binding content binding\r
+        * @return instance\r
+        * @throws IOException \r
+        */\r
+       public static Object readFile(File file, Binding binding) throws IOException {\r
+               BinaryFile rf = new BinaryFile( file );\r
+               try {\r
+                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
+                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );\r
+                       \r
+                       if (type.equals(binding.type())) {\r
+                               return Bindings.getSerializerUnchecked( binding ).deserialize(rf);\r
+                       } else {\r
+                               try {\r
+                                       Binding fileContentBinding = Bindings.getMutableBinding(type);\r
+                                       Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);\r
+                                       Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize(rf);\r
+                                       return adapter.adapt( value );\r
+                               } catch (AdapterConstructionException e) {\r
+                                       throw new IOException(e);\r
+                               } catch (AdaptException e) {\r
+                                       throw new IOException(e);\r
+                               }\r
+                       }\r
+               } finally {\r
+                       rf.close();\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Read a file to an object.\r
+        * \r
+        * @param file\r
+        * @param binding\r
+        * @param dst\r
+        * @throws IOException \r
+        */\r
+       public static void readFile(File file, RecordBinding binding, Object dst) throws IOException {\r
+               BinaryFile rf = new BinaryFile( file );\r
+               try {\r
+                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
+                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );\r
+                       \r
+                       if (type.equals(binding.type())) {\r
+                               Serializer s = Bindings.getSerializerUnchecked( binding ); \r
+                               s.deserializeTo(rf, dst);\r
+                       } else {\r
+                               try {\r
+                                       Binding fileContentBinding = Bindings.getMutableBinding(type);\r
+                                       Serializer s = Bindings.getSerializerUnchecked( fileContentBinding );\r
+                                       Object tmpObj = s.deserialize( rf );                                    \r
+                                       binding.readFrom(fileContentBinding, tmpObj, dst);\r
+                               } catch (BindingException e) {\r
+                                       throw new IOException(e);\r
+                               }\r
+                       }\r
+               } finally {\r
+                       rf.close();\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Read input stream into a java instance. Binary file is a variant, \r
+        * there is a filetype in the header of the file. If requested binding is not the \r
+        * exact binding of the file, an adapter is tried.<p>\r
+        * \r
+        * The implementation reads the inputstream fully into memory.<p>\r
+        * \r
+        * @param is input stream\r
+        * @param binding content binding\r
+        * @return instance\r
+        * @throws IOException \r
+        */\r
+       public static Object readFile(InputStream is, Binding binding) throws IOException {\r
+               BinaryReadable readable = InputStreamReadable.readFully( is );\r
+               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
+               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );\r
+                       \r
+               if (!type.equals(binding.type())) {\r
+                       try {\r
+                               Binding fileContentBinding = Bindings.getMutableBinding(type);\r
+                               Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);\r
+                               Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );\r
+                               return adapter.adapt( value );\r
+                       } catch (AdapterConstructionException e) {\r
+                               throw new IOException(e);\r
+                       } catch (AdaptException e) {\r
+                               throw new IOException(e);\r
+                       }\r
+               }\r
+                       \r
+               return Bindings.getSerializerUnchecked( binding ).deserialize( readable );\r
+       }\r
+       \r
+       /**\r
+        * Read input stream into a java instance. If requested binding is not the \r
+        * exact binding of the file, an adapter is tried.\r
+        * \r
+        * @param is input stream\r
+        * @param streamLength\r
+        * @param binding content binding\r
+        * @return instance\r
+        * @throws IOException \r
+        * @throws AccessorException \r
+        */\r
+       public static Object readFile(InputStream is, long streamLength, Binding binding) throws IOException {\r
+               BinaryReadable readable = new InputStreamReadable( is, streamLength );\r
+               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
+               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );\r
+                       \r
+               if (!type.equals(binding.type())) {\r
+                       try {\r
+                               Binding fileContentBinding = Bindings.getMutableBinding(type);\r
+                               Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);\r
+                               Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );\r
+                               return adapter.adapt( value );\r
+                       } catch (AdapterConstructionException e) {\r
+                               throw new IOException(e);\r
+                       } catch (AdaptException e) {\r
+                               throw new IOException(e);\r
+                       }\r
+               }\r
+                       \r
+               return Bindings.getSerializerUnchecked( binding ).deserialize( readable );\r
+       }\r
+\r
+       /**\r
+        * Write value as binary file (.dbb).\r
+        * \r
+        * @param file file\r
+        * @param binding content binding\r
+        * @param value value\r
+        * @throws IOException \r
+        */\r
+       public static void writeFile(File file, Binding binding, Object value) throws IOException {\r
+               BinaryFile rf = new BinaryFile( file );\r
+               try {\r
+                       MutableVariant v = new MutableVariant(binding, value);\r
+                       Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );\r
+                       s.serialize(v, file);\r
+               } finally {\r
+                       rf.close();\r
+               }\r
+       }       \r
+\r
+       public static DataInput openInput( InputStream is )\r
+       {\r
+               return new InputStreamReadable(is, Long.MAX_VALUE);\r
+       }\r
+       \r
+       public static DataInput openInput( File file ) throws IOException\r
+       {\r
+               return new BinaryFile(file);\r
+       }\r
+       \r
+       public static DataInput openInput( byte[] data ) \r
+       {\r
+               ByteBuffer buffer = ByteBuffer.wrap( data );\r
+               return new ByteBufferReadable( buffer );\r
+       }\r
+       \r
+       public static DataOutput openOutput( OutputStream os )\r
+       {\r
+               return new OutputStreamWriteable( os );\r
+       }\r
+       \r
+       public static DataOutput openOutput( File file ) throws IOException\r
+       {\r
+               return new BinaryFile( file );\r
+       }\r
+       \r
+       public static DataOutput openOutput( byte[] data ) throws IOException\r
+       {\r
+               ByteBuffer buffer = ByteBuffer.wrap( data );\r
+               return new ByteBufferWriteable( buffer );\r
+       }       \r
+\r
+}\r
+\r