| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 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.importtrace; |
| 14 | |
| 15 | import java.io.IOException; |
| 16 | import java.io.InputStream; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 21 | import org.eclipse.ui.internal.wizards.datatransfer.DataTransferMessages; |
| 22 | import org.eclipse.ui.internal.wizards.datatransfer.ILeveledImportStructureProvider; |
| 23 | |
| 24 | /** |
| 25 | * Leveled Structure provider for Gzip file |
| 26 | */ |
| 27 | @SuppressWarnings("restriction") |
| 28 | public class GzipLeveledStructureProvider implements ILeveledImportStructureProvider { |
| 29 | |
| 30 | private final GzipFile fFile; |
| 31 | private final GzipEntry root = new GzipEntry(); |
| 32 | private final GzipEntry fEntry; |
| 33 | |
| 34 | /** |
| 35 | * Creates a <code>GzipFileStructureProvider</code>, which will operate on |
| 36 | * the passed Gzip file. |
| 37 | * |
| 38 | * @param sourceFile |
| 39 | * the source GzipFile |
| 40 | */ |
| 41 | public GzipLeveledStructureProvider(GzipFile sourceFile) { |
| 42 | super(); |
| 43 | |
| 44 | fFile = sourceFile; |
| 45 | fEntry = sourceFile.entries().nextElement(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public List getChildren(Object element) { |
| 50 | ArrayList<Object> children = new ArrayList<>(); |
| 51 | if (element == root) { |
| 52 | children.add(fEntry); |
| 53 | } |
| 54 | return children; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public InputStream getContents(Object element) { |
| 59 | return fFile.getInputStream((GzipEntry) element); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String getFullPath(Object element) { |
| 64 | return ((GzipEntry) element).getName(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String getLabel(Object element) { |
| 69 | if (element != root && element != fEntry) { |
| 70 | throw new IllegalArgumentException(); |
| 71 | } |
| 72 | return ((GzipEntry) element).getName(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the entry that this importer uses as the root sentinel. |
| 77 | * |
| 78 | * @return GzipEntry entry |
| 79 | */ |
| 80 | @Override |
| 81 | public GzipEntry getRoot() { |
| 82 | return root; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean closeArchive() { |
| 87 | try { |
| 88 | fFile.close(); |
| 89 | } catch (IOException e) { |
| 90 | Activator.getDefault().logError(DataTransferMessages.ZipImport_couldNotClose |
| 91 | + fFile.getName(), e); |
| 92 | return false; |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean isFolder(Object element) { |
| 99 | return ((GzipEntry) element).getFileType() == GzipEntry.DIRECTORY; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void setStrip(int level) { |
| 104 | // Do nothing |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int getStrip() { |
| 109 | return 0; |
| 110 | } |
| 111 | } |