< prev index next >

src/java.base/share/classes/java/nio/file/Paths.java

Print this page
rev 51675 : 8207690: Parsing API for classpath and similar path strings

@@ -23,17 +23,26 @@
  * questions.
  */
 
 package java.nio.file;
 
-import java.nio.file.spi.FileSystemProvider;
+import jdk.internal.util.PathParser;
+
+import java.io.File;
 import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * This class consists exclusively of static methods that return a {@link Path}
  * by converting a path string or {@link URI}.
  *
+ * <p>Unless otherwise noted, passing a {@code null} argument to a method
+ * in this class will cause a {@link NullPointerException} to be thrown.
+ *
  * @apiNote
  * It is recommended to obtain a {@code Path} via the {@code Path.of} methods
  * instead of via the {@code get} methods defined in this class as this class
  * may be deprecated in a future release.
  *

@@ -94,6 +103,57 @@
      * @see Path#of(URI)
      */
     public static Path get(URI uri) {
         return Path.of(uri);
     }
+
+    /**
+     * Returns a list of path strings parsed from a string with empty paths removed.
+     * The {@link File#pathSeparator} is used to split the string and
+     * empty strings are removed.  A list is created of the remaining strings.
+     * <p>
+     * The {@code pathSeparator} character can be included in a path
+     * on operating systems that support quoting segments of the string.
+     *
+     * @implNote On Windows, quoting of path segments is supported.
+     * Each {@link File#pathSeparator} between pairs of quotation marks (@code 'U+0022')
+     * is considered an ordinary character and the quotes are omitted from the path.
+     * An unmatched double-quote is matched by the end of the string.
+     *
+     * @param path a {@code non-null} string containing paths separated by
+     *             {@link File#pathSeparator}.
+     * @return a {@code non-null} immutable list of strings for each non-empty path
+     */
+    public static List<String> pathToStrings(String path) {
+        Objects.requireNonNull(path, "path");
+        return List.of(PathParser.parsePath(path, null));
+    }
+
+    /**
+     * Returns a list of Paths parsed from a string with empty paths removed.
+     * The {@link File#pathSeparator} is used to split the string and
+     * empty strings are removed.  A list is created of the remaining strings after
+     * mapping each string using {@link Path#of Path.of} using the
+     * {@link FileSystems#getDefault default} {@link FileSystem}.
+     * <p>
+     * The {@code pathSeparator} character can be included in a path
+     * on operating systems that support quoting segments of the string.
+     *
+     * @implNote On Windows, quoting of path segments is supported.
+     * Each {@link File#pathSeparator} between pairs of quotation marks (@code 'U+0022')
+     * is considered an ordinary character and the quotes are omitted from the path.
+     * An unmatched double-quote is matched by the end of the string.
+     *
+     * @param path a {@code non-null} string containing paths separated by
+     *             {@link File#pathSeparator}.
+     * @return a {@code non-null} immutable list of Paths for each non-empty path
+     *
+     * @throws  InvalidPathException
+     *          if each path string cannot be converted to a {@code Path}
+     */
+    public static List<Path> pathToPaths(String path) {
+        Objects.requireNonNull(path, "path");
+        return Arrays.stream(PathParser.parsePath(path, null))
+                .map(s -> Path.of(s))
+                .collect(Collectors.toList());
+    }
 }
< prev index next >