]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/ZipPublisher.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / ZipPublisher.java
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/impl/ZipPublisher.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/impl/ZipPublisher.java
new file mode 100644 (file)
index 0000000..521808e
--- /dev/null
@@ -0,0 +1,209 @@
+package org.simantics.export.core.impl;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipOutputStream;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.databoard.Accessors;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.accessor.RecordAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.reference.LabelReference;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.forms.DataboardForm;\r
+import org.simantics.databoard.type.RecordType;\r
+import org.simantics.databoard.util.StreamUtil;\r
+import org.simantics.export.core.ExportContext;\r
+import org.simantics.export.core.error.ExportException;\r
+import org.simantics.export.core.intf.PublisherClass;\r
+import org.simantics.export.core.manager.Content;\r
+import org.simantics.export.core.util.ExporterUtils;\r
+\r
+/**\r
+ * There are two fields in this publisher:\r
+ *  [ ] Overwrite file(s)\r
+ *  [ ] Export to Zip\r
+ *  \r
+ * TODO Zip pref saving should be based on content selection\r
+ *  \r
+ * @author toni.kalajainen@semantum.fi\r
+ */\r
+public class ZipPublisher implements PublisherClass {\r
+\r
+       private static final String ZIP_EXTENSION = ".zip";\r
+\r
+       public static RecordType RT_ZIP;\r
+\r
+       public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");\r
+       public static LabelReference P_ZIP = new LabelReference("Export to .zip");\r
+       \r
+       static {\r
+               RT_ZIP = new RecordType();\r
+               RT_ZIP.addComponent(P_ZIP.label, DataboardForm.fileSaveDialog(ZIP_EXTENSION, "*.zip"));\r
+               RT_ZIP.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);\r
+       }\r
+       \r
+       @Override\r
+       public void publish(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions, IProgressMonitor monitor) throws ExportException {\r
+               \r
+               Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);\r
+               String zipPath = ExporterUtils.getString( locationOptions, P_ZIP );\r
+               if ( zipPath == null ) throw new ExportException("Zip option missing");\r
+               if ( canOverwrite == null ) throw new ExportException("CanOverwrite option missing");\r
+\r
+               zipPath = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, zipPath);\r
+               File file = new File( zipPath );\r
+               if ( file.exists() ) {\r
+                       if ( canOverwrite ) {\r
+                               file.delete();\r
+                       } else {\r
+                               throw new ExportException("Would not overwrite " + file.getAbsolutePath());\r
+                       }\r
+               }\r
+               \r
+               // Assert the data is OK. Expected to be.\r
+               for ( Content content : contents ) {\r
+                       if ( content.tmpFile == null ) throw new ExportException("Internal error, tmpFile was null");\r
+               }\r
+               \r
+               FileOutputStream fos = null;\r
+               try {\r
+                       fos = new FileOutputStream( file );\r
+                       ZipOutputStream zos = new ZipOutputStream(fos);\r
+                       for ( Content content : contents ) {\r
+                               FileInputStream fis = new FileInputStream( content.tmpFile );\r
+                               try {\r
+                                       zos.putNextEntry(new ZipEntry( content.filename ));\r
+                                       StreamUtil.copyStream(fis, zos);\r
+                                       zos.closeEntry();\r
+                               } finally {\r
+                                       fis.close();\r
+                               }\r
+                       }\r
+                       zos.close();\r
+               } catch (IOException e) {\r
+                       throw new ExportException( e );\r
+               } finally {\r
+                       if ( fos != null ) try { fos.close(); } catch (IOException e) {}\r
+               }\r
+                       \r
+       }\r
+\r
+       @Override\r
+       public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {\r
+\r
+               List<String> result = new ArrayList<String>();\r
+               \r
+               Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);\r
+               String pathName = ExporterUtils.getString( locationOptions, P_ZIP );\r
+               if ( pathName == null ) { result.add("Zip option missing?"); return result; }\r
+               if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }\r
+               if ( pathName.isEmpty() ) { result.add("Zip must be entered."); return result; }\r
+               pathName = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, pathName);\r
+               File file = new File( pathName );\r
+               if ( !canOverwrite && file.exists() ) \r
+                       result.add( file.getAbsolutePath()+ " already exists." );\r
+               \r
+               return result;\r
+       }\r
+       \r
+       @Override\r
+       public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {\r
+               return RT_ZIP;\r
+       }\r
+       \r
+       @Override\r
+       public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {\r
+               // Make Dirs to the path.\r
+               String zipName = ExporterUtils.getString( locationOptions, P_ZIP );\r
+               if ( zipName == null ) throw new ExportException("Zip option not found?");\r
+               File file = new File( zipName );\r
+               File path = file.getParentFile();\r
+               if ( path == null ) return locationOptions;\r
+               if ( path.exists() && !path.isDirectory()) throw new ExportException(path+" exists but is not a directory.");\r
+               if ( !path.mkdirs() ) throw new ExportException( "Failed to create "+path);\r
+               return locationOptions;\r
+       }\r
+       \r
+        @Override\r
+       public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {\r
+               try {\r
+                       RecordAccessor ra = Accessors.getAccessor(locationOptions);                     \r
+                       String zipName = (String) ra.getValue(P_ZIP, Bindings.STRING);\r
+                       if ( zipName == null ) return false;\r
+                       File file = new File( zipName );\r
+                       File path = file.getParentFile();\r
+                       if ( path == null ) return false;\r
+                       return path.exists() && path.isDirectory();\r
+               } catch (AccessorConstructionException e) {\r
+                       throw new ExportException( e );\r
+               } catch (AccessorException e) {\r
+                       throw new ExportException( e );\r
+               }               \r
+       }\r
+\r
+       @Override\r
+       public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {              \r
+               try {\r
+                       RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
+               ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);\r
+               ra.setValue(P_ZIP, Bindings.STRING, "");\r
+               } catch (AccessorConstructionException e) {\r
+                       throw new ExportException(e);\r
+               } catch (AccessorException e) {\r
+                       throw new ExportException(e);\r
+               }               \r
+       }\r
+       \r
+       @Override\r
+       public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {\r
+               try {\r
+                       RecordAccessor ra = Accessors.getAccessor( locationOptions );\r
+\r
+                       Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);\r
+                       if ( b!=null ) {\r
+                               contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);\r
+                               workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);\r
+                       }\r
+       \r
+                       String s = (String) ra.getValue(P_ZIP, Bindings.STRING);\r
+                       if ( s!=null ) {\r
+                               contentScopeNode.put(P_ZIP.tail().toString(), s);\r
+                               //workspaceScopeNode.put(P_ZIP.tail().toString(), s);\r
+                       }\r
+                       \r
+               } catch (AccessorException e) {\r
+                       throw new ExportException( e );\r
+               } catch (AccessorConstructionException e) {\r
+                       throw new ExportException( e );\r
+               }\r
+       }\r
+       \r
+       @Override\r
+       public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {\r
+               try {\r
+                       RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
+                       \r
+                       Boolean b = ExporterUtils.getPrefBoolean( contentScopePrefs, workspaceScopePrefs, P_ALLOW_OVERWRITE.tail().toString() );\r
+                       if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);\r
+                       \r
+                       String s = ExporterUtils.getPrefString( contentScopePrefs, null, P_ZIP.tail().toString() );\r
+                       if ( s!=null ) ra.setValue(P_ZIP, Bindings.STRING, s);\r
+                       \r
+               } catch (AccessorConstructionException e) {\r
+                       throw new ExportException( e );\r
+               } catch (AccessorException e) {\r
+                       throw new ExportException( e );\r
+               }               \r
+       }\r
+       \r
+}\r