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