Class PathTool


  • public class PathTool
    extends java.lang.Object
    Path tool contains static methods to assist in determining path-related information such as relative paths.

    This class originally got developed at Apache Anakia and later maintained in maven-utils of Apache Maven-1. Some external fixes by Apache Committers have been applied later.

    • Constructor Summary

      Constructors 
      Constructor Description
      PathTool()
      Deprecated.
      This is a utility class with only static methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.String buildRelativePath​(java.lang.String toPath, java.lang.String fromPath, char separatorChar)  
      private static java.lang.String determineRelativePath​(java.lang.String filename, java.lang.String separator)
      Determines the relative path of a filename.
      private static java.lang.String determineSeparator​(java.lang.String filename)
      Helper method to determine the file separator (forward or backward slash) used in a filename.
      static java.lang.String getRelativeFilePath​(java.lang.String oldPath, java.lang.String newPath)
      This method can calculate the relative path between two pathes on a file system.
      static java.lang.String getRelativePath​(java.lang.String basedir, java.lang.String filename)
      Determines the relative path of a filename from a base directory.
      (package private) static java.lang.String uppercaseDrive​(java.lang.String path)
      Cygwin prefers lowercase drive letters, but other parts of maven use uppercase
      • Methods inherited from class java.lang.Object

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

      • PathTool

        @Deprecated
        public PathTool()
        Deprecated.
        This is a utility class with only static methods. Don't create instances of it.
        The constructor.
    • Method Detail

      • getRelativePath

        public static java.lang.String getRelativePath​(@Nullable
                                                       java.lang.String basedir,
                                                       @Nullable
                                                       java.lang.String filename)
        Determines the relative path of a filename from a base directory. This method is useful in building relative links within pages of a web site. It provides similar functionality to Anakia's $relativePath context variable. The arguments to this method may contain either forward or backward slashes as file separators. The relative path returned is formed using forward slashes as it is expected this path is to be used as a link in a web page (again mimicking Anakia's behavior).

        This method is thread-safe.

         PathTool.getRelativePath( null, null )                                   = ""
         PathTool.getRelativePath( null, "/usr/local/java/bin" )                  = ""
         PathTool.getRelativePath( "/usr/local/", null )                          = ""
         PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin" )         = ".."
         PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin/java.sh" ) = "../.."
         PathTool.getRelativePath( "/usr/local/java/bin/java.sh", "/usr/local/" ) = ""
         
        Parameters:
        basedir - The base directory.
        filename - The filename that is relative to the base directory.
        Returns:
        The relative path of the filename from the base directory. This value is not terminated with a forward slash. A zero-length string is returned if: the filename is not relative to the base directory, basedir is null or zero-length, or filename is null or zero-length.
      • getRelativeFilePath

        public static java.lang.String getRelativeFilePath​(java.lang.String oldPath,
                                                           java.lang.String newPath)
        This method can calculate the relative path between two pathes on a file system.
         PathTool.getRelativeFilePath( null, null )                                   = ""
         PathTool.getRelativeFilePath( null, "/usr/local/java/bin" )                  = ""
         PathTool.getRelativeFilePath( "/usr/local", null )                           = ""
         PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin" )          = "java/bin"
         PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin/" )         = "java/bin"
         PathTool.getRelativeFilePath( "/usr/local/java/bin", "/usr/local/" )         = "../.."
         PathTool.getRelativeFilePath( "/usr/local/", "/usr/local/java/bin/java.sh" ) = "java/bin/java.sh"
         PathTool.getRelativeFilePath( "/usr/local/java/bin/java.sh", "/usr/local/" ) = "../../.."
         PathTool.getRelativeFilePath( "/usr/local/", "/bin" )                        = "../../bin"
         PathTool.getRelativeFilePath( "/bin", "/usr/local/" )                        = "../usr/local"
         
        Note: On Windows based system, the / character should be replaced by \ character.
        Parameters:
        oldPath - old path
        newPath - new path
        Returns:
        a relative file path from oldPath.
      • determineRelativePath

        @Nonnull
        private static java.lang.String determineRelativePath​(@Nonnull
                                                              java.lang.String filename,
                                                              @Nonnull
                                                              java.lang.String separator)
        Determines the relative path of a filename. For each separator within the filename (except the leading if present), append the "../" string to the return value.
        Parameters:
        filename - The filename to parse.
        separator - The separator used within the filename.
        Returns:
        The relative path of the filename. This value is not terminated with a forward slash. A zero-length string is returned if: the filename is zero-length.
      • determineSeparator

        private static java.lang.String determineSeparator​(java.lang.String filename)
        Helper method to determine the file separator (forward or backward slash) used in a filename. The slash that occurs more often is returned as the separator.
        Parameters:
        filename - The filename parsed to determine the file separator.
        Returns:
        The file separator used within filename. This value is either a forward or backward slash.
      • uppercaseDrive

        static java.lang.String uppercaseDrive​(@Nullable
                                               java.lang.String path)
        Cygwin prefers lowercase drive letters, but other parts of maven use uppercase
        Parameters:
        path - old path
        Returns:
        String
      • buildRelativePath

        @Nonnull
        private static java.lang.String buildRelativePath​(@Nonnull
                                                          java.lang.String toPath,
                                                          @Nonnull
                                                          java.lang.String fromPath,
                                                          char separatorChar)