Class PathUtil


  • public final class PathUtil
    extends java.lang.Object
    Utility methods for manipulation of UNIX-like paths. TODO(tbreisacher): We can probably nuke this entire class and replace it with https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String collapseDots​(java.lang.String path)
      Removes all ../ and ./ entries from within the given path.
      static java.lang.String makeAbsolute​(java.lang.String path)
      Converts the given path into an absolute one.
      static java.lang.String makeAbsolute​(java.lang.String path, java.lang.String rootPath)
      Converts the given path into an absolute one.
      static java.lang.String makeRelative​(java.lang.String basePath, java.lang.String targetPath)
      Returns targetPath relative to basePath.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • collapseDots

        public static java.lang.String collapseDots​(java.lang.String path)
        Removes all ../ and ./ entries from within the given path. If there are extra ..s that move beyond the first directory given, they are removed. Examples: "a/b/../c" results in "a/c" "./foo/./../bar" results in "bar" "a/.." results in "" "a/../../foo" results in "foo"
        Parameters:
        path - The path to remove dots from.
        Returns:
        The path with all dots collapsed.
      • makeAbsolute

        public static java.lang.String makeAbsolute​(java.lang.String path)
        Converts the given path into an absolute one. This prepends the current working directory and removes all .'s from the path. If an absolute path is given, it will not be prefixed.

        Unlike File.getAbsolutePath(), this function does remove .'s from the path and unlike File.getCanonicalPath(), this function does not resolve symlinks and does not use filesystem calls.

        Parameters:
        path - The path to make absolute.
        Returns:
        The path made absolute.
      • makeAbsolute

        public static java.lang.String makeAbsolute​(java.lang.String path,
                                                    java.lang.String rootPath)
        Converts the given path into an absolute one. This prepends the given rootPath and removes all .'s from the path. If an absolute path is given, it will not be prefixed.

        Unlike File.getAbsolutePath(), this function does remove .'s from the path and unlike File.getCanonicalPath(), this function does not resolve symlinks and does not use filesystem calls.

        Parameters:
        rootPath - The path to prefix to path if path is not already absolute.
        path - The path to make absolute.
        Returns:
        The path made absolute.
      • makeRelative

        public static java.lang.String makeRelative​(java.lang.String basePath,
                                                    java.lang.String targetPath)
        Returns targetPath relative to basePath.

        basePath and targetPath must either both be relative, or both be absolute paths.

        This function is different from makeRelative in that it is able to add in ../ components and collapse existing ones as well.

        Examples: base="some/relative/path" target="some/relative/path/foo" return="foo" base="some/relative/path" target="some/relative" return=".." base="some/relative/path" target="foo/bar" return="../../../foo/bar" base="/some/abs/path" target="/foo/bar" return="../../../foo/bar"
        Parameters:
        basePath - The path to make targetPath relative to.
        targetPath - The path to make relative.
        Returns:
        A path relative to targetPath. The returned value will never start with a slash.