Class URIReference
- All Implemented Interfaces:
Serializable
,Comparable<URIReference>
The parse(String)
method parses a given string as a URI reference according
to RFC 3986 and creates a URIReference
instance, representing either
a URI or a relative reference. The isRelativeReference()
can be used
to check if the parsed URI instance represents a relative reference (returns
true
) or a URI (returns false
). Access URI components such as
scheme
using corresponding getter methods like getScheme()
.
The resolve(String)
method and resolve(URIReference)
method return a new URIReference
instance representing a URI reference
obtained by resolving the current URI reference against a given base URI, following
the rules in RFC 3986, 5 Reference Resolution.
The normalize()
method returns a new URIReference
instance representing
a URI reference obtained by normalizing the current URI reference, in accordance
with RFC 3986, 6 Normalization and Comparison. Note that a URI reference must
be resolved before it can be normalized.
This class is immutable.
Examples:
//---------------------------------------------------------------------------
// Parsing.
//---------------------------------------------------------------------------
URIReference uriRef = URIReference
.parse("http://example.com/a"); // Parse a URI.
System.out.println(uriRef.isRelativeReference()); // false
System.out.println(uriRef.getScheme()); // "http"
System.out.println(uriRef.hasAuthority()); // true
System.out.println(uriRef.getAuthority().toString()); // "example.com"
System.out.println(uriRef.getUserinfo()); // null
System.out.println(uriRef.getHost().getType()); // "REGNAME"
System.out.println(uriRef.getHost().getValue()); // "example.com"
System.out.println(uriRef.getPort()); // -1
System.out.println(uriRef.getPath()); // "/a"
System.out.println(uriRef.getQuery()); // null
System.out.println(uriRef.getFragment()); // null
//---------------------------------------------------------------------------
// Resolution.
//---------------------------------------------------------------------------
URIReference uriRef = URIReference
.parse("http://example.com") // Parse a base URI.
.resolve("/a/b"); // Resolve a relative reference against the base URI.
System.out.println(uriRef.isRelativeReference()); // false
System.out.println(uriRef.getScheme()); // "http"
System.out.println(uriRef.hasAuthority()); // true
System.out.println(uriRef.getAuthority().toString()); // "example.com"
System.out.println(uriRef.getUserinfo()); // null
System.out.println(uriRef.getHost().getType()); // "REGNAME"
System.out.println(uriRef.getHost().getValue()); // "example.com"
System.out.println(uriRef.getPort()); // -1
System.out.println(uriRef.getPath()); // "/a/b"
System.out.println(uriRef.getQuery()); // null
System.out.println(uriRef.getFragment()); // null
//---------------------------------------------------------------------------
// Normalization.
//---------------------------------------------------------------------------
URIReference uriRef = URIReference
.parse("hTTp://example.com:80/a/b/c/../d/") // Parse a URI.
.normalize(); // Normalize it.
System.out.println(uriRef.isRelativeReference()); // false
System.out.println(uriRef.getScheme()); // "http"
System.out.println(uriRef.hasAuthority()); // true
System.out.println(uriRef.getAuthority().toString()); // "example.com:80"
System.out.println(uriRef.getUserinfo()); // null
System.out.println(uriRef.getHost().getType()); // "REGNAME"
System.out.println(uriRef.getHost().getValue()); // "example.com"
System.out.println(uriRef.getPort()); // -1
System.out.println(uriRef.getPath()); // "/a/b/d/"
System.out.println(uriRef.getQuery()); // null
System.out.println(uriRef.getFragment()); // null
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionint
compareTo
(URIReference other) Compares thisURIReference
object with anotherURIReference
object for order.boolean
Compares thisURIReference
object with the specified object for equality.Get the authority of this URI reference.Returns the charset used for percent-encoding some characters (e.g.Get the fragment of this URI reference.getHost()
Get the host of this URI reference.getPath()
Get the path of this URI reference.int
getPort()
Get the port of this URI reference.getQuery()
Get the query of this URI reference.Get the scheme of this URI reference.Get the userinfo of this URI reference.boolean
Checks whether or not this URI reference has an authority.int
hashCode()
Returns a hash code value for thisURIReference
object.boolean
Returnstrue
if the URI reference is a relative reference.Normalizes this URI reference.static URIReference
Parses a string based on in RFC 3986 and creates aURIReference
instance if parsing succeeds.static URIReference
Parses a string based on in RFC 3986 and creates aURIReference
instance if parsing succeeds.Resolves the given URI reference against this URI reference.resolve
(URIReference uriRef) Resolve the given URI reference against this URI reference.toString()
Returns a string representation of thisURIReference
object.
-
Method Details
-
parse
Parses a string based on in RFC 3986 and creates aURIReference
instance if parsing succeeds. If parsing fails due to invalid input string, anIllegalArgumentException
will be thrown.Note that this method works as if invoking it were equivalent to evaluating the expression
.parse
(String uriRef,StandardCharsets
.UTF_8
)Examples:
// Example 1. Parse a string as a URI. URIReference.parse("http://example.com"); // Example 2. Parse a string as a relative reference. URIReference.parse("//example.com/path1"); // Example 3. Parse a string with an IPV4 host as a URI. URIReference.parse("http://101.102.103.104"); // Example 4. Parse a string with an IPV6 host as a URI. URIReference.parse("http://[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]"); // Example 5. Parse a string with percent-encoded values as a URI. URIReference.parse("http://%6A%6F%68%6E@example.com");
- Parameters:
uriRef
- A input string to parse as a URI reference.- Returns:
- The
URIReference
instance obtained by parsing the input string. - Throws:
NullPointerException
- IfuriRef
orcharset
isnull
.IllegalArgumentException
- IfuriRef
is invalid as a URI reference.- See Also:
-
parse
Parses a string based on in RFC 3986 and creates a
URIReference
instance if parsing succeeds. If parsing fails due to invalid input string, anIllegalArgumentException
will be thrown.Examples:
// Example 1. Create a URI using UTF-8 encoding. URIReference.parse("http://example.com", StandardCharsets.UTF_8); // Example 2. Create a relative reference using UTF-8 encoding. URIReference.parse("//example.com/path1", StandardCharsets.UTF_8); // Example 3. Create a URI with IPV4 host using UTF-8 encoding. URIReference.parse("http://101.102.103.104", StandardCharsets.UTF_8); // Example 4. Create a URI with IPV6 host using UTF-8 encoding. URIReference.parse("http://[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]", StandardCharsets.UTF_8); // Example 5. Create a URI with percent-encoded values using UTF-8 encoding. URIReference.parse("http://%6A%6F%68%6E@example.com", StandardCharsets.UTF_8);
- Parameters:
uriRef
- The input string to be parsed as aURIReference
instance.charset
- The charset used for percent-encoding some characters (e.g. reserved characters) contained in the input string.- Returns:
- The
URIReference
instance obtained by parsing the input string. - Throws:
NullPointerException
- IfuriRef
orcharset
isnull
.IllegalArgumentException
- IfuriRef
is invalid as a URI reference.- See Also:
-
getCharset
Returns the charset used for percent-encoding some characters (e.g. reserved characters) contained in this URI reference.- Returns:
- The charset used for percent-encoding some characters (e.g. reserved characters) contained in this URI reference.
-
isRelativeReference
public boolean isRelativeReference()Returnstrue
if the URI reference is a relative reference. See RFC 3986, 4.2. Relative Reference for more details.- Returns:
true
if the URI reference is a relative reference. otherwise,false
.- See Also:
-
getScheme
Get the scheme of this URI reference. If this URI reference is a relative reference,null
is returned.- Returns:
- The scheme of this URI reference.
-
getAuthority
Get the authority of this URI reference.- Returns:
- The authority of this URI reference.
-
getUserinfo
Get the userinfo of this URI reference.- Returns:
- The userinfo of this URI reference.
-
getHost
Get the host of this URI reference.- Returns:
- The host of this URI reference.
-
getPort
public int getPort()Get the port of this URI reference.- Returns:
- The port of this URI reference.
-
getPath
Get the path of this URI reference.- Returns:
- The path of this URI reference.
-
getQuery
Get the query of this URI reference.- Returns:
- The query of this URI reference.
-
getFragment
Get the fragment of this URI reference.- Returns:
- The fragment of this URI reference.
-
hasAuthority
public boolean hasAuthority()Checks whether or not this URI reference has an authority.- Returns:
true
if this URI reference has an authority; otherwise,false
.
-
toString
Returns a string representation of thisURIReference
object.The string is constructed by concatenating the
scheme
,authority
,path
,query
, andfragment
components, separated by appropriate delimiters.- Overrides:
toString
in classObject
- Returns:
- A string representation of this
URIReference
object.
-
equals
Compares thisURIReference
object with the specified object for equality.The comparison is based on the values of
scheme
,authority
,path
,query
, andfragment
components.- Overrides:
equals
in classObject
- Parameters:
obj
- The object to be compared for equality with thisURIReference
.- Returns:
true
if the specified object is equal to thisURIReference
.
-
hashCode
public int hashCode()Returns a hash code value for thisURIReference
object.The hash code is generated based on the values of
scheme
,authority
,path
,query
, andfragment
components. -
compareTo
Compares thisURIReference
object with anotherURIReference
object for order. The comparison is based on the string representation of theURIReference
objects.- Specified by:
compareTo
in interfaceComparable<URIReference>
- Parameters:
other
- TheURIReference
object to be compared.- Returns:
- 0 if this
URIReference
object is equal to the specifiedURIReference
object. A negative value if thisURIReference
object is less than the specified object. A positive value if thisURIReference
object is greater than the specified object. - Throws:
NullPointerException
- If the specifiedURIReference
object isnull
.
-
resolve
Resolves the given URI reference against this URI reference. This method works as if invoking it were equivalent to evaluating the expression
.resolve
(parse
(uriRef,getCharset()
))Examples:
// A base URI. URIReference baseUri = URIReference.parse("http://example.com"); // A relative reference. String relRef = "/path1/path2"; // Resolve the relative reference against the base URI. URIReference resolved = baseUri.resolve(relRef); // This will output "http://example.com/path1/path2". System.out.println(resolved.toString());
- Parameters:
uriRef
- A string representing a URI reference to be resolved against this URI reference.- Returns:
- The URI reference obtained by resolving the input string against this URI reference.
- Throws:
NullPointerException
- IfuriRef
isnull
.IllegalStateException
- If this URI reference is not an absolute URI.
-
resolve
Resolve the given URI reference against this URI reference.Examples:
// A base URI. URIReference baseUri = URIReference.parse("http://example.com"); // A relative reference. URIReference relRef = URIReference.parse("/path1/path2"); // Resolve the relative reference against the base URI. URIReference resolved = baseUri.resolve(relRef); // This will output "http://example.com/path1/path2". System.out.println(resolved.toString());
- Parameters:
uriRef
- A URI reference to be resolved against this URI reference.- Returns:
- The URI reference obtained by resolving the input string against this URI reference.
- Throws:
NullPointerException
- Ifstr
isnull
.IllegalStateException
- If this URI reference is not an absolute URI.
-
normalize
Normalizes this URI reference.This method does not modify the state of the original
URIReference
instance on which this method is called. Instead, it creates a newURIReference
instance and initializes it with the information about the normalized URI reference.Note that this method throws an
IllegalStateException
if this URI reference has not been resolved yet.- Returns:
- A new
URIReference
instance representing the normalized URI reference. - Throws:
IllegalStateException
- If this URI reference has not been resolved yet.
-