This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
commit c6d42385afc730ce1aec8493a0e7f31198a1fc4d Author: Andy Seaborne <[email protected]> AuthorDate: Fri Jun 13 08:38:49 2025 +0100 Add FileOps.pathComponents --- .../java/org/apache/jena/atlas/lib/FileOps.java | 36 ++++++++++++++------ .../org/apache/jena/atlas/lib/TestFileOps.java | 38 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/jena-base/src/main/java/org/apache/jena/atlas/lib/FileOps.java b/jena-base/src/main/java/org/apache/jena/atlas/lib/FileOps.java index 0e77118502..51209c882b 100644 --- a/jena-base/src/main/java/org/apache/jena/atlas/lib/FileOps.java +++ b/jena-base/src/main/java/org/apache/jena/atlas/lib/FileOps.java @@ -21,6 +21,9 @@ package org.apache.jena.atlas.lib ; import java.io.File ; import java.io.IOException ; import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; import org.apache.jena.atlas.AtlasException ; import org.apache.jena.atlas.io.IO ; @@ -202,6 +205,28 @@ public class FileOps { return iExt > iSlash ? filename.substring(iExt + 1).toLowerCase() : "" ; } + /** + * Return the components of a path as a list. + * A path of "/" returns an empty list. + * A path of "" returns a list of exactly "". + */ + public static List<String> pathComponents(String path) { + return pathComponents(Path.of(path)); + } + + /** + * Return the components of a path as a list. + * A path of "/" returns an empty list. + * A path of "" returns a list of exactly "". + */ + public static List<String> pathComponents(Path path) { + List<String> x = new ArrayList<>(); + for ( int i = 0; i < path.getNameCount(); i++ ) { + x.add(path.getName(i).toString()); + } + return x; + } + public static String concatPaths(String directory, String path) { return Lib.concatPaths(directory, path); } @@ -225,19 +250,10 @@ public class FileOps { /** Copy a file */ public static void copyFile(File source, File dest) { try { - Files.copy(source.toPath(), dest.toPath()); + Files.copy(source.toPath(), dest.toPath()); } catch (IOException ex) { IO.exception(ex) ; } } - - // public static String getExt(String filename) - // { - // int i = filename.lastIndexOf('.') ; - // int j = filename.lastIndexOf('/') ; - // if ( i > j ) - // return filename.substring(i+1) ; - // return null ; - // } } diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestFileOps.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestFileOps.java index 55c4470ed9..492c4cc3ca 100644 --- a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestFileOps.java +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestFileOps.java @@ -19,6 +19,10 @@ package org.apache.jena.atlas.lib; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; import org.apache.jena.atlas.lib.tuple.Tuple ; import org.junit.Test ; @@ -43,6 +47,15 @@ public class TestFileOps assertEquals(expected, result); } + static void testComponents(String path, String...expected) { + List<String> actualList = FileOps.pathComponents(path); + if ( path.equals("/") ) { + assertTrue(actualList.isEmpty() ); + return; + } + List<String> expectedList = Arrays.asList(expected); + assertEquals(expectedList, actualList); + } @Test public void split01() { testParts("/aa/bb/cc.ext", "/aa/bb", "cc", "ext") ; } @@ -88,4 +101,29 @@ public class TestFileOps @Test public void concat06() { testConcat("/xyz", "", "/xyz"); } + + @Test public void components01() + { testComponents("", ""); } + + @Test public void components02() + { testComponents("/"); } + + @Test public void components03() + { testComponents("x", "x"); } + + @Test public void components04() + { testComponents("/x", "x"); } + + @Test public void components05() + { testComponents("/x/y/z", "x", "y", "z"); } + + @Test public void components06() + { testComponents("/x/y/z/", "x", "y", "z"); } + + @Test public void components07() + { testComponents("/x/../z/", "x", "..", "z"); } + + @Test public void components08() + { testComponents("../y/z", "..", "y", "z"); } + }
