]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/DirPublisher.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / DirPublisher.java
index 221ab15c99a252925a336363056c20aecf42c2a3..01765914966889aa75dff582cd9d84e122578b1c 100644 (file)
-package org.simantics.export.core.impl;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.ArrayList;\r
-import java.util.List;\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.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
-import org.simantics.utils.FileUtils;\r
-\r
-/**\r
- * There are two fields in this publisher:\r
- *  [ ] Overwrite file(s)\r
- *  [ ] Export Location\r
- * \r
- * @author toni.kalajainen@semantum.fi\r
- */\r
-public class DirPublisher implements PublisherClass {\r
-\r
-       public static RecordType RT_DIR;\r
-\r
-       public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");\r
-       public static LabelReference P_EXPORT_LOCATION = new LabelReference("Export location");\r
-       \r
-       static {\r
-               RT_DIR = new RecordType();\r
-               RT_DIR.addComponent(P_EXPORT_LOCATION.label, DataboardForm.directoryDialog());\r
-               RT_DIR.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 path = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );\r
-               if ( path == null ) throw new ExportException("Path option missing");\r
-               if ( canOverwrite == null ) throw new ExportException("CanOverwrite option missing");\r
-               \r
-               for ( Content content : contents ) {\r
-                       if ( content.tmpFile == null ) {\r
-                               throw new ExportException("Internal error, tmpFile was null");\r
-                       }\r
-\r
-                       File file = new File( path, content.filename );\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
-                       if ( !content.tmpFile.exists() ) {\r
-                               throw new ExportException("temporary file "+content.tmpFile.getAbsolutePath()+" did not exist?");\r
-                       }\r
-                       if ( !content.tmpFile.renameTo(file) ) {\r
-                               // File.renameTo is not guaranteed to work between file systems.\r
-                               // In that case, move by copying and deleting\r
-                               try {\r
-                                       FileUtils.copyFile(content.tmpFile, file);\r
-                                       if (!content.tmpFile.delete()) {\r
-                                               throw new IOException("Failed to delete " + content.tmpFile.getAbsolutePath() + " after copying it");\r
-                                       }\r
-                               } catch (IOException e) {\r
-                                       throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath(), e);\r
-                               }\r
-                       }\r
-                       if ( content.tmpFile.exists() ) {\r
-                               throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath());\r
-                       }\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_EXPORT_LOCATION );\r
-               if ( pathName == null ) { result.add("Location option missing?"); return result; }\r
-               if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }\r
-               if ( pathName.isEmpty() ) { result.add("Location must be entered."); return result; }\r
-               File path = new File( pathName );\r
-               \r
-               if ( path.exists() && !path.isDirectory() ) {\r
-                       result.add( pathName+" is not a directory.");\r
-               }\r
-               \r
-               // We allow non-existing path to pass validator\r
-               // The location is created with createLocation\r
-               \r
-               if ( !canOverwrite && path.exists() ) {\r
-                       for ( Content content : contents ) {\r
-                               File file = new File( path, content.filename );\r
-                               if ( file.exists() ) {\r
-                                       result.add( file.getAbsolutePath()+ " already exists." );\r
-                               }\r
-                       }                       \r
-               }\r
-               \r
-               return result;\r
-       }\r
-       \r
-       @Override\r
-       public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {\r
-               return RT_DIR;\r
-       }\r
-       \r
-       @Override\r
-       public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {\r
-               // Make Dirs to the path.\r
-               String pathName = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );\r
-               if ( pathName == null ) throw new ExportException("Location option not found?");\r
-               File path = new File( pathName );\r
-               if ( path.exists() && !path.isDirectory()) throw new ExportException(pathName+" exists but is not a directory.");\r
-               if ( !path.mkdirs() ) throw new ExportException( "Failed to create "+pathName);\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 location = (String) ra.getValue(P_EXPORT_LOCATION, Bindings.STRING);\r
-                       if ( location == null ) return false;\r
-                       File path = new File( location );\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_EXPORT_LOCATION, 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_EXPORT_LOCATION, Bindings.STRING);\r
-                       if ( s!=null ) {\r
-                               contentScopeNode.put(P_EXPORT_LOCATION.tail().toString(), s);\r
-                               workspaceScopeNode.put(P_EXPORT_LOCATION.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, workspaceScopePrefs, P_EXPORT_LOCATION.tail().toString() );\r
-                       if ( s!=null ) ra.setValue(P_EXPORT_LOCATION, 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
-}\r
+package org.simantics.export.core.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+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.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;
+import org.simantics.utils.FileUtils;
+
+/**
+ * There are two fields in this publisher:
+ *  [ ] Overwrite file(s)
+ *  [ ] Export Location
+ * 
+ * @author toni.kalajainen@semantum.fi
+ */
+public class DirPublisher implements PublisherClass {
+
+       public static RecordType RT_DIR;
+
+       public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
+       public static LabelReference P_EXPORT_LOCATION = new LabelReference("Export location");
+       
+       static {
+               RT_DIR = new RecordType();
+               RT_DIR.addComponent(P_EXPORT_LOCATION.label, DataboardForm.directoryDialog());
+               RT_DIR.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 path = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );
+               if ( path == null ) throw new ExportException("Path option missing");
+               if ( canOverwrite == null ) throw new ExportException("CanOverwrite option missing");
+               
+               for ( Content content : contents ) {
+                       if ( content.tmpFile == null ) {
+                               throw new ExportException("Internal error, tmpFile was null");
+                       }
+
+                       File file = new File( path, content.filename );
+                       if ( file.exists() ) {
+                               if ( canOverwrite ) {
+                                       file.delete();
+                               } else {
+                                       throw new ExportException("Would not overwrite " + file.getAbsolutePath());
+                               }
+                       }
+                       
+                       if ( !content.tmpFile.exists() ) {
+                               throw new ExportException("temporary file "+content.tmpFile.getAbsolutePath()+" did not exist?");
+                       }
+                       if ( !content.tmpFile.renameTo(file) ) {
+                               // File.renameTo is not guaranteed to work between file systems.
+                               // In that case, move by copying and deleting
+                               try {
+                                       FileUtils.copyFile(content.tmpFile, file);
+                                       if (!content.tmpFile.delete()) {
+                                               throw new IOException("Failed to delete " + content.tmpFile.getAbsolutePath() + " after copying it");
+                                       }
+                               } catch (IOException e) {
+                                       throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath(), e);
+                               }
+                       }
+                       if ( content.tmpFile.exists() ) {
+                               throw new ExportException("Failed to move temporary file "+content.tmpFile.getAbsolutePath()+" to "+file.getAbsolutePath());
+                       }
+               }
+               
+       }
+
+       @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_EXPORT_LOCATION );
+               if ( pathName == null ) { result.add("Location option missing?"); return result; }
+               if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }
+               if ( pathName.isEmpty() ) { result.add("Location must be entered."); return result; }
+               File path = new File( pathName );
+               
+               if ( path.exists() && !path.isDirectory() ) {
+                       result.add( pathName+" is not a directory.");
+               }
+               
+               // We allow non-existing path to pass validator
+               // The location is created with createLocation
+               
+               if ( !canOverwrite && path.exists() ) {
+                       for ( Content content : contents ) {
+                               File file = new File( path, content.filename );
+                               if ( file.exists() ) {
+                                       result.add( file.getAbsolutePath()+ " already exists." );
+                               }
+                       }                       
+               }
+               
+               return result;
+       }
+       
+       @Override
+       public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
+               return RT_DIR;
+       }
+       
+       @Override
+       public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {
+               // Make Dirs to the path.
+               String pathName = ExporterUtils.getString( locationOptions, P_EXPORT_LOCATION );
+               if ( pathName == null ) throw new ExportException("Location option not found?");
+               File path = new File( pathName );
+               if ( path.exists() && !path.isDirectory()) throw new ExportException(pathName+" exists but is not a directory.");
+               if ( !path.mkdirs() ) throw new ExportException( "Failed to create "+pathName);
+               return locationOptions;
+       }
+       
+        @Override
+       public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
+               try {
+                       RecordAccessor ra = Accessors.getAccessor(locationOptions);                     
+                       String location = (String) ra.getValue(P_EXPORT_LOCATION, Bindings.STRING);
+                       if ( location == null ) return false;
+                       File path = new File( location );
+                       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_EXPORT_LOCATION, 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_EXPORT_LOCATION, Bindings.STRING);
+                       if ( s!=null ) {
+                               contentScopeNode.put(P_EXPORT_LOCATION.tail().toString(), s);
+                               workspaceScopeNode.put(P_EXPORT_LOCATION.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, workspaceScopePrefs, P_EXPORT_LOCATION.tail().toString() );
+                       if ( s!=null ) ra.setValue(P_EXPORT_LOCATION, Bindings.STRING, s);
+                       
+               } catch (AccessorConstructionException e) {
+                       throw new ExportException( e );
+               } catch (AccessorException e) {
+                       throw new ExportException( e );
+               }               
+       }
+
+       
+}