releng: Add SWTBot integration tests for import wizard
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / SafePathZipLeveledStructureProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2016 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
10 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
11
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipFile;
14
15 import org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider;
16
17 /**
18 * A Zip structure provider that makes sure to return safe paths. For example,
19 * if a Zip entry contains a ':' and is extracted on Windows, it will be changed
20 * to a '_'
21 */
22 @SuppressWarnings("restriction")
23 public class SafePathZipLeveledStructureProvider extends ZipLeveledStructureProvider {
24
25 /**
26 * Creates a provider which will operate on the passed Zip file.
27 *
28 * @param sourceFile
29 * The source file to create the provider around
30 */
31 public SafePathZipLeveledStructureProvider(ZipFile sourceFile) {
32 super(sourceFile);
33 }
34
35 @Override
36 public String getFullPath(Object element) {
37 String name = ((ZipEntry) element).getName();
38 return ArchiveUtil.toValidNamesPath(name).toOSString();
39 }
40
41 @Override
42 public String getLabel(Object element) {
43 if (element.equals(getRoot())) {
44 return ((ZipEntry) element).getName();
45 }
46 String name = ((ZipEntry) element).getName();
47 return stripPath(ArchiveUtil.toValidNamesPath(name).lastSegment());
48 }
49
50 /**
51 * Strip the leading directories from the path. Copied from
52 * {@link ZipLeveledStructureProvider}
53 */
54 private String stripPath(String path) {
55 String strippedPath = path;
56 String pathOrig = new String(strippedPath);
57 for (int i = 0; i < getStrip(); i++) {
58 int firstSep = strippedPath.indexOf('/');
59 // If the first character was a separator we must strip to the next
60 // separator as well
61 if (firstSep == 0) {
62 strippedPath = strippedPath.substring(1);
63 firstSep = strippedPath.indexOf('/');
64 }
65 // No separator was present so we're in a higher directory right
66 // now
67 if (firstSep == -1) {
68 return pathOrig;
69 }
70 strippedPath = strippedPath.substring(firstSep);
71 }
72 return strippedPath;
73 }
74 }
This page took 0.031711 seconds and 5 git commands to generate.