]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/ZipPublisher.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / ZipPublisher.java
1 package org.simantics.export.core.impl;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileInputStream;\r
5 import java.io.FileOutputStream;\r
6 import java.io.IOException;\r
7 import java.util.ArrayList;\r
8 import java.util.List;\r
9 import java.util.zip.ZipEntry;\r
10 import java.util.zip.ZipOutputStream;\r
11 \r
12 import org.eclipse.core.runtime.IProgressMonitor;\r
13 import org.osgi.service.prefs.Preferences;\r
14 import org.simantics.databoard.Accessors;\r
15 import org.simantics.databoard.Bindings;\r
16 import org.simantics.databoard.Datatypes;\r
17 import org.simantics.databoard.accessor.RecordAccessor;\r
18 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
19 import org.simantics.databoard.accessor.error.AccessorException;\r
20 import org.simantics.databoard.accessor.reference.LabelReference;\r
21 import org.simantics.databoard.binding.mutable.Variant;\r
22 import org.simantics.databoard.forms.DataboardForm;\r
23 import org.simantics.databoard.type.RecordType;\r
24 import org.simantics.databoard.util.StreamUtil;\r
25 import org.simantics.export.core.ExportContext;\r
26 import org.simantics.export.core.error.ExportException;\r
27 import org.simantics.export.core.intf.PublisherClass;\r
28 import org.simantics.export.core.manager.Content;\r
29 import org.simantics.export.core.util.ExporterUtils;\r
30 \r
31 /**\r
32  * There are two fields in this publisher:\r
33  *  [ ] Overwrite file(s)\r
34  *  [ ] Export to Zip\r
35  *  \r
36  * TODO Zip pref saving should be based on content selection\r
37  *  \r
38  * @author toni.kalajainen@semantum.fi\r
39  */\r
40 public class ZipPublisher implements PublisherClass {\r
41 \r
42         private static final String ZIP_EXTENSION = ".zip";\r
43 \r
44         public static RecordType RT_ZIP;\r
45 \r
46         public static LabelReference P_ALLOW_OVERWRITE = new LabelReference("Overwrite file(s)");\r
47         public static LabelReference P_ZIP = new LabelReference("Export to .zip");\r
48         \r
49         static {\r
50                 RT_ZIP = new RecordType();\r
51                 RT_ZIP.addComponent(P_ZIP.label, DataboardForm.fileSaveDialog(ZIP_EXTENSION, "*.zip"));\r
52                 RT_ZIP.addComponent(P_ALLOW_OVERWRITE.label, Datatypes.BOOLEAN);\r
53         }\r
54         \r
55         @Override\r
56         public void publish(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions, IProgressMonitor monitor) throws ExportException {\r
57                 \r
58                 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);\r
59                 String zipPath = ExporterUtils.getString( locationOptions, P_ZIP );\r
60                 if ( zipPath == null ) throw new ExportException("Zip option missing");\r
61                 if ( canOverwrite == null ) throw new ExportException("CanOverwrite option missing");\r
62 \r
63                 zipPath = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, zipPath);\r
64                 File file = new File( zipPath );\r
65                 if ( file.exists() ) {\r
66                         if ( canOverwrite ) {\r
67                                 file.delete();\r
68                         } else {\r
69                                 throw new ExportException("Would not overwrite " + file.getAbsolutePath());\r
70                         }\r
71                 }\r
72                 \r
73                 // Assert the data is OK. Expected to be.\r
74                 for ( Content content : contents ) {\r
75                         if ( content.tmpFile == null ) throw new ExportException("Internal error, tmpFile was null");\r
76                 }\r
77                 \r
78                 FileOutputStream fos = null;\r
79                 try {\r
80                         fos = new FileOutputStream( file );\r
81                         ZipOutputStream zos = new ZipOutputStream(fos);\r
82                         for ( Content content : contents ) {\r
83                                 FileInputStream fis = new FileInputStream( content.tmpFile );\r
84                                 try {\r
85                                         zos.putNextEntry(new ZipEntry( content.filename ));\r
86                                         StreamUtil.copyStream(fis, zos);\r
87                                         zos.closeEntry();\r
88                                 } finally {\r
89                                         fis.close();\r
90                                 }\r
91                         }\r
92                         zos.close();\r
93                 } catch (IOException e) {\r
94                         throw new ExportException( e );\r
95                 } finally {\r
96                         if ( fos != null ) try { fos.close(); } catch (IOException e) {}\r
97                 }\r
98                         \r
99         }\r
100 \r
101         @Override\r
102         public List<String> validate(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {\r
103 \r
104                 List<String> result = new ArrayList<String>();\r
105                 \r
106                 Boolean canOverwrite = ExporterUtils.getBoolean(locationOptions, P_ALLOW_OVERWRITE);\r
107                 String pathName = ExporterUtils.getString( locationOptions, P_ZIP );\r
108                 if ( pathName == null ) { result.add("Zip option missing?"); return result; }\r
109                 if ( canOverwrite == null ) { result.add("CanOverwrite option missing?"); return result; }\r
110                 if ( pathName.isEmpty() ) { result.add("Zip must be entered."); return result; }\r
111                 pathName = PublisherUtil.ensureEndsWith(true, ZIP_EXTENSION, pathName);\r
112                 File file = new File( pathName );\r
113                 if ( !canOverwrite && file.exists() ) \r
114                         result.add( file.getAbsolutePath()+ " already exists." );\r
115                 \r
116                 return result;\r
117         }\r
118         \r
119         @Override\r
120         public RecordType locationOptions(ExportContext ctx, List<Content> contents) throws ExportException {\r
121                 return RT_ZIP;\r
122         }\r
123         \r
124         @Override\r
125         public Variant createLocation(ExportContext ctx, Variant locationOptions) throws ExportException {\r
126                 // Make Dirs to the path.\r
127                 String zipName = ExporterUtils.getString( locationOptions, P_ZIP );\r
128                 if ( zipName == null ) throw new ExportException("Zip option not found?");\r
129                 File file = new File( zipName );\r
130                 File path = file.getParentFile();\r
131                 if ( path == null ) return locationOptions;\r
132                 if ( path.exists() && !path.isDirectory()) throw new ExportException(path+" exists but is not a directory.");\r
133                 if ( !path.mkdirs() ) throw new ExportException( "Failed to create "+path);\r
134                 return locationOptions;\r
135         }\r
136         \r
137          @Override\r
138         public boolean locationExists(ExportContext ctx, Variant locationOptions) throws ExportException {\r
139                 try {\r
140                         RecordAccessor ra = Accessors.getAccessor(locationOptions);                     \r
141                         String zipName = (String) ra.getValue(P_ZIP, Bindings.STRING);\r
142                         if ( zipName == null ) return false;\r
143                         File file = new File( zipName );\r
144                         File path = file.getParentFile();\r
145                         if ( path == null ) return false;\r
146                         return path.exists() && path.isDirectory();\r
147                 } catch (AccessorConstructionException e) {\r
148                         throw new ExportException( e );\r
149                 } catch (AccessorException e) {\r
150                         throw new ExportException( e );\r
151                 }               \r
152         }\r
153 \r
154         @Override\r
155         public void fillDefaultPrefs(ExportContext ctx, List<Content> contents, Variant options, Variant locationOptions) throws ExportException {              \r
156                 try {\r
157                         RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
158                 ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, true);\r
159                 ra.setValue(P_ZIP, Bindings.STRING, "");\r
160                 } catch (AccessorConstructionException e) {\r
161                         throw new ExportException(e);\r
162                 } catch (AccessorException e) {\r
163                         throw new ExportException(e);\r
164                 }               \r
165         }\r
166         \r
167         @Override\r
168         public void savePref(Variant locationOptions, Preferences contentScopeNode, Preferences workspaceScopeNode) throws ExportException {\r
169                 try {\r
170                         RecordAccessor ra = Accessors.getAccessor( locationOptions );\r
171 \r
172                         Boolean b = (Boolean) ra.getValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN);\r
173                         if ( b!=null ) {\r
174                                 contentScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);\r
175                                 workspaceScopeNode.putBoolean(P_ALLOW_OVERWRITE.tail().toString(), b);\r
176                         }\r
177         \r
178                         String s = (String) ra.getValue(P_ZIP, Bindings.STRING);\r
179                         if ( s!=null ) {\r
180                                 contentScopeNode.put(P_ZIP.tail().toString(), s);\r
181                                 //workspaceScopeNode.put(P_ZIP.tail().toString(), s);\r
182                         }\r
183                         \r
184                 } catch (AccessorException e) {\r
185                         throw new ExportException( e );\r
186                 } catch (AccessorConstructionException e) {\r
187                         throw new ExportException( e );\r
188                 }\r
189         }\r
190         \r
191         @Override\r
192         public void loadPref(Variant locationOptions, Preferences contentScopePrefs, Preferences workspaceScopePrefs) throws ExportException {\r
193                 try {\r
194                         RecordAccessor ra = Accessors.getAccessor(locationOptions);\r
195                         \r
196                         Boolean b = ExporterUtils.getPrefBoolean( contentScopePrefs, workspaceScopePrefs, P_ALLOW_OVERWRITE.tail().toString() );\r
197                         if ( b!=null ) ra.setValue(P_ALLOW_OVERWRITE, Bindings.BOOLEAN, b);\r
198                         \r
199                         String s = ExporterUtils.getPrefString( contentScopePrefs, null, P_ZIP.tail().toString() );\r
200                         if ( s!=null ) ra.setValue(P_ZIP, Bindings.STRING, s);\r
201                         \r
202                 } catch (AccessorConstructionException e) {\r
203                         throw new ExportException( e );\r
204                 } catch (AccessorException e) {\r
205                         throw new ExportException( e );\r
206                 }               \r
207         }\r
208         \r
209 }\r