1 package org.simantics.export.core.impl;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.ArrayList;
9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipOutputStream;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.osgi.service.prefs.Preferences;
14 import org.simantics.databoard.Accessors;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.databoard.Datatypes;
17 import org.simantics.databoard.accessor.RecordAccessor;
18 import org.simantics.databoard.accessor.error.AccessorConstructionException;
19 import org.simantics.databoard.accessor.error.AccessorException;
20 import org.simantics.databoard.accessor.reference.LabelReference;
21 import org.simantics.databoard.binding.mutable.Variant;
22 import org.simantics.databoard.forms.DataboardForm;
23 import org.simantics.databoard.type.RecordType;
24 import org.simantics.databoard.util.StreamUtil;
25 import org.simantics.export.core.ExportContext;
26 import org.simantics.export.core.error.ExportException;
27 import org.simantics.export.core.intf.PublisherClass;
28 import org.simantics.export.core.manager.Content;
29 import org.simantics.export.core.util.ExporterUtils;
32 * There are two fields in this publisher:
33 * [ ] Overwrite file(s)
36 * TODO Zip pref saving should be based on content selection
38 * @author toni.kalajainen@semantum.fi
40 public class ZipPublisher implements PublisherClass {
42 private static final String ZIP_EXTENSION = ".zip";
44 public static RecordType RT_ZIP;
46 public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
47 public static LabelReference P_ZIP = new LabelReference("Export to .zip");
50 RT_ZIP = new RecordType();
51 RT_ZIP.addComponent(P_ZIP.label, DataboardForm.fileSaveDialog(ZIP_EXTENSION, "*.zip"));
52 RT_ZIP.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);
56 public void publish(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions, IProgressMonitor monitor) throws ExportException {
58 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);
59 String zipPath = ExporterUtils.getString( locationOptions, P_ZIP );
60 if ( zipPath == null ) throw new ExportException("Zip option missing");
61 if ( canOverwrite == null ) throw new ExportException("CanOverwrite option missing");
63 zipPath = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, zipPath);
64 File file = new File( zipPath );
65 if ( file.exists() ) {
69 throw new ExportException("Would not overwrite " + file.getAbsolutePath());
73 // Assert the data is OK. Expected to be.
74 for ( Content content : contents ) {
75 if ( content.tmpFile == null ) throw new ExportException("Internal error, tmpFile was null");
78 FileOutputStream fos = null;
80 fos = new FileOutputStream( file );
81 ZipOutputStream zos = new ZipOutputStream(fos);
82 for ( Content content : contents ) {
83 FileInputStream fis = new FileInputStream( content.tmpFile );
85 zos.putNextEntry(new ZipEntry( content.filename ));
86 StreamUtil.copyStream(fis, zos);
93 } catch (IOException e) {
94 throw new ExportException( e );
96 if ( fos != null ) try { fos.close(); } catch (IOException e) {}
102 public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
104 List<String> result = new ArrayList<String>();
106 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);
107 String pathName = ExporterUtils.getString( locationOptions, P_ZIP );
108 if ( pathName == null ) { result.add("Zip option missing?"); return result; }
109 if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }
110 if ( pathName.isEmpty() ) { result.add("Zip must be entered."); return result; }
111 pathName = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, pathName);
112 File file = new File( pathName );
113 if ( !canOverwrite && file.exists() )
114 result.add( file.getAbsolutePath()+ " already exists." );
120 public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
125 public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {
126 // Make Dirs to the path.
127 String zipName = ExporterUtils.getString( locationOptions, P_ZIP );
128 if ( zipName == null ) throw new ExportException("Zip option not found?");
129 File file = new File( zipName );
130 File path = file.getParentFile();
131 if ( path == null ) return locationOptions;
132 if ( path.exists() && !path.isDirectory()) throw new ExportException(path+" exists but is not a directory.");
133 if ( !path.mkdirs() ) throw new ExportException( "Failed to create "+path);
134 return locationOptions;
138 public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
140 RecordAccessor ra = Accessors.getAccessor(locationOptions);
141 String zipName = (String) ra.getValue(P_ZIP, Bindings.STRING);
142 if ( zipName == null ) return false;
143 File file = new File( zipName );
144 File path = file.getParentFile();
145 if ( path == null ) return false;
146 return path.exists() && path.isDirectory();
147 } catch (AccessorConstructionException e) {
148 throw new ExportException( e );
149 } catch (AccessorException e) {
150 throw new ExportException( e );
155 public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
157 RecordAccessor ra = Accessors.getAccessor(locationOptions);
158 ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);
159 ra.setValue(P_ZIP, Bindings.STRING, "");
160 } catch (AccessorConstructionException e) {
161 throw new ExportException(e);
162 } catch (AccessorException e) {
163 throw new ExportException(e);
168 public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {
170 RecordAccessor ra = Accessors.getAccessor( locationOptions );
172 Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);
174 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
175 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
178 String s = (String) ra.getValue(P_ZIP, Bindings.STRING);
180 contentScopeNode.put(P_ZIP.tail().toString(), s);
181 //workspaceScopeNode.put(P_ZIP.tail().toString(), s);
184 } catch (AccessorException e) {
185 throw new ExportException( e );
186 } catch (AccessorConstructionException e) {
187 throw new ExportException( e );
192 public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
194 RecordAccessor ra = Accessors.getAccessor(locationOptions);
196 Boolean b = ExporterUtils.getPrefBoolean( contentScopePrefs, workspaceScopePrefs, P_ALLOW_OVERWRITE.tail().toString() );
197 if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);
199 String s = ExporterUtils.getPrefString( contentScopePrefs, null, P_ZIP.tail().toString() );
200 if ( s!=null ) ra.setValue(P_ZIP, Bindings.STRING, s);
202 } catch (AccessorConstructionException e) {
203 throw new ExportException( e );
204 } catch (AccessorException e) {
205 throw new ExportException( e );