1 /*******************************************************************************
2 * Copyright (c) 2013 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
.linuxtools
.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
.ZipException
;
21 import java
.util
.zip
.ZipFile
;
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
;
30 * An abstract operation containing common code useful for other trace package
33 * @author Marc-Andre Laperle
35 @SuppressWarnings("restriction")
36 abstract public class AbstractTracePackageOperation
{
37 private IStatus fStatus
;
38 private final String fFileName
;
41 * Constructs a new trace package operation
44 * the output file name
46 public AbstractTracePackageOperation(String fileName
) {
51 * Run the operation. The status (result) of the operation can be obtained
52 * with {@link #getStatus}
54 * @param progressMonitor
55 * the progress monitor to use to display progress and receive
56 * requests for cancellation
58 public abstract void run(IProgressMonitor progressMonitor
);
61 * Returns the status of the operation (result)
63 * @return the status of the operation
65 public IStatus
getStatus() {
70 * Set the status for this operation
75 protected void setStatus(IStatus status
) {
80 * Get the file name of the package
82 * @return the file name
84 protected String
getFileName() {
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
93 * @return the archive file
95 public ArchiveFile
getSpecifiedArchiveFile() {
96 if (fFileName
.length() == 0) {
101 ZipFile zipFile
= new ZipFile(fFileName
);
102 return new ZipArchiveFile(zipFile
);
103 } catch (ZipException e
) {
105 } catch (IOException e
) {
110 TarFile tarFile
= new TarFile(fFileName
);
111 return new TarArchiveFile(tarFile
);
112 } catch (TarException e
) {
114 } catch (IOException e
) {
122 * Get the number of checked elements in the array and the children
125 * the elements to check for checked
126 * @return the number of checked elements
128 protected int getNbCheckedElements(TracePackageElement
[] elements
) {
130 for (TracePackageElement tracePackageElement
: elements
) {
131 if (tracePackageElement
.getChildren() != null) {
132 totalWork
+= getNbCheckedElements(tracePackageElement
.getChildren());
133 } else if (tracePackageElement
.isChecked()) {
142 * Returns whether or not the Files element is checked under the given trace
145 * @param tracePackageElement
146 * the trace package element
147 * @return whether or not the Files element is checked under the given trace
150 public static boolean isFilesChecked(TracePackageElement tracePackageElement
) {
151 for (TracePackageElement element
: tracePackageElement
.getChildren()) {
152 if (element
instanceof TracePackageFilesElement
) {
153 return element
.isChecked();
161 * Common interface between ZipEntry and TarEntry
163 protected interface ArchiveEntry
{
165 * The name of the entry
167 * @return The name of the entry
173 * Common interface between ZipFile and TarFile
175 protected interface ArchiveFile
{
177 * Returns an enumeration cataloging the archive.
179 * @return enumeration of all files in the archive
181 Enumeration
<?
extends ArchiveEntry
> entries();
184 * Close the file input stream.
186 * @throws IOException
188 void close() throws IOException
;
191 * Returns a new InputStream for the given file in the archive.
195 * @return an input stream for the given file
196 * @throws TarException
197 * @throws IOException
199 InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
;
203 * Adapter for TarFile to ArchiveFile
205 protected class TarArchiveFile
implements ArchiveFile
{
207 private TarFile fTarFile
;
210 * Constructs a TarAchiveFile for a TarFile
215 public TarArchiveFile(TarFile tarFile
) {
216 this.fTarFile
= tarFile
;
220 public Enumeration
<?
extends ArchiveEntry
> entries() {
221 Vector
<ArchiveEntry
> v
= new Vector
<ArchiveEntry
>();
222 for (Enumeration
<?
> e
= fTarFile
.entries(); e
.hasMoreElements();) {
223 v
.add(new TarArchiveEntry((TarEntry
) e
.nextElement()));
230 public void close() throws IOException
{
235 public InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
{
236 return fTarFile
.getInputStream(((TarArchiveEntry
) entry
).getTarEntry());
241 * Adapter for TarEntry to ArchiveEntry
243 protected class TarArchiveEntry
implements ArchiveEntry
{
244 private TarEntry fTarEntry
;
247 * Constructs a TarArchiveEntry for a TarEntry
252 public TarArchiveEntry(TarEntry tarEntry
) {
253 this.fTarEntry
= tarEntry
;
257 public String
getName() {
258 return fTarEntry
.getName();
262 * Get the corresponding TarEntry
264 * @return the corresponding TarEntry
266 public TarEntry
getTarEntry() {
271 public String
toString() {
277 * Adapter for ArchiveEntry to ArchiveEntry
279 protected class ZipAchiveEntry
implements ArchiveEntry
{
281 private ZipEntry fZipEntry
;
284 * Constructs a ZipAchiveEntry for a ZipEntry
289 public ZipAchiveEntry(ZipEntry zipEntry
) {
290 this.fZipEntry
= zipEntry
;
294 public String
getName() {
295 return fZipEntry
.getName();
299 * Get the corresponding ZipEntry
301 * @return the corresponding ZipEntry
303 public ZipEntry
getZipEntry() {
308 public String
toString() {
314 * Adapter for ZipFile to ArchiveFile
316 protected class ZipArchiveFile
implements ArchiveFile
{
318 private ZipFile fZipFile
;
321 * Constructs a ZipArchiveFile for a ZipFile
326 public ZipArchiveFile(ZipFile zipFile
) {
327 this.fZipFile
= zipFile
;
331 public Enumeration
<?
extends ArchiveEntry
> entries() {
332 Vector
<ArchiveEntry
> v
= new Vector
<ArchiveEntry
>();
333 for (Enumeration
<?
> e
= fZipFile
.entries(); e
.hasMoreElements();) {
334 v
.add(new ZipAchiveEntry((ZipEntry
) e
.nextElement()));
341 public void close() throws IOException
{
346 public InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
{
347 return fZipFile
.getInputStream(((ZipAchiveEntry
) entry
).getZipEntry());