From 493aad7b481ff6cc9736c1bc0f28ea6fb80e5884 Mon Sep 17 00:00:00 2001 From: Per Cederberg Date: Thu, 21 Dec 2023 10:47:51 +0100 Subject: [PATCH] core: Refactored ClasspathUtil to use StringUtils and java.nio.Path --- .../org/rapidcontext/util/ClasspathUtil.java | 58 +++++++------------ 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/src/java/org/rapidcontext/util/ClasspathUtil.java b/src/java/org/rapidcontext/util/ClasspathUtil.java index 708a21d8..c0d530e4 100644 --- a/src/java/org/rapidcontext/util/ClasspathUtil.java +++ b/src/java/org/rapidcontext/util/ClasspathUtil.java @@ -19,8 +19,11 @@ import java.net.URL; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.jar.Manifest; +import org.apache.commons.lang3.StringUtils; + /** * A set of utility methods for working with the classpath and class loaders. * @@ -54,7 +57,7 @@ public static ClassLoader classLoader(Class cls) { * null if not found */ public static URL locate(Class cls) { - String path = cls.getName().replace('.', '/') + ".class"; + String path = cls.getName().replace('.', File.separatorChar) + ".class"; return locate(cls, path); } @@ -62,7 +65,7 @@ public static URL locate(Class cls) { * Returns the resource URL corresponding the the specified path. The * resource will be located using the class loader for the specified class. * Also, the resource path will be normalized, removing any "classpath:" - * prefix and fixing the usage of "/" chars. + * prefix and normalizing the file path. * * @param cls the base class (loader) to use * @param path the resource path @@ -71,13 +74,8 @@ public static URL locate(Class cls) { * null if not found */ public static URL locate(Class cls, String path) { - if (path.startsWith("classpath:")) { - path = path.substring(10); - } - path = path.replace("//", File.separator); - if (!path.startsWith(File.separator)) { - path = File.separator + path; - } + path = StringUtils.removeStartIgnoreCase(path, "classpath:"); + path = Path.of(path).normalize().toString(); return cls.getResource(path); } @@ -92,35 +90,21 @@ public static URL locate(Class cls, String path) { * null if not found */ public static File locateFile(Class cls) { - URL url = locate(cls); - String path; - String str; - File file; - - if (url == null) { - return null; - } - path = url.toExternalForm(); - do { - str = path.toLowerCase(); - if (str.startsWith("jar:")) { - path = path.substring(4); - if (path.contains("!/")) { - path = path.substring(0, path.indexOf("!/")); - } - } else if (str.startsWith("file:/")) { - path = path.substring(6); - if (!path.startsWith(File.separator)) { - path = File.separator + path; - } - while (path.length() > 1 && path.charAt(1) == File.separatorChar) { - path = path.substring(1); - } + URL url = locate(cls); + if (url != null) { + String path = url.toExternalForm(); + if (StringUtils.startsWithIgnoreCase(path, "jar:")) { + path = StringUtils.substringBefore(path.substring(4), "!/"); + } + if (StringUtils.startsWithIgnoreCase(path, "file:")) { + path = path.substring(5); + path = Path.of(path).normalize().toString(); + path = StringUtils.prependIfMissing(path, File.separator); } - } while (path.length() < str.length()); - path = URLDecoder.decode(path, StandardCharsets.UTF_8); - file = new File(path); - return file.exists() ? file.getAbsoluteFile() : null; + File file = new File(URLDecoder.decode(path, StandardCharsets.UTF_8)); + return file.exists() ? file.getAbsoluteFile() : null; + } + return null; } /**