@@ -39,6 +39,8 @@ public boolean accept(File dir, String name) {
39
39
40
40
public static final Pattern REPEATED_URI_SEPARATOR_PATTERN = Pattern .compile ("//+" );
41
41
42
+ public static final String FILE_URI_SCHEME = "file" ;
43
+
42
44
/**
43
45
* Cleans the specified path. All files and subdirectories in the path will be deleted. (ie you'll be left with an
44
46
* empty directory).
@@ -82,7 +84,7 @@ public static void deleteRecursively(File file) {
82
84
/**
83
85
* Move files accepted by a filter from their relative path under source to the same relative path under
84
86
* destination. Creates missing destination subdirectories as needed.
85
- *
87
+ *
86
88
* @param source Must be a directory.
87
89
* @param destination Must be a directory if it exists.
88
90
* @param filter Applied to normal files, only. We recurse on directories automatically.
@@ -129,7 +131,7 @@ private static void moveRecursivelyInternal(File source, File destination, FileF
129
131
130
132
/**
131
133
* Recursive delete method that copes with .nfs files. Uses the file's parent as the trash directory.
132
- *
134
+ *
133
135
* @param file
134
136
*/
135
137
public static void deleteRecursivelyOnNFS (File file ) {
@@ -138,7 +140,7 @@ public static void deleteRecursivelyOnNFS(File file) {
138
140
139
141
/**
140
142
* Recursive delete method that copes with .nfs files.
141
- *
143
+ *
142
144
* @param trashFile Filename to move regular files to before deletion. .nfs files may be created in its parent
143
145
* directory.
144
146
* @param fileToBeDeleted File or directory at which to begin recursive deletion.
@@ -169,7 +171,7 @@ public static void deleteRecursivelyOnNFS(final File trashFile, final File fileT
169
171
170
172
/**
171
173
* Scan directory recursively to find all files
172
- *
174
+ *
173
175
* @param dir
174
176
* @return
175
177
*/
@@ -282,21 +284,36 @@ public static URI convertToURI(final String source, final boolean isDirectory) {
282
284
URI uri ;
283
285
try {
284
286
uri = new URI (source );
287
+ if (uri .getScheme () == null ) {
288
+ // Convert to a "file" URI
289
+ return convertToURI (new File (source ), isDirectory );
290
+ }
291
+ if (uri .getScheme ().equals (FILE_URI_SCHEME )) {
292
+ return convertToURI (new File (uri ), isDirectory );
293
+ }
294
+ String path = uri .getPath ();
295
+ final boolean endsWithSlash = path .charAt (path .length () - 1 ) == URI_SEPARATOR_CHAR ;
296
+ if (!isDirectory && endsWithSlash ) {
297
+ throw new IllegalArgumentException ("Non-directory URI should not end with a slash: " + uri );
298
+ }
299
+ boolean isUpdated = false ;
300
+ if (isDirectory && !endsWithSlash ) {
301
+ path = path + URI_SEPARATOR_CHAR ;
302
+ isUpdated = true ;
303
+ }
285
304
// Replace two or more consecutive slashes in the path with a single slash
286
- final String path = uri .getPath ();
287
305
if (path .contains (REPEATED_URI_SEPARATOR )) {
288
- final String canonicalizedPath = REPEATED_URI_SEPARATOR_PATTERN .matcher (path ).replaceAll (URI_SEPARATOR );
289
- uri = new URI (uri .getScheme (), uri .getUserInfo (), uri .getHost (), uri .getPort (), canonicalizedPath ,
290
- uri .getQuery (), uri .getFragment ());
306
+ path = REPEATED_URI_SEPARATOR_PATTERN .matcher (path ).replaceAll (URI_SEPARATOR );
307
+ isUpdated = true ;
308
+ }
309
+ if (isUpdated ) {
310
+ uri = new URI (uri .getScheme (), uri .getUserInfo (), uri .getHost (), uri .getPort (), path , uri .getQuery (),
311
+ uri .getFragment ());
291
312
}
292
313
} catch (final URISyntaxException e ) {
293
314
// If the URI is invalid, assume it's a file path
294
315
return convertToURI (new File (source ), isDirectory );
295
316
}
296
- if (uri .getScheme () == null ) {
297
- // Convert to a "file" URI
298
- return convertToURI (new File (source ), isDirectory );
299
- }
300
317
return uri ;
301
318
}
302
319
@@ -314,17 +331,11 @@ public static URI convertToURI(final File file, final boolean isDirectory) {
314
331
if (File .separatorChar != URI_SEPARATOR_CHAR ) {
315
332
absPath = absPath .replace (File .separatorChar , URI_SEPARATOR_CHAR );
316
333
}
317
- if (absPath .charAt (0 ) != URI_SEPARATOR_CHAR ) {
318
- absPath = URI_SEPARATOR_CHAR + absPath ;
319
- }
320
334
if (isDirectory && absPath .charAt (absPath .length () - 1 ) != URI_SEPARATOR_CHAR ) {
321
335
absPath = absPath + URI_SEPARATOR_CHAR ;
322
336
}
323
- if (absPath .startsWith (REPEATED_URI_SEPARATOR )) {
324
- absPath = REPEATED_URI_SEPARATOR + absPath ;
325
- }
326
337
try {
327
- return new URI ("file" , null , absPath , null );
338
+ return new URI (FILE_URI_SCHEME , null , absPath , null );
328
339
} catch (final URISyntaxException e ) {
329
340
throw new IllegalStateException ("Failed to convert file to URI: " + file , e );
330
341
}
0 commit comments