Package com.yahoo.net

Class URI

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<URI>

    public class URI
    extends java.lang.Object
    implements java.lang.Cloneable, java.io.Serializable, java.lang.Comparable<URI>

    An URI. This is a pure (immutable) value object.

    This does more normalization of hierarchical URIs (URLs) than described in the RFC and allows hosts with underscores.

    Author:
    bratseth
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  URI.Token  
      static class  URI.URLContext  
    • Constructor Summary

      Constructors 
      Constructor Description
      URI​(java.lang.String uriString)
      Creates an URI without keeping the fragment (the part starting by #).
      URI​(java.lang.String uriString, boolean keepFragment)
      Creates an URI, optionaly keeping the fragment (the part starting by #).
      URI​(java.lang.String uriString, boolean keepFragment, boolean hierarchicalOnly)
      Creates an URI, optionaly keeping the fragment (the part starting by #).
      URI​(java.lang.String scheme, java.lang.String host, int port, java.lang.String rest)
      Creates an url type uri
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      URI addParameter​(java.lang.String name, java.lang.String value)
      Returns a new uri with the an additional parameter
      java.lang.Object clone()  
      int compareTo​(URI object)  
      boolean equals​(java.lang.Object object)  
      int getDepth()
      Returns the depth of this uri.
      java.lang.String getDomain()  
      java.lang.String getExtension()  
      java.lang.String getFilename()  
      java.lang.String getFragment()  
      java.lang.String getHost()
      Returns the normalized host of this URI.
      java.lang.String getInvalidExplanation()
      Returns an explanation of why this uri is invalid, or null if it is valid
      java.lang.String getMainTld()  
      java.lang.String getParams()  
      java.lang.String getPath()  
      int getPort()
      Returns the port number of this scheme if set explicitly, or -1 otherwise
      java.lang.String getQuery()  
      java.lang.String getRest()
      Returns the rest of this uri, that is what is following the host or port.
      java.lang.String getScheme()
      Returns the normalized scheme of this URI.
      int hashCode()  
      boolean isOpaque()
      Returns whether this URI is hierarchical or opaque.
      boolean isValid()
      Returns whether this is a valid URI (after normalizing).
      URI setHost​(java.lang.String host)
      Returns a new URI with a changed host (or authority)
      URI setPort​(int port)
      Returns a new URI with a changed port
      URI setRest​(java.lang.String rest)
      Returns a new URI with a changed rest
      URI setScheme​(java.lang.String scheme)
      Returns a new URI with a changed scheme
      java.lang.String stringValue()
      Returns this uri as a string
      java.util.List<URI.Token> tokenize()  
      static java.lang.String[] tokenize​(java.lang.String item)  
      java.lang.String toString()
      Returns this URI as a string
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • URI

        public URI​(java.lang.String uriString)

        Creates an URI without keeping the fragment (the part starting by #). If the uri is hierarchical, it is normalized and incorrect hierarchical uris which looks like urls are attempted repaired.

        Relative uris are not supported.

        Parameters:
        uriString - the uri string
        Throws:
        java.lang.NullPointerException - if the given uriString is null
      • URI

        public URI​(java.lang.String uriString,
                   boolean keepFragment)
        Creates an URI, optionaly keeping the fragment (the part starting by #). If the uri is hierarchical, it is normalized and incorrect hierarchical uris which looks like urls are attempted repaired.

        Relative uris are not supported.

        Parameters:
        uriString - the uri string
        keepFragment - true to keep the fragment
        Throws:
        java.lang.NullPointerException - if the given uriString is null
      • URI

        public URI​(java.lang.String uriString,
                   boolean keepFragment,
                   boolean hierarchicalOnly)
        Creates an URI, optionaly keeping the fragment (the part starting by #). If the uri is hierarchical, it is normalized and incorrect hierarchical uris which looks like urls are attempted repaired.

        Relative uris are not supported.

        Parameters:
        uriString - the uri string
        keepFragment - true to keep the fragment
        hierarchicalOnly - will force any uri string given to be parsed as a hierarchical one, causing the uri to be invalid if it isn't
        Throws:
        java.lang.NullPointerException - if the given uriString is null
      • URI

        public URI​(java.lang.String scheme,
                   java.lang.String host,
                   int port,
                   java.lang.String rest)
        Creates an url type uri
    • Method Detail

      • isValid

        public boolean isValid()
        Returns whether this is a valid URI (after normalizing). All non-hierarchical uri's containing a scheme is valid.
      • getScheme

        public java.lang.String getScheme()
        Returns the normalized scheme of this URI.
        Returns:
        the normalized scheme (protocol), or null if there is none, which may only be the case with non-hierarchical URIs
      • isOpaque

        public boolean isOpaque()
        Returns whether this URI is hierarchical or opaque. A typical example of an hierarchical URI is an URL, while URI's are mailto, news and such.
        Returns:
        true if the url is opaque, false if it is hierarchical
      • getHost

        public java.lang.String getHost()
        Returns the normalized host of this URI.
        Returns:
        the normalized host, or null if there is none, which may only be the case if this is a non-hierarchical uri
      • getPort

        public int getPort()
        Returns the port number of this scheme if set explicitly, or -1 otherwise
      • getRest

        public java.lang.String getRest()
        Returns the rest of this uri, that is what is following the host or port. This is path, query and fragment as defined in RFC 2396. Returns an empty string if this uri has no rest.
      • getDomain

        public java.lang.String getDomain()
      • getMainTld

        public java.lang.String getMainTld()
      • getPath

        public java.lang.String getPath()
      • getFilename

        public java.lang.String getFilename()
      • getExtension

        public java.lang.String getExtension()
      • getQuery

        public java.lang.String getQuery()
      • getFragment

        public java.lang.String getFragment()
      • getParams

        public java.lang.String getParams()
      • tokenize

        public static java.lang.String[] tokenize​(java.lang.String item)
      • tokenize

        public java.util.List<URI.Token> tokenize()
      • getInvalidExplanation

        public java.lang.String getInvalidExplanation()
        Returns an explanation of why this uri is invalid, or null if it is valid
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • compareTo

        public int compareTo​(URI object)
        Specified by:
        compareTo in interface java.lang.Comparable<URI>
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • setScheme

        public URI setScheme​(java.lang.String scheme)
        Returns a new URI with a changed scheme
      • setHost

        public URI setHost​(java.lang.String host)
        Returns a new URI with a changed host (or authority)
      • setPort

        public URI setPort​(int port)
        Returns a new URI with a changed port
      • setRest

        public URI setRest​(java.lang.String rest)
        Returns a new URI with a changed rest
      • addParameter

        public URI addParameter​(java.lang.String name,
                                java.lang.String value)
        Returns a new uri with the an additional parameter
      • stringValue

        public java.lang.String stringValue()
        Returns this uri as a string
      • toString

        public java.lang.String toString()
        Returns this URI as a string
        Overrides:
        toString in class java.lang.Object
      • getDepth

        public int getDepth()
        Returns the depth of this uri. The depth of an hierarchical uri equals the number of slashes which are not separating the protocol and the host, and not at the end.
        Returns:
        the depth of this uri if it is hierarchical, or 0 if it is opaque