java.net
Class URI
- Comparable<T>, Serializable
A URI instance represents that defined by
RFC3986,
with some deviations.
At its highest level, a URI consists of:
[scheme:]scheme-specific-part
[#fragment]
where
# and
: are literal characters,
and those parts enclosed in square brackets are optional.
There are two main types of URI. An
opaque URI is one
which just consists of the above three parts, and is not further
defined. An example of such a URI would be
mailto: URI.
In contrast,
hierarchical URIs give further definition
to the scheme-specific part, so as represent some part of a hierarchical
structure.
[//authority][path]
[?query]
with
/ and
? being literal characters.
When server-based, the authority section is further subdivided into:
[user-info@]host
[:port]
with
@ and
: as literal characters.
Authority sections that are not server-based are said to be registry-based.
Hierarchical URIs can be either relative or absolute. Absolute URIs
always start with a `
/', while relative URIs don't
specify a scheme. Opaque URIs are always absolute.
Each part of the URI may have one of three states: undefined, empty
or containing some content. The former two of these are represented
by
null
and the empty string in Java, respectively.
The scheme-specific part may never be undefined. It also follows from
this that the path sub-part may also not be undefined, so as to ensure
the former.
Character Escaping and Quoting
The characters that can be used within a valid URI are restricted.
There are two main classes of characters which can't be used as is
within the URI:
- Characters outside the US-ASCII character set.
These have to be escaped in order to create
an RFC-compliant URI; this means replacing the character with the
appropriate hexadecimal value, preceded by a `%'.
- Illegal characters (e.g. space characters,
control characters) are quoted, which results in them being encoded
in the same way as non-US-ASCII characters.
The set of valid characters differs depending on the section of the URI:
- Scheme: Must be an alphanumeric, `-', `.' or '+'.
- Authority:Composed of the username, host, port, `@'
and `:'.
- Username: Allows unreserved or percent-encoded
characters, sub-delimiters and `:'.
- Host: Allows unreserved or percent-encoded
characters, sub-delimiters and square brackets (`[' and `]') for IPv6
addresses.
- Port: Digits only.
- Path: Allows the path characters and `/'.
- Query: Allows the path characters, `?' and '/'.
- Fragment: Allows the path characters, `?' and '/'.
These definitions reference the following sets of characters:
- Unreserved characters: The alphanumerics plus
`-', `.', `_', and `~'.
- Sub-delimiters: `!', `$', `&', `(', `)', `*',
`+', `,', `;', `=' and the single-quote itself.
- Path characters: Unreserved and percent-encoded
characters and the sub-delimiters along with `@' and `:'.
The constructors and accessor methods allow the use and retrieval of
URI components which contain non-US-ASCII characters directly.
They are only escaped when the
toASCIIString()
method
is used. In contrast, illegal characters are always quoted, with the
exception of the return values of the non-raw accessors.
URI(String str) - Creates an URI from the given string
|
URI(String scheme, String ssp, String fragment) - Create an URI from the given components
|
URI(String scheme, String userInfo, String host, int port, String path, String query, String fragment) - Create an URI from the given components
|
URI(String scheme, String host, String path, String fragment) - Create an URI from the given components
|
URI(String scheme, String authority, String path, String query, String fragment) - Create an URI from the given components
|
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
URI
public URI(String scheme,
String ssp,
String fragment)
throws URISyntaxException
Create an URI from the given components
scheme
- The scheme namessp
- The scheme specific partfragment
- The fragment
URI
public URI(String scheme,
String userInfo,
String host,
int port,
String path,
String query,
String fragment)
throws URISyntaxException
Create an URI from the given components
scheme
- The scheme nameuserInfo
- The username and authorization infohost
- The hostnameport
- The port numberpath
- The pathquery
- The queryfragment
- The fragment
URI
public URI(String scheme,
String authority,
String path,
String query,
String fragment)
throws URISyntaxException
Create an URI from the given components
scheme
- The scheme nameauthority
- The authoritypath
- The apthquery
- The queryfragment
- The fragment
compareTo
public int compareTo(URI uri)
throws ClassCastException
Compare the URI with another URI.
Undefined components are taken to be less than any other component.
The following criteria are observed:
- Two URIs with different schemes are compared according to their
scheme, regardless of case.
- A hierarchical URI is less than an opaque URI with the same
scheme.
- For opaque URIs:
- URIs with differing scheme-specific parts are ordered according
to the ordering of the scheme-specific part.
- URIs with the same scheme-specific part are ordered by the
raw fragment.
For hierarchical URIs:
- URIs are ordered according to their raw authority sections,
if they are unequal.
- For registry-based authorities:
- they are ordered according to the ordering of the authority
component.
For server-based authorities:
- URIs are ordered according to the raw user information.
- URIs with the same user information are ordered by the host,
ignoring case.
- URIs with the same host are ordered by the port.
URIs with the same authority section are ordered by the raw path.
URIs with the same path are ordered by their raw query.
URIs with the same query are ordered by their raw fragments.
uri
- The other URI to compare this URI with
- a negative integer, zero or a positive integer depending
on whether this URI is less than, equal to or greater
than that supplied, respectively.
create
public static URI create(String str)
Create an URI from the given string
str
- The string to create the URI from
equals
public boolean equals(Object obj)
Compares the URI with the given object for equality. If the
object is not a
URI
, then the method returns false.
Otherwise, the following criteria are observed:
- The scheme of the URIs must either be null (undefined) in both cases,
or equal, ignorant of case.
- The raw fragment of the URIs must either be null (undefined) in both
cases, or equal, ignorant of case.
- Both URIs must be of the same type (opaque or hierarchial)
- For opaque URIs:
- The raw scheme-specific parts must be equal.
For hierarchical URIs:
- The raw paths must be equal, ignorant of case.
- The raw queries are either both undefined or both equal, ignorant
of case.
- The raw authority sections are either both undefined or:
- For registry-based authorities:
For server-based authorities:
- the hosts are equal, ignoring case
- the ports are equal
- the user information components are equal
- equals in interface Object
obj
- the obj to compare the URI with.
true
if the objects are equal, according to
the specification above.
getPort
public int getPort()
Returns the port number of the URI
getRawSchemeSpecificPart
public String getRawSchemeSpecificPart()
Returns the raw scheme specific part of this URI.
The scheme-specific part is never undefined, though it may be empty
getUserInfo
public String getUserInfo()
Returns the decoded user info part of this URI
isAbsolute
public boolean isAbsolute()
Tells whether this URI is absolute or not
isOpaque
public boolean isOpaque()
Tell whether this URI is opaque or not
normalize
public URI normalize()
Returns a normalized version of the URI. If the URI is opaque,
or its path is already in normal form, then this URI is simply
returned. Otherwise, the following transformation of the path
element takes place:
- All `.' segments are removed.
- Each `..' segment which can be paired with a prior non-`..' segment
is removed along with the preceding segment.
- A `.' segment is added to the front if the first segment contains
a colon (`:'). This is a deviation from the RFC, which prevents
confusion between the path and the scheme.
The resulting URI will be free of `.' and `..' segments, barring those
that were prepended or which couldn't be paired, respectively.
parseServerAuthority
public URI parseServerAuthority()
throws URISyntaxException
Attempts to parse this URI's authority component, if defined,
into user-information, host, and port components. The purpose
of this method was to disambiguate between some authority sections,
which form invalid server-based authories, but valid registry
based authorities. In the updated RFC 3986, the authority section
is defined differently, with registry-based authorities part of
the host section. Thus, this method is now simply an explicit
way of parsing any authority section.
- the URI, with the authority section parsed into user
information, host and port components.
relativize
public URI relativize(URI uri)
Relativizes the given URI against this URI. The following
algorithm is used:
- If either URI is opaque, the given URI is returned.
- If the schemes of the URIs differ, the given URI is returned.
- If the authority components of the URIs differ, then the given
URI is returned.
- If the path of this URI is not a prefix of the supplied URI,
then the given URI is returned.
- If all the above conditions hold, a new URI is created using the
query and fragment components of the given URI, along with a path
computed by removing the path of this URI from the start of the path
of the supplied URI.
uri
- the URI to relativize agsint this URI
resolve
public URI resolve(URI uri)
Resolves the given URI against this URI
uri
- The URI to resolve against this URI
- The resulting URI, or null when it couldn't be resolved
for some reason.
toASCIIString
public String toASCIIString()
Returns the URI as US-ASCII string. This is the same as the result
from toString()
for URIs that don't contain any non-US-ASCII
characters. Otherwise, the non-US-ASCII characters are replaced
by their percent-encoded representations.
- a string representation of the URI, containing only US-ASCII
characters.
toString
public String toString()
Returns the URI as a String. If the URI was created using a constructor,
then this will be the same as the original input string.
- toString in interface Object
- a string representation of the URI.
URI.java -- An URI class
Copyright (C) 2002, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.