Class AddressParser
- java.lang.Object
-
- org.simpleframework.util.parse.Parser
-
- org.simpleframework.http.parse.AddressParser
-
- All Implemented Interfaces:
Address
public class AddressParser extends Parser implements Address
This parser is used to parse uniform resource identifiers. The uniform resource identifier syntax is given in RFC 2396. This parser can parse relative and absolute URI's. The uniform resource identifier syntax that this parser will parse are based on the generic web based URL similar to the syntax represented in RFC 2616 section 3.2.2. The syntax used to parse this URI is a modified version of RFC 2396URI = (absoluteURI | relativeURI) absoluteURI = scheme ":" ("//" netpath | relativeURI) relativeURI = path ["?" querypart] netpath = domain [":" port] relativeURI path = *("/" segment) segment = *pchar *( ";" param )This implements theAddressinterface and provides methods that access the various parts of the URI. The parameters in the path segments of the uniform resource identifier are stored in name value pairs. If parameter names are not unique across the path segments then only the deepest parameter will be stored from the path segment. For example if the URI represented washttp://domain/path1;x=y/path2;x=zthe value for the parameter namedxwould bez.This will normalize the path part of the uniform resource identifier. A normalized path is one that contains no back references like "./" and "../". The normalized path will not contain the path parameters.
The
setPathmethod is used to reset the path this uniform resource identifier has, it also resets the parameters. The parameters are extracted from the new path given.- Author:
- Niall Gallagher
-
-
Constructor Summary
Constructors Constructor Description AddressParser()Default constructor will create aAddressParserthat contains no specifics.AddressParser(java.lang.String text)This is primarily a convenience constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetDomain()This is used to retrive the domain of this URI.KeyMap<java.lang.String>getParameters()This extracts the parameter values from the uniform resource identifier represented by this object.PathgetPath()This is used to retrive the path of this URI.intgetPort()This is used to retrive the port of the uniform resource identifier.QuerygetQuery()This is used to retrieve the query of this URI.java.lang.StringgetScheme()This allows the scheme of the URL given to be returned.protected voidinit()This will empty each tokens cache.protected voidparse()This will check to see what type of URI this is if it is anabsoluteURIor arelativeURI.voidsetDomain(java.lang.String value)This will set the domain to whatever value is in the string parameter.voidsetPath(java.lang.String text)This will set the path to whatever value it is given.voidsetPath(Path path)This will set the path to whatever value it is given.voidsetPort(int port)This will set the port to whatever value it is given.voidsetQuery(java.lang.String value)This will set the query to whatever value it is given.voidsetQuery(Query query)This will set the query to whatever value it is given.voidsetScheme(java.lang.String value)This allows the scheme for the URI to be specified.java.lang.StringtoString()This is used to convert this URI object into aStringobject.
-
-
-
Constructor Detail
-
AddressParser
public AddressParser()
Default constructor will create aAddressParserthat contains no specifics. The instance will returnnullfor all the get methods. The parsers get methods are populated by using theparsemethod.
-
AddressParser
public AddressParser(java.lang.String text)
This is primarily a convenience constructor. This will parse theStringgiven to extract the specifics. This could be achieved by calling the default no-arg constructor and then using the instance to invoke theparsemethod on thatStringto extract the parts.- Parameters:
text- aStringcontaining a URI value
-
-
Method Detail
-
getScheme
public java.lang.String getScheme()
This allows the scheme of the URL given to be returned. If the URI does not contain a scheme then this will return null. The scheme of the URI is the part that specifies the type of protocol that the URI is used for, an examplegopher://domain/pathis a URI that is intended for the gopher protocol. The scheme is the stringgopher.
-
getDomain
public java.lang.String getDomain()
This is used to retrive the domain of this URI. The domain part in the URI is an optional part, an examplehttp://domain/path?querypart. This will return the value of the domain part. If there is no domain part then this will return null otherwise the domain value found in the uniform resource identifier.
-
getPath
public Path getPath()
This is used to retrive the path of this URI. The path part is the most fundemental part of the URI. This will return the value of the path. If there is no path part then this will return/to indicate the root.The
Pathobject returned by this will contain no path parameters. The path parameters are available using theAddressmethods. The reason that this does not contain any of the path parameters is so that if the path is needed to be converted into ab OS specific path then the path parameters will not need to be separately parsed out.
-
getQuery
public Query getQuery()
This is used to retrieve the query of this URI. The query part in the URI is an optional part. This will return the value of the query part. If there is no query part then this will return an emptyQueryobject. The query is an optional member of a URI and comes after the path part, it is preceded by a question mark,?character. For example the following URI containsqueryfor its query part,http://host:port/path?query.This returns a
org.simpleframework.http.Queryobject that can be used to interact directly with the query values. TheQueryobject is a read-only interface to the query parameters, and so will not affect the URI.
-
getPort
public int getPort()
This is used to retrive the port of the uniform resource identifier. The port part in this is an optional part, an examplehttp://host:port/path?querypart. This will return the value of the port. If there is no port then this will return-1because this represents an impossible uniform resource identifier port. The port is an optional part.
-
getParameters
public KeyMap<java.lang.String> getParameters()
This extracts the parameter values from the uniform resource identifier represented by this object. The parameters that a uniform resource identifier contains are embedded in the path part of the URI. If the path contains no parameters then this will return an emptyMapinstance.This will produce unique name and value parameters. Thus if the URI contains several path segments with similar parameter names this will return the deepest parameter. For example if the URI represented was
http://domain/path1;x=y/path2;x=zthe value for the parameter namedxwould bez.- Specified by:
getParametersin interfaceAddress- Returns:
- this will return the parameter names found in the URI
-
setScheme
public void setScheme(java.lang.String value)
This allows the scheme for the URI to be specified. If the URI does not contain a scheme then this will attach the scheme and the://identifier to ensure that theAddress.toStringwill produce the correct syntax.Caution must be taken to ensure that the port and the scheme are consistent. So if the original URI was
http://domain:80/pathand the scheme was changed toftpthe port number that remains is the standard HTTP port not the FTP port.- Parameters:
value- this specifies the protocol this URI is intended for
-
setDomain
public void setDomain(java.lang.String value)
This will set the domain to whatever value is in the string parameter. If the string is null then this URI objectstoStringmethod will not contain the domain. The result of thetoStringmethod will be/path/path?query. If the path is non-null this URI will contain the path.- Parameters:
value- this will be the new domain of this uniform resource identifier, if it is not null
-
setPort
public void setPort(int port)
This will set the port to whatever value it is given. If the value is 0 or less then thetoStringwill will not contain the optional port. If port number is above 0 then thetoStringmethod will produce a URI likehttp://host:123/pathbut only if there is a valid domain.- Parameters:
port- the port value that this URI is to have
-
setPath
public void setPath(java.lang.String text)
This will set the path to whatever value it is given. If the value is null then thisAddress.toStringmethod will not contain the path, that is if path is null then it will be interpreted as/.This will reset the parameters this URI has. If the value given to this method has embedded parameters these will form the parameters of this URI. The value given may not be the same value that the
getPathproduces. The path will have all back references and parameters stripped.- Parameters:
text- the path that this URI is to be set with
-
setPath
public void setPath(Path path)
This will set the path to whatever value it is given. If the value is null then thisAddress.toStringmethod will not contain the path, that is if path is null then it will be interpreted as/.This will reset the parameters this URI has. If the value given to this method has embedded parameters these will form the parameters of this URI. The value given may not be the same value that the
getPathproduces. The path will have all back references and parameters stripped.- Parameters:
path- the path that this URI is to be set with
-
setQuery
public void setQuery(java.lang.String value)
This will set the query to whatever value it is given. If the value is null then thisAddress.toStringmethod will not contain the query. If the query wasabcthen thetoStringmethod would produca a string likehttp://host:port/path?abc. If the query is null this URI would have no query part. The query must not contain the?character.- Parameters:
value- the query that this uniform resource identifier is to be set to if it is non-null
-
setQuery
public void setQuery(Query query)
This will set the query to whatever value it is given. If the value is null then thisAddress.toStringmethod will not contain the query. If theQuery.toStringreturns null then the query will be empty. This is basically thesetQuery(String)method with the string value from the issuedQuery.toStringmethod.- Parameters:
query- aQueryobject that contains the name value parameters for the query
-
parse
protected void parse()
This will check to see what type of URI this is if it is anabsoluteURIor arelativeURI. To see the definition of a URI see RFC 2616 for the definition of a URL and for more specifics see RFC 2396 for the expressions.
-
init
protected void init()
This will empty each tokens cache. A tokens cache is used to represent a token once the token'stoStringmethod has been called. Thus when thetoStringmethod is called then the token depends on the value of the cache alone in further calls totoString. However if a URI has just been parsed and that method has not been invoked then the cache is created from the buf if its length is greater than zero.
-
toString
public java.lang.String toString()
This is used to convert this URI object into aStringobject. This will only convert the parts of the URI that exist, so the URI may not contain the domain or the query part and it will not contain the path parameters. If the URI contains all these parts then it will return somthing likescheme://host:port/path/path?querypart
It can return
/path/path?querypartstyle relative URI's. If any of the parts are set to null then that part will be missing, for example ifsetDomainmethod is invoked with a null parameter then the domain and port will be missing from the resulting URI. If the path part is set to null using thesetPaththen the path will be/. An example URI with the path part of null would bescheme://host:port/?querypart
-
-