This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git

commit 022deca67a48642543c181fcedde5c863f484396
Author: Mark Thomas <[email protected]>
AuthorDate: Mon Sep 7 17:29:15 2026 +0100

    Make file extension check more robust.
---
 CHANGES.md                                              |  1 +
 src/main/java/org/apache/tomcat/jakartaee/Util.java     | 16 +++++++++++-----
 src/test/java/org/apache/tomcat/jakartaee/UtilTest.java | 15 +++++++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index a258506..a595584 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,7 @@
 - Improve robustness of source and destination manipulation operations when 
migrating. (remm)
 - Improve processing of relevant version numbers in manifests. (remm)
 - Avoid shallow copy style issue when converting manifests attributes. (remm)
+- Make file extension check more robust. (markt)
 
 ## 1.0.12
 - Add Maven Wrapper Plugin to manage the Maven wrapper. (markt)
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Util.java 
b/src/main/java/org/apache/tomcat/jakartaee/Util.java
index 67a4bbf..c5b3adb 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Util.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Util.java
@@ -21,6 +21,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.charset.Charset;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Locale;
 
 /**
@@ -29,22 +31,26 @@ import java.util.Locale;
 public class Util {
 
     /**
-     * Get the string after the last dot in the given path.
+     * Get the string after the last dot in filename in the given path / name.
      * <p>
      * Returns the substring after the last '{@code .}' character in the path,
-     * converted to lower case. For paths with dots in directory names, this
-     * may not return the actual file extension.
+     * converted to lower case.
      *
      * @param path the file path or name
      * @return the extension (lowercase) or an empty string if no dot is found
      */
     public static String getExtension(String path) {
+        Path filePath = Paths.get(path).getFileName();
+        if (filePath == null) {
+            return "";
+        }
+        String fileName = filePath.toString();
         // Extract the extension
-        int lastPeriod = path.lastIndexOf('.');
+        int lastPeriod = fileName.lastIndexOf('.');
         if (lastPeriod == -1) {
             return "";
         }
-        return path.substring(lastPeriod + 1).toLowerCase(Locale.ENGLISH);
+        return fileName.substring(lastPeriod + 1).toLowerCase(Locale.ENGLISH);
     }
 
     /**
diff --git a/src/test/java/org/apache/tomcat/jakartaee/UtilTest.java 
b/src/test/java/org/apache/tomcat/jakartaee/UtilTest.java
index 0e69cb3..7b0a3f0 100644
--- a/src/test/java/org/apache/tomcat/jakartaee/UtilTest.java
+++ b/src/test/java/org/apache/tomcat/jakartaee/UtilTest.java
@@ -65,6 +65,21 @@ public class UtilTest {
         assertEquals("java", Util.getExtension("File.JaVa"));
     }
 
+    @Test
+    public void testGetExtensionPathWithEmpty() {
+        assertEquals("", Util.getExtension("/path/some.path/file"));
+    }
+
+    @Test
+    public void testGetExtensionPathWithExtension() {
+        assertEquals("txt", Util.getExtension("/path/some.path/file.txt"));
+    }
+
+    @Test
+    public void testGetExtensionPathRoot() {
+        assertEquals("", Util.getExtension("/"));
+    }
+
     @Test
     public void testCopy() throws IOException {
         byte[] source = "Hello, World!".getBytes(StandardCharsets.UTF_8);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to