com.ibm.wsspi.kernel.service.utils

Class PathUtils

  • java.lang.Object
    • com.ibm.wsspi.kernel.service.utils.PathUtils


  • public class PathUtils
    extends java.lang.Object
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class and Description
      static class  PathUtils.PathComparator
      A Path Comparator that will sort paths in order.
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static java.util.Comparator<java.lang.String> PATH_COMPARATOR 
    • Constructor Summary

      Constructors 
      Constructor and Description
      PathUtils() 
    • Method Summary

      Methods 
      Modifier and Type Method and Description
      static java.lang.String checkAndNormalizeRootPath(java.lang.String path)
      This method will do the following to the path:
      static boolean checkCase(java.io.File file, java.lang.String pathToTest)
      This method will test that the case of the supplied existing File matches the case in the pathToTest.
      static boolean containsSymbol(java.lang.String s) 
      static java.util.Set<java.io.File> fixPathFiles(java.util.Collection<java.io.File> files)
      Returns a set of files that have the correct case.
      static java.lang.String fixPathString(java.io.File absPath) 
      static java.lang.String fixPathString(java.lang.String absPath) 
      static java.lang.String getChildUnder(java.lang.String path, java.lang.String parentPath)
      Given a path, and a super path, return the immediate child of the super path in the path.
      static java.lang.String getFirstPathComponent(java.lang.String path)
      Obtains the 1st component of a path
      static java.util.Set<java.io.File> getFixedPathFiles(java.util.Collection<java.lang.String> paths)
      Returns a set of files that have the correct case.
      static java.lang.String getName(java.lang.String pathAndName)
      Gets the name from a path component
      static java.lang.String getParent(java.lang.String path)
      Gets the parent section of a given path.
      static java.lang.String getSymbol(java.lang.String s) 
      static boolean isNormalizedPathAbsolute(java.lang.String nPath) 
      static boolean isSymbol(java.lang.String s) 
      static boolean isUnixStylePathAbsolute(java.lang.String unixStylePath)
      This method ensures that the supplied path is not attempting to traverse up the directory structure from its current location by using the ..
      static java.lang.String normalize(java.lang.String path)
      Strip "." and ".." elements from path.
      static java.lang.String normalizeDescendentPath(java.lang.String path)
      Normalizes the path parameter; enforces descendant relative path.
      static java.lang.String normalizeRelative(java.lang.String relativePath)
      Normalize the path; enforces a relative path.
      static java.lang.String normalizeUnixStylePath(java.lang.String path)
      Normalizes the provided path, resolving as many "/.." as possible, but leaving unresolved ones in the path.
      static boolean pathIsAbsolute(java.lang.String normalizedPath)
      Test if a path is absolute.
      static java.lang.String replaceRestrictedCharactersInFileName(java.lang.String name)
      Replace any characters from a string which would be restricted when used as a file name.
      static java.lang.String slashify(java.lang.String filePath)
      Convert \ separators into /'s
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PATH_COMPARATOR

        public static final java.util.Comparator<java.lang.String> PATH_COMPARATOR
    • Constructor Detail

      • PathUtils

        public PathUtils()
    • Method Detail

      • slashify

        public static java.lang.String slashify(java.lang.String filePath)
        Convert \ separators into /'s
        Parameters:
        filePath - path to process, must not be null.
        Returns:
        filePath with \'s converted to /.
      • normalizeDescendentPath

        public static java.lang.String normalizeDescendentPath(java.lang.String path)
        Normalizes the path parameter; enforces descendant relative path.
        Parameters:
        path -
        Returns:
        normalized path
        Throws:
        MalformedLocationException - if normalized path begins with "..", as that would navigate to the parent
        See Also:
        normalizeRelative(String)
      • normalizeRelative

        public static java.lang.String normalizeRelative(java.lang.String relativePath)
        Normalize the path; enforces a relative path.
        Parameters:
        relativePath -
        Returns:
        Throws:
        MalformedLocationException - if path is not relative (is an absolute path, a file URI, or starts with a symbol)
      • pathIsAbsolute

        public static boolean pathIsAbsolute(java.lang.String normalizedPath)
        Test if a path is absolute.

        Returns true if the path is an absolute one.
        Eg. c:/wibble/fish, /wibble/fish, ${wibble}/fish

        Parameters:
        normalizedPath -
        Returns:
        true if absolute, false otherwise.
      • normalize

        public static java.lang.String normalize(java.lang.String path)
        Strip "." and ".." elements from path.
        Parameters:
        path -
        Returns:
      • isSymbol

        public static boolean isSymbol(java.lang.String s)
      • containsSymbol

        public static boolean containsSymbol(java.lang.String s)
      • getSymbol

        public static java.lang.String getSymbol(java.lang.String s)
      • getParent

        public static java.lang.String getParent(java.lang.String path)
        Gets the parent section of a given path.

        Eg, for /wibble/fish/monkey, will return /wibble/fish
        for /wibble, will return /
        for / will return null

        Parameters:
        path - the path to be interpreted.
        Returns:
        parent path, or null if there is no parent path possible.
      • getName

        public static java.lang.String getName(java.lang.String pathAndName)
        Gets the name from a path component

        Eg.
        for /wibble/fish returns fish
        for /wibble returns wibble
        for / returns /
        for sheep returns sheep

        Names ending in / are illegal, and will fail. Callee's Responsibility.

        Parameters:
        pathAndName -
        Returns:
        name part of path and name.
      • getFirstPathComponent

        public static java.lang.String getFirstPathComponent(java.lang.String path)
        Obtains the 1st component of a path

        Eg, for /wibble/fish will return wibble
        for /we/like/pie will return we
        for /fish will return fish
        for sheep will return sheep
        for sheep/like/pies will return sheep
        for sheep/shearing will return sheep
        for / will return ""

        Parameters:
        path - path to parse
        Returns:
        1st path component.
      • getChildUnder

        public static java.lang.String getChildUnder(java.lang.String path,
                                     java.lang.String parentPath)
        Given a path, and a super path, return the immediate child of the super path in the path.

        Eg, for /we/like/pies and /we/like will return pies
        for /fish/are/friends/not/food and /fish/are/friends will return not

        Will explode in fiery ball of death if parentPath is not a parent of path, you have been warned.

        Parameters:
        path - full path
        parentPath - path segment that is a super path within path
        Returns:
        1st child name.
      • isUnixStylePathAbsolute

        public static boolean isUnixStylePathAbsolute(java.lang.String unixStylePath)
        This method ensures that the supplied path is not attempting to traverse up the directory structure from its current location by using the .. notation at the start of the path.
        Parameters:
        unixStylePath - The path to check once normalized
        Returns:
        true if the path isn't trying to go up from the current location
      • isNormalizedPathAbsolute

        public static boolean isNormalizedPathAbsolute(java.lang.String nPath)
      • checkAndNormalizeRootPath

        public static java.lang.String checkAndNormalizeRootPath(java.lang.String path)
                                                          throws java.lang.IllegalArgumentException

        This method will do the following to the path:

        1. Check that it is a valid path by calling isUnixStylePathAbsolute(String)
        2. Add a "/" to the front of the path if it is not present
        3. Remove a "/" from the end of the path if it is present
        4. Normalize the path via a call to normalizeUnixStylePath(String)
        5. Check that the path is not equals to "/" or ""
        Parameters:
        path - The path to normalize
        Returns:
        The normalized path for the root
        Throws:
        java.lang.IllegalArgumentException - If the path is trying to leave the root element via .. notation or is "/" or ""
        java.lang.NullPointerException - If the path is null
      • normalizeUnixStylePath

        public static java.lang.String normalizeUnixStylePath(java.lang.String path)
        Normalizes the provided path, resolving as many "/.." as possible, but leaving unresolved ones in the path. Leaves the leading '/' in place but removes any other trailing '/'.
        Parameters:
        path - a unix-style path using / as the element separator
        Returns:
        the normalized path
      • checkCase

        public static boolean checkCase(java.io.File file,
                        java.lang.String pathToTest)
        This method will test that the case of the supplied existing File matches the case in the pathToTest. It assumes that the final part of the file's path will be equal to the whole of the path to test. The path to test should be a unix style path with "/" as the separator character, regardless of the operating system. If the file is a directory then the path to test can contain a trailing / or not.
        Parameters:
        file - The existing file to compare against
        pathToTest - The path to test if it is the same
        Returns:
        true if the case is the same in the file and the pathToTest
      • replaceRestrictedCharactersInFileName

        public static java.lang.String replaceRestrictedCharactersInFileName(java.lang.String name)
        Replace any characters from a string which would be restricted when used as a file name. Since Windows has the most restrictive file naming rules, the Windows rules will be what determine what is valid and what is not. The following characters are not allowed: < > : " / \ | ? * In addition, all control characters are not allowed. Any characters which are not valid will be replaced with a '.' character. We are only checking the file name here - we are doing nothing to make sure that the path leading up to the file name is valid.
        Parameters:
        name - The name which will be used as the file name. The name should not contain the path leading up to the file name.
        Returns:
        A string containing the valid file name, with all restricted characters replaced. If all characters are replaced, null will be returned, since the resulting name will be meaningless. If any characters were replaced, and the resulting string is '.' or '..', null will also be returned.
      • fixPathString

        public static java.lang.String fixPathString(java.lang.String absPath)
      • fixPathString

        public static java.lang.String fixPathString(java.io.File absPath)
      • getFixedPathFiles

        public static java.util.Set<java.io.File> getFixedPathFiles(java.util.Collection<java.lang.String> paths)
        Returns a set of files that have the correct case. This is important for Windows systems where "c:\wlp" and "C:\WLP" point to the same directory. For more info, see comments in fixPathString above.
      • fixPathFiles

        public static java.util.Set<java.io.File> fixPathFiles(java.util.Collection<java.io.File> files)
        Returns a set of files that have the correct case. This is important for Windows systems where "c:\wlp" and "C:\WLP" point to the same directory. For more info, see comments in fixPathString above.