|
@@ -2,50 +2,39 @@ |
2 |
2 |
|
|
3 |
3 |
|
import com.esotericsoftware.scar.*; |
4 |
4 |
|
|
5 |
|
- |
public class FileSupport |
6 |
|
- |
{ |
|
5 |
+ |
public class FileSupport { |
7 |
6 |
|
public static final String WINDOWS_UNC_PATH_PREFIX = "\\\\"; |
8 |
7 |
|
|
9 |
|
- |
public static String getWindowsDriveIndicator( String path ) |
10 |
|
- |
{ |
|
8 |
+ |
public static String getWindowsDriveIndicator( String path ) { |
11 |
9 |
|
return ((path.length() > 1) && (path.charAt( 1 ) == ':')) ? path.substring( 0, 2 ).toUpperCase() : ""; |
12 |
10 |
|
} |
13 |
11 |
|
|
14 |
|
- |
public static String removeFromFront( String pToRemove, String path ) |
15 |
|
- |
{ |
|
12 |
+ |
public static String removeFromFront( String pToRemove, String path ) { |
16 |
13 |
|
return (pToRemove.length() == 0) ? path : path.substring( pToRemove.length() ); |
17 |
14 |
|
} |
18 |
15 |
|
|
19 |
|
- |
public static String normalizePath( IFileSystem pFileSystem, String path ) |
20 |
|
- |
{ |
|
16 |
+ |
public static String normalizePath( IFileSystem pFileSystem, String path ) { |
21 |
17 |
|
path = path.trim(); |
22 |
18 |
|
String zPrefix = ""; |
23 |
|
- |
if ( pFileSystem.isWindows() ) |
24 |
|
- |
{ |
25 |
|
- |
if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) |
26 |
|
- |
{ |
|
19 |
+ |
if ( pFileSystem.isWindows() ) { |
|
20 |
+ |
if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) { |
27 |
21 |
|
path = removeFromFront( zPrefix = WINDOWS_UNC_PATH_PREFIX, path ).trim(); |
28 |
|
- |
} |
29 |
|
- |
else |
30 |
|
- |
{ |
|
22 |
+ |
} else { |
31 |
23 |
|
path = removeFromFront( zPrefix = getWindowsDriveIndicator( path ), path ).trim(); // Handle Drive Letter |
32 |
24 |
|
} |
33 |
25 |
|
} |
34 |
26 |
|
path = path.trim(); |
35 |
|
- |
if ( '/' != pFileSystem.separatorChar() ) |
36 |
|
- |
{ |
|
27 |
+ |
if ( '/' != pFileSystem.separatorChar() ) { |
37 |
28 |
|
path = path.replace( '/', pFileSystem.separatorChar() ); |
38 |
29 |
|
} |
39 |
30 |
|
int at = path.indexOf( pFileSystem.separatorChar() ); |
40 |
|
- |
if ( at != -1 ) |
41 |
|
- |
{ |
|
31 |
+ |
if ( at != -1 ) { |
42 |
32 |
|
String zFileSeparator = "" + pFileSystem.separatorChar(); |
43 |
33 |
|
|
44 |
34 |
|
// remove white space around file Parts |
45 |
35 |
|
StringBuilder sb = new StringBuilder( path.length() ); |
46 |
36 |
|
int from = 0; |
47 |
|
- |
do |
48 |
|
- |
{ |
|
37 |
+ |
do { |
49 |
38 |
|
sb.append( path.substring( from, at ).trim() ).append( pFileSystem.separatorChar() ); |
50 |
39 |
|
from = at + 1; |
51 |
40 |
|
} |
|
@@ -57,74 +46,60 @@ |
57 |
46 |
|
path = Utils.replace( path, zFileSeparator + zFileSeparator, zFileSeparator ); // "//" |
58 |
47 |
|
|
59 |
48 |
|
// Remove ending "/." |
60 |
|
- |
while ( path.endsWith( zFileSeparator + "." ) ) |
61 |
|
- |
{ |
|
49 |
+ |
while ( path.endsWith( zFileSeparator + "." ) ) { |
62 |
50 |
|
path = path.substring( 0, path.length() - 2 ); |
63 |
51 |
|
} |
64 |
52 |
|
// Remove leading "./" |
65 |
|
- |
while ( path.startsWith( "." + zFileSeparator ) ) |
66 |
|
- |
{ |
|
53 |
+ |
while ( path.startsWith( "." + zFileSeparator ) ) { |
67 |
54 |
|
path = path.substring( 2 ); |
68 |
55 |
|
} |
69 |
56 |
|
|
70 |
57 |
|
// Process Funky .. |
71 |
58 |
|
String zPrefixDotDotSlash = ""; |
72 |
59 |
|
String zDotDotSlash = ".." + zFileSeparator; |
73 |
|
- |
while ( path.startsWith( zDotDotSlash ) ) |
74 |
|
- |
{ |
|
60 |
+ |
while ( path.startsWith( zDotDotSlash ) ) { |
75 |
61 |
|
zPrefixDotDotSlash += zDotDotSlash; |
76 |
62 |
|
path = path.substring( 3 ); |
77 |
63 |
|
} |
78 |
64 |
|
String zUpLevel = zFileSeparator + ".."; |
79 |
|
- |
if ( path.endsWith( zUpLevel ) ) |
80 |
|
- |
{ |
|
65 |
+ |
if ( path.endsWith( zUpLevel ) ) { |
81 |
66 |
|
path += zFileSeparator; |
82 |
67 |
|
} |
83 |
68 |
|
zUpLevel += zFileSeparator; |
84 |
|
- |
for ( at = path.indexOf( zUpLevel ); at > 0; at = path.indexOf( zUpLevel ) ) |
85 |
|
- |
{ |
|
69 |
+ |
for ( at = path.indexOf( zUpLevel ); at > 0; at = path.indexOf( zUpLevel ) ) { |
86 |
70 |
|
path = removeDotDot( path, at, pFileSystem.separatorChar() ); |
87 |
71 |
|
} |
88 |
72 |
|
path = zPrefixDotDotSlash + path; |
89 |
|
- |
if ( (path.length() > 1) && path.endsWith( zFileSeparator ) ) |
90 |
|
- |
{ |
|
73 |
+ |
if ( (path.length() > 1) && path.endsWith( zFileSeparator ) ) { |
91 |
74 |
|
path = path.substring( 0, path.length() - 1 ); |
92 |
75 |
|
} |
93 |
76 |
|
} |
94 |
|
- |
if ( path.length() == 0 ) |
95 |
|
- |
{ |
|
77 |
+ |
if ( path.length() == 0 ) { |
96 |
78 |
|
path = "."; |
97 |
79 |
|
} |
98 |
80 |
|
path = zPrefix + path; |
99 |
81 |
|
return path; |
100 |
82 |
|
} |
101 |
83 |
|
|
102 |
|
- |
private static String removeDotDot( String path, int pAt, char pSeparatorChar ) |
103 |
|
- |
{ |
|
84 |
+ |
private static String removeDotDot( String path, int pAt, char pSeparatorChar ) { |
104 |
85 |
|
int zEnd = pAt + 4; |
105 |
|
- |
while ( path.charAt( --pAt ) != pSeparatorChar ) |
106 |
|
- |
{ |
107 |
|
- |
if ( pAt == 0 ) |
108 |
|
- |
{ |
|
86 |
+ |
while ( path.charAt( --pAt ) != pSeparatorChar ) { |
|
87 |
+ |
if ( pAt == 0 ) { |
109 |
88 |
|
return path.substring( zEnd ); |
110 |
89 |
|
} |
111 |
90 |
|
} |
112 |
91 |
|
return path.substring( 0, pAt + 1 ) + path.substring( zEnd ); |
113 |
92 |
|
} |
114 |
93 |
|
|
115 |
|
- |
public static boolean isAbsoluteNormalizedPath( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelativeForWindowsDriveLetter, String path ) |
116 |
|
- |
{ |
117 |
|
- |
if ( pFileSystem.isWindows() ) |
118 |
|
- |
{ |
119 |
|
- |
if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) |
120 |
|
- |
{ |
|
94 |
+ |
public static boolean isAbsoluteNormalizedPath( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelativeForWindowsDriveLetter, String path ) { |
|
95 |
+ |
if ( pFileSystem.isWindows() ) { |
|
96 |
+ |
if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) { |
121 |
97 |
|
return true; |
122 |
98 |
|
} |
123 |
99 |
|
String zDriveIndicator = getWindowsDriveIndicator( path ); |
124 |
100 |
|
if ( zDriveIndicator.length() != 0 ) // Handle Drive Letter |
125 |
101 |
|
{ |
126 |
|
- |
if ( !pCanonicalParentDirIfPathRelativeForWindowsDriveLetter.startsWith( zDriveIndicator ) || !pFileSystem.canonicalCurrentPath().startsWith( zDriveIndicator ) ) |
127 |
|
- |
{ |
|
102 |
+ |
if ( !pCanonicalParentDirIfPathRelativeForWindowsDriveLetter.startsWith( zDriveIndicator ) || !pFileSystem.canonicalCurrentPath().startsWith( zDriveIndicator ) ) { |
128 |
103 |
|
return true; // Has Drive Letter and it is NOT the same both the 'CanonicalDirForWindowDriveLetterSourceRelativeness' && pFileSystem.canonicalCurrentPath() |
129 |
104 |
|
} |
130 |
105 |
|
path = removeFromFront( zDriveIndicator, path ); |
|
@@ -133,19 +108,15 @@ |
133 |
108 |
|
return (path.length() > 0) && (path.charAt( 0 ) == pFileSystem.separatorChar()); |
134 |
109 |
|
} |
135 |
110 |
|
|
136 |
|
- |
public static String canonicalizeNormalizedPath( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String path ) |
137 |
|
- |
{ |
138 |
|
- |
if ( !pFileSystem.isWindows() ) |
139 |
|
- |
{ |
140 |
|
- |
if ( !isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, path ) ) |
141 |
|
- |
{ |
|
111 |
+ |
public static String canonicalizeNormalizedPath( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String path ) { |
|
112 |
+ |
if ( !pFileSystem.isWindows() ) { |
|
113 |
+ |
if ( !isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, path ) ) { |
142 |
114 |
|
path = normalizePath( pFileSystem, pCanonicalParentDirIfPathRelative + pFileSystem.separatorChar() + path ); |
143 |
115 |
|
} |
144 |
116 |
|
return canonicalizeAbsoluteNormalizedPath( pFileSystem, path ); |
145 |
117 |
|
} |
146 |
118 |
|
// Windows! |
147 |
|
- |
if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) |
148 |
|
- |
{ |
|
119 |
+ |
if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) { |
149 |
120 |
|
return canonicalizeAbsoluteNormalizedPath( pFileSystem, path ); |
150 |
121 |
|
} |
151 |
122 |
|
String zDriveIndicator = getWindowsDriveIndicator( path ); |
|
@@ -164,24 +135,19 @@ |
164 |
135 |
|
{ |
165 |
136 |
|
path = "."; |
166 |
137 |
|
} |
167 |
|
- |
if ( (path.charAt( 0 ) != pFileSystem.separatorChar()) && !path.startsWith( "." ) ) |
168 |
|
- |
{ |
|
138 |
+ |
if ( (path.charAt( 0 ) != pFileSystem.separatorChar()) && !path.startsWith( "." ) ) { |
169 |
139 |
|
path = "." + pFileSystem.separatorChar() + path; |
170 |
140 |
|
} |
171 |
141 |
|
return canonicalizeAbsoluteNormalizedPath( pFileSystem, zDriveIndicator + path ); |
172 |
142 |
|
} |
173 |
143 |
|
|
174 |
|
- |
private static String canonicalizeAbsoluteNormalizedPath( IFileSystem pFileSystem, String path ) |
175 |
|
- |
{ |
|
144 |
+ |
private static String canonicalizeAbsoluteNormalizedPath( IFileSystem pFileSystem, String path ) { |
176 |
145 |
|
String origPath = path; |
177 |
|
- |
if ( !pFileSystem.exists( origPath ) ) |
178 |
|
- |
{ |
|
146 |
+ |
if ( !pFileSystem.exists( origPath ) ) { |
179 |
147 |
|
String zEnd = ""; |
180 |
|
- |
for ( int at; -1 != (at = path.lastIndexOf( pFileSystem.separatorChar() )); ) |
181 |
|
- |
{ |
|
148 |
+ |
for ( int at; -1 != (at = path.lastIndexOf( pFileSystem.separatorChar() )); ) { |
182 |
149 |
|
zEnd = path.substring( at ) + zEnd; |
183 |
|
- |
if ( pFileSystem.exists( path = path.substring( 0, at ) ) ) |
184 |
|
- |
{ |
|
150 |
+ |
if ( pFileSystem.exists( path = path.substring( 0, at ) ) ) { |
185 |
151 |
|
return pFileSystem.canonicalizeNormalizedExisting( path ) + zEnd; |
186 |
152 |
|
} |
187 |
153 |
|
} |