public class URI extends Object
Parsing of a URI specification is done according to the URI syntax described in RFC 2396, and amended by RFC 2732.
Every absolute URI consists of a scheme, followed by a colon (':'), followed by a scheme-specific part. For URIs that follow the "generic URI" syntax, the scheme-specific part begins with two slashes ("//") and may be followed by an authority segment (comprised of user information, host, and port), path segment, query segment and fragment. Note that RFC 2396 no longer specifies the use of the parameters segment and excludes the "user:password" syntax as part of the authority segment. If "user:password" appears in a URI, the entire user/password string is stored as userinfo.
For URIs that do not follow the "generic URI" syntax (e.g. mailto), the entire scheme-specific part is treated as the "path" portion of the URI.
Note that, unlike the java.net.URL class, this class does not provide any built-in network access functionality nor does it provide any scheme-specific functionality (for example, it does not know a default port for a specific scheme). Rather, it only knows the grammar and basic set of operations that can be applied to a URI.
| Modifier and Type | Class and Description |
|---|---|
static class |
URI.MalformedURIException
MalformedURIExceptions are thrown in the process of building a URI or setting
fields on a URI when an operation would result in an invalid URI
specification.
|
| Constructor and Description |
|---|
URI(String uriSpec)
Construct a new URI from a URI specification string.
|
URI(String uriSpec,
boolean allowNonAbsoluteURI)
Construct a new URI from a URI specification string.
|
URI(String scheme,
String userinfo,
String host,
int port,
String path,
String queryString,
String fragment)
Construct a new URI that follows the generic URI syntax from its component
parts.
|
URI(String scheme,
String host,
String path,
String queryString,
String fragment)
Construct a new URI that follows the generic URI syntax from its component
parts.
|
URI(URI base,
String uriSpec)
Construct a new URI from a base URI and a URI specification string.
|
URI(URI base,
String uriSpec,
boolean allowNonAbsoluteURI)
Construct a new URI from a base URI and a URI specification string.
|
| Modifier and Type | Method and Description |
|---|---|
void |
absolutize(URI base)
Absolutize URI with given base URI.
|
boolean |
equals(Object test)
Determines if the passed-in Object is equivalent to this URI.
|
String |
getFragment()
Get the fragment for this URI.
|
String |
getHost()
Get the host for this URI.
|
String |
getPath()
Get the path for this URI.
|
int |
getPort()
Get the port for this URI.
|
String |
getQueryString()
Get the query string for this URI.
|
String |
getRegBasedAuthority()
Get the registry based authority for this URI.
|
String |
getScheme()
Get the scheme for this URI.
|
String |
getSchemeSpecificPart()
Get the scheme-specific part for this URI (everything following the scheme
and the first colon).
|
String |
getUserinfo()
Get the userinfo for this URI.
|
boolean |
isAbsoluteURI()
Returns whether this URI represents an absolute URI.
|
boolean |
isGenericURI()
Get the indicator as to whether this URI uses the "generic URI" syntax.
|
static boolean |
isWellFormedAddress(String address)
Determine whether a string is syntactically capable of representing a valid
IPv4 address, IPv6 reference or the domain name of a network host.
|
static boolean |
isWellFormedIPv4Address(String address)
Determines whether a string is an IPv4 address as defined by RFC 2373, and
under the further constraint that it must be a 32-bit address.
|
static boolean |
isWellFormedIPv6Reference(String address)
Determines whether a string is an IPv6 reference as defined by RFC 2732,
where IPv6address is defined in RFC 2373.
|
void |
setFragment(String fragment)
Set the fragment for this URI.
|
void |
setHost(String host)
Set the host for this URI.
|
void |
setPath(String path)
Set the path for this URI.
|
void |
setPort(int port)
Set the port for this URI. -1 is used to indicate that the port is not
specified, otherwise valid port numbers are between 0 and 65535.
|
void |
setQueryString(String queryString)
Set the query string for this URI.
|
void |
setScheme(String scheme)
Set the scheme for this URI.
|
void |
setUserinfo(String userinfo)
Set the userinfo for this URI.
|
String |
toString()
Get the URI as a string specification.
|
public URI(String uriSpec) throws URI.MalformedURIException
uriSpec - the URI specification string (cannot be null or empty)URI.MalformedURIException - if p_uriSpec violates any syntax rulespublic URI(String uriSpec, boolean allowNonAbsoluteURI) throws URI.MalformedURIException
uriSpec - the URI specification string (cannot be null or
empty)allowNonAbsoluteURI - true to permit non-absolute URIs, false otherwise.URI.MalformedURIException - if p_uriSpec violates any syntax rulespublic URI(URI base, String uriSpec) throws URI.MalformedURIException
base - the base URI (cannot be null if p_uriSpec is null or empty)uriSpec - the URI specification string (cannot be null or empty if
p_base is null)URI.MalformedURIException - if p_uriSpec violates any syntax rulespublic URI(URI base, String uriSpec, boolean allowNonAbsoluteURI) throws URI.MalformedURIException
base - the base URI (cannot be null if p_uriSpec is null
or empty)uriSpec - the URI specification string (cannot be null or
empty if p_base is null)allowNonAbsoluteURI - true to permit non-absolute URIs, false otherwise.URI.MalformedURIException - if p_uriSpec violates any syntax rulespublic URI(String scheme, String host, String path, String queryString, String fragment) throws URI.MalformedURIException
scheme - the URI scheme (cannot be null or empty)host - the hostname, IPv4 address or IPv6 reference for the URIpath - the URI path - if the path contains '?' or '#', then the
query string and/or fragment will be set from the path;
however, if the query and fragment are specified both in
the path and as separate parameters, an exception is
thrownqueryString - the URI query string (cannot be specified if path is
null)fragment - the URI fragment (cannot be specified if path is null)URI.MalformedURIException - if any of the parameters violates syntax
rules or semantic rulespublic URI(String scheme, String userinfo, String host, int port, String path, String queryString, String fragment) throws URI.MalformedURIException
scheme - the URI scheme (cannot be null or empty)userinfo - the URI userinfo (cannot be specified if host is null)host - the hostname, IPv4 address or IPv6 reference for the URIport - the URI port (may be -1 for "unspecified"; cannot be
specified if host is null)path - the URI path - if the path contains '?' or '#', then the
query string and/or fragment will be set from the path;
however, if the query and fragment are specified both in
the path and as separate parameters, an exception is
thrownqueryString - the URI query string (cannot be specified if path is
null)fragment - the URI fragment (cannot be specified if path is null)URI.MalformedURIException - if any of the parameters violates syntax
rules or semantic rulespublic void absolutize(URI base)
base - base URI for absolutizationpublic String getScheme()
public String getSchemeSpecificPart()
public String getUserinfo()
public String getHost()
public int getPort()
public String getRegBasedAuthority()
public String getPath()
public String getQueryString()
public String getFragment()
public void setScheme(String scheme) throws URI.MalformedURIException
scheme - the scheme for this URI (cannot be null)URI.MalformedURIException - if p_scheme is not a conformant scheme namepublic void setUserinfo(String userinfo) throws URI.MalformedURIException
userinfo - the userinfo for this URIURI.MalformedURIException - if p_userinfo contains invalid characterspublic void setHost(String host) throws URI.MalformedURIException
Set the host for this URI. If null is passed in, the userinfo field is also set to null and the port is set to -1.
Note: This method overwrites registry based authority if it previously existed in this URI.
host - the host for this URIURI.MalformedURIException - if p_host is not a valid IP address or DNS
hostname.public void setPort(int port)
throws URI.MalformedURIException
port - the port number for this URIURI.MalformedURIException - if p_port is not -1 and not a valid port
numberpublic void setPath(String path) throws URI.MalformedURIException
path - the path for this URI (may be null)URI.MalformedURIException - if p_path contains invalid characterspublic void setQueryString(String queryString) throws URI.MalformedURIException
queryString - the query string for this URIURI.MalformedURIException - if p_queryString is not null and this URI
does not conform to the generic URI syntax
or if the path is nullpublic void setFragment(String fragment) throws URI.MalformedURIException
fragment - the fragment for this URIURI.MalformedURIException - if p_fragment is not null and this URI does
not conform to the generic URI syntax or if
the path is nullpublic boolean equals(Object test)
public String toString()
public boolean isGenericURI()
public boolean isAbsoluteURI()
public static boolean isWellFormedAddress(String address)
address - the addresspublic static boolean isWellFormedIPv4Address(String address)
Determines whether a string is an IPv4 address as defined by RFC 2373, and under the further constraint that it must be a 32-bit address. Though not expressed in the grammar, in order to satisfy the 32-bit address constraint, each segment of the address cannot be greater than 255 (8 bits of information).
IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
address - the addresspublic static boolean isWellFormedIPv6Reference(String address)
Determines whether a string is an IPv6 reference as defined by RFC 2732, where IPv6address is defined in RFC 2373. The IPv6 address is parsed according to Section 2.2 of RFC 2373, with the additional constraint that the address be composed of 128 bits of information.
IPv6reference = "[" IPv6address "]"
Note: The BNF expressed in RFC 2373 Appendix B does not accurately describe section 2.2, and was in fact removed from RFC 3513, the successor of RFC 2373.
address - the addressCopyright © 2024 HtmlUnit. All rights reserved.