Commit | Line | Data |
---|---|---|
6e651d8b | 1 | /******************************************************************************* |
8d03681c | 2 | * Copyright (c) 2013, 2014 Ericsson |
6e651d8b MAL |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg; |
6e651d8b MAL |
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; | |
6e651d8b MAL |
20 | import java.util.zip.ZipFile; |
21 | ||
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
5b3fe39a | 24 | import org.eclipse.jdt.annotation.NonNull; |
6e651d8b MAL |
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") | |
574f43ad | 36 | public abstract class AbstractTracePackageOperation { |
6e651d8b | 37 | private IStatus fStatus; |
8d03681c MAL |
38 | // Result of this operation, if any |
39 | private TracePackageElement[] fResultElements; | |
40 | ||
6e651d8b MAL |
41 | private final String fFileName; |
42 | ||
43 | /** | |
44 | * Constructs a new trace package operation | |
45 | * | |
46 | * @param fileName | |
47 | * the output file name | |
48 | */ | |
49 | public AbstractTracePackageOperation(String fileName) { | |
50 | fFileName = fileName; | |
51 | } | |
52 | ||
53 | /** | |
54 | * Run the operation. The status (result) of the operation can be obtained | |
55 | * with {@link #getStatus} | |
56 | * | |
57 | * @param progressMonitor | |
58 | * the progress monitor to use to display progress and receive | |
59 | * requests for cancellation | |
60 | */ | |
61 | public abstract void run(IProgressMonitor progressMonitor); | |
62 | ||
63 | /** | |
64 | * Returns the status of the operation (result) | |
65 | * | |
66 | * @return the status of the operation | |
67 | */ | |
68 | public IStatus getStatus() { | |
69 | return fStatus; | |
70 | } | |
71 | ||
8d03681c MAL |
72 | /** |
73 | * Get the resulting elements for this operation, if any | |
74 | * | |
75 | * @return the resulting elements or null if no result is produced by this | |
76 | * operation | |
77 | */ | |
78 | public TracePackageElement[] getResultElements() { | |
79 | return fResultElements; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Set the resulting elements for this operation, if any | |
84 | * | |
85 | * @param elements | |
86 | * the resulting elements produced by this operation, can be set | |
87 | * to null | |
88 | */ | |
89 | public void setResultElements(TracePackageElement[] elements) { | |
90 | fResultElements = elements; | |
91 | } | |
92 | ||
6e651d8b MAL |
93 | /** |
94 | * Set the status for this operation | |
95 | * | |
96 | * @param status | |
97 | * the status | |
98 | */ | |
99 | protected void setStatus(IStatus status) { | |
100 | fStatus = status; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Get the file name of the package | |
105 | * | |
106 | * @return the file name | |
107 | */ | |
108 | protected String getFileName() { | |
109 | return fFileName; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Answer a handle to the archive file currently specified as being the | |
114 | * source. Return null if this file does not exist or is not of valid | |
115 | * format. | |
116 | * | |
117 | * @return the archive file | |
118 | */ | |
119 | public ArchiveFile getSpecifiedArchiveFile() { | |
120 | if (fFileName.length() == 0) { | |
121 | return null; | |
122 | } | |
123 | ||
124 | try { | |
7b3eb8c0 | 125 | return new ZipArchiveFile(new ZipFile(fFileName)); |
6e651d8b MAL |
126 | } catch (IOException e) { |
127 | // ignore | |
128 | } | |
129 | ||
130 | try { | |
7b3eb8c0 AM |
131 | return new TarArchiveFile(new TarFile(fFileName)); |
132 | } catch (TarException | IOException e) { | |
6e651d8b MAL |
133 | // ignore |
134 | } | |
135 | ||
136 | return null; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Get the number of checked elements in the array and the children | |
141 | * | |
142 | * @param elements | |
143 | * the elements to check for checked | |
144 | * @return the number of checked elements | |
145 | */ | |
146 | protected int getNbCheckedElements(TracePackageElement[] elements) { | |
147 | int totalWork = 0; | |
148 | for (TracePackageElement tracePackageElement : elements) { | |
353a8e06 MAL |
149 | TracePackageElement[] children = tracePackageElement.getChildren(); |
150 | if (children != null && children.length > 0) { | |
151 | totalWork += getNbCheckedElements(children); | |
6e651d8b MAL |
152 | } else if (tracePackageElement.isChecked()) { |
153 | ++totalWork; | |
154 | } | |
155 | } | |
156 | ||
157 | return totalWork; | |
158 | } | |
159 | ||
195355a9 MAL |
160 | /** |
161 | * Returns whether or not the Files element is checked under the given trace | |
162 | * package element | |
163 | * | |
164 | * @param tracePackageElement | |
165 | * the trace package element | |
166 | * @return whether or not the Files element is checked under the given trace | |
167 | * package element | |
168 | */ | |
169 | public static boolean isFilesChecked(TracePackageElement tracePackageElement) { | |
170 | for (TracePackageElement element : tracePackageElement.getChildren()) { | |
171 | if (element instanceof TracePackageFilesElement) { | |
172 | return element.isChecked(); | |
173 | } | |
174 | } | |
175 | ||
176 | return false; | |
177 | } | |
178 | ||
6e651d8b MAL |
179 | /** |
180 | * Common interface between ZipEntry and TarEntry | |
181 | */ | |
182 | protected interface ArchiveEntry { | |
183 | /** | |
184 | * The name of the entry | |
185 | * | |
186 | * @return The name of the entry | |
187 | */ | |
188 | String getName(); | |
189 | } | |
190 | ||
191 | /** | |
192 | * Common interface between ZipFile and TarFile | |
193 | */ | |
194 | protected interface ArchiveFile { | |
195 | /** | |
196 | * Returns an enumeration cataloging the archive. | |
197 | * | |
198 | * @return enumeration of all files in the archive | |
199 | */ | |
5b3fe39a | 200 | Enumeration<@NonNull ? extends ArchiveEntry> entries(); |
6e651d8b MAL |
201 | |
202 | /** | |
203 | * Close the file input stream. | |
204 | * | |
205 | * @throws IOException | |
206 | */ | |
207 | void close() throws IOException; | |
208 | ||
209 | /** | |
210 | * Returns a new InputStream for the given file in the archive. | |
211 | * | |
212 | * @param entry | |
213 | * the given file | |
214 | * @return an input stream for the given file | |
215 | * @throws TarException | |
216 | * @throws IOException | |
217 | */ | |
218 | InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException; | |
219 | } | |
220 | ||
221 | /** | |
222 | * Adapter for TarFile to ArchiveFile | |
223 | */ | |
224 | protected class TarArchiveFile implements ArchiveFile { | |
225 | ||
226 | private TarFile fTarFile; | |
227 | ||
228 | /** | |
229 | * Constructs a TarAchiveFile for a TarFile | |
230 | * | |
231 | * @param tarFile | |
232 | * the TarFile | |
233 | */ | |
234 | public TarArchiveFile(TarFile tarFile) { | |
235 | this.fTarFile = tarFile; | |
236 | } | |
237 | ||
238 | @Override | |
5b3fe39a AM |
239 | public Enumeration<@NonNull ? extends ArchiveEntry> entries() { |
240 | Vector<@NonNull ArchiveEntry> v = new Vector<>(); | |
6e651d8b MAL |
241 | for (Enumeration<?> e = fTarFile.entries(); e.hasMoreElements();) { |
242 | v.add(new TarArchiveEntry((TarEntry) e.nextElement())); | |
243 | } | |
244 | ||
245 | return v.elements(); | |
246 | } | |
247 | ||
248 | @Override | |
249 | public void close() throws IOException { | |
250 | fTarFile.close(); | |
251 | } | |
252 | ||
253 | @Override | |
254 | public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException { | |
255 | return fTarFile.getInputStream(((TarArchiveEntry) entry).getTarEntry()); | |
256 | } | |
257 | } | |
258 | ||
259 | /** | |
260 | * Adapter for TarEntry to ArchiveEntry | |
261 | */ | |
262 | protected class TarArchiveEntry implements ArchiveEntry { | |
263 | private TarEntry fTarEntry; | |
264 | ||
265 | /** | |
266 | * Constructs a TarArchiveEntry for a TarEntry | |
267 | * | |
268 | * @param tarEntry | |
269 | * the TarEntry | |
270 | */ | |
271 | public TarArchiveEntry(TarEntry tarEntry) { | |
272 | this.fTarEntry = tarEntry; | |
273 | } | |
274 | ||
275 | @Override | |
276 | public String getName() { | |
277 | return fTarEntry.getName(); | |
278 | } | |
279 | ||
280 | /** | |
281 | * Get the corresponding TarEntry | |
282 | * | |
283 | * @return the corresponding TarEntry | |
284 | */ | |
285 | public TarEntry getTarEntry() { | |
286 | return fTarEntry; | |
287 | } | |
288 | ||
289 | @Override | |
290 | public String toString() { | |
291 | return getName(); | |
292 | } | |
293 | } | |
294 | ||
295 | /** | |
296 | * Adapter for ArchiveEntry to ArchiveEntry | |
297 | */ | |
298 | protected class ZipAchiveEntry implements ArchiveEntry { | |
299 | ||
300 | private ZipEntry fZipEntry; | |
301 | ||
302 | /** | |
303 | * Constructs a ZipAchiveEntry for a ZipEntry | |
304 | * | |
305 | * @param zipEntry | |
306 | * the ZipEntry | |
307 | */ | |
308 | public ZipAchiveEntry(ZipEntry zipEntry) { | |
309 | this.fZipEntry = zipEntry; | |
310 | } | |
311 | ||
312 | @Override | |
313 | public String getName() { | |
314 | return fZipEntry.getName(); | |
315 | } | |
316 | ||
317 | /** | |
318 | * Get the corresponding ZipEntry | |
319 | * | |
320 | * @return the corresponding ZipEntry | |
321 | */ | |
322 | public ZipEntry getZipEntry() { | |
323 | return fZipEntry; | |
324 | } | |
325 | ||
326 | @Override | |
327 | public String toString() { | |
328 | return getName(); | |
329 | } | |
330 | } | |
331 | ||
332 | /** | |
333 | * Adapter for ZipFile to ArchiveFile | |
334 | */ | |
335 | protected class ZipArchiveFile implements ArchiveFile { | |
336 | ||
337 | private ZipFile fZipFile; | |
338 | ||
339 | /** | |
340 | * Constructs a ZipArchiveFile for a ZipFile | |
341 | * | |
342 | * @param zipFile | |
343 | * the ZipFile | |
344 | */ | |
345 | public ZipArchiveFile(ZipFile zipFile) { | |
346 | this.fZipFile = zipFile; | |
347 | } | |
348 | ||
349 | @Override | |
5b3fe39a AM |
350 | public Enumeration<@NonNull ? extends ArchiveEntry> entries() { |
351 | Vector<@NonNull ArchiveEntry> v = new Vector<>(); | |
6e651d8b MAL |
352 | for (Enumeration<?> e = fZipFile.entries(); e.hasMoreElements();) { |
353 | v.add(new ZipAchiveEntry((ZipEntry) e.nextElement())); | |
354 | } | |
355 | ||
356 | return v.elements(); | |
357 | } | |
358 | ||
359 | @Override | |
360 | public void close() throws IOException { | |
361 | fZipFile.close(); | |
362 | } | |
363 | ||
364 | @Override | |
365 | public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException { | |
366 | return fZipFile.getInputStream(((ZipAchiveEntry) entry).getZipEntry()); | |
367 | } | |
368 | } | |
369 | ||
370 | } |