public class UrlBuilder
extends java.lang.Object
Simple class that encapsulates the process of building up a URL from a path fragment and a zero or more parameters. Parameters can be single valued, array valued or collection valued. In the case of arrays and collections, each value in the array or collection will be added as a separate URL parameter (with the same name). The assembled URL can then be retrieved by calling toString().
While not immediately obvious, it is possible to add a single parameter with multiple values by invoking the addParameter() method that uses varargs, and supplying a Collection as the single parameter value to the method.
Modifier | Constructor and Description |
---|---|
protected |
UrlBuilder(java.util.Locale locale,
boolean isForPage)
Sets the locale and sets the parameter separator based on the value of
isForPage . |
|
UrlBuilder(java.util.Locale locale,
java.lang.Class<? extends ActionBean> beanType,
boolean isForPage)
Constructs a UrlBuilder that references an
ActionBean . |
|
UrlBuilder(java.util.Locale locale,
java.lang.String url,
boolean isForPage)
Constructs a UrlBuilder with the path to a resource.
|
|
UrlBuilder(java.lang.String url,
boolean isForPage)
Deprecated.
As of Stripes 1.5, this constructor has been replaced by
UrlBuilder(Locale, String, boolean) . |
Modifier and Type | Method and Description |
---|---|
UrlBuilder |
addParameter(java.lang.String name,
java.lang.Object... values)
Appends one or more values of a parameter to the URL.
|
UrlBuilder |
addParameters(java.util.Map<? extends java.lang.Object,? extends java.lang.Object> parameters)
Appends one or more parameters to the URL.
|
protected java.lang.String |
build()
Build and return the URL
|
protected java.lang.String |
format(java.lang.Object value)
Attempts to format an object using an appropriate
Formatter . |
java.lang.String |
getAnchor()
Gets the anchor, if any, that will be appended to the URL.
|
protected java.lang.String |
getBaseURL(java.lang.String baseUrl,
java.util.Collection<net.sourceforge.stripes.util.UrlBuilder.Parameter> parameters)
Get the base URL (without a query string).
|
java.lang.String |
getEvent()
Get the name of the event to be executed by this URL.
|
protected Formatter |
getFormatter(java.lang.Object value)
Tries to get a formatter for the given value using the
FormatterFactory . |
java.lang.String |
getParameterSeparator()
Returns the string that will be used to separate parameters in the query string.
|
protected java.util.Map<java.lang.String,ValidationMetadata> |
getValidationMetadata()
Get a map of property names to
ValidationMetadata for the ActionBean class
bound to the URL being built. |
UrlBuilder |
setAnchor(java.lang.String anchor)
Sets the anchor, if any, that will be appended to the URL.
|
UrlBuilder |
setEvent(java.lang.String event)
Set the name of the event to be executed by this URL.
|
void |
setParameterSeparator(java.lang.String parameterSeparator)
Sets the string that will be used to separate parameters.
|
java.lang.String |
toString()
Returns the URL composed thus far as a String.
|
@Deprecated public UrlBuilder(java.lang.String url, boolean isForPage)
UrlBuilder(Locale, String, boolean)
.url
- the path part of the URLisForPage
- true if the URL is to be embedded in a page (e.g. in an anchor of img
tag), false if for some other purpose.public UrlBuilder(java.util.Locale locale, java.lang.String url, boolean isForPage)
locale
- the locale to use when formatting parameters with a Formatter
url
- the path part of the URLisForPage
- true if the URL is to be embedded in a page (e.g. in an anchor of img
tag), false if for some other purpose.public UrlBuilder(java.util.Locale locale, java.lang.Class<? extends ActionBean> beanType, boolean isForPage)
ActionBean
. Parameters can be added later
using addParameter(). If the link is to be used in a page then the ampersand character
usually used to separate parameters will be escaped using the XML entity for ampersand.locale
- the locale to use when formatting parameters with a Formatter
beanType
- ActionBean
class for which the URL will be builtisForPage
- true if the URL is to be embedded in a page (e.g. in an anchor of img tag),
false if for some other purpose.protected UrlBuilder(java.util.Locale locale, boolean isForPage)
isForPage
.locale
- the locale to use when formatting parameters with a Formatter
isForPage
- true if the URL is to be embedded in a page (e.g. in an anchor of img tag),
false if for some other purpose.public java.lang.String getEvent()
public UrlBuilder setEvent(java.lang.String event)
ActionBean
using either DefaultHandler
or by assigning a default value to
the $event parameter in a clean URL. If event
is null then the default event name
will be ignored and no event parameter will be added to the URL.event
- the event namepublic java.lang.String getParameterSeparator()
public void setParameterSeparator(java.lang.String parameterSeparator)
public UrlBuilder addParameter(java.lang.String name, java.lang.Object... values)
Appends one or more values of a parameter to the URL. Checks to see if each value is null, and if so generates a parameter with no value. URL Encodes the parameter values to make sure that it is safe to insert into the URL.
If any parameter value passed is a Collection or an Array then this method is called recursively with the contents of the collection or array. As a result you can pass arbitrarily nested arrays and collections to this method and it will recurse through them adding all scalar values as parameters to the URL.
name
- the name of the request parameter being addedvalues
- one or more values for the parameter suppliedpublic UrlBuilder addParameters(java.util.Map<? extends java.lang.Object,? extends java.lang.Object> parameters)
parameters
- a non-null Map as described abovepublic java.lang.String getAnchor()
public UrlBuilder setAnchor(java.lang.String anchor)
anchor
- the anchor with or without the leading pound sign, or null to disablepublic java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.String format(java.lang.Object value)
Formatter
. If
no formatter is available for the object, then this method will call
toString()
on the object. A null value
will
be formatted as an empty string.value
- the object to be formattedprotected Formatter getFormatter(java.lang.Object value)
FormatterFactory
. Returns
null if there is no Configuration
or FormatterFactory
available (e.g. in a
test environment) or if there is no Formatter
configured for the value's type.value
- the object to be formattedprotected java.util.Map<java.lang.String,ValidationMetadata> getValidationMetadata()
ValidationMetadata
for the ActionBean
class
bound to the URL being built. If the URL does not point to an ActionBean class or no
validation metadata exists for the ActionBean class then an empty map will be returned.ValidationMetadataProvider.getValidationMetadata(Class)
protected java.lang.String build()
protected java.lang.String getBaseURL(java.lang.String baseUrl, java.util.Collection<net.sourceforge.stripes.util.UrlBuilder.Parameter> parameters)
UrlBinding
exists for the URL or
ActionBean
type that was passed into the constructor, then this method will return
the base URL after appending any URI parameters that have been added with a call to
addParameter(String, Object[])
or addParameters(Map)
. Otherwise, it
returns the original base URL.baseUrl
- The base URL to start with. In many cases, this value will be returned
unchanged.parameters
- The query parameters. Any parameters that should not be appended to the
query string by build()
(e.g., because they are embedded in the URL)
should be removed from the collection before this method returns.UrlBuilder(Locale, Class, boolean)
,
UrlBuilder(Locale, String, boolean)
? Copyright 2005-2006, Stripes Development Team.