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