Class FastPathResolver


  • public final class FastPathResolver
    extends java.lang.Object
    Resolve relative paths and URLs/URIs against a base path in a way that is faster than Java's URL/URI parser (and much faster than Path), while aiming for cross-platform compatibility, and hopefully in particular being robust to the many forms of Windows path weirdness.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.regex.Pattern percentMatcher
      Match %-encoded characters in URLs.
      private static java.util.regex.Pattern schemeOneSlashMatcher
      Match custom URLs that are followed by one slash.
      private static java.util.regex.Pattern schemeTwoSlashMatcher
      Match custom URLs that are followed by two slashes.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FastPathResolver()
      Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static int hexCharToInt​(char c)
      Hex char to int.
      static java.lang.String normalizePath​(java.lang.String path, boolean isFileOrJarURL)
      Parse percent encoding, e.g.
      static java.lang.String resolve​(java.lang.String pathStr)
      Strip away any "jar:" prefix from a filename URI, and convert it to a file path, handling possibly-broken mixes of filesystem and URI conventions.
      static java.lang.String resolve​(java.lang.String resolveBasePath, java.lang.String relativePath)
      Strip away any "jar:" prefix from a filename URI, and convert it to a file path, handling possibly-broken mixes of filesystem and URI conventions; resolve relative paths relative to resolveBasePath.
      private static void translateSeparator​(java.lang.String path, int startIdx, int endIdx, boolean stripFinalSeparator, java.lang.StringBuilder buf)
      Translate backslashes to forward slashes, optionally removing trailing separator.
      private static void unescapePercentEncoding​(java.lang.String path, int startIdx, int endIdx, java.lang.StringBuilder buf)
      Unescape runs of percent encoding, e.g.
      • Methods inherited from class java.lang.Object

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

      • percentMatcher

        private static final java.util.regex.Pattern percentMatcher
        Match %-encoded characters in URLs.
      • schemeTwoSlashMatcher

        private static final java.util.regex.Pattern schemeTwoSlashMatcher
        Match custom URLs that are followed by two slashes.
      • schemeOneSlashMatcher

        private static final java.util.regex.Pattern schemeOneSlashMatcher
        Match custom URLs that are followed by one slash.
    • Constructor Detail

      • FastPathResolver

        private FastPathResolver()
        Constructor.
    • Method Detail

      • translateSeparator

        private static void translateSeparator​(java.lang.String path,
                                               int startIdx,
                                               int endIdx,
                                               boolean stripFinalSeparator,
                                               java.lang.StringBuilder buf)
        Translate backslashes to forward slashes, optionally removing trailing separator.
        Parameters:
        path - the path
        startIdx - the start index
        endIdx - the end index
        stripFinalSeparator - if true, strip the final separator
        buf - the buf
      • hexCharToInt

        private static int hexCharToInt​(char c)
        Hex char to int.
        Parameters:
        c - the character
        Returns:
        the integer value of the character
      • unescapePercentEncoding

        private static void unescapePercentEncoding​(java.lang.String path,
                                                    int startIdx,
                                                    int endIdx,
                                                    java.lang.StringBuilder buf)
        Unescape runs of percent encoding, e.g. "%20%43%20" -> " + "
        Parameters:
        path - the path
        startIdx - the start index
        endIdx - the end index
        buf - the buf
      • normalizePath

        public static java.lang.String normalizePath​(java.lang.String path,
                                                     boolean isFileOrJarURL)
        Parse percent encoding, e.g. "%20" -> " "; convert '/' or '\\' to SEP; remove trailing separator char if present.
        Parameters:
        path - The path to normalize.
        isFileOrJarURL - True if this is a "file:" or "jar:" URL.
        Returns:
        The normalized path.
      • resolve

        public static java.lang.String resolve​(java.lang.String resolveBasePath,
                                               java.lang.String relativePath)
        Strip away any "jar:" prefix from a filename URI, and convert it to a file path, handling possibly-broken mixes of filesystem and URI conventions; resolve relative paths relative to resolveBasePath.
        Parameters:
        resolveBasePath - The base path.
        relativePath - The path to resolve relative to the base path.
        Returns:
        The resolved path.
      • resolve

        public static java.lang.String resolve​(java.lang.String pathStr)
        Strip away any "jar:" prefix from a filename URI, and convert it to a file path, handling possibly-broken mixes of filesystem and URI conventions. Returns null if relativePathStr is an "http(s):" path.
        Parameters:
        pathStr - The path to resolve.
        Returns:
        The resolved path.