public abstract class UriTemplate extends Object
This is the main class for creating and manipulating URI templates. This project implements
RFC6570 URI Templates and produces output
that is compliant with the spec. The template processor supports levels
1 through 4 as well as supports composite types. In addition to supporting Map
and List values as composite types, the library also supports the use of Java objects
as well. Please see the VarExploder and DefaultVarExploder for more info.
There are many ways to use this library. The simplest way is to create a template from a URI template string:
UriTemplate template = UriTemplate.fromTemplate("http://example.com/search{?q,lang}");
Replacement values are added by calling the set(String, Object) method on the template:
template.set("q","cat")
.set("lang","en");
String uri = template.expand();
The expand() method will replace the variable names with the supplied values
and return the following URI:
http://example.com/search?q=cat&lang=en
| Modifier and Type | Class and Description |
|---|---|
static class |
UriTemplate.Encoding |
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_SEPARATOR |
protected DateFormat |
defaultDateFormat |
protected Map<String,Object> |
values
The collection of values that will be applied to the URI expression in the
expansion process.
|
| Constructor and Description |
|---|
UriTemplate() |
| Modifier and Type | Method and Description |
|---|---|
UriTemplate |
append(String template)
Appends the expression from a base URI template expression, such as:
|
static boolean |
containsOperator(String op) |
abstract String |
expand()
Applies variable substitution the URI Template and returns the expanded
URI.
|
abstract String |
expand(Map<String,Object> vars)
Expand the URI template using the supplied values
|
static String |
expand(String expression,
Map<String,Object> values)
Expands the given expression string using the variable replacements
in the supplied
Map. |
UriTemplate |
expression(String expression)
Deprecated.
use
#template(String) instead |
static UriTemplate |
fromExpression(String expression)
Deprecated.
use
fromTemplate(String) instead |
static UriTemplate |
fromTemplate(String template)
Creates a new
UriTemplate from the template. |
static UriTemplate |
fromTemplate(UriTemplate base)
Creates a new
UriTemplate from a root UriTemplate. |
String |
getExpression()
Deprecated.
use
getTemplate() instead. |
String |
getTemplate()
Returns the original URI template expression.
|
Map<String,Object> |
getValues()
Returns the collection of name/value pairs contained in the instance.
|
UriTemplate |
set(Map<String,Object> values)
Adds the name/value pairs in the supplied
Map to the collection
of values within this URI template instance. |
UriTemplate |
set(String variableName,
Date value)
Sets a Date value into the list of variable substitutions using the
default
DateFormat. |
UriTemplate |
set(String variableName,
Object value)
Sets a value on the URI template expression variable.
|
protected void |
setTemplate(String template)
FIXME Comment this
|
UriTemplate |
withDefaultDateFormat(DateFormat dateFormat) |
UriTemplate |
withDefaultDateFormat(String dateFormatString) |
public static final String DEFAULT_SEPARATOR
protected DateFormat defaultDateFormat
@Deprecated public static final UriTemplate fromExpression(String expression)
fromTemplate(String) insteadUriTemplate from the expression.expression - public static final UriTemplate fromTemplate(String template)
UriTemplate from the template.expression - public static UriTemplate fromTemplate(UriTemplate base)
Creates a new UriTemplate from a root UriTemplate. This
method will create a new UriTemplate from the base and copy the variables
from the base template to the new UriTemplate.
This method is useful when the base template is less volatile than the child expression and you want to merge the two.
base - public static String expand(String expression, Map<String,Object> values)
Map.expression - values - @Deprecated public String getExpression()
getTemplate() instead.public String getTemplate()
public UriTemplate append(String template)
Appends the expression from a base URI template expression, such as:
UriTemplate template = UriTemplate.fromExpression("http://api.github.com");
A child expression can be appended by:
UriTemplate template = UriTemplate.fromExpression("http://api.github.com")
.expression("/repos/{user}/{repo}/commits");
The resulting expression would result in:
http://api.github.com/repos/{user}/{repo}/commits
Multiple expressions can be appended to the template as follows:
UriTemplate template = UriTemplate.fromTemplate("http://myhost")
.append("{/version}")
.append("{/myId}")
.append("/things/{thingId}")
.set("myId","damnhandy")
.set("version","v1")
.set("thingId","12345");
This will result in the following template and URI:
Template: http://myhost{/version}{/myId}/things/{thingId}
URI: http://myhost/v1/damnhandy/things/12345
Since a URI template is not a URI, there no way to accurately determine how the variable expression should be delimited. Therefore, it is up to the expression author to include the necessary delimiters in each sub expression.
template - @Deprecated public UriTemplate expression(String expression)
#template(String) insteadexpression - protected void setTemplate(String template)
template - public Map<String,Object> getValues()
public UriTemplate withDefaultDateFormat(String dateFormatString)
dateFormatString - public UriTemplate withDefaultDateFormat(DateFormat dateFormat)
dateFormat - public UriTemplate set(String variableName, Object value)
variableName - value - public UriTemplate set(String variableName, Date value)
DateFormat.variableName - value - public UriTemplate set(Map<String,Object> values)
Map to the collection
of values within this URI template instance.values - public abstract String expand(Map<String,Object> vars)
vars - The values that will be used in the expansionpublic abstract String expand()
public static boolean containsOperator(String op)
op - Copyright © April 30, 2012-2012 Ryan J. McDonough. All Rights Reserved.