| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2010 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.internal.p2.update; |
| 15 |
|
| 16 |
import java.io.File; |
| 17 |
import java.net.URL; |
| 18 |
import org.eclipse.core.runtime.*; |
| 19 |
|
| 20 |
/** |
| 21 |
* Note the methods on this class have inconsistent and unintuitive behaviour. However |
| 22 |
* they are unchanged for backwards compatibility. Clients should really use |
| 23 |
* {@link URIUtil} instead. |
| 24 |
* @since 1.0 |
| 25 |
*/ |
| 26 |
public class PathUtil { |
| 27 |
public static boolean isWindows = System.getProperty("os.name").startsWith("Win"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 28 |
|
| 29 |
private static final String FILE_PROTOCOL = "file:"; //$NON-NLS-1$ |
| 30 |
|
| 31 |
public static String makeRelative(String urlString, URL rootURL) { |
| 32 |
// we only traffic in file: URLs |
| 33 |
int index = urlString.indexOf(FILE_PROTOCOL); |
| 34 |
if (index == -1) |
| 35 |
return urlString; |
| 36 |
index = index + 5; |
| 37 |
|
| 38 |
// ensure we have an absolute path to start with |
| 39 |
boolean done = false; |
| 40 |
URL url = null; |
| 41 |
String file = urlString; |
| 42 |
while (!done) { |
| 43 |
try { |
| 44 |
url = new URL(file); |
| 45 |
file = url.getFile(); |
| 46 |
} catch (java.net.MalformedURLException e) { |
| 47 |
done = true; |
| 48 |
} |
| 49 |
} |
| 50 |
if (url == null || !new File(url.getFile()).isAbsolute()) |
| 51 |
return urlString; |
| 52 |
|
| 53 |
String rootString = rootURL.toExternalForm(); |
| 54 |
return urlString.substring(0, index) + makeRelative(urlString.substring(index), rootString.substring(rootString.indexOf(FILE_PROTOCOL) + 5)); |
| 55 |
} |
| 56 |
|
| 57 |
public static String makeRelative(String original, String rootPath) { |
| 58 |
IPath path = IPath.fromOSString(original); |
| 59 |
// ensure we have an absolute path to start with |
| 60 |
if (!path.isAbsolute()) |
| 61 |
return original; |
| 62 |
|
| 63 |
//Returns the original string if no relativization has been done |
| 64 |
String result = makeRelative(path, IPath.fromOSString(rootPath)); |
| 65 |
return path.toOSString().equals(result) ? original : result; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Make the given path relative to the specified root, if applicable. If not, then |
| 70 |
* return the path as-is. |
| 71 |
*/ |
| 72 |
private static String makeRelative(IPath toRel, IPath base) { |
| 73 |
//can't make relative if devices are not equal |
| 74 |
final String device = toRel.getDevice(); |
| 75 |
if (device != base.getDevice() && (device == null || !device.equalsIgnoreCase(base.getDevice()))) |
| 76 |
return toRel.toOSString(); |
| 77 |
int i = base.matchingFirstSegments(toRel); |
| 78 |
if (i == 0) { |
| 79 |
return toRel.toOSString(); |
| 80 |
} |
| 81 |
String result = ""; //$NON-NLS-1$ |
| 82 |
for (int j = 0; j < base.segmentCount() - i; j++) { |
| 83 |
result += ".." + IPath.SEPARATOR; //$NON-NLS-1$ |
| 84 |
} |
| 85 |
if (i == toRel.segmentCount()) |
| 86 |
return "."; //$NON-NLS-1$ |
| 87 |
//TODO This will return mixed path with some / and some \ on windows!! |
| 88 |
result += toRel.setDevice(null).removeFirstSegments(i).toOSString(); |
| 89 |
return result; |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* Make the given path absolute to the specified root, if applicable. If not, then |
| 94 |
* return the path as-is. |
| 95 |
* |
| 96 |
* Method similar to one from SimpleConfigurationManipulatorImpl. |
| 97 |
*/ |
| 98 |
public static String makeAbsolute(String original, String rootPath) { |
| 99 |
IPath path = IPath.fromOSString(original); |
| 100 |
// ensure we have a relative path to start with |
| 101 |
if (path.isAbsolute()) |
| 102 |
return original; |
| 103 |
IPath root = IPath.fromOSString(rootPath); |
| 104 |
return root.addTrailingSeparator().append(original.replace(':', '}')).toOSString().replace('}', ':'); |
| 105 |
} |
| 106 |
|
| 107 |
public static String makeAbsolute(String urlString, URL rootURL) { |
| 108 |
// we only traffic in file: URLs |
| 109 |
int index = urlString.indexOf(FILE_PROTOCOL); |
| 110 |
if (index == -1) |
| 111 |
return urlString; |
| 112 |
index = index + 5; |
| 113 |
|
| 114 |
// ensure we have a relative path to start with |
| 115 |
boolean done = false; |
| 116 |
URL url = null; |
| 117 |
String file = urlString; |
| 118 |
while (!done) { |
| 119 |
try { |
| 120 |
url = new URL(file); |
| 121 |
file = url.getFile(); |
| 122 |
} catch (java.net.MalformedURLException e) { |
| 123 |
done = true; |
| 124 |
} |
| 125 |
} |
| 126 |
if (url == null || new File(url.getFile()).isAbsolute()) |
| 127 |
return urlString; |
| 128 |
|
| 129 |
return urlString.substring(0, index - 5) + makeAbsolute(urlString.substring(index), rootURL.toExternalForm()); |
| 130 |
} |
| 131 |
} |