]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.export.core/src/org/simantics/export/core/manager/ExportSingleContent.java
ImportPdfReader now implements Closeable
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / manager / ExportSingleContent.java
index c1364d133c22fdcf7cf3abf33c8ccf97185e024f..b1b835cab03241b59c507cfb54a3eab14f0dc785 100644 (file)
-package org.simantics.export.core.manager;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.util.URIUtil;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.export.core.ExportContext;\r
-import org.simantics.export.core.error.ExportException;\r
-import org.simantics.export.core.intf.ContentType;\r
-import org.simantics.export.core.intf.Exporter;\r
-import org.simantics.export.core.intf.Format;\r
-import org.simantics.export.core.util.ExportQueries;\r
-\r
-/**\r
- * Exports a single content file into a tmp file.\r
- * \r
- * The tmp file value is written to the Content object. \r
- *\r
- * @author toni.kalajainen@semantum.fi\r
- */\r
-public class ExportSingleContent extends ExportAction {\r
-       \r
-       public String contentUri;\r
-       public String contentTypeId;\r
-       public String formatId;\r
-       public Content content;\r
-       public File outputFile;\r
-       \r
-       public ExportSingleContent(Content content) throws ExportException \r
-       {\r
-               if ( content.url == null || content.formatId == null || content.label == null || content.contentTypeId == null)\r
-                       throw new ExportException("Invalid input parameters in Content (null)");\r
-               \r
-               this.content = content;\r
-               this.contentUri = content.url;\r
-               this.formatId = content.formatId;\r
-               this.contentTypeId = content.contentTypeId;             \r
-       }\r
-       \r
-       public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options) \r
-       throws ExportException\r
-       {\r
-               Format format = ctx.eep.getFormat( formatId );\r
-               ContentType contentType = ctx.eep.getContentType( contentTypeId );\r
-               Exporter[] exporters = ctx.eep.getExporters( formatId, contentTypeId );\r
-               \r
-               if ( exporters.length == 0 ) {\r
-                       throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());\r
-               }\r
-\r
-               try {\r
-                       // Prefix must be at least 3 characters in length\r
-                       String prefix = "___" + URIUtil.encodeFilename(content.label);\r
-                       content.tmpFile = outputFile = File.createTempFile( prefix, URIUtil.encodeFilename(format.fileext()), Simantics.getTemporaryDirectory("export.core") );\r
-               } catch (IOException e) {\r
-                       throw new ExportException(e);\r
-               } \r
-               \r
-               Object writer = format.createFile(ctx, outputFile, options);\r
-               try {\r
-                       for (Exporter exporter : exporters ) {\r
-                               exporter.exportAction().export(\r
-                                               Collections.singletonList( content ),\r
-                                               writer, \r
-                                               ctx, \r
-                                               options,\r
-                                               monitor,\r
-                                               null);\r
-                       }\r
-                       \r
-               } catch (ExportException ee) {\r
-                       format.closeFile( ctx, writer );\r
-                       writer = null;\r
-                       outputFile.delete();\r
-                       throw ee;\r
-               } finally {\r
-                       if (writer!=null) format.closeFile( ctx, writer );\r
-               }\r
-               \r
-               monitor.worked(10);\r
-       }\r
-\r
-       @Override\r
-       public String label(ExportContext ctx) {\r
-               String label;\r
-               try {\r
-                       label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );\r
-               } catch (DatabaseException e) {\r
-                       label = "<error: "+e.getMessage()+">";\r
-               }\r
-               \r
-               if ( outputFile == null ) return label;\r
-               return label + " export to "+outputFile.getName();\r
-       }\r
-\r
-       @Override\r
-       public int work(ExportContext ctx) {\r
-               return 10;\r
-       }\r
-\r
-       public List<String> validate(ExportContext context, Variant options) {\r
-               Format format = context.eep.getFormat( formatId );\r
-               ContentType contentType = context.eep.getContentType( contentTypeId );\r
-               Exporter[] exporters = context.eep.getExporters( formatId, contentTypeId );\r
-               \r
-               if ( exporters.length == 0 ) {\r
-                       return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );\r
-               }\r
-\r
-               List<String> result = new ArrayList<String>(1);\r
-               for (Exporter exporter : exporters) {\r
-                       try {\r
-                               result.addAll( exporter.exportAction().validate(contentUri, context, options) ) ;\r
-                       } catch (ExportException e) {\r
-                               result.add( "Could not create export action for the "+exporter.formatId()+" exporter. "+e.getMessage() );\r
-                       }\r
-               }\r
-               \r
-               return result;\r
-       }\r
-\r
-       @Override\r
-       public void cleanup(ExportContext ctx, IProgressMonitor progress, Variant options) throws ExportException {\r
-               if ( this.outputFile != null ) { \r
-                       this.outputFile.delete();\r
-                       content.tmpFile = null;                 \r
-               }\r
-       }\r
-               \r
-}\r
+package org.simantics.export.core.manager;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.simantics.Simantics;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.databoard.util.URIUtil;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.export.core.ExportContext;
+import org.simantics.export.core.error.ExportException;
+import org.simantics.export.core.intf.ContentType;
+import org.simantics.export.core.intf.Exporter;
+import org.simantics.export.core.intf.Format;
+import org.simantics.export.core.util.ExportQueries;
+
+/**
+ * Exports a single content file into a tmp file.
+ * 
+ * The tmp file value is written to the Content object. 
+ *
+ * @author toni.kalajainen@semantum.fi
+ */
+public class ExportSingleContent extends ExportAction {
+       
+       public String contentUri;
+       public String contentTypeId;
+       public String formatId;
+       public Content content;
+       public File outputFile;
+       
+       public ExportSingleContent(Content content) throws ExportException 
+       {
+               if ( content.url == null || content.formatId == null || content.label == null || content.contentTypeId == null)
+                       throw new ExportException("Invalid input parameters in Content (null)");
+               
+               this.content = content;
+               this.contentUri = content.url;
+               this.formatId = content.formatId;
+               this.contentTypeId = content.contentTypeId;             
+       }
+       
+       public void execute(ExportContext ctx, IProgressMonitor monitor, Variant options) 
+       throws ExportException
+       {
+               Format format = ctx.eep.getFormat( formatId );
+               ContentType contentType = ctx.eep.getContentType( contentTypeId );
+               Exporter[] exporters = ctx.eep.getExporters( formatId, contentTypeId );
+               
+               if ( exporters.length == 0 ) {
+                       throw new ExportException("No suitable exporter found for exporting "+contentType.label()+" to a "+format.label());
+               }
+
+               try {
+                       // Prefix must be at least 3 characters in length
+                       String prefix = "___" + URIUtil.encodeFilename(content.label);
+                       content.tmpFile = outputFile = File.createTempFile( prefix, URIUtil.encodeFilename(format.fileext()), Simantics.getTemporaryDirectory("export.core") );
+               } catch (IOException e) {
+                       throw new ExportException(e);
+               } 
+               
+               Object writer = format.createFile(ctx, outputFile, options);
+               try {
+                       for (Exporter exporter : exporters ) {
+                               exporter.exportAction().export(
+                                               Collections.singletonList( content ),
+                                               writer, 
+                                               ctx, 
+                                               options,
+                                               monitor,
+                                               null);
+                       }
+                       
+               } catch (ExportException ee) {
+                       format.closeFile( ctx, writer );
+                       writer = null;
+                       outputFile.delete();
+                       throw ee;
+               } finally {
+                       if (writer!=null) format.closeFile( ctx, writer );
+               }
+               
+               monitor.worked(10);
+       }
+
+       @Override
+       public String label(ExportContext ctx) {
+               String label;
+               try {
+                       label = ctx.session.syncRequest( ExportQueries.label( contentUri ) );
+               } catch (DatabaseException e) {
+                       label = "<error: "+e.getMessage()+">";
+               }
+               
+               if ( outputFile == null ) return label;
+               return label + " export to "+outputFile.getName();
+       }
+
+       @Override
+       public int work(ExportContext ctx) {
+               return 10;
+       }
+
+       public List<String> validate(ExportContext context, Variant options) {
+               Format format = context.eep.getFormat( formatId );
+               ContentType contentType = context.eep.getContentType( contentTypeId );
+               Exporter[] exporters = context.eep.getExporters( formatId, contentTypeId );
+               
+               if ( exporters.length == 0 ) {
+                       return Collections.singletonList( "No suitable exporter found for exporting "+contentType.label()+" to a "+format.label() );
+               }
+
+               List<String> result = new ArrayList<String>(1);
+               for (Exporter exporter : exporters) {
+                       try {
+                               result.addAll( exporter.exportAction().validate(contentUri, context, options) ) ;
+                       } catch (ExportException e) {
+                               result.add( "Could not create export action for the "+exporter.formatId()+" exporter. "+e.getMessage() );
+                       }
+               }
+               
+               return result;
+       }
+
+       @Override
+       public void cleanup(ExportContext ctx, IProgressMonitor progress, Variant options) throws ExportException {
+               if ( this.outputFile != null ) { 
+                       this.outputFile.delete();
+                       content.tmpFile = null;                 
+               }
+       }
+               
+}