. Every 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.
- Version:
- $Id: URI.java,v 1.17 2004/07/13 14:50:24 chris-dollin Exp $
|
Field Summary |
static int |
ABSOLUTE
|
private static boolean |
DEBUG
|
(package private) static java.lang.String[] |
exact
|
static int |
GRANDPARENT
|
private boolean |
haveCheckedNFC
|
private boolean |
isNFC
|
(package private) static org.apache.commons.logging.Log |
logger
|
private java.lang.String |
m_fragment
If specified, stores the fragment for this URI; otherwise null |
private java.lang.String |
m_host
If specified, stores the host for this URI; otherwise null |
private java.lang.String |
m_path
If specified, stores the path for this URI; otherwise null |
private java.lang.String |
m_port
If specified, stores the port for this URI; otherwise null |
private java.lang.String |
m_queryString
If specified, stores the query string for this URI; otherwise
null. |
private java.lang.String |
m_scheme
Stores the scheme (usually the protocol) for this URI. |
private java.lang.String[] |
m_subPaths
Lazily assigned variable being: ??
{ m_path +"a", dir, parent-dir, grand-parent-dir }
Each component is lazily assigned too. |
private java.lang.String |
m_userinfo
If specified, stores the userinfo for this URI; otherwise null |
private static java.lang.String |
MARK_CHARACTERS
URI punctuation mark characters - these, combined with
alphanumerics, constitute the "unreserved" characters |
private int |
n_port
|
static int |
NETWORK
|
static int |
PARENT
|
private static int[][] |
prefs
|
static int |
RELATIVE
|
private static java.lang.String |
RESERVED_CHARACTERS
reserved characters |
static int |
SAMEDOCUMENT
|
private static java.lang.String |
SCHEME_CHARACTERS
scheme can be composed of alphanumerics and these characters |
(package private) static java.lang.String[] |
sub
|
private static java.lang.String |
USERINFO_CHARACTERS
userinfo can be composed of unreserved, escaped and these
characters |
|
Constructor Summary |
URI()
Construct a new and uninitialized URI. |
URI(java.lang.String p_uriSpec)
Construct a new URI from a URI specification string. |
URI(java.lang.String p_scheme,
java.lang.String p_schemeSpecificPart)
Construct a new URI that does not follow the generic URI syntax. |
URI(java.lang.String p_scheme,
java.lang.String p_userinfo,
java.lang.String p_host,
int p_port,
java.lang.String p_path,
java.lang.String p_queryString,
java.lang.String p_fragment)
Construct a new URI that follows the generic URI syntax from its
component parts. |
URI(java.lang.String p_scheme,
java.lang.String p_host,
java.lang.String p_path,
java.lang.String p_queryString,
java.lang.String p_fragment)
Construct a new URI that follows the generic URI syntax from its
component parts. |
URI(URI p_other)
Construct a new URI from another URI. |
URI(URI p_base,
java.lang.String p_uriSpec)
Construct a new URI from a base URI and a URI specification string. |
|
Method Summary |
private void |
appendPath(java.lang.String p_addToPath)
Append to the end of the path of this URI. |
private boolean |
equal(java.lang.String s1,
java.lang.String s2)
|
boolean |
equals(java.lang.Object p_test)
Determines if the passed-in Object is equivalent to this URI. |
java.lang.String |
getFragment()
Get the fragment for this URI. |
java.lang.String |
getHost()
Get the host for this URI. |
private static java.lang.String |
getLastSlash(java.lang.String s)
|
java.lang.String |
getPath()
Get the path for this URI. |
java.lang.String |
getPath(boolean p_includeQueryString,
boolean p_includeFragment)
Get the path for this URI (optionally with the query string and
fragment). |
int |
getPort()
Get the port for this URI. |
java.lang.String |
getQueryString()
Get the query string for this URI. |
java.lang.String |
getScheme()
Get the scheme for this URI. |
java.lang.String |
getSchemeSpecificPart()
Get the scheme-specific part for this URI (everything following the
scheme and the first colon). |
java.lang.String |
getURIString()
Get the URI as a string specification. |
java.lang.String |
getUserinfo()
Get the userinfo for this URI. |
int |
hashCode()
Get a value that represents this Object, as uniquely as
possible within the confines of an int. |
private void |
initialize(URI p_other)
Initialize all fields of this URI from another URI. |
private void |
initialize(URI p_base,
java.lang.String p_uriSpec)
Initializes this URI from a base URI and a URI specification string. |
private void |
initializeAuthority(java.lang.String p_uriSpec)
Initialize the authority (userinfo, host and port) for this
URI from a URI string spec. |
private void |
initializePath(java.lang.String p_uriSpec)
Initialize the path for this URI from a URI string spec. |
private void |
initializeScheme(java.lang.String p_uriSpec)
Initialize the scheme for this URI from a URI string spec. |
private static boolean |
isAlpha(char p_char)
Determine whether a char is an alphabetic character: a-z or A-Z |
private static boolean |
isAlphanum(char p_char)
Determine whether a char is an alphanumeric: 0-9, a-z or A-Z |
static boolean |
isConformantSchemeName(java.lang.String p_scheme)
Determine whether a scheme conforms to the rules for a scheme name. |
private static boolean |
isDigit(char p_char)
Determine whether a char is a digit. |
boolean |
isGenericURI()
Get the indicator as to whether this URI uses the "generic URI"
syntax. |
private static boolean |
isHex(char p_char)
Determine whether a character is a hexadecimal character. |
boolean |
isNormalFormC()
|
private static boolean |
isReservedCharacter(char p_char)
Determine whether a character is a reserved character:
';', '/', '?', ':', '@', '&', '=', '+', '$' or ',' |
private static boolean |
isUnreservedCharacter(char p_char)
Determine whether a char is an unreserved character. |
private static boolean |
isURIString(java.lang.String p_uric)
Determine whether a given string contains only URI characters (also
called "uric" in RFC 2396). |
static boolean |
isWellFormedAddress(java.lang.String p_address)
Determine whether a string is syntactically capable of representing
a valid IPv4 address or the domain name of a network host. |
java.lang.String |
relativize(java.lang.String abs,
int flags)
|
void |
setFragment(java.lang.String p_fragment)
Set the fragment for this URI. |
private void |
setHost(java.lang.String p_host)
Set the host for this URI. |
private void |
setPath(java.lang.String p_path)
Set the path for this URI. |
private void |
setPort(int p_port)
Set the port for this URI. |
private void |
setQueryString(java.lang.String p_queryString)
Set the query string for this URI. |
private void |
setScheme(java.lang.String p_scheme)
Set the scheme for this URI. |
private void |
setUserinfo(java.lang.String p_userinfo)
Set the userinfo for this URI. |
java.lang.String |
toString()
produce a human-consumable string for the URI |
logger
static org.apache.commons.logging.Log logger
RESERVED_CHARACTERS
private static final java.lang.String RESERVED_CHARACTERS
- reserved characters
- See Also:
- Constant Field Values
MARK_CHARACTERS
private static final java.lang.String MARK_CHARACTERS
- URI punctuation mark characters - these, combined with
alphanumerics, constitute the "unreserved" characters
- See Also:
- Constant Field Values
SCHEME_CHARACTERS
private static final java.lang.String SCHEME_CHARACTERS
- scheme can be composed of alphanumerics and these characters
- See Also:
- Constant Field Values
USERINFO_CHARACTERS
private static final java.lang.String USERINFO_CHARACTERS
- userinfo can be composed of unreserved, escaped and these
characters
- See Also:
- Constant Field Values
m_scheme
private java.lang.String m_scheme
- Stores the scheme (usually the protocol) for this URI.
m_userinfo
private java.lang.String m_userinfo
- If specified, stores the userinfo for this URI; otherwise null
m_host
private java.lang.String m_host
- If specified, stores the host for this URI; otherwise null
m_port
private java.lang.String m_port
- If specified, stores the port for this URI; otherwise null
n_port
private int n_port
m_path
private java.lang.String m_path
- If specified, stores the path for this URI; otherwise null
m_subPaths
private java.lang.String[] m_subPaths
- Lazily assigned variable being: ??
{ m_path +"a", dir, parent-dir, grand-parent-dir }
Each component is lazily assigned too.
m_queryString
private java.lang.String m_queryString
- If specified, stores the query string for this URI; otherwise
null.
m_fragment
private java.lang.String m_fragment
- If specified, stores the fragment for this URI; otherwise null
DEBUG
private static boolean DEBUG
haveCheckedNFC
private boolean haveCheckedNFC
isNFC
private boolean isNFC
SAMEDOCUMENT
public static final int SAMEDOCUMENT
- See Also:
- Constant Field Values
NETWORK
public static final int NETWORK
- See Also:
- Constant Field Values
ABSOLUTE
public static final int ABSOLUTE
- See Also:
- Constant Field Values
RELATIVE
public static final int RELATIVE
- See Also:
- Constant Field Values
PARENT
public static final int PARENT
- See Also:
- Constant Field Values
GRANDPARENT
public static final int GRANDPARENT
- See Also:
- Constant Field Values
prefs
private static int[][] prefs
exact
static java.lang.String[] exact
sub
static java.lang.String[] sub
URI
public URI()
- Construct a new and uninitialized URI.
URI
public URI(URI p_other)
- Construct a new URI from another URI. All fields for this URI are
set equal to the fields of the URI passed in.
URI
public URI(java.lang.String p_uriSpec)
throws MalformedURIException
- Construct a new URI from a URI specification string. If the
specification follows the "generic URI" syntax, (two slashes
following the first colon), the specification will be parsed
accordingly - setting the scheme, userinfo, host,port, path, query
string and fragment fields as necessary. If the specification does
not follow the "generic URI" syntax, the specification is parsed
into a scheme and scheme-specific part (stored as the path) only.
URI
public URI(URI p_base,
java.lang.String p_uriSpec)
throws MalformedURIException
- Construct a new URI from a base URI and a URI specification string.
The URI specification string may be a relative URI.
URI
public URI(java.lang.String p_scheme,
java.lang.String p_schemeSpecificPart)
throws MalformedURIException
- Construct a new URI that does not follow the generic URI syntax.
Only the scheme and scheme-specific part (stored as the path) are
initialized.
URI
public URI(java.lang.String p_scheme,
java.lang.String p_host,
java.lang.String p_path,
java.lang.String p_queryString,
java.lang.String p_fragment)
throws MalformedURIException
- Construct a new URI that follows the generic URI syntax from its
component parts. Each component is validated for syntax and some
basic semantic checks are performed as well. See the individual
setter methods for specifics.
URI
public URI(java.lang.String p_scheme,
java.lang.String p_userinfo,
java.lang.String p_host,
int p_port,
java.lang.String p_path,
java.lang.String p_queryString,
java.lang.String p_fragment)
throws MalformedURIException
- Construct a new URI that follows the generic URI syntax from its
component parts. Each component is validated for syntax and some
basic semantic checks are performed as well. See the individual
setter methods for specifics.
initialize
private void initialize(URI p_other)
- Initialize all fields of this URI from another URI.
initialize
private void initialize(URI p_base,
java.lang.String p_uriSpec)
throws MalformedURIException
- Initializes this URI from a base URI and a URI specification string.
See RFC 2396 Section 4 and Appendix B for specifications on parsing
the URI and Section 5 for specifications on resolving relative URIs
and relative paths.
initializeScheme
private void initializeScheme(java.lang.String p_uriSpec)
throws MalformedURIException
- Initialize the scheme for this URI from a URI string spec.
initializeAuthority
private void initializeAuthority(java.lang.String p_uriSpec)
throws MalformedURIException
- Initialize the authority (userinfo, host and port) for this
URI from a URI string spec.
initializePath
private void initializePath(java.lang.String p_uriSpec)
throws MalformedURIException
- Initialize the path for this URI from a URI string spec.
getScheme
public java.lang.String getScheme()
- Get the scheme for this URI.
getSchemeSpecificPart
public java.lang.String getSchemeSpecificPart()
- Get the scheme-specific part for this URI (everything following the
scheme and the first colon). See RFC 2396 Section 5.2 for spec.
getUserinfo
public java.lang.String getUserinfo()
- Get the userinfo for this URI.
getHost
public java.lang.String getHost()
- Get the host for this URI.
getPort
public int getPort()
- Get the port for this URI.
getPath
public java.lang.String getPath(boolean p_includeQueryString,
boolean p_includeFragment)
- Get the path for this URI (optionally with the query string and
fragment).
getPath
public java.lang.String getPath()
- Get the path for this URI. Note that the value returned is the path
only and does not include the query string or fragment.
getQueryString
public java.lang.String getQueryString()
- Get the query string for this URI.
getFragment
public java.lang.String getFragment()
- Get the fragment for this URI.
setScheme
private void setScheme(java.lang.String p_scheme)
throws MalformedURIException
- Set the scheme for this URI. The scheme is converted to lowercase
before it is set.
setUserinfo
private void setUserinfo(java.lang.String p_userinfo)
throws MalformedURIException
- Set the userinfo for this URI. If a non-null value is passed in and
the host value is null, then an exception is thrown.
setHost
private void setHost(java.lang.String p_host)
throws 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.
setPort
private void setPort(int p_port)
throws MalformedURIException
- 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.
If a valid port number is passed in and the host field is null,
an exception is thrown.
setPath
private void setPath(java.lang.String p_path)
throws MalformedURIException
- Set the path for this URI. If the supplied path is null, then the
query string and fragment are set to null as well. If the supplied
path includes a query string and/or fragment, these fields will be
parsed and set as well. Note that, for URIs following the "generic
URI" syntax, the path specified should start with a slash.
For URIs that do not follow the generic URI syntax, this method
sets the scheme-specific part.
appendPath
private void appendPath(java.lang.String p_addToPath)
throws MalformedURIException
- Append to the end of the path of this URI. If the current path does
not end in a slash and the path to be appended does not begin with
a slash, a slash will be appended to the current path before the
new segment is added. Also, if the current path ends in a slash
and the new segment begins with a slash, the extra slash will be
removed before the new segment is appended.
setQueryString
private void setQueryString(java.lang.String p_queryString)
throws MalformedURIException
- Set the query string for this URI. A non-null value is valid only
if this is an URI conforming to the generic URI syntax and
the path value is not null.
setFragment
public void setFragment(java.lang.String p_fragment)
throws MalformedURIException
- Set the fragment for this URI. A non-null value is valid only
if this is a URI conforming to the generic URI syntax and
the path value is not null.
equals
public boolean equals(java.lang.Object p_test)
- Determines if the passed-in Object is equivalent to this URI.
toString
public java.lang.String toString()
- produce a human-consumable string for the URI
getURIString
public java.lang.String getURIString()
- Get the URI as a string specification. See RFC 2396 Section 5.2.
hashCode
public int hashCode()
- Description copied from class:
java.lang.Object
- Get a value that represents this Object, as uniquely as
possible within the confines of an int.
There are some requirements on this method which
subclasses must follow:
- Semantic equality implies identical hashcodes. In other
words, if
a.equals(b) is true, then
a.hashCode() == b.hashCode() must be as well.
However, the reverse is not necessarily true, and two
objects may have the same hashcode without being equal.
- It must be consistent. Whichever value o.hashCode()
returns on the first invocation must be the value
returned on all later invocations as long as the object
exists. Notice, however, that the result of hashCode may
change between separate executions of a Virtual Machine,
because it is not invoked on the same object.
Notice that since hashCode is used in
java.util.Hashtable and other hashing classes,
a poor implementation will degrade the performance of hashing
(so don't blindly implement it as returning a constant!). Also,
if calculating the hash is time-consuming, a class may consider
caching the results.
The default implementation returns
System.identityHashCode(this)
isGenericURI
public boolean isGenericURI()
- Get the indicator as to whether this URI uses the "generic URI"
syntax.
isConformantSchemeName
public static boolean isConformantSchemeName(java.lang.String p_scheme)
- Determine whether a scheme conforms to the rules for a scheme name.
A scheme is conformant if it starts with an alphanumeric, and
contains only alphanumerics, '+','-' and '.'.
isWellFormedAddress
public static boolean isWellFormedAddress(java.lang.String p_address)
- Determine whether a string is syntactically capable of representing
a valid IPv4 address or the domain name of a network host. A valid
IPv4 address consists of four decimal digit groups separated by a
'.'. A hostname consists of domain labels (each of which must
begin and end with an alphanumeric but may contain '-') separated
& by a '.'. See RFC 2396 Section 3.2.2.
isDigit
private static boolean isDigit(char p_char)
- Determine whether a char is a digit.
isHex
private static boolean isHex(char p_char)
- Determine whether a character is a hexadecimal character.
isAlpha
private static boolean isAlpha(char p_char)
- Determine whether a char is an alphabetic character: a-z or A-Z
isAlphanum
private static boolean isAlphanum(char p_char)
- Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
isReservedCharacter
private static boolean isReservedCharacter(char p_char)
- Determine whether a character is a reserved character:
';', '/', '?', ':', '@', '&', '=', '+', '$' or ','
isUnreservedCharacter
private static boolean isUnreservedCharacter(char p_char)
- Determine whether a char is an unreserved character.
isNormalFormC
public boolean isNormalFormC()
isURIString
private static boolean isURIString(java.lang.String p_uric)
- Determine whether a given string contains only URI characters (also
called "uric" in RFC 2396). uric consist of all reserved
characters, unreserved characters and escaped characters.
equal
private boolean equal(java.lang.String s1,
java.lang.String s2)
relativize
public java.lang.String relativize(java.lang.String abs,
int flags)
throws MalformedURIException
getLastSlash
private static java.lang.String getLastSlash(java.lang.String s)