Class UrlUtils


  • public class UrlUtils
    extends Object
    Utilities to deal with Java URL instances
    Author:
    markr
    • Constructor Detail

      • UrlUtils

        public UrlUtils()
    • Method Detail

      • isLocal

        public static boolean isLocal​(URL url)
        Test if URL is local, i.e. a local file or directory
        Parameters:
        url - to verify
        Returns:
        true when local, false otherwise
      • isLocalFile

        public static boolean isLocalFile​(URL url)
        Test if URL is a local file
        Parameters:
        url - to verify
        Returns:
        true when local, false otherwise
      • isLocalDirectory

        public static boolean isLocalDirectory​(URL url)
        Test if URL is a local file
        Parameters:
        url - to verify
        Returns:
        true when local, false otherwise
      • isLocalZipFile

        public static boolean isLocalZipFile​(URL url)
        Test if URL is a local file
        Parameters:
        url - to verify
        Returns:
        true when local, false otherwise
      • hasHost

        public static boolean hasHost​(URL url)
        Verify if the URL has a host specified
        Parameters:
        url - to check
        Returns:
        true when host present, false otherwise
      • createFrom

        public static URL createFrom​(String path)
        Construct a URL based on a given a location either local or not
        Parameters:
        path - to convert
        Returns:
        URL representation
      • createFromLocalPath

        public static URL createFromLocalPath​(String path)
        Construct a URL based on a given local file location
        Parameters:
        path - to convert
        Returns:
        URL representation
      • createFromLocalPath

        public static URL createFromLocalPath​(Path path)
        Construct a URL based on a given (local) path
        Parameters:
        path - to convert
        Returns:
        URL representation
      • createFromLocalPathOrResource

        public static URL createFromLocalPathOrResource​(String path)
        Construct a URL based on a given (local) path
        Parameters:
        path - to convert
        Returns:
        URL representation
      • createFromLocalPathOrResource

        public static URL createFromLocalPathOrResource​(Path path)
        Construct a URL based on a given (local) path
        Parameters:
        path - to convert
        Returns:
        URL representation
      • appendRelativePathToURL

        public static URL appendRelativePathToURL​(URL baseUrl,
                                                  String relativePath)
        Append the given relative path to the URL. Implementation based on answer by Martin Senne via https://stackoverflow.com/questions/7498030/append-relative-url-to-java-net-url
        Parameters:
        baseUrl - base url
        relativePath - to append
        Returns:
        combined URL
      • asLocalPath

        public static Path asLocalPath​(URL url)
        converts to a Path. Has the benefit compared to using getPath() on the URL directly that this removes the preceding slash for local files, e.g. instead of getting "/C:/something" we obtain "C:/something" which can be used directly as a local path or file etc.
        Parameters:
        url - to convert to path
        Returns:
        created path, null if not possible