Package com.yahoo.net
Class URI
- java.lang.Object
-
- com.yahoo.net.URI
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<URI>
public class URI extends Object implements Cloneable, Serializable, 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(String uriString)
Creates an URI without keeping the fragment (the part starting by #).URI(String uriString, boolean keepFragment)
Creates an URI, optionaly keeping the fragment (the part starting by #).URI(String uriString, boolean keepFragment, boolean hierarchicalOnly)
Creates an URI, optionaly keeping the fragment (the part starting by #).URI(String scheme, String host, int port, String rest)
Creates an url type uri
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description URI
addParameter(String name, String value)
Returns a new uri with the an additional parameterObject
clone()
int
compareTo(URI object)
boolean
equals(Object object)
int
getDepth()
Returns the depth of this uri.String
getDomain()
String
getExtension()
String
getFilename()
String
getFragment()
String
getHost()
Returns the normalized host of this URI.String
getInvalidExplanation()
Returns an explanation of why this uri is invalid, or null if it is validString
getMainTld()
String
getParams()
String
getPath()
int
getPort()
Returns the port number of this scheme if set explicitly, or -1 otherwiseString
getQuery()
String
getRest()
Returns the rest of this uri, that is what is following the host or port.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(String host)
Returns a new URI with a changed host (or authority)URI
setPort(int port)
Returns a new URI with a changed portURI
setRest(String rest)
Returns a new URI with a changed restURI
setScheme(String scheme)
Returns a new URI with a changed schemeString
stringValue()
Returns this uri as a stringList<URI.Token>
tokenize()
static String[]
tokenize(String item)
String
toString()
Returns this URI as a string
-
-
-
Constructor Detail
-
URI
public URI(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:
NullPointerException
- if the given uriString is null
-
URI
public URI(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 stringkeepFragment
- true to keep the fragment- Throws:
NullPointerException
- if the given uriString is null
-
URI
public URI(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 stringkeepFragment
- true to keep the fragmenthierarchicalOnly
- will force any uri string given to be parsed as a hierarchical one, causing the uri to be invalid if it isn't- Throws:
NullPointerException
- if the given uriString is null
-
-
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 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 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 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 String getDomain()
-
getMainTld
public String getMainTld()
-
getPath
public String getPath()
-
getFilename
public String getFilename()
-
getExtension
public String getExtension()
-
getQuery
public String getQuery()
-
getFragment
public String getFragment()
-
getParams
public String getParams()
-
getInvalidExplanation
public String getInvalidExplanation()
Returns an explanation of why this uri is invalid, or null if it is valid
-
compareTo
public int compareTo(URI object)
- Specified by:
compareTo
in interfaceComparable<URI>
-
setPort
public URI setPort(int port)
Returns a new URI with a changed port
-
addParameter
public URI addParameter(String name, String value)
Returns a new uri with the an additional parameter
-
stringValue
public String stringValue()
Returns this uri as a string
-
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
-
-