|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ObjectMutableURI
public class MutableURI
Mutable class for creating URIs.
There is little checking that an instance of this class produces a legal
URI reference as defined by
RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax.
The minimal checking for syntax is on constructors that take a String
representation or the URI, a URI
, or a URL
.
To avoid the cost of continually checking the syntax, it is up to the
user to ensure that the components are set correctly.
The setters of this class also assume that the data components are
already encoded correctly for the given encoding of this URI, unless noted
otherwise as in the methods to add unecoded parameters to the query.
Then this class will handle the encoding.
See addParameter( String name, String value, boolean encoded )
and addParameters( Map newParams, boolean encoded )
There is a static convenience method in this class so callers can easily encode unencoded components before setting it in this object.
Field Summary | |
---|---|
static String |
DEFAULT_ENCODING
Value used to set the encoding as undefined. |
static int |
UNDEFINED_PORT
Value used to set the port as undefined. |
Constructor Summary | |
---|---|
MutableURI()
Constructs a MutableURI . |
|
MutableURI(String uriString,
boolean encoded)
Constructs a MutableURI . |
|
MutableURI(String scheme,
String userInfo,
String host,
int port,
String path,
String query,
String fragment)
Constructs a MutableURI . |
|
MutableURI(URI uri)
Constructs a MutableURI . |
|
MutableURI(URL url)
Constructs a MutableURI . |
Method Summary | |
---|---|
void |
addParameter(String name,
String value,
boolean encoded)
Add a parameter for the query string. |
void |
addParameters(Map newParams,
boolean encoded)
Adds all the parameters in a Map to a URI's query string. |
String |
encode(String url)
Convenience method to encode unencoded components of a URI. |
static String |
encode(String url,
String encoding)
Convenience method to encode unencoded components of a URI. |
protected static URI |
encodeURI(String original)
Parse a URI reference, as a String , into its
components and use URI to encode the
components correctly. |
boolean |
equals(Object object)
Determines if the passed-in Object is equivalent to this MutableURI. |
static URIContext |
getDefaultContext()
Returns a default URIContext . |
String |
getEncoding()
Returns the character encoding that is used when adding unencoded parameters. |
String |
getFragment()
Returns the fragment. |
String |
getHost()
Returns the host. |
String |
getParameter(String name)
Returns the value of the parameter. |
Map |
getParameters()
Returns an unmodifiable Map of all parameters. |
List |
getParameters(String name)
Returns the values of the given parameter. |
String |
getPath()
Returns the path. |
int |
getPort()
Returns the port. |
String |
getQuery(URIContext uriContext)
Returns the query string (encoded/escaped). |
String |
getScheme()
Returns the protocol/scheme. |
String |
getSchemeSpecificPart()
Returns the scheme-specific part of this URI if it is opaque. |
String |
getURIString(URIContext uriContext)
Returns a string form of this URI. |
String |
getUserInfo()
Returns the userInfo. |
int |
hashCode()
Returns a hash code value for the object. |
protected static int |
indexFirstOf(String s,
String delims,
int offset)
Get the earliest index, searching for the first occurrance of any one of the given delimiters. |
boolean |
isAbsolute()
Tells whether or not this URI is absolute. |
boolean |
isOpaque()
Tells whether or not this URI is opaque. |
void |
removeParameter(String name)
Removes the given parameter. |
void |
setEncoding(String encoding)
Set the encoding used when adding unencoded parameters. |
void |
setFragment(String fragment)
Sets the fragment. |
void |
setHost(String host)
Sets the host. |
void |
setOpaque(String scheme,
String schemeSpecificPart)
Sets the URI to be opaque using the given scheme and schemeSpecificPart. |
void |
setPath(String path)
Sets the path. |
void |
setPort(int port)
Sets the port. |
void |
setQuery(String query)
Sets (and resets) the query string. |
void |
setScheme(String scheme)
Sets the protocol/scheme. |
protected void |
setSchemeSpecificPart(String schemeSpecificPart)
Set the scheme-specific part of this (opaque) URI |
void |
setURI(String uriString,
boolean encoded)
Set the value of the MutableURI . |
void |
setURI(URI uri)
Set the value of the MutableURI . |
void |
setUserInfo(String userInfo)
Sets the userInfo. |
Methods inherited from class Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int UNDEFINED_PORT
public static final String DEFAULT_ENCODING
Constructor Detail |
---|
public MutableURI()
MutableURI
.
public MutableURI(String uriString, boolean encoded) throws URISyntaxException
MutableURI
.
uriString
- the string to be parsed into a URIencoded
- Flag indicating whether the string is
already encoded.
URISyntaxException
public MutableURI(String scheme, String userInfo, String host, int port, String path, String query, String fragment)
MutableURI
. Assumes the individual components
are already encoded and escaped.
scheme
- the name of the protocol to useuserInfo
- the username and passwordhost
- the name of the hostport
- the port number on the hostpath
- the file on the hostquery
- the query part of this URIfragment
- the fragment part of this URI (internal reference in the URL)public MutableURI(URI uri)
MutableURI
.
uri
- the initial value for this mutable URIpublic MutableURI(URL url) throws URISyntaxException
MutableURI
.
This is just a convenience constructor that functions the same as
MutableURI(URI)
constructor with
URL.toURI()
as the argument.
Note, any URL instance that complies with RFC 2396 can be converted
to a URI. However, some URLs that are not strictly in compliance
can not be converted to a URI. See URL
url
- the initial value for this mutable URI
URISyntaxException
- if this URL is not formatted strictly
to RFC2396 and cannot be converted to a URI.URL.toURI()
Method Detail |
---|
public void setURI(String uriString, boolean encoded) throws URISyntaxException
MutableURI
.
This method can also be used to clear the MutableURI
.
uriString
- the string to be parsed into a URIencoded
- Flag indicating whether the string is
already encoded.
URISyntaxException
public void setURI(URI uri)
MutableURI
.
This method can also be used to clear the MutableURI
.
uri
- the URIpublic void setEncoding(String encoding)
encoding
- public String getEncoding()
public void setScheme(String scheme)
scheme
- protocol/schemepublic String getScheme()
public void setUserInfo(String userInfo)
userInfo
- userInfopublic String getUserInfo()
public void setHost(String host)
host
- hostpublic String getHost()
public void setPort(int port)
port
- portpublic int getPort()
public void setPath(String path)
path
- pathpublic String getPath()
public void setQuery(String query)
query
- Query stringpublic String getQuery(URIContext uriContext)
The context states whether or not to use the default delimiter, usually the "&" entity, to separate the parameters. Otherwise, the "&" character is used.
uriContext
- has property indicating if we use the HTML Amp entity
to separate the query parameters.
public void addParameter(String name, String value, boolean encoded)
If the encoded flag is true then this method assumes that the name and value do not need encoding or are already encoded correctly. Otherwise, it translates the name and value with the character encoding of this URI and adds them to the set of parameters for the query. If the encoding for this URI has not been set, then the default encoding used is "UTF-8".
Multiple values for the same parameter can be set by calling this method multiple times with the same name.
name
- namevalue
- valueencoded
- Flag indicating whether the names and values are
already encoded.public void addParameters(Map newParams, boolean encoded)
Adds all the parameters in a Map
to a URI's query string.
If the encoded flag is true then this method assumes that the name and value do not need encoding or are already encoded correctly. Otherwise, it translates the name and value with the character encoding of this URI and adds them to the set of parameters for the query. If the encoding for this URI has not been set, then the default encoding used is "UTF-8".
The query parameters are added in the order the keys of the Map.
newParams
- the map of new parameters to add to the URIencoded
- Flag indicating whether the names and values are already encoded.public String getParameter(String name)
name
- a name of the parameter
public List getParameters(String name)
name
- name of the parameter
List
of values for the given parameter namepublic Map getParameters()
Map
of all parameters.
Map
of names and values for all parameterspublic void removeParameter(String name)
name
- namepublic void setFragment(String fragment)
fragment
- fragmentpublic String getFragment()
public boolean isAbsolute()
A URI is absolute if, and only if, it has a scheme component.
public boolean isOpaque()
public void setOpaque(String scheme, String schemeSpecificPart)
From URI
: "A URI is opaque if, and only
if, it is absolute and its scheme-specific part does not begin with
a slash character ('/'). An opaque URI has a scheme, a
scheme-specific part, and possibly a fragment; all other components
are undefined."
scheme
- the scheme component of this URIschemeSpecificPart
- the scheme-specific part of this URIpublic String getSchemeSpecificPart()
protected void setSchemeSpecificPart(String schemeSpecificPart)
schemeSpecificPart
- the scheme-specific part of this URIpublic String getURIString(URIContext uriContext)
URIContext
encapsulates the data needed to write out the string form.
E.g. Defines if the "&" entity or the '&' character should be used to separate quary parameters.
uriContext
- data required to write out the string form.
String
public static URIContext getDefaultContext()
URIContext
.
public static String encode(String url, String encoding)
url
- the string to be encoded by URLCodec
encoding
- the character encoding to use
public String encode(String url)
url
- the string to be encoded by URLCodec
public boolean equals(Object object)
equals
in class Object
object
- the Object to test for equality.
public int hashCode()
Implemented in conjunction with equals() override.
This is a mutable class implying that we're basing the hash code on the member data that can change. Therefor it's important not to use this class as a key in a hashtable as it would still appear with an enumeration but not when calling contains. I.E. The object could get lost in the hashtable. A call for the hashcode would return a different value than when it was first placed in the hashtable.
With this in mind, we simply return the same value to support the rules of equality.
hashCode
in class Object
protected static URI encodeURI(String original) throws URISyntaxException
String
, into its
components and use URI
to encode the
components correctly. This comes from the parsing
algorithm of the Apache Commons HttpClient code for
its URI class.
original
- the original character sequence
URISyntaxException
- If an error occurs.protected static int indexFirstOf(String s, String delims, int offset)
s
- the string to be indexeddelims
- the delimiters used to indexoffset
- the from index
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |