]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.export.core.impl;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipOutputStream;
11
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;
30
31 /**
32  * There are two fields in this publisher:
33  *  [ ] Overwrite file(s)
34  *  [ ] Export to Zip
35  *  
36  * TODO Zip pref saving should be based on content selection
37  *  
38  * @author toni.kalajainen@semantum.fi
39  */
40 public class ZipPublisher implements PublisherClass {
41
42         private static final String ZIP_EXTENSION = ".zip";
43
44         public static RecordType RT_ZIP;
45
46         public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");
47         public static LabelReference P_ZIP = new LabelReference("Export to .zip");
48         
49         static {
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);
53         }
54         
55         @Override
56         public void publish(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions, IProgressMonitor monitor) throws ExportException {
57                 
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");
62
63                 zipPath = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, zipPath);
64                 File file = new File( zipPath );
65                 if ( file.exists() ) {
66                         if ( canOverwrite ) {
67                                 file.delete();
68                         } else {
69                                 throw new ExportException("Would not overwrite " + file.getAbsolutePath());
70                         }
71                 }
72                 
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");
76                 }
77                 
78                 FileOutputStream fos = null;
79                 try {
80                         fos = new FileOutputStream( file );
81                         ZipOutputStream zos = new ZipOutputStream(fos);
82                         for ( Content content : contents ) {
83                                 FileInputStream fis = new FileInputStream( content.tmpFile );
84                                 try {
85                                         zos.putNextEntry(new ZipEntry( content.filename ));
86                                         StreamUtil.copyStream(fis, zos);
87                                         zos.closeEntry();
88                                 } finally {
89                                         fis.close();
90                                 }
91                         }
92                         zos.close();
93                 } catch (IOException e) {
94                         throw new ExportException( e );
95                 } finally {
96                         if ( fos != null ) try { fos.close(); } catch (IOException e) {}
97                 }
98                         
99         }
100
101         @Override
102         public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {
103
104                 List<String> result = new ArrayList<String>();
105                 
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." );
115                 
116                 return result;
117         }
118         
119         @Override
120         public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {
121                 return RT_ZIP;
122         }
123         
124         @Override
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;
135         }
136         
137          @Override
138         public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {
139                 try {
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 );
151                 }               
152         }
153
154         @Override
155         public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {              
156                 try {
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);
164                 }               
165         }
166         
167         @Override
168         public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {
169                 try {
170                         RecordAccessor ra = Accessors.getAccessor( locationOptions );
171
172                         Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);
173                         if ( b!=null ) {
174                                 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
175                                 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);
176                         }
177         
178                         String s = (String) ra.getValue(P_ZIP, Bindings.STRING);
179                         if ( s!=null ) {
180                                 contentScopeNode.put(P_ZIP.tail().toString(), s);
181                                 //workspaceScopeNode.put(P_ZIP.tail().toString(), s);
182                         }
183                         
184                 } catch (AccessorException e) {
185                         throw new ExportException( e );
186                 } catch (AccessorConstructionException e) {
187                         throw new ExportException( e );
188                 }
189         }
190         
191         @Override
192         public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {
193                 try {
194                         RecordAccessor ra = Accessors.getAccessor(locationOptions);
195                         
196                         Boolean b = ExporterUtils.getPrefBoolean( contentScopePrefs, workspaceScopePrefs, P_ALLOW_OVERWRITE.tail().toString() );
197                         if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);
198                         
199                         String s = ExporterUtils.getPrefString( contentScopePrefs, null, P_ZIP.tail().toString() );
200                         if ( s!=null ) ra.setValue(P_ZIP, Bindings.STRING, s);
201                         
202                 } catch (AccessorConstructionException e) {
203                         throw new ExportException( e );
204                 } catch (AccessorException e) {
205                         throw new ExportException( e );
206                 }               
207         }
208         
209 }