Class URL2
- All Implemented Interfaces:
Serializable
,Comparable<URL2>
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
boolean
Compares two URLs.Returns the authority part of thisURL
.Extracts domain name for a given URL.getFile()
Returns the file name of thisURL
.Returns the file name extension of thisURL
.An alias forgetRef()
.getHost()
Returns the host name of thisURL
, if applicable.getPath()
Returns the path part of thisURL
.int
getPort()
Returns the port number of thisURL
.Returns the protocol name of thisURL
.getQuery()
Returns the query part of thisURL
.getRef()
Returns the anchor (also known as the "reference") of thisURL
.An alias forgetProtocol()
.Returns the userInfo part of thisURL
.int
hashCode()
long
boolean
isValid()
static String
normalizeURLFragment
(String fragment) Normalizes a URL fragment.static String
normalizeURLFragment
(CharsetEncoder UTF8Encoder, String fragment) Normalizes a URL fragment.protected void
Parses the string representation of aURL
into aURL
object.protected void
set
(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) Sets the specified 8 fields of the URL.toString()
Constructs a string representation of thisURL
.
-
Constructor Details
-
URL2
Creates aURL
object from theString
representation.This constructor is equivalent to a call to the two-argument constructor with a
null
first argument.- Parameters:
spec
- theString
to parse as a URL.
-
URL2
Creates a URL by parsing the given spec within a specified context. The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers : Generic Syntax" :
The reference is parsed into the scheme, authority, path, query and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise the any fragment and query parts present in the spec are used in the new URL. If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise the scheme component is inherited from the context URL. If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context. If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path. Otherwise the path is treated as a relative path and is appended to the context path. The path is canonicalized through the removal of directory changes made by occurences of ".." and ".". For a more detailed description of URL parsing, refer to RFC2396. NOTE: some sanitization is now performed on paths and queries. In particular, "//" sequences are collapsed in paths, and "/" is %-encoded in queries.<scheme>://<authority><path>?<query>#<fragment>
- Parameters:
context
- the context in which to parse the specification.spec
- theString
to parse as a URL.
-
-
Method Details
-
normalizeURLFragment
public static String normalizeURLFragment(CharsetEncoder UTF8Encoder, String fragment) throws CharacterCodingException Normalizes a URL fragment.This method return the normalization of its argument. All character that are illegal are first UTF-8 encoded, and then represented with the %-notation.
- Parameters:
UTF8Encoder
- a (possibly cached) UTF-8 encoder.fragment
- a URL fragment (possiblynull
).- Returns:
- the normalized version.
- Throws:
CharacterCodingException
-
normalizeURLFragment
Normalizes a URL fragment.This method return the normalization of its argument. All character that are illegal are first UTF-8 encoded, and then represented with the %-notation.
- Parameters:
fragment
- a URL fragment (possiblynull
).- Returns:
- the normalized version.
- Throws:
CharacterCodingException
-
parseURL
Parses the string representation of aURL
into aURL
object.If there is any inherited context, then it has already been copied into the
URL
argument.The
parseURL
method ofURLStreamHandler
parses the string representation as if it were anhttp
specification. Most URL protocol families have a similar parsing. A stream protocol handler for a protocol that has a different syntax must override this routine.- Parameters:
u
- theURL
to receive the result of parsing the spec.spec
- theString
representing the URL that must be parsed.start
- the character index at which to begin parsing. This is just past the ':
' (if there is one) that specifies the determination of the protocol name.limit
- the character position to stop parsing at. This is the end of the string or the position of the "#
" character, if present. All information after the sharp sign indicates an anchor.
-
set
protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) Sets the specified 8 fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.- Parameters:
protocol
- the name of the protocol to usehost
- the name of the hostport
- the port number on the hostauthority
- the authority part for the urluserInfo
- the username and passwordpath
- the file on the hostquery
- the query part of this URLref
- the internal reference in the URL
-
isValid
public boolean isValid() -
getQuery
Returns the query part of thisURL
.- Returns:
- the query part of this
URL
.
-
getPath
Returns the path part of thisURL
.- Returns:
- the path part of this
URL
.
-
getUserInfo
Returns the userInfo part of thisURL
.- Returns:
- the userInfo part of this
URL
.
-
getAuthority
Returns the authority part of thisURL
.- Returns:
- the authority part of this
URL
.
-
getPort
public int getPort()Returns the port number of thisURL
. Returns -1 if the port is not set.- Returns:
- the port number
-
getProtocol
Returns the protocol name of thisURL
.- Returns:
- the protocol of this
URL
.
-
getScheme
An alias forgetProtocol()
.- Returns:
- the protocol of this
URL
.
-
getHost
Returns the host name of thisURL
, if applicable.- Returns:
- the host name of this
URL
.
-
getFile
Returns the file name of thisURL
.- Returns:
- the file name of this
URL
.
-
getFileExtension
Returns the file name extension of thisURL
. In case of file name isindex.html
,html
will be returned but if no valid extension is foundnull
will be returned.- Returns:
- the file name extension of this
URL
.
-
getRef
Returns the anchor (also known as the "reference") of thisURL
.- Returns:
- the anchor (also known as the "reference") of this
URL
.
-
getFragment
An alias forgetRef()
.- Returns:
- the anchor (also known as the "reference") of this
URL
.
-
equals
Compares two URLs. The result istrue
if and only if the argument is notnull
and is aURL
object that represents the sameURL
as this object. Two URL objects are equal if they have the same protocol and reference the same host, the same port number on the host, and the same file and anchor on the host. -
compareTo
- Specified by:
compareTo
in interfaceComparable<URL2>
-
hashCode
public int hashCode() -
hashCode64
public long hashCode64() -
toString
Constructs a string representation of thisURL
. -
getDomain
Extracts domain name for a given URL. Very useful to avoid correlated-links. This method works by considering the right-most, most-significant and non-common suffix of a given URL. Examples:http://www.ox.ac.uk/
returns:ox.ac.uk
http://something.somethingelse.web.com/
returns:somethingelse.web.com
http://www.microsoft.com/
returns:microsoft.com
http://www.dsi.unimi.it/
returns:unimi.it
- Returns:
- a
String
indicating the domain name.
-