Fixing FileID.getFileName when dealing with opaque URIs

This commit is contained in:
Joakim Erdfelt 2022-12-21 14:51:28 -06:00
parent ff48442fa0
commit 0485fdf2f3
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 16 additions and 1 deletions

View File

@ -55,7 +55,21 @@ public class FileID
{
if (uri == null)
return "";
String path = uri.getPath();
if (uri.isOpaque())
{
return getFileName(uri.getSchemeSpecificPart());
}
return getFileName(uri.getPath());
}
/**
* Get the last segment of a String path returning it as the filename
*
* @param path the string path to look for the filename
* @return The last segment of the path
*/
public static String getFileName(String path)
{
if (path == null || "/".equals(path))
return "";
int idx = path.lastIndexOf('/');

View File

@ -106,6 +106,7 @@ public class FileIDTest
Arguments.of(URI.create("file:zed/"), ""),
Arguments.of(URI.create("file:///path/to/test.txt"), "test.txt"),
Arguments.of(URI.create("file:///path/to/dir/"), ""),
Arguments.of(URI.create("jar:file:///home/user/libs/jetty-server-12.jar!/org/eclipse/jetty/server/jetty-dir.css"), "jetty-dir.css"),
Arguments.of(URI.create("http://eclipse.org/jetty/"), ""),
Arguments.of(URI.create("http://eclipse.org/jetty/index.html"), "index.html"),
Arguments.of(URI.create("http://eclipse.org/jetty/docs.html?query=val#anchor"), "docs.html")