Class URIUtil


  • public final class URIUtil
    extends Object
    A utility class for manipulating URIs. This class works around some of the undesirable behavior of the URI class, and provides additional path manipulation methods that are not available on the URI class.
    Since:
    org.eclipse.equinox.common 3.5
    Restriction:
    This class is not intended to be instantiated by clients.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static URI append​(URI base, String extension)
      Returns a new URI with all the same components as the given base URI, but with a path component created by appending the given extension to the base URI's path.
      static URI fromString​(String uriString)
      Returns a URI corresponding to the given unencoded string.
      static boolean isFileURI​(URI uri)
      Returns whether the given URI refers to a local file system URI.
      static String lastSegment​(URI location)
      Returns the last segment of the given URI.
      static URI makeAbsolute​(URI relative, URI baseURI)
      Returns an absolute URI that is created by appending the given relative URI to the given base.
      static URI makeRelative​(URI original, URI baseURI)
      Returns a URI equivalent to the given original URI, but relative to the given base URI if possible.
      static URI removeFileExtension​(URI uri)
      Returns a new URI which is the same as this URI but with the file extension removed from the path part.
      static boolean sameURI​(URI uri1, URI uri2)
      Returns true if the two URIs are equal.
      static File toFile​(URI uri)
      Returns the URI as a local file, or null if the given URI does not represent a local file.
      static URI toJarURI​(URI uri, IPath entryPath)
      Returns a Java ARchive (JAR) URI for an entry in a jar or zip file.
      static String toUnencodedString​(URI uri)
      Returns a string representation of the given URI that doesn't have illegal characters encoded.
      static URI toURI​(URL url)
      Returns the URL as a URI.
      static URL toURL​(URI uri)
      Returns a URI as a URL.
    • Method Detail

      • append

        public static URI append​(URI base,
                                 String extension)
        Returns a new URI with all the same components as the given base URI, but with a path component created by appending the given extension to the base URI's path.

        The important difference between this method and URI.resolve(String) is in the treatment of the final segment. The URI resolve method drops the last segment if there is no trailing slash as specified in section 5.2 of RFC 2396. This leads to unpredictable behaviour when working with file: URIs, because the existence of the trailing slash depends on the existence of a local file on disk. This method operates like a traditional path append and always preserves all segments of the base path.

        Parameters:
        base - The base URI to append to
        extension - The unencoded path extension to be added
        Returns:
        The appended URI
      • fromString

        public static URI fromString​(String uriString)
                              throws URISyntaxException
        Returns a URI corresponding to the given unencoded string. This method will take care of encoding any characters that must be encoded according to the URI specification. This method must not be called with a string that already contains an encoded URI, since this will result in the URI escape character ('%') being escaped itself.
        Parameters:
        uriString - An unencoded URI string
        Returns:
        A URI corresponding to the given string
        Throws:
        URISyntaxException - If the string cannot be formed into a valid URI
      • isFileURI

        public static boolean isFileURI​(URI uri)
        Returns whether the given URI refers to a local file system URI.
        Parameters:
        uri - The URI to check
        Returns:
        true if the URI is a local file system location, and false otherwise
      • lastSegment

        public static String lastSegment​(URI location)
        Returns the last segment of the given URI. For a hierarchical URL this returns the last segment of the path. For opaque URIs this treats the scheme-specific part as a path and returns the last segment. Returns null for a hierarchical URI with an empty path, and for opaque URIs whose scheme-specific part cannot be interpreted as a path.
      • removeFileExtension

        public static URI removeFileExtension​(URI uri)
        Returns a new URI which is the same as this URI but with the file extension removed from the path part. If this URI does not have an extension, this path is returned.

        The file extension portion is defined as the string following the last period (".") character in the last segment. If there is no period in the last segment, the path has no file extension portion. If the last segment ends in a period, the file extension portion is the empty string.

        Returns:
        the new URI
      • sameURI

        public static boolean sameURI​(URI uri1,
                                      URI uri2)
        Returns true if the two URIs are equal. URIs are considered equal if URI.equals(Object) returns true, if the string representation of the URIs is equal, or if they URIs are represent the same local file.
        Parameters:
        uri1 - The first URI to compare
        uri2 - The second URI to compare
        Returns:
        true if the URIs are the same, and false otherwise.
      • toFile

        public static File toFile​(URI uri)
        Returns the URI as a local file, or null if the given URI does not represent a local file.
        Parameters:
        uri - The URI to return the file for
        Returns:
        The local file corresponding to the given URI, or null
      • toJarURI

        public static URI toJarURI​(URI uri,
                                   IPath entryPath)
        Returns a Java ARchive (JAR) URI for an entry in a jar or zip file. The given input URI should represent a zip or jar file, but this method will not check for existence or validity of a file at the given URI.

        The entry path parameter can optionally be used to obtain the URI of an entry in a zip or jar file. If an entry path of null is provided, the resulting URI will represent the jar file itself.

        Parameters:
        uri - The URI of a zip or jar file
        entryPath - The path of a file inside the jar, or null to obtain the URI for the jar file itself.
        Returns:
        A URI with the "jar" scheme for the given input URI and entry path
        See Also:
        JarURLConnection
      • toURI

        public static URI toURI​(URL url)
                         throws URISyntaxException
        Returns the URL as a URI. This method will handle URLs that are not properly encoded (for example they contain unencoded space characters).
        Parameters:
        url - The URL to convert into a URI
        Returns:
        A URI representing the given URL
        Throws:
        URISyntaxException
      • toUnencodedString

        public static String toUnencodedString​(URI uri)
        Returns a string representation of the given URI that doesn't have illegal characters encoded. This string is suitable for later passing to fromString(String).
        Parameters:
        uri - The URI to convert to string format
        Returns:
        An unencoded string representation of the URI
      • makeAbsolute

        public static URI makeAbsolute​(URI relative,
                                       URI baseURI)
        Returns an absolute URI that is created by appending the given relative URI to the given base. If the relative URI is already absolute it is simply returned.

        This method is guaranteed to be the inverse of makeRelative(URI, URI). That is, if R = makeRelative(O, B), then makeAbsolute(R, B), will return the original URI O.

        Parameters:
        relative - the relative URI
        baseURI - the base URI
        Returns:
        an absolute URI
      • makeRelative

        public static URI makeRelative​(URI original,
                                       URI baseURI)
        Returns a URI equivalent to the given original URI, but relative to the given base URI if possible.

        This method is equivalent to URI.relativize(java.net.URI), except for its handling of file URIs. For file URIs, this method handles file system path devices. If the URIs are not on the same device, then the original URI is returned.

        Parameters:
        original - the original URI
        baseURI - the base URI
        Returns:
        a relative URI