1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
;
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
.ZipFile
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
.TarEntry
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
.TarException
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
.TarFile
;
30 * An abstract operation containing common code useful for other trace package
33 * @author Marc-Andre Laperle
35 public abstract class AbstractTracePackageOperation
{
36 private IStatus fStatus
;
37 // Result of this operation, if any
38 private TracePackageElement
[] fResultElements
;
40 private final String fFileName
;
43 * Constructs a new trace package operation
46 * the output file name
48 public AbstractTracePackageOperation(String fileName
) {
53 * Run the operation. The status (result) of the operation can be obtained
54 * with {@link #getStatus}
56 * @param progressMonitor
57 * the progress monitor to use to display progress and receive
58 * requests for cancellation
60 public abstract void run(IProgressMonitor progressMonitor
);
63 * Returns the status of the operation (result)
65 * @return the status of the operation
67 public IStatus
getStatus() {
72 * Get the resulting elements for this operation, if any
74 * @return the resulting elements or null if no result is produced by this
77 public TracePackageElement
[] getResultElements() {
78 return fResultElements
;
82 * Set the resulting elements for this operation, if any
85 * the resulting elements produced by this operation, can be set
88 public void setResultElements(TracePackageElement
[] elements
) {
89 fResultElements
= elements
;
93 * Set the status for this operation
98 protected void setStatus(IStatus status
) {
103 * Get the file name of the package
105 * @return the file name
107 protected String
getFileName() {
112 * Answer a handle to the archive file currently specified as being the
113 * source. Return null if this file does not exist or is not of valid
116 * @return the archive file
118 public ArchiveFile
getSpecifiedArchiveFile() {
119 if (fFileName
.length() == 0) {
124 return new ZipArchiveFile(new ZipFile(fFileName
));
125 } catch (IOException e
) {
130 return new TarArchiveFile(new TarFile(fFileName
));
131 } catch (TarException
| IOException e
) {
139 * Get the number of checked elements in the array and the children
142 * the elements to check for checked
143 * @return the number of checked elements
145 protected int getNbCheckedElements(TracePackageElement
[] elements
) {
147 for (TracePackageElement tracePackageElement
: elements
) {
148 TracePackageElement
[] children
= tracePackageElement
.getChildren();
149 if (children
!= null && children
.length
> 0) {
150 totalWork
+= getNbCheckedElements(children
);
151 } else if (tracePackageElement
.isChecked()) {
160 * Returns whether or not the Files element is checked under the given trace
163 * @param tracePackageElement
164 * the trace package element
165 * @return whether or not the Files element is checked under the given trace
168 public static boolean isFilesChecked(TracePackageElement tracePackageElement
) {
169 for (TracePackageElement element
: tracePackageElement
.getChildren()) {
170 if (element
instanceof TracePackageFilesElement
) {
171 return element
.isChecked();
179 * Common interface between ZipEntry and TarEntry
181 protected interface ArchiveEntry
{
183 * The name of the entry
185 * @return The name of the entry
191 * Common interface between ZipFile and TarFile
193 protected interface ArchiveFile
{
195 * Returns an enumeration cataloging the archive.
197 * @return enumeration of all files in the archive
199 Enumeration
<@NonNull ?
extends ArchiveEntry
> entries();
202 * Close the file input stream.
204 * @throws IOException
206 void close() throws IOException
;
209 * Returns a new InputStream for the given file in the archive.
213 * @return an input stream for the given file
214 * @throws TarException
215 * @throws IOException
217 InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
;
221 * Adapter for TarFile to ArchiveFile
223 protected class TarArchiveFile
implements ArchiveFile
{
225 private TarFile fTarFile
;
228 * Constructs a TarAchiveFile for a TarFile
233 public TarArchiveFile(TarFile tarFile
) {
234 this.fTarFile
= tarFile
;
238 public Enumeration
<@NonNull ?
extends ArchiveEntry
> entries() {
239 Vector
<@NonNull ArchiveEntry
> v
= new Vector
<>();
240 for (Enumeration
<?
> e
= fTarFile
.entries(); e
.hasMoreElements();) {
241 v
.add(new TarArchiveEntry((TarEntry
) e
.nextElement()));
248 public void close() throws IOException
{
253 public InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
{
254 return fTarFile
.getInputStream(((TarArchiveEntry
) entry
).getTarEntry());
259 * Adapter for TarEntry to ArchiveEntry
261 protected class TarArchiveEntry
implements ArchiveEntry
{
262 private TarEntry fTarEntry
;
265 * Constructs a TarArchiveEntry for a TarEntry
270 public TarArchiveEntry(TarEntry tarEntry
) {
271 this.fTarEntry
= tarEntry
;
275 public String
getName() {
276 return fTarEntry
.getName();
280 * Get the corresponding TarEntry
282 * @return the corresponding TarEntry
284 public TarEntry
getTarEntry() {
289 public String
toString() {
295 * Adapter for ArchiveEntry to ArchiveEntry
297 protected class ZipAchiveEntry
implements ArchiveEntry
{
299 private ZipEntry fZipEntry
;
302 * Constructs a ZipAchiveEntry for a ZipEntry
307 public ZipAchiveEntry(ZipEntry zipEntry
) {
308 this.fZipEntry
= zipEntry
;
312 public String
getName() {
313 return fZipEntry
.getName();
317 * Get the corresponding ZipEntry
319 * @return the corresponding ZipEntry
321 public ZipEntry
getZipEntry() {
326 public String
toString() {
332 * Adapter for ZipFile to ArchiveFile
334 protected class ZipArchiveFile
implements ArchiveFile
{
336 private ZipFile fZipFile
;
339 * Constructs a ZipArchiveFile for a ZipFile
344 public ZipArchiveFile(ZipFile zipFile
) {
345 this.fZipFile
= zipFile
;
349 public Enumeration
<@NonNull ?
extends ArchiveEntry
> entries() {
350 Vector
<@NonNull ArchiveEntry
> v
= new Vector
<>();
351 for (Enumeration
<?
> e
= fZipFile
.entries(); e
.hasMoreElements();) {
352 v
.add(new ZipAchiveEntry((ZipEntry
) e
.nextElement()));
359 public void close() throws IOException
{
364 public InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
{
365 return fZipFile
.getInputStream(((ZipAchiveEntry
) entry
).getZipEntry());