Class URIReference
- java.lang.Object
-
- org.czeal.urireference.URIReference
-
- All Implemented Interfaces:
Serializable,Comparable<URIReference>
public class URIReference extends Object implements Serializable, Comparable<URIReference>
A class representing a URI reference, as defined in RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax. This class provides various methods for working with URI references, including parsing, resolving, and normalizing them.The
parse(String)method parses a given string as a URI reference according to RFC 3986 and creates aURIReferenceinstance, representing either a URI or a relative reference. TheisRelativeReference()can be used to check if the parsed URI instance represents a relative reference (returnstrue) or a URI (returnsfalse). Access URI components such asschemeusing corresponding getter methods likegetScheme().The
resolve(String)method andresolve(URIReference)method return a newURIReferenceinstance 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 newURIReferenceinstance 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- Author:
- Hideki Ikeda
- See Also:
- RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax, Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(URIReference other)Compares thisURIReferenceobject with anotherURIReferenceobject for order.booleanequals(Object obj)Compares thisURIReferenceobject with the specified object for equality.AuthoritygetAuthority()Get the authority of this URI reference.CharsetgetCharset()Returns the charset used for percent-encoding some characters (e.g. reserved characters) contained in this URI reference.StringgetFragment()Get the fragment of this URI reference.HostgetHost()Get the host of this URI reference.StringgetPath()Get the path of this URI reference.intgetPort()Get the port of this URI reference.StringgetQuery()Get the query of this URI reference.StringgetScheme()Get the scheme of this URI reference.StringgetUserinfo()Get the userinfo of this URI reference.booleanhasAuthority()Checks whether or not this URI reference has an authority.inthashCode()Returns a hash code value for thisURIReferenceobject.booleanisRelativeReference()Returnstrueif the URI reference is a relative reference.URIReferencenormalize()Normalizes this URI reference.static URIReferenceparse(String uriRef)Parses a string based on in RFC 3986 and creates aURIReferenceinstance if parsing succeeds.static URIReferenceparse(String uriRef, Charset charset)Parses a string based on in RFC 3986 and creates aURIReferenceinstance if parsing succeeds.URIReferenceresolve(String uriRef)Resolves the given URI reference against this URI reference.URIReferenceresolve(URIReference uriRef)Resolve the given URI reference against this URI reference.StringtoString()Returns a string representation of thisURIReferenceobject.
-
-
-
Method Detail
-
parse
public static URIReference parse(String uriRef)
Parses a string based on in RFC 3986 and creates aURIReferenceinstance if parsing succeeds. If parsing fails due to invalid input string, anIllegalArgumentExceptionwill 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
URIReferenceinstance obtained by parsing the input string. - Throws:
NullPointerException- IfuriReforcharsetisnull.IllegalArgumentException- IfuriRefis invalid as a URI reference.- See Also:
- RFC 3986 Uniform Resource Identifier (URI): Generic Syntax
-
parse
public static URIReference parse(String uriRef, Charset charset)
Parses a string based on in RFC 3986 and creates a
URIReferenceinstance if parsing succeeds. If parsing fails due to invalid input string, anIllegalArgumentExceptionwill 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 aURIReferenceinstance.charset- The charset used for percent-encoding some characters (e.g. reserved characters) contained in the input string.- Returns:
- The
URIReferenceinstance obtained by parsing the input string. - Throws:
NullPointerException- IfuriReforcharsetisnull.IllegalArgumentException- IfuriRefis invalid as a URI reference.- See Also:
- RFC 3986 Uniform Resource Identifier (URI): Generic Syntax
-
getCharset
public Charset 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()
Returnstrueif the URI reference is a relative reference. See RFC 3986, 4.2. Relative Reference for more details.- Returns:
trueif the URI reference is a relative reference. otherwise,false.- See Also:
- RFC 3986, 4.2. Relative Reference
-
getScheme
public String getScheme()
Get the scheme of this URI reference. If this URI reference is a relative reference,nullis returned.- Returns:
- The scheme of this URI reference.
-
getAuthority
public Authority getAuthority()
Get the authority of this URI reference.- Returns:
- The authority of this URI reference.
-
getUserinfo
public String getUserinfo()
Get the userinfo of this URI reference.- Returns:
- The userinfo of this URI reference.
-
getHost
public Host 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
public String getPath()
Get the path of this URI reference.- Returns:
- The path of this URI reference.
-
getQuery
public String getQuery()
Get the query of this URI reference.- Returns:
- The query of this URI reference.
-
getFragment
public String 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:
trueif this URI reference has an authority; otherwise,false.
-
toString
public String toString()
Returns a string representation of thisURIReferenceobject.The string is constructed by concatenating the
scheme,authority,path,query, andfragmentcomponents, separated by appropriate delimiters.- Overrides:
toStringin classObject- Returns:
- A string representation of this
URIReferenceobject.
-
equals
public boolean equals(Object obj)
Compares thisURIReferenceobject with the specified object for equality.The comparison is based on the values of
scheme,authority,path,query, andfragmentcomponents.- Overrides:
equalsin classObject- Parameters:
obj- The object to be compared for equality with thisURIReference.- Returns:
trueif the specified object is equal to thisURIReference.
-
hashCode
public int hashCode()
Returns a hash code value for thisURIReferenceobject.The hash code is generated based on the values of
scheme,authority,path,query, andfragmentcomponents.
-
compareTo
public int compareTo(URIReference other)
Compares thisURIReferenceobject with anotherURIReferenceobject for order. The comparison is based on the string representation of theURIReferenceobjects.- Specified by:
compareToin interfaceComparable<URIReference>- Parameters:
other- TheURIReferenceobject to be compared.- Returns:
- 0 if this
URIReferenceobject is equal to the specifiedURIReferenceobject. A negative value if thisURIReferenceobject is less than the specified object. A positive value if thisURIReferenceobject is greater than the specified object. - Throws:
NullPointerException- If the specifiedURIReferenceobject isnull.
-
resolve
public URIReference resolve(String uriRef)
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- IfuriRefisnull.IllegalStateException- If this URI reference is not an absolute URI.
-
resolve
public URIReference resolve(URIReference uriRef)
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- Ifstrisnull.IllegalStateException- If this URI reference is not an absolute URI.
-
normalize
public URIReference normalize()
Normalizes this URI reference.This method does not modify the state of the original
URIReferenceinstance on which this method is called. Instead, it creates a newURIReferenceinstance and initializes it with the information about the normalized URI reference.Note that this method throws an
IllegalStateExceptionif this URI reference has not been resolved yet.- Returns:
- A new
URIReferenceinstance representing the normalized URI reference. - Throws:
IllegalStateException- If this URI reference has not been resolved yet.
-
-