tmf: Import and Export trace package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / wizards / tracepkg / AbstractTracePackageOperation.java
1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Enumeration;
18 import java.util.Vector;
19 import java.util.zip.ZipEntry;
20 import java.util.zip.ZipException;
21 import java.util.zip.ZipFile;
22
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.ui.internal.wizards.datatransfer.TarEntry;
26 import org.eclipse.ui.internal.wizards.datatransfer.TarException;
27 import org.eclipse.ui.internal.wizards.datatransfer.TarFile;
28
29 /**
30 * An abstract operation containing common code useful for other trace package
31 * operations
32 *
33 * @author Marc-Andre Laperle
34 */
35 @SuppressWarnings("restriction")
36 abstract public class AbstractTracePackageOperation {
37 private IStatus fStatus;
38 private final String fFileName;
39
40 /**
41 * Constructs a new trace package operation
42 *
43 * @param fileName
44 * the output file name
45 */
46 public AbstractTracePackageOperation(String fileName) {
47 fFileName = fileName;
48 }
49
50 /**
51 * Run the operation. The status (result) of the operation can be obtained
52 * with {@link #getStatus}
53 *
54 * @param progressMonitor
55 * the progress monitor to use to display progress and receive
56 * requests for cancellation
57 */
58 public abstract void run(IProgressMonitor progressMonitor);
59
60 /**
61 * Returns the status of the operation (result)
62 *
63 * @return the status of the operation
64 */
65 public IStatus getStatus() {
66 return fStatus;
67 }
68
69 /**
70 * Set the status for this operation
71 *
72 * @param status
73 * the status
74 */
75 protected void setStatus(IStatus status) {
76 fStatus = status;
77 }
78
79 /**
80 * Get the file name of the package
81 *
82 * @return the file name
83 */
84 protected String getFileName() {
85 return fFileName;
86 }
87
88 /**
89 * Answer a handle to the archive file currently specified as being the
90 * source. Return null if this file does not exist or is not of valid
91 * format.
92 *
93 * @return the archive file
94 */
95 public ArchiveFile getSpecifiedArchiveFile() {
96 if (fFileName.length() == 0) {
97 return null;
98 }
99
100 try {
101 ZipFile zipFile = new ZipFile(fFileName);
102 return new ZipArchiveFile(zipFile);
103 } catch (ZipException e) {
104 // ignore
105 } catch (IOException e) {
106 // ignore
107 }
108
109 try {
110 TarFile tarFile = new TarFile(fFileName);
111 return new TarArchiveFile(tarFile);
112 } catch (TarException e) {
113 // ignore
114 } catch (IOException e) {
115 // ignore
116 }
117
118 return null;
119 }
120
121 /**
122 * Get the number of checked elements in the array and the children
123 *
124 * @param elements
125 * the elements to check for checked
126 * @return the number of checked elements
127 */
128 protected int getNbCheckedElements(TracePackageElement[] elements) {
129 int totalWork = 0;
130 for (TracePackageElement tracePackageElement : elements) {
131 if (tracePackageElement.getChildren() != null) {
132 totalWork += getNbCheckedElements(tracePackageElement.getChildren());
133 } else if (tracePackageElement.isChecked()) {
134 ++totalWork;
135 }
136 }
137
138 return totalWork;
139 }
140
141 /**
142 * Common interface between ZipEntry and TarEntry
143 */
144 protected interface ArchiveEntry {
145 /**
146 * The name of the entry
147 *
148 * @return The name of the entry
149 */
150 String getName();
151 }
152
153 /**
154 * Common interface between ZipFile and TarFile
155 */
156 protected interface ArchiveFile {
157 /**
158 * Returns an enumeration cataloging the archive.
159 *
160 * @return enumeration of all files in the archive
161 */
162 Enumeration<? extends ArchiveEntry> entries();
163
164 /**
165 * Close the file input stream.
166 *
167 * @throws IOException
168 */
169 void close() throws IOException;
170
171 /**
172 * Returns a new InputStream for the given file in the archive.
173 *
174 * @param entry
175 * the given file
176 * @return an input stream for the given file
177 * @throws TarException
178 * @throws IOException
179 */
180 InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException;
181 }
182
183 /**
184 * Adapter for TarFile to ArchiveFile
185 */
186 protected class TarArchiveFile implements ArchiveFile {
187
188 private TarFile fTarFile;
189
190 /**
191 * Constructs a TarAchiveFile for a TarFile
192 *
193 * @param tarFile
194 * the TarFile
195 */
196 public TarArchiveFile(TarFile tarFile) {
197 this.fTarFile = tarFile;
198 }
199
200 @Override
201 public Enumeration<? extends ArchiveEntry> entries() {
202 Vector<ArchiveEntry> v = new Vector<ArchiveEntry>();
203 for (Enumeration<?> e = fTarFile.entries(); e.hasMoreElements();) {
204 v.add(new TarArchiveEntry((TarEntry) e.nextElement()));
205 }
206
207 return v.elements();
208 }
209
210 @Override
211 public void close() throws IOException {
212 fTarFile.close();
213 }
214
215 @Override
216 public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException {
217 return fTarFile.getInputStream(((TarArchiveEntry) entry).getTarEntry());
218 }
219 }
220
221 /**
222 * Adapter for TarEntry to ArchiveEntry
223 */
224 protected class TarArchiveEntry implements ArchiveEntry {
225 private TarEntry fTarEntry;
226
227 /**
228 * Constructs a TarArchiveEntry for a TarEntry
229 *
230 * @param tarEntry
231 * the TarEntry
232 */
233 public TarArchiveEntry(TarEntry tarEntry) {
234 this.fTarEntry = tarEntry;
235 }
236
237 @Override
238 public String getName() {
239 return fTarEntry.getName();
240 }
241
242 /**
243 * Get the corresponding TarEntry
244 *
245 * @return the corresponding TarEntry
246 */
247 public TarEntry getTarEntry() {
248 return fTarEntry;
249 }
250
251 @Override
252 public String toString() {
253 return getName();
254 }
255 }
256
257 /**
258 * Adapter for ArchiveEntry to ArchiveEntry
259 */
260 protected class ZipAchiveEntry implements ArchiveEntry {
261
262 private ZipEntry fZipEntry;
263
264 /**
265 * Constructs a ZipAchiveEntry for a ZipEntry
266 *
267 * @param zipEntry
268 * the ZipEntry
269 */
270 public ZipAchiveEntry(ZipEntry zipEntry) {
271 this.fZipEntry = zipEntry;
272 }
273
274 @Override
275 public String getName() {
276 return fZipEntry.getName();
277 }
278
279 /**
280 * Get the corresponding ZipEntry
281 *
282 * @return the corresponding ZipEntry
283 */
284 public ZipEntry getZipEntry() {
285 return fZipEntry;
286 }
287
288 @Override
289 public String toString() {
290 return getName();
291 }
292 }
293
294 /**
295 * Adapter for ZipFile to ArchiveFile
296 */
297 protected class ZipArchiveFile implements ArchiveFile {
298
299 private ZipFile fZipFile;
300
301 /**
302 * Constructs a ZipArchiveFile for a ZipFile
303 *
304 * @param zipFile
305 * the ZipFile
306 */
307 public ZipArchiveFile(ZipFile zipFile) {
308 this.fZipFile = zipFile;
309 }
310
311 @Override
312 public Enumeration<? extends ArchiveEntry> entries() {
313 Vector<ArchiveEntry> v = new Vector<ArchiveEntry>();
314 for (Enumeration<?> e = fZipFile.entries(); e.hasMoreElements();) {
315 v.add(new ZipAchiveEntry((ZipEntry) e.nextElement()));
316 }
317
318 return v.elements();
319 }
320
321 @Override
322 public void close() throws IOException {
323 fZipFile.close();
324 }
325
326 @Override
327 public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException {
328 return fZipFile.getInputStream(((ZipAchiveEntry) entry).getZipEntry());
329 }
330 }
331
332 }
This page took 0.03801 seconds and 5 git commands to generate.