author | Ed Swartz <ed.swartz@nokia.com> |
Mon, 14 Dec 2009 12:59:27 -0600 | |
changeset 684 | 8e7900690341 |
permissions | -rw-r--r-- |
684
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
1 |
/* |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
3 |
* All rights reserved. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
4 |
* This component and the accompanying materials are made available |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
6 |
* which accompanies this distribution, and is available |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
8 |
* |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
9 |
* Initial Contributors: |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
10 |
* Nokia Corporation - initial contribution. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
11 |
* |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
12 |
* Contributors: |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
13 |
* |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
14 |
* Description: |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
15 |
* |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
16 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
17 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
18 |
package com.nokia.cpp.internal.api.utils.core; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
19 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
20 |
import java.io.File; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
21 |
import java.io.FilenameFilter; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
22 |
import java.io.IOException; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
23 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
24 |
import org.eclipse.core.runtime.IPath; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
25 |
import org.eclipse.core.runtime.Path; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
26 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
27 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
28 |
* These utilities handle some common portability issues when dealing with |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
29 |
* (absolute) paths which may be in a format intended for another operating system. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
30 |
* It also handles shortcomings in the org.eclipse.core.runtime.Path |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
31 |
* implementation, which is not able to construct a meaningful path from |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
32 |
* a Win32 path outside of Windows. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
33 |
* <p> |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
34 |
* NOTE: please keep this in sync with the org.eclipse.cdt.debug.edc version of this class! |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
35 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
36 |
public class PathUtils { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
37 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
38 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
39 |
* Convert a variable constructed blindly for a Win32 environment into |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
40 |
* Unix-like syntax. This is typically used for PATH or lists |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
41 |
* of paths where ';' is the entry separator and '\' is the |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
42 |
* path component separator. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
43 |
* <p> |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
44 |
* NOTE: we assume that the entries in the |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
45 |
* path list are already legal Unix paths, but just with the |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
46 |
* wrong slash. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
47 |
* @param env |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
48 |
* @return converted string |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
49 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
50 |
public static String convertPathListToUnix(String env) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
51 |
if (env == null) return null; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
52 |
env = env.replaceAll(";", ":"); // entry separators |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
53 |
env = env.replaceAll("\\\\", "/"); // path separators |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
54 |
return env; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
55 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
56 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
57 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
58 |
* Convert a path constructed blindly for a Win32 environment into |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
59 |
* Unix-like syntax. <p> |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
60 |
* NOTE: we assume that the path is already a legal Unix path, |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
61 |
* but just with the wrong slash. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
62 |
* @param file |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
63 |
* @return converted string |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
64 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
65 |
public static String convertPathToUnix(String file) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
66 |
if (file == null) return null; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
67 |
// handle Windows slashes and canonicalize |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
68 |
file = file.replaceAll("\\\\", "/"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
69 |
return file; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
70 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
71 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
72 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
73 |
* Convert a path which may be in Windows or Unix format to Windows format. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
74 |
* NOTE: we assume that the path is already a legal path, |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
75 |
* but just with the wrong slash. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
76 |
* @param file |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
77 |
* @return converted string |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
78 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
79 |
public static String convertPathToWindows(String file) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
80 |
if (file == null) return null; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
81 |
file = file.replaceAll("/", "\\\\"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
82 |
return file; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
83 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
84 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
85 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
86 |
* Convert a path which may be in Windows or Unix format to Windows format. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
87 |
* NOTE: we assume that the path is already a legal path, |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
88 |
* but just with the wrong slash. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
89 |
* @param file |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
90 |
* @return converted string |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
91 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
92 |
public static String convertPathToWindows(IPath path) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
93 |
return convertPathToWindows(path.toPortableString()); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
94 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
95 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
96 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
97 |
* Convert a path which may be in the opposite slash format to the local slash format. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
98 |
* NOTE: we assume that the path is already a legal path, |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
99 |
* but just with the wrong slash. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
100 |
* @param file |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
101 |
* @return converted string |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
102 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
103 |
public static String convertPathToNative(String path) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
104 |
if (path == null) return null; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
105 |
if (HostOS.IS_UNIX) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
106 |
return path.replaceAll("\\\\", "/"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
107 |
else |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
108 |
return path.replaceAll("/", "\\\\"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
109 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
110 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
111 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
112 |
* Create an IPath from a string which may be a Win32 path. <p> |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
113 |
* <p> |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
114 |
* ("new Path(...)" won't work in Unix when using a Win32 path: the backslash |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
115 |
* separator and the device notation are completely munged.) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
116 |
* @param path |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
117 |
* @return converted string |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
118 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
119 |
public static IPath createPath(String path) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
120 |
if (path == null) return null; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
121 |
if (path.contains("\\")) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
122 |
// handle Windows slashes and canonicalize |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
123 |
path = path.replaceAll("\\\\", "/"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
124 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
125 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
126 |
// also check for device or UNC |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
127 |
int idx = path.indexOf(":"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
128 |
if (idx > 0) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
129 |
String device = path.substring(0, idx + 1); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
130 |
path = path.substring(idx + 1); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
131 |
return new Path(path).setDevice(device); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
132 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
133 |
else { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
134 |
// Cygwin or UNC path |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
135 |
if (path.startsWith("//")) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
136 |
String network; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
137 |
idx = path.indexOf("/", 2); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
138 |
if (idx > 0) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
139 |
network = path.substring(0, idx); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
140 |
path = path.substring(idx); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
141 |
} else { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
142 |
network = path; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
143 |
path = ""; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
144 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
145 |
return new Path(network, path).makeUNC(true); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
146 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
147 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
148 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
149 |
// fallthrough |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
150 |
return new Path(path); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
151 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
152 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
153 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
154 |
* Get the PATH entries from the given path environment value or the |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
155 |
* system environment. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
156 |
* @param pathValue the expected PATH/Path value, or <code>null</code> for the system value |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
157 |
* @return array of IPath, never <code>null</code> |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
158 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
159 |
public static IPath[] getPathEntries(String pathValue) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
160 |
String pathVar = null; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
161 |
if (pathValue != null) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
162 |
pathVar = pathValue; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
163 |
} else { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
164 |
if (HostOS.IS_WIN32) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
165 |
// canonical name, plus fallback below |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
166 |
pathVar = System.getenv("Path"); //$NON-NLS-1$ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
167 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
168 |
if (pathVar == null) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
169 |
pathVar = System.getenv("PATH"); //$NON-NLS-1$ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
170 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
171 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
172 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
173 |
if (pathVar == null) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
174 |
pathVar = ""; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
175 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
176 |
String pathSeparator = System.getProperty("path.separator"); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
177 |
String[] pathEntries = pathVar.split(pathSeparator); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
178 |
IPath[] paths = new IPath[pathEntries.length]; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
179 |
for (int i = 0; i < pathEntries.length; i++) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
180 |
paths[i] = new Path(pathEntries[i]); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
181 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
182 |
return paths; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
183 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
184 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
185 |
/** |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
186 |
* If the filesystem is case sensitive, locate the file on the filesystem |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
187 |
* on the given path, by ignoring case sensitivity differences. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
188 |
* This is needed on case-preserving but not case-insensitive filesystems. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
189 |
* @param path |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
190 |
* @return path pointing to existing file (possibly with different case in components) or |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
191 |
* original path if there is no match |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
192 |
*/ |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
193 |
public static IPath findExistingPathIfCaseSensitive(IPath path) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
194 |
// case is insensitive already |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
195 |
if (HostOS.IS_WIN32) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
196 |
return path; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
197 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
198 |
if (path == null || !path.isAbsolute()) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
199 |
return path; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
200 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
201 |
File pathFile = path.toFile(); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
202 |
if (pathFile.exists()) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
203 |
try { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
204 |
return new Path(pathFile.getCanonicalPath()); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
205 |
} catch (IOException e) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
206 |
// should not happen |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
207 |
return path; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
208 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
209 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
210 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
211 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
212 |
// start with the assumption that the path is mostly correct except for the |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
213 |
// last N segments. |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
214 |
IPath goodPath = Path.ROOT; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
215 |
if (path.getDevice() != null) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
216 |
goodPath = goodPath.setDevice(path.getDevice()); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
217 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
218 |
// if bad drive or no root (?!), just skip |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
219 |
if (!goodPath.toFile().exists()) |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
220 |
return path; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
221 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
222 |
for (int seg = path.segmentCount(); seg > 0; seg--) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
223 |
final IPath prefix = path.uptoSegment(seg - 1); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
224 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
225 |
if (prefix.toFile().exists()) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
226 |
goodPath = prefix; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
227 |
break; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
228 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
229 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
230 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
231 |
StringBuilder builder = new StringBuilder(); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
232 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
233 |
builder.append(goodPath.addTrailingSeparator().toOSString()); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
234 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
235 |
boolean failedLookup = false; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
236 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
237 |
for (int seg = goodPath.segmentCount(); seg < path.segmentCount(); seg++) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
238 |
final String segment = path.segment(seg); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
239 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
240 |
final String[] matches = { segment }; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
241 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
242 |
if (!failedLookup) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
243 |
File dir = new File(builder.toString()); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
244 |
if (!new File(dir, matches[0]).exists()) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
245 |
// component has wrong case; find the first one matching case-insensitively |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
246 |
String[] names = dir.list(new FilenameFilter() { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
247 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
248 |
public boolean accept(File dir, String name) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
249 |
if (name.equalsIgnoreCase(segment)) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
250 |
matches[0] = name; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
251 |
return true; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
252 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
253 |
return false; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
254 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
255 |
}); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
256 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
257 |
if (names.length == 0) { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
258 |
// no matches! the rest of the path won't match either |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
259 |
failedLookup = true; |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
260 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
261 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
262 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
263 |
builder.append(matches[0]); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
264 |
builder.append('/'); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
265 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
266 |
|
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
267 |
if (!path.hasTrailingSeparator() && builder.length() > 0 && builder.charAt(builder.length() - 1) == '/') { |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
268 |
builder.setLength(builder.length() - 1); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
269 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
270 |
return new Path(builder.toString()); |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
271 |
} |
8e7900690341
Extract path-manipulation utilities into PathUtils, to correspond with the same classes in EDC.
Ed Swartz <ed.swartz@nokia.com>
parents:
diff
changeset
|
272 |
} |