tmf: Switch tmf.ui to Java 7 + fix warnings
[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 * Returns whether or not the Files element is checked under the given trace
143 * package element
144 *
145 * @param tracePackageElement
146 * the trace package element
147 * @return whether or not the Files element is checked under the given trace
148 * package element
149 */
150 public static boolean isFilesChecked(TracePackageElement tracePackageElement) {
151 for (TracePackageElement element : tracePackageElement.getChildren()) {
152 if (element instanceof TracePackageFilesElement) {
153 return element.isChecked();
154 }
155 }
156
157 return false;
158 }
159
160 /**
161 * Common interface between ZipEntry and TarEntry
162 */
163 protected interface ArchiveEntry {
164 /**
165 * The name of the entry
166 *
167 * @return The name of the entry
168 */
169 String getName();
170 }
171
172 /**
173 * Common interface between ZipFile and TarFile
174 */
175 protected interface ArchiveFile {
176 /**
177 * Returns an enumeration cataloging the archive.
178 *
179 * @return enumeration of all files in the archive
180 */
181 Enumeration<? extends ArchiveEntry> entries();
182
183 /**
184 * Close the file input stream.
185 *
186 * @throws IOException
187 */
188 void close() throws IOException;
189
190 /**
191 * Returns a new InputStream for the given file in the archive.
192 *
193 * @param entry
194 * the given file
195 * @return an input stream for the given file
196 * @throws TarException
197 * @throws IOException
198 */
199 InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException;
200 }
201
202 /**
203 * Adapter for TarFile to ArchiveFile
204 */
205 protected class TarArchiveFile implements ArchiveFile {
206
207 private TarFile fTarFile;
208
209 /**
210 * Constructs a TarAchiveFile for a TarFile
211 *
212 * @param tarFile
213 * the TarFile
214 */
215 public TarArchiveFile(TarFile tarFile) {
216 this.fTarFile = tarFile;
217 }
218
219 @Override
220 public Enumeration<? extends ArchiveEntry> entries() {
221 Vector<ArchiveEntry> v = new Vector<>();
222 for (Enumeration<?> e = fTarFile.entries(); e.hasMoreElements();) {
223 v.add(new TarArchiveEntry((TarEntry) e.nextElement()));
224 }
225
226 return v.elements();
227 }
228
229 @Override
230 public void close() throws IOException {
231 fTarFile.close();
232 }
233
234 @Override
235 public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException {
236 return fTarFile.getInputStream(((TarArchiveEntry) entry).getTarEntry());
237 }
238 }
239
240 /**
241 * Adapter for TarEntry to ArchiveEntry
242 */
243 protected class TarArchiveEntry implements ArchiveEntry {
244 private TarEntry fTarEntry;
245
246 /**
247 * Constructs a TarArchiveEntry for a TarEntry
248 *
249 * @param tarEntry
250 * the TarEntry
251 */
252 public TarArchiveEntry(TarEntry tarEntry) {
253 this.fTarEntry = tarEntry;
254 }
255
256 @Override
257 public String getName() {
258 return fTarEntry.getName();
259 }
260
261 /**
262 * Get the corresponding TarEntry
263 *
264 * @return the corresponding TarEntry
265 */
266 public TarEntry getTarEntry() {
267 return fTarEntry;
268 }
269
270 @Override
271 public String toString() {
272 return getName();
273 }
274 }
275
276 /**
277 * Adapter for ArchiveEntry to ArchiveEntry
278 */
279 protected class ZipAchiveEntry implements ArchiveEntry {
280
281 private ZipEntry fZipEntry;
282
283 /**
284 * Constructs a ZipAchiveEntry for a ZipEntry
285 *
286 * @param zipEntry
287 * the ZipEntry
288 */
289 public ZipAchiveEntry(ZipEntry zipEntry) {
290 this.fZipEntry = zipEntry;
291 }
292
293 @Override
294 public String getName() {
295 return fZipEntry.getName();
296 }
297
298 /**
299 * Get the corresponding ZipEntry
300 *
301 * @return the corresponding ZipEntry
302 */
303 public ZipEntry getZipEntry() {
304 return fZipEntry;
305 }
306
307 @Override
308 public String toString() {
309 return getName();
310 }
311 }
312
313 /**
314 * Adapter for ZipFile to ArchiveFile
315 */
316 protected class ZipArchiveFile implements ArchiveFile {
317
318 private ZipFile fZipFile;
319
320 /**
321 * Constructs a ZipArchiveFile for a ZipFile
322 *
323 * @param zipFile
324 * the ZipFile
325 */
326 public ZipArchiveFile(ZipFile zipFile) {
327 this.fZipFile = zipFile;
328 }
329
330 @Override
331 public Enumeration<? extends ArchiveEntry> entries() {
332 Vector<ArchiveEntry> v = new Vector<>();
333 for (Enumeration<?> e = fZipFile.entries(); e.hasMoreElements();) {
334 v.add(new ZipAchiveEntry((ZipEntry) e.nextElement()));
335 }
336
337 return v.elements();
338 }
339
340 @Override
341 public void close() throws IOException {
342 fZipFile.close();
343 }
344
345 @Override
346 public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException {
347 return fZipFile.getInputStream(((ZipAchiveEntry) entry).getZipEntry());
348 }
349 }
350
351 }
This page took 0.063543 seconds and 5 git commands to generate.