Class VFSPaths


  • public class VFSPaths
    extends Object
    This is compatible with the Path interface but does not implement it. The reason is that while FileSystem is really slick, policies for accessing files are set by the security manager per JVM. So, if this code is to run on a server with restricted access to the real file system (but access its own virtual mounts) we have to restrict access for the entire JVM which means that the server can no longer function, at least without very specific configuration which is going to be hard for others to do. As such until something changes, we can't actually use the Java stuff, which, to be fair, is made so that Java developers have seamless access to various file systems.

    A path in the store is of the form
         scheme#/a/b/c/d  (absolute)
         scheme#p/q/r     (relative)
     
    as such these are almost uris. The problem with using a URI vs this is that we must have a scheme at all times to tell us which store this goes in and relative uris do not have schemes. No scheme (which probably should include #/a/b/c) means native file.

    Created by Jeff Gaynor
    on 2/29/20 at 7:03 AM

    • Field Detail

      • SCHEME_DELIMITER

        public static String SCHEME_DELIMITER
      • PATH_SEPARATOR

        public static String PATH_SEPARATOR
      • THIS_COMPONENT

        public static String THIS_COMPONENT
      • PARENT_COMPONENT

        public static String PARENT_COMPONENT
    • Constructor Detail

      • VFSPaths

        public VFSPaths()
    • Method Detail

      • isVFSPath

        public static boolean isVFSPath​(String path)
      • checkScheme

        public static boolean checkScheme​(String scheme,
                                          String path)
        Check is a path has this scheme. True if so, false if not
        Parameters:
        scheme -
        path -
        Returns:
      • hasSameScheme

        public static boolean hasSameScheme​(String path1,
                                            String path2)
        Check if two paths have the same scheme
        Parameters:
        path1 -
        path2 -
        Returns:
      • isUnq

        public static boolean isUnq​(String path)
        Is this path unqualified? I.e., there is no scheme
        Parameters:
        path -
        Returns:
      • getScheme

        public static String getScheme​(String path)
        Returns the scheme for this path or an empty string if there is none.
        Parameters:
        path -
        Returns:
      • isAbsolute

        public static boolean isAbsolute​(String path)
        Returns if this path is absolute, i.e. if it starts with the path separator. So A#/a/b/c is absolute, A#b/c is not.

        An absolute path contains enough information to find the file. A relative path does not. This will compare them ignoring schemes.
        Parameters:
        path -
        Returns:
      • getUnqPath

        public static String getUnqPath​(String fqPath)
        Takes a fully qualified path and returns the path without the scheme
        Parameters:
        fqPath -
        Returns:
      • getFileName

        public static String getFileName​(String path)
      • getParentPath

        public static String getParentPath​(String path)
      • getPathComponentCount

        public int getPathComponentCount​(String path)
        The number of components in this path. Note that this does not, e.g. normalize(String) this path first so if that is an issue, normalize first.
        Parameters:
        path -
        Returns:
      • toPathComponents

        public static String[] toPathComponents​(String path)
        Return the un-normalized components of this path. So A#a/b/c would return the array {"a","b","c"}, as would the relative path a/b/c.
        Parameters:
        path -
        Returns:
      • getComponentAt

        public static String getComponentAt​(String path,
                                            int index)
        Returns the path component at the given index. So A#a/b/c and index = 1 returns "b".
        Parameters:
        path -
        index -
        Returns:
      • subpath

        public static String subpath​(String path,
                                     int beginIndex,
                                     int endIndex)
        Returns a subpath of components. This is FQ if the argument was/
        Parameters:
        beginIndex -
        endIndex -
        Returns:
      • startsWith

        public static boolean startsWith​(String path,
                                         String other)
        Does the path start with the other path? So if path=A#/a/b/c/d and other=B#/a/b the answer is true. Note that the paths are compared here, not the schemes so you can use this when comparing paths in different stores.
        Parameters:
        path -
        other -
        Returns:
      • endsWith

        public static boolean endsWith​(String path,
                                       String other)
        Does this path end with the other? E.g. path=A#/a/b/c/d other=B#c/d would return true.
        Parameters:
        path -
        other -
        Returns:
      • normalize

        public static String normalize​(String path)
        Removes redundant path components.
        Parameters:
        path -
        Returns:
      • resolve

        public static String resolve​(String path,
                                     String relativePath)
        Resolves the relativePath against the path. So if path=A#/a/b/c and relativePath=B#q/r then this returns A#/a/b/c/q/r. Note two edge cases. If the relativePath is actually absolute, this will return that. If the relativePath is trivial, the path is returned. In point of fact

        relativize(p,resolve(p,q)).equals(q)

        as long as they have the same scheme (or the relative path gets the scheme of the path).
        Parameters:
        path -
        relativePath -
        Returns:
      • resolveSibling

        public static String resolveSibling​(String path,
                                            String other)
        This resolves the other against the parent of the path -- making them siblings, i.e. in the same directory. So if path=A#/a/b/c/d and other=q the result is A#/a/b/c/q, i.e., d and q are now siblings in the directory A#/a/b/c.
        Parameters:
        path -
        other -
        Returns:
      • relativize

        public static String relativize​(String path,
                                        String other)
        Attempts to relativize the other path against the path. So if path=/a/b other=/a/b/c/d then this returns c/d. In point of fact

        relativize(p,resolve(p,q)).equals(q)

        as long as they have the same scheme (or the relative path gets the scheme of the path).
        Parameters:
        path -
        other -
        Returns:
      • compareTo

        public static int compareTo​(String path,
                                    String other)
        Lexically compares two paths without schemes. This returns a 0 (zero) if they are equal. This may not make much sense if they are in different schemes, but it does allow to see when two paths are otherwise equal.
        Parameters:
        path -
        other -
        Returns:
      • equals

        public static boolean equals​(String path1,
                                     String path2)
        Compares two paths. These are equal if their schemes are equal and their normalizations are.
        Parameters:
        path1 -
        path2 -
        Returns: