| Constructor: |
protected URI() {
}
Create an instance as an internal use |
public URI(char[] escaped) throws URIException, NullPointerException {
parseUriReference(new String(escaped), true);
}
Construct a URI as an escaped form of a character array.
An URI can be placed within double-quotes or angle brackets like
"http://test.com/" and <http://test.com/> Parameters:
escaped - the URI character sequence
Throws:
URIException - If the URI cannot be created.
NullPointerException - if escaped is null
Also see:
- getDefaultProtocolCharset
|
public URI(String original) throws URIException {
parseUriReference(original, false);
}
Construct a URI from the given string.
URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
An URI can be placed within double-quotes or angle brackets like
"http://test.com/" and <http://test.com/> Parameters:
original - the string to be represented to URI character sequence
It is one of absoluteURI and relativeURI.
Throws:
URIException - If the URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(String s,
boolean escaped) throws URIException, NullPointerException {
parseUriReference(s, escaped);
}
Construct a URI from a string with the given charset. The input string can
be either in escaped or unescaped form. Parameters:
s - URI character sequence
escaped - true if URI character sequence is in escaped form.
false otherwise.
Throws:
URIException - If the URI cannot be created.
NullPointerException - if input string is null
Also see:
- getProtocolCharset
- since:
3.0 -
|
public URI(char[] escaped,
String charset) throws URIException, NullPointerException {
protocolCharset = charset;
parseUriReference(new String(escaped), true);
}
Construct a URI as an escaped form of a character array with the given
charset. Parameters:
escaped - the URI character sequence
charset - the charset string to do escape encoding
Throws:
URIException - If the URI cannot be created.
NullPointerException - if escaped is null
Also see:
- getProtocolCharset
|
public URI(String original,
String charset) throws URIException {
protocolCharset = charset;
parseUriReference(original, false);
}
Construct a URI from the given string with the given charset. Parameters:
original - the string to be represented to URI character sequence
It is one of absoluteURI and relativeURI.
charset - the charset string to do escape encoding
Throws:
URIException - If the URI cannot be created.
Also see:
- getProtocolCharset
|
public URI(URI base,
String relative) throws URIException {
this(base, new URI(relative));
}
Construct a general URI with the given relative URI string. Parameters:
base - the base URI
relative - the relative URI string
Throws:
URIException - If the new URI cannot be created.
|
public URI(URI base,
URI relative) throws URIException {
if (base._scheme == null) {
throw new URIException(URIException.PARSING, "base URI required");
}
if (base._scheme != null) {
this._scheme = base._scheme;
this._authority = base._authority;
this._is_net_path = base._is_net_path;
}
if (base._is_opaque_part || relative._is_opaque_part) {
this._scheme = base._scheme;
this._is_opaque_part = base._is_opaque_part
|| relative._is_opaque_part;
this._opaque = relative._opaque;
this._fragment = relative._fragment;
this.setURI();
return;
}
boolean schemesEqual = Arrays.equals(base._scheme,relative._scheme);
if (relative._scheme != null
&& (!schemesEqual || relative._authority != null)) {
this._scheme = relative._scheme;
this._is_net_path = relative._is_net_path;
this._authority = relative._authority;
if (relative._is_server) {
this._is_server = relative._is_server;
this._userinfo = relative._userinfo;
this._host = relative._host;
this._port = relative._port;
} else if (relative._is_reg_name) {
this._is_reg_name = relative._is_reg_name;
}
this._is_abs_path = relative._is_abs_path;
this._is_rel_path = relative._is_rel_path;
this._path = relative._path;
} else if (base._authority != null && relative._scheme == null) {
this._is_net_path = base._is_net_path;
this._authority = base._authority;
if (base._is_server) {
this._is_server = base._is_server;
this._userinfo = base._userinfo;
this._host = base._host;
this._port = base._port;
} else if (base._is_reg_name) {
this._is_reg_name = base._is_reg_name;
}
}
if (relative._authority != null) {
this._is_net_path = relative._is_net_path;
this._authority = relative._authority;
if (relative._is_server) {
this._is_server = relative._is_server;
this._userinfo = relative._userinfo;
this._host = relative._host;
this._port = relative._port;
} else if (relative._is_reg_name) {
this._is_reg_name = relative._is_reg_name;
}
this._is_abs_path = relative._is_abs_path;
this._is_rel_path = relative._is_rel_path;
this._path = relative._path;
}
// resolve the path and query if necessary
if (relative._authority == null
&& (relative._scheme == null || schemesEqual)) {
if ((relative._path == null || relative._path.length == 0)
&& relative._query == null) {
// handle a reference to the current document, see RFC 2396
// section 5.2 step 2
this._path = base._path;
this._query = base._query;
} else {
this._path = resolvePath(base._path, relative._path);
}
}
// base._query removed
if (relative._query != null) {
this._query = relative._query;
}
// base._fragment removed
if (relative._fragment != null) {
this._fragment = relative._fragment;
}
this.setURI();
// reparse the newly built URI, this will ensure that all flags are set correctly.
// TODO there must be a better way to do this
parseUriReference(new String(_uri), true);
}
Construct a general URI with the given relative URI.
URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
Resolving Relative References to Absolute Form.
Examples of Resolving Relative URI References
Within an object with a well-defined base URI of
http://a/b/c/d;p?q
the relative URI would be resolved as follows:
Normal Examples
g:h = g:h
g = http://a/b/c/g
./g = http://a/b/c/g
g/ = http://a/b/c/g/
/g = http://a/g
//g = http://g
?y = http://a/b/c/?y
g?y = http://a/b/c/g?y
#s = (current document)#s
g#s = http://a/b/c/g#s
g?y#s = http://a/b/c/g?y#s
;x = http://a/b/c/;x
g;x = http://a/b/c/g;x
g;x?y#s = http://a/b/c/g;x?y#s
. = http://a/b/c/
./ = http://a/b/c/
.. = http://a/b/
../ = http://a/b/
../g = http://a/b/g
../.. = http://a/
../../ = http://a/
../../g = http://a/g
Some URI schemes do not allow a hierarchical syntax matching the
syntax, and thus cannot use relative references. Parameters:
base - the base URI
relative - the relative URI
Throws:
URIException - If the new URI cannot be created.
|
public URI(String s,
boolean escaped,
String charset) throws URIException, NullPointerException {
protocolCharset = charset;
parseUriReference(s, escaped);
}
Construct a URI from a string with the given charset. The input string can
be either in escaped or unescaped form. Parameters:
s - URI character sequence
escaped - true if URI character sequence is in escaped form.
false otherwise.
charset - the charset string to do escape encoding, if required
Throws:
URIException - If the URI cannot be created.
NullPointerException - if input string is null
Also see:
- getProtocolCharset
- since:
3.0 -
|
public URI(String scheme,
String schemeSpecificPart,
String fragment) throws URIException {
// validate and contruct the URI character sequence
if (scheme == null) {
throw new URIException(URIException.PARSING, "scheme required");
}
char[] s = scheme.toLowerCase().toCharArray();
if (validate(s, URI.scheme)) {
_scheme = s; // is_absoluteURI
} else {
throw new URIException(URIException.PARSING, "incorrect scheme");
}
_opaque = encode(schemeSpecificPart, allowed_opaque_part,
getProtocolCharset());
// Set flag
_is_opaque_part = true;
_fragment = fragment == null ? null : fragment.toCharArray();
setURI();
}
Construct a general URI from the given components.
URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
absoluteURI = scheme ":" ( hier_part | opaque_part )
opaque_part = uric_no_slash *uric
It's for absolute URI = <scheme>:<scheme-specific-part>#
<fragment>. Parameters:
scheme - the scheme string
schemeSpecificPart - scheme_specific_part
fragment - the fragment string
Throws:
URIException - If the URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(URI base,
String relative,
boolean escaped) throws URIException {
this(base, new URI(relative, escaped));
}
Construct a general URI with the given relative URI string. Parameters:
base - the base URI
relative - the relative URI string
escaped - true if URI character sequence is in escaped form.
false otherwise.
Throws:
URIException - If the new URI cannot be created.
- since:
3.0 -
|
public URI(String scheme,
String userinfo,
String host,
int port) throws URIException {
this(scheme, userinfo, host, port, null, null, null);
}
Construct a general URI from the given components. Parameters:
scheme - the scheme string
userinfo - the userinfo string
host - the host string
port - the port number
Throws:
URIException - If the new URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(String scheme,
String host,
String path,
String fragment) throws URIException {
this(scheme, host, path, null, fragment);
}
Construct a general URI from the given components. Parameters:
scheme - the scheme string
host - the host string
path - the path string
fragment - the fragment string
Throws:
URIException - If the new URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(String scheme,
String authority,
String path,
String query,
String fragment) throws URIException {
// validate and contruct the URI character sequence
StringBuffer buff = new StringBuffer();
if (scheme != null) {
buff.append(scheme);
buff.append(':");
}
if (authority != null) {
buff.append("//");
buff.append(authority);
}
if (path != null) { // accept empty path
if ((scheme != null || authority != null)
&& !path.startsWith("/")) {
throw new URIException(URIException.PARSING,
"abs_path requested");
}
buff.append(path);
}
if (query != null) {
buff.append('?");
buff.append(query);
}
if (fragment != null) {
buff.append('#");
buff.append(fragment);
}
parseUriReference(buff.toString(), false);
}
Construct a general URI from the given components.
URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
absoluteURI = scheme ":" ( hier_part | opaque_part )
relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
hier_part = ( net_path | abs_path ) [ "?" query ]
It's for absolute URI = <scheme>:<path>?<query>#<
fragment> and relative URI = <path>?<query>#<fragment
>. Parameters:
scheme - the scheme string
authority - the authority string
path - the path string
query - the query string
fragment - the fragment string
Throws:
URIException - If the new URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(String scheme,
String userinfo,
String host,
int port,
String path) throws URIException {
this(scheme, userinfo, host, port, path, null, null);
}
Construct a general URI from the given components. Parameters:
scheme - the scheme string
userinfo - the userinfo string
host - the host string
port - the port number
path - the path string
Throws:
URIException - If the new URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(String scheme,
String userinfo,
String host,
int port,
String path,
String query) throws URIException {
this(scheme, userinfo, host, port, path, query, null);
}
Construct a general URI from the given components. Parameters:
scheme - the scheme string
userinfo - the userinfo string
host - the host string
port - the port number
path - the path string
query - the query string
Throws:
URIException - If the new URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
public URI(String scheme,
String userinfo,
String host,
int port,
String path,
String query,
String fragment) throws URIException {
this(scheme, (host == null) ? null
: ((userinfo != null) ? userinfo + '@" : "") + host
+ ((port != -1) ? ":" + port : ""), path, query, fragment);
}
Construct a general URI from the given components. Parameters:
scheme - the scheme string
userinfo - the userinfo string
host - the host string
port - the port number
path - the path string
query - the query string
fragment - the fragment string
Throws:
URIException - If the new URI cannot be created.
Also see:
- getDefaultProtocolCharset
|
| Method from org.apache.commons.httpclient.URI Detail: |
public synchronized Object clone() throws CloneNotSupportedException {
URI instance = (URI) super.clone();
instance._uri = _uri;
instance._scheme = _scheme;
instance._opaque = _opaque;
instance._authority = _authority;
instance._userinfo = _userinfo;
instance._host = _host;
instance._port = _port;
instance._path = _path;
instance._query = _query;
instance._fragment = _fragment;
// the charset to do escape encoding for this instance
instance.protocolCharset = protocolCharset;
// flags
instance._is_hier_part = _is_hier_part;
instance._is_opaque_part = _is_opaque_part;
instance._is_net_path = _is_net_path;
instance._is_abs_path = _is_abs_path;
instance._is_rel_path = _is_rel_path;
instance._is_reg_name = _is_reg_name;
instance._is_server = _is_server;
instance._is_hostname = _is_hostname;
instance._is_IPv4address = _is_IPv4address;
instance._is_IPv6reference = _is_IPv6reference;
return instance;
}
Create and return a copy of this object, the URI-reference containing
the userinfo component. Notice that the whole URI-reference including
the userinfo component counld not be gotten as a String.
To copy the identical URI object including the userinfo
component, it should be used. |
public int compareTo(Object obj) throws ClassCastException {
URI another = (URI) obj;
if (!equals(_authority, another.getRawAuthority())) {
return -1;
}
return toString().compareTo(another.toString());
}
Compare this URI to another object. |
protected static String decode(char[] component,
String charset) throws URIException {
if (component == null) {
throw new IllegalArgumentException("Component array of chars may not be null");
}
return decode(new String(component), charset);
}
Decodes URI encoded string.
This is a two mapping, one from URI characters to octets, and
subsequently a second from octets to original characters:
URI character sequence->octet sequence->original character sequence
A URI must be separated into its components before the escaped
characters within those components can be allowedly decoded.
Notice that there is a chance that URI characters that are non UTF-8
may be parsed as valid UTF-8. A recent non-scientific analysis found
that EUC encoded Japanese words had a 2.7% false reading; SJIS had a
0.0005% false reading; other encoding such as ASCII or KOI-8 have a 0%
false reading.
The percent "%" character always has the reserved purpose of being
the escape indicator, it must be escaped as "%25" in order to be used
as data within a URI.
The unescape method is internally performed within this method. |
protected static String decode(String component,
String charset) throws URIException {
if (component == null) {
throw new IllegalArgumentException("Component array of chars may not be null");
}
byte[] rawdata = null;
try {
rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(component));
} catch (DecoderException e) {
throw new URIException(e.getMessage());
}
return EncodingUtil.getString(rawdata, charset);
}
Decodes URI encoded string.
This is a two mapping, one from URI characters to octets, and
subsequently a second from octets to original characters:
URI character sequence->octet sequence->original character sequence
A URI must be separated into its components before the escaped
characters within those components can be allowedly decoded.
Notice that there is a chance that URI characters that are non UTF-8
may be parsed as valid UTF-8. A recent non-scientific analysis found
that EUC encoded Japanese words had a 2.7% false reading; SJIS had a
0.0005% false reading; other encoding such as ASCII or KOI-8 have a 0%
false reading.
The percent "%" character always has the reserved purpose of being
the escape indicator, it must be escaped as "%25" in order to be used
as data within a URI.
The unescape method is internally performed within this method. |
protected static char[] encode(String original,
BitSet allowed,
String charset) throws URIException {
if (original == null) {
throw new IllegalArgumentException("Original string may not be null");
}
if (allowed == null) {
throw new IllegalArgumentException("Allowed bitset may not be null");
}
byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtil.getBytes(original, charset));
return EncodingUtil.getAsciiString(rawdata).toCharArray();
}
Encodes URI string.
This is a two mapping, one from original characters to octets, and
subsequently a second from octets to URI characters:
original character sequence->octet sequence->URI character sequence
An escaped octet is encoded as a character triplet, consisting of the
percent character "%" followed by the two hexadecimal digits
representing the octet code. For example, "%20" is the escaped
encoding for the US-ASCII space character.
Conversion from the local filesystem character set to UTF-8 will
normally involve a two step process. First convert the local character
set to the UCS; then convert the UCS to UTF-8.
The first step in the process can be performed by maintaining a mapping
table that includes the local character set code and the corresponding
UCS code.
The next step is to convert the UCS character code to the UTF-8 encoding.
Mapping between vendor codepages can be done in a very similar manner
as described above.
The only time escape encodings can allowedly be made is when a URI is
being created from its component parts. The escape and validate methods
are internally performed within this method. |
public boolean equals(Object obj) {
// normalize and test each components
if (obj == this) {
return true;
}
if (!(obj instanceof URI)) {
return false;
}
URI another = (URI) obj;
// scheme
if (!equals(_scheme, another._scheme)) {
return false;
}
// is_opaque_part or is_hier_part? and opaque
if (!equals(_opaque, another._opaque)) {
return false;
}
// is_hier_part
// has_authority
if (!equals(_authority, another._authority)) {
return false;
}
// path
if (!equals(_path, another._path)) {
return false;
}
// has_query
if (!equals(_query, another._query)) {
return false;
}
// has_fragment? should be careful of the only fragment case.
if (!equals(_fragment, another._fragment)) {
return false;
}
return true;
}
Test an object if this URI is equal to another. |
protected boolean equals(char[] first,
char[] second) {
if (first == null && second == null) {
return true;
}
if (first == null || second == null) {
return false;
}
if (first.length != second.length) {
return false;
}
for (int i = 0; i < first.length; i++) {
if (first[i] != second[i]) {
return false;
}
}
return true;
}
Test if the first array is equal to the second array. |
public String getAboveHierPath() throws URIException {
char[] path = getRawAboveHierPath();
return (path == null) ? null : decode(path, getProtocolCharset());
}
Get the level above the this hierarchy level. |
public String getAuthority() throws URIException {
return (_authority == null) ? null : decode(_authority,
getProtocolCharset());
}
|
public String getCurrentHierPath() throws URIException {
char[] path = getRawCurrentHierPath();
return (path == null) ? null : decode(path, getProtocolCharset());
}
Get the current hierarchy level. |
public static String getDefaultDocumentCharset() {
return defaultDocumentCharset;
}
Get the recommended default charset of the document. |
public static String getDefaultDocumentCharsetByLocale() {
return defaultDocumentCharsetByLocale;
}
Get the default charset of the document by locale. |
public static String getDefaultDocumentCharsetByPlatform() {
return defaultDocumentCharsetByPlatform;
}
Get the default charset of the document by platform. |
public static String getDefaultProtocolCharset() {
return defaultProtocolCharset;
}
Get the default charset of the protocol.
An individual URI scheme may require a single charset, define a default
charset, or provide a way to indicate the charset used.
To work globally either requires support of a number of character sets
and to be able to convert between them, or the use of a single preferred
character set.
For support of global compatibility it is STRONGLY RECOMMENDED that
clients and servers use UTF-8 encoding when exchanging URIs. |
public String getEscapedAboveHierPath() throws URIException {
char[] path = getRawAboveHierPath();
return (path == null) ? null : new String(path);
}
Get the level above the this hierarchy level. |
public String getEscapedAuthority() {
return (_authority == null) ? null : new String(_authority);
}
Get the escaped authority. |
public String getEscapedCurrentHierPath() throws URIException {
char[] path = getRawCurrentHierPath();
return (path == null) ? null : new String(path);
}
Get the escaped current hierarchy level. |
public String getEscapedFragment() {
return (_fragment == null) ? null : new String(_fragment);
}
Get the escaped fragment. |
public String getEscapedName() {
char[] basename = getRawName();
return (basename == null) ? null : new String(basename);
}
Get the escaped basename of the path. |
public String getEscapedPath() {
char[] path = getRawPath();
return (path == null) ? null : new String(path);
}
|
public String getEscapedPathQuery() {
char[] rawPathQuery = getRawPathQuery();
return (rawPathQuery == null) ? null : new String(rawPathQuery);
}
|
public String getEscapedQuery() {
return (_query == null) ? null : new String(_query);
}
|
public String getEscapedURI() {
return (_uri == null) ? null : new String(_uri);
}
It can be gotten the URI character sequence. It's escaped.
For the purpose of the protocol to be transported, it will be useful. |
public String getEscapedURIReference() {
char[] uriReference = getRawURIReference();
return (uriReference == null) ? null : new String(uriReference);
}
Get the escaped URI reference string. |
public String getEscapedUserinfo() {
return (_userinfo == null) ? null : new String(_userinfo);
}
Get the escaped userinfo. |
public String getFragment() throws URIException {
return (_fragment == null) ? null : decode(_fragment,
getProtocolCharset());
}
|
public String getHost() throws URIException {
if (_host != null) {
return decode(_host, getProtocolCharset());
} else {
return null;
}
}
|
public String getName() throws URIException {
char[] basename = getRawName();
return (basename == null) ? null : decode(getRawName(),
getProtocolCharset());
}
Get the basename of the path. |
public String getPath() throws URIException {
char[] path = getRawPath();
return (path == null) ? null : decode(path, getProtocolCharset());
}
|
public String getPathQuery() throws URIException {
char[] rawPathQuery = getRawPathQuery();
return (rawPathQuery == null) ? null : decode(rawPathQuery,
getProtocolCharset());
}
|
public int getPort() {
return _port;
}
Get the port. In order to get the specfic default port, the specific
protocol-supported class extended from the URI class should be used.
It has the server-based naming authority. |
public String getProtocolCharset() {
return (protocolCharset != null)
? protocolCharset
: defaultProtocolCharset;
}
Get the protocol charset used by this current URI instance.
It was set by the constructor for this instance. If it was not set by
contructor, it will return the default protocol charset. |
public String getQuery() throws URIException {
return (_query == null) ? null : decode(_query, getProtocolCharset());
}
|
public char[] getRawAboveHierPath() throws URIException {
char[] path = getRawCurrentHierPath();
return (path == null) ? null : getRawCurrentHierPath(path);
}
Get the level above the this hierarchy level. |
public char[] getRawAuthority() {
return _authority;
}
Get the raw-escaped authority. |
public char[] getRawCurrentHierPath() throws URIException {
return (_path == null) ? null : getRawCurrentHierPath(_path);
}
Get the raw-escaped current hierarchy level. |
protected char[] getRawCurrentHierPath(char[] path) throws URIException {
if (_is_opaque_part) {
throw new URIException(URIException.PARSING, "no hierarchy level");
}
if (path == null) {
throw new URIException(URIException.PARSING, "empty path");
}
String buff = new String(path);
int first = buff.indexOf('/");
int last = buff.lastIndexOf('/");
if (last == 0) {
return rootPath;
} else if (first != last && last != -1) {
return buff.substring(0, last).toCharArray();
}
// FIXME: it could be a document on the server side
return path;
}
Get the raw-escaped current hierarchy level in the given path.
If the last namespace is a collection, the slash mark ('/') should be
ended with at the last character of the path string. |
public char[] getRawFragment() {
return _fragment;
}
Get the raw-escaped fragment.
The optional fragment identifier is not part of a URI, but is often used
in conjunction with a URI.
The format and interpretation of fragment identifiers is dependent on
the media type [RFC2046] of the retrieval result.
A fragment identifier is only meaningful when a URI reference is
intended for retrieval and the result of that retrieval is a document
for which the identified fragment is consistently defined. |
public char[] getRawHost() {
return _host;
}
|
public char[] getRawName() {
if (_path == null) {
return null;
}
int at = 0;
for (int i = _path.length - 1; i >= 0; i--) {
if (_path[i] == '/") {
at = i + 1;
break;
}
}
int len = _path.length - at;
char[] basename = new char[len];
System.arraycopy(_path, at, basename, 0, len);
return basename;
}
Get the raw-escaped basename of the path. |
public char[] getRawPath() {
return _is_opaque_part ? _opaque : _path;
}
|
public char[] getRawPathQuery() {
if (_path == null && _query == null) {
return null;
}
StringBuffer buff = new StringBuffer();
if (_path != null) {
buff.append(_path);
}
if (_query != null) {
buff.append('?");
buff.append(_query);
}
return buff.toString().toCharArray();
}
Get the raw-escaped path and query. |
public char[] getRawQuery() {
return _query;
}
Get the raw-escaped query. |
public char[] getRawScheme() {
return _scheme;
}
|
public char[] getRawURI() {
return _uri;
}
It can be gotten the URI character sequence. It's raw-escaped.
For the purpose of the protocol to be transported, it will be useful.
It is clearly unwise to use a URL that contains a password which is
intended to be secret. In particular, the use of a password within
the 'userinfo' component of a URL is strongly disrecommended except
in those rare cases where the 'password' parameter is intended to be
public.
When you want to get each part of the userinfo, you need to use the
specific methods in the specific URL. It depends on the specific URL. |
public char[] getRawURIReference() {
if (_fragment == null) {
return _uri;
}
if (_uri == null) {
return _fragment;
}
// if _uri != null && _fragment != null
String uriReference = new String(_uri) + "#" + new String(_fragment);
return uriReference.toCharArray();
}
Get the URI reference character sequence. |
public char[] getRawUserinfo() {
return _userinfo;
}
Get the raw-escaped userinfo. |
public String getScheme() {
return (_scheme == null) ? null : new String(_scheme);
}
|
public String getURI() throws URIException {
return (_uri == null) ? null : decode(_uri, getProtocolCharset());
}
It can be gotten the URI character sequence. |
public String getURIReference() throws URIException {
char[] uriReference = getRawURIReference();
return (uriReference == null) ? null : decode(uriReference,
getProtocolCharset());
}
Get the original URI reference string. |
public String getUserinfo() throws URIException {
return (_userinfo == null) ? null : decode(_userinfo,
getProtocolCharset());
}
|
public boolean hasAuthority() {
return (_authority != null) || _is_net_path;
}
Tell whether or not this URI has authority.
It's the same function as the is_net_path() method. |
public boolean hasFragment() {
return (_fragment != null);
}
Tell whether or not this URI has fragment. |
public boolean hasQuery() {
return (_query != null);
}
Tell whether or not this URI has query. |
public boolean hasUserinfo() {
return (_userinfo != null);
}
Tell whether or not this URI has userinfo. |
public int hashCode() {
if (hash == 0) {
char[] c = _uri;
if (c != null) {
for (int i = 0, len = c.length; i < len; i++) {
hash = 31 * hash + c[i];
}
}
c = _fragment;
if (c != null) {
for (int i = 0, len = c.length; i < len; i++) {
hash = 31 * hash + c[i];
}
}
}
return hash;
}
Return a hash code for this URI. |
protected int indexFirstOf(String s,
String delims) {
return indexFirstOf(s, delims, -1);
}
Get the earlier index that to be searched for the first occurrance in
one of any of the given string. |
protected int indexFirstOf(char[] s,
char delim) {
return indexFirstOf(s, delim, 0);
}
Get the earlier index that to be searched for the first occurrance in
one of any of the given array. |
protected int indexFirstOf(String s,
String delims,
int offset) {
if (s == null || s.length() == 0) {
return -1;
}
if (delims == null || delims.length() == 0) {
return -1;
}
// check boundaries
if (offset < 0) {
offset = 0;
} else if (offset > s.length()) {
return -1;
}
// s is never null
int min = s.length();
char[] delim = delims.toCharArray();
for (int i = 0; i < delim.length; i++) {
int at = s.indexOf(delim[i], offset);
if (at >= 0 && at < min) {
min = at;
}
}
return (min == s.length()) ? -1 : min;
}
Get the earlier index that to be searched for the first occurrance in
one of any of the given string. |
protected int indexFirstOf(char[] s,
char delim,
int offset) {
if (s == null || s.length == 0) {
return -1;
}
// check boundaries
if (offset < 0) {
offset = 0;
} else if (offset > s.length) {
return -1;
}
for (int i = offset; i < s.length; i++) {
if (s[i] == delim) {
return i;
}
}
return -1;
}
Get the earlier index that to be searched for the first occurrance in
one of any of the given array. |
public boolean isAbsPath() {
return _is_abs_path;
}
Tell whether or not the relativeURI or hier_part of this URI is abs_path. |
public boolean isAbsoluteURI() {
return (_scheme != null);
}
Tell whether or not this URI is absolute. |
public boolean isHierPart() {
return _is_hier_part;
}
Tell whether or not the absoluteURI of this URI is hier_part. |
public boolean isHostname() {
return _is_hostname;
}
Tell whether or not the host part of this URI is hostname. |
public boolean isIPv4address() {
return _is_IPv4address;
}
Tell whether or not the host part of this URI is IPv4address. |
public boolean isIPv6reference() {
return _is_IPv6reference;
}
Tell whether or not the host part of this URI is IPv6reference. |
public boolean isNetPath() {
return _is_net_path || (_authority != null);
}
Tell whether or not the relativeURI or heir_part of this URI is net_path.
It's the same function as the has_authority() method. |
public boolean isOpaquePart() {
return _is_opaque_part;
}
Tell whether or not the absoluteURI of this URI is opaque_part. |
public boolean isRegName() {
return _is_reg_name;
}
Tell whether or not the authority component of this URI is reg_name. |
public boolean isRelPath() {
return _is_rel_path;
}
Tell whether or not the relativeURI of this URI is rel_path. |
public boolean isRelativeURI() {
return (_scheme == null);
}
Tell whether or not this URI is relative. |
public boolean isServer() {
return _is_server;
}
Tell whether or not the authority component of this URI is server. |
public void normalize() throws URIException {
if (isAbsPath()) {
_path = normalize(_path);
setURI();
}
}
Normalizes the path part of this URI. Normalization is only meant to be performed on
URIs with an absolute path. Calling this method on a relative path URI will have no
effect. |
protected char[] normalize(char[] path) throws URIException {
if (path == null) {
return null;
}
String normalized = new String(path);
// If the buffer begins with "./" or "../", the "." or ".." is removed.
if (normalized.startsWith("./")) {
normalized = normalized.substring(1);
} else if (normalized.startsWith("../")) {
normalized = normalized.substring(2);
} else if (normalized.startsWith("..")) {
normalized = normalized.substring(2);
}
// All occurrences of "/./" in the buffer are replaced with "/"
int index = -1;
while ((index = normalized.indexOf("/./")) != -1) {
normalized = normalized.substring(0, index) + normalized.substring(index + 2);
}
// If the buffer ends with "/.", the "." is removed.
if (normalized.endsWith("/.")) {
normalized = normalized.substring(0, normalized.length() - 1);
}
int startIndex = 0;
// All occurrences of "/< segment >/../" in the buffer, where ".."
// and < segment > are complete path segments, are iteratively replaced
// with "/" in order from left to right until no matching pattern remains.
// If the buffer ends with "/< segment >/..", that is also replaced
// with "/". Note that < segment > may be empty.
while ((index = normalized.indexOf("/../", startIndex)) != -1) {
int slashIndex = normalized.lastIndexOf('/", index - 1);
if (slashIndex >= 0) {
normalized = normalized.substring(0, slashIndex) + normalized.substring(index + 3);
} else {
startIndex = index + 3;
}
}
if (normalized.endsWith("/..")) {
int slashIndex = normalized.lastIndexOf('/", normalized.length() - 4);
if (slashIndex >= 0) {
normalized = normalized.substring(0, slashIndex + 1);
}
}
// All prefixes of "< segment >/../" in the buffer, where ".."
// and < segment > are complete path segments, are iteratively replaced
// with "/" in order from left to right until no matching pattern remains.
// If the buffer ends with "< segment >/..", that is also replaced
// with "/". Note that < segment > may be empty.
while ((index = normalized.indexOf("/../")) != -1) {
int slashIndex = normalized.lastIndexOf('/", index - 1);
if (slashIndex >= 0) {
break;
} else {
normalized = normalized.substring(index + 3);
}
}
if (normalized.endsWith("/..")) {
int slashIndex = normalized.lastIndexOf('/", normalized.length() - 4);
if (slashIndex < 0) {
normalized = "/";
}
}
return normalized.toCharArray();
}
Normalize the given hier path part.
Algorithm taken from URI reference parser at
http://www.apache.org/~fielding/uri/rev-2002/issues.html. |
protected void parseAuthority(String original,
boolean escaped) throws URIException {
// Reset flags
_is_reg_name = _is_server =
_is_hostname = _is_IPv4address = _is_IPv6reference = false;
// set the charset to do escape encoding
String charset = getProtocolCharset();
boolean hasPort = true;
int from = 0;
int next = original.indexOf('@");
if (next != -1) { // neither -1 and 0
// each protocol extented from URI supports the specific userinfo
_userinfo = (escaped) ? original.substring(0, next).toCharArray()
: encode(original.substring(0, next), allowed_userinfo,
charset);
from = next + 1;
}
next = original.indexOf('[", from);
if (next >= from) {
next = original.indexOf(']", from);
if (next == -1) {
throw new URIException(URIException.PARSING, "IPv6reference");
} else {
next++;
}
// In IPv6reference, '[', ']' should be excluded
_host = (escaped) ? original.substring(from, next).toCharArray()
: encode(original.substring(from, next), allowed_IPv6reference,
charset);
// Set flag
_is_IPv6reference = true;
} else { // only for !_is_IPv6reference
next = original.indexOf(':", from);
if (next == -1) {
next = original.length();
hasPort = false;
}
// REMINDME: it doesn't need the pre-validation
_host = original.substring(from, next).toCharArray();
if (validate(_host, IPv4address)) {
// Set flag
_is_IPv4address = true;
} else if (validate(_host, hostname)) {
// Set flag
_is_hostname = true;
} else {
// Set flag
_is_reg_name = true;
}
}
if (_is_reg_name) {
// Reset flags for a server-based naming authority
_is_server = _is_hostname = _is_IPv4address =
_is_IPv6reference = false;
// set a registry-based naming authority
if (escaped) {
_authority = original.toCharArray();
if (!validate(_authority, reg_name)) {
throw new URIException("Invalid authority");
}
} else {
_authority = encode(original, allowed_reg_name, charset);
}
} else {
if (original.length() - 1 > next && hasPort
&& original.charAt(next) == ':") { // not empty
from = next + 1;
try {
_port = Integer.parseInt(original.substring(from));
} catch (NumberFormatException error) {
throw new URIException(URIException.PARSING,
"invalid port number");
}
}
// set a server-based naming authority
StringBuffer buf = new StringBuffer();
if (_userinfo != null) { // has_userinfo
buf.append(_userinfo);
buf.append('@");
}
if (_host != null) {
buf.append(_host);
if (_port != -1) {
buf.append(':");
buf.append(_port);
}
}
_authority = buf.toString().toCharArray();
// Set flag
_is_server = true;
}
}
Parse the authority component. |
protected void parseUriReference(String original,
boolean escaped) throws URIException {
// validate and contruct the URI character sequence
if (original == null) {
throw new URIException("URI-Reference required");
}
/* @
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
*/
String tmp = original.trim();
/*
* The length of the string sequence of characters.
* It may not be equal to the length of the byte array.
*/
int length = tmp.length();
/*
* Remove the delimiters like angle brackets around an URI.
*/
if (length > 0) {
char[] firstDelimiter = { tmp.charAt(0) };
if (validate(firstDelimiter, delims)) {
if (length >= 2) {
char[] lastDelimiter = { tmp.charAt(length - 1) };
if (validate(lastDelimiter, delims)) {
tmp = tmp.substring(1, length - 1);
length = length - 2;
}
}
}
}
/*
* The starting index
*/
int from = 0;
/*
* The test flag whether the URI is started from the path component.
*/
boolean isStartedFromPath = false;
int atColon = tmp.indexOf(':");
int atSlash = tmp.indexOf('/");
if ((atColon < = 0 && !tmp.startsWith("//"))
|| (atSlash >= 0 && atSlash < atColon)) {
isStartedFromPath = true;
}
/*
* < p >< blockquote >< pre >
* @@@@@@@@
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
* < /pre >< /blockquote >< p >
*/
int at = indexFirstOf(tmp, isStartedFromPath ? "/?#" : ":/?#", from);
if (at == -1) {
at = 0;
}
/*
* Parse the scheme.
* < p >< blockquote >< pre >
* scheme = $2 = http
* @
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
* < /pre >< /blockquote >< p >
*/
if (at > 0 && at < length && tmp.charAt(at) == ':") {
char[] target = tmp.substring(0, at).toLowerCase().toCharArray();
if (validate(target, scheme)) {
_scheme = target;
} else {
throw new URIException("incorrect scheme");
}
from = ++at;
}
/*
* Parse the authority component.
* < p >< blockquote >< pre >
* authority = $4 = jakarta.apache.org
* @@
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
* < /pre >< /blockquote >< p >
*/
// Reset flags
_is_net_path = _is_abs_path = _is_rel_path = _is_hier_part = false;
if (0 < = at && at < length && tmp.charAt(at) == '/") {
// Set flag
_is_hier_part = true;
if (at + 2 < length && tmp.charAt(at + 1) == '/"
&& !isStartedFromPath) {
// the temporary index to start the search from
int next = indexFirstOf(tmp, "/?#", at + 2);
if (next == -1) {
next = (tmp.substring(at + 2).length() == 0) ? at + 2
: tmp.length();
}
parseAuthority(tmp.substring(at + 2, next), escaped);
from = at = next;
// Set flag
_is_net_path = true;
}
if (from == at) {
// Set flag
_is_abs_path = true;
}
}
/*
* Parse the path component.
* < p >< blockquote >< pre >
* path = $5 = /ietf/uri/
* @@@@@@
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
* < /pre >< /blockquote >< p >
*/
if (from < length) {
// rel_path = rel_segment [ abs_path ]
int next = indexFirstOf(tmp, "?#", from);
if (next == -1) {
next = tmp.length();
}
if (!_is_abs_path) {
if (!escaped
&& prevalidate(tmp.substring(from, next), disallowed_rel_path)
|| escaped
&& validate(tmp.substring(from, next).toCharArray(), rel_path)) {
// Set flag
_is_rel_path = true;
} else if (!escaped
&& prevalidate(tmp.substring(from, next), disallowed_opaque_part)
|| escaped
&& validate(tmp.substring(from, next).toCharArray(), opaque_part)) {
// Set flag
_is_opaque_part = true;
} else {
// the path component may be empty
_path = null;
}
}
String s = tmp.substring(from, next);
if (escaped) {
setRawPath(s.toCharArray());
} else {
setPath(s);
}
at = next;
}
// set the charset to do escape encoding
String charset = getProtocolCharset();
/*
* Parse the query component.
* < p >< blockquote >< pre >
* query = $7 = < undefined >
* @@@@@@@@@
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
* < /pre >< /blockquote >< p >
*/
if (0 < = at && at + 1 < length && tmp.charAt(at) == '?") {
int next = tmp.indexOf('#", at + 1);
if (next == -1) {
next = tmp.length();
}
if (escaped) {
_query = tmp.substring(at + 1, next).toCharArray();
if (!validate(_query, uric)) {
throw new URIException("Invalid query");
}
} else {
_query = encode(tmp.substring(at + 1, next), allowed_query, charset);
}
at = next;
}
/*
* Parse the fragment component.
* < p >< blockquote >< pre >
* fragment = $9 = Related
* @@@@@@@@
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
* < /pre >< /blockquote >< p >
*/
if (0 < = at && at + 1 < = length && tmp.charAt(at) == '#") {
if (at + 1 == length) { // empty fragment
_fragment = "".toCharArray();
} else {
_fragment = (escaped) ? tmp.substring(at + 1).toCharArray()
: encode(tmp.substring(at + 1), allowed_fragment, charset);
}
}
// set this URI.
setURI();
}
In order to avoid any possilbity of conflict with non-ASCII characters,
Parse a URI reference as a String with the character
encoding of the local system or the document.
The following line is the regular expression for breaking-down a URI
reference into its components.
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
12 3 4 5 6 7 8 9
For example, matching the above expression to
http://jakarta.apache.org/ietf/uri/#Related
results in the following subexpression matches:
$1 = http:
scheme = $2 = http
$3 = //jakarta.apache.org
authority = $4 = jakarta.apache.org
path = $5 = /ietf/uri/
$6 =
query = $7 =
$8 = #Related
fragment = $9 = Related
|
protected boolean prevalidate(String component,
BitSet disallowed) {
// prevalidate the given component by disallowed characters
if (component == null) {
return false; // undefined
}
char[] target = component.toCharArray();
for (int i = 0; i < target.length; i++) {
if (disallowed.get(target[i])) {
return false;
}
}
return true;
}
Pre-validate the unescaped URI string within a specific component. |
protected char[] removeFragmentIdentifier(char[] component) {
if (component == null) {
return null;
}
int lastIndex = new String(component).indexOf('#");
if (lastIndex != -1) {
component = new String(component).substring(0,
lastIndex).toCharArray();
}
return component;
}
Remove the fragment identifier of the given component. |
protected char[] resolvePath(char[] basePath,
char[] relPath) throws URIException {
// REMINDME: paths are never null
String base = (basePath == null) ? "" : new String(basePath);
// _path could be empty
if (relPath == null || relPath.length == 0) {
return normalize(basePath);
} else if (relPath[0] == '/") {
|