public class UriTemplate extends Object implements Serializable
This is the primary 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. 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
Deprecated.
Replaced by
defaultDateTimeFormatter |
| Modifier | Constructor and Description |
|---|---|
protected |
UriTemplate(LinkedList<UriTemplateComponent> components)
Create a new UriTemplate.
|
| Modifier and Type | Method and Description |
|---|---|
static UriTemplateBuilder |
buildFromTemplate(String template)
Creates a new
UriTemplateBuilder from the template string. |
static UriTemplateBuilder |
buildFromTemplate(UriTemplate baseTemplate)
Creates a new
UriTemplateBuilder from a root UriTemplate. |
static boolean |
containsOperator(String op) |
static UriTemplateBuilder |
createBuilder()
Creates a new
UriTemplateBuilder instance. |
String |
expand()
Applies variable substitution the URI Template and returns the expanded
URI.
|
String |
expand(Map<String,Object> vars)
Expand the URI template using the supplied values
|
static String |
expand(String templateString,
Map<String,Object> values)
Expands the given template string using the variable replacements
in the supplied
Map. |
String |
expandPartial() |
static String |
expandPartial(String templateString,
Map<String,Object> values)
Expands the given template string using the variable replacements
in the supplied
Map. |
int |
expressionCount()
Returns the number of expressions found in this template
|
static UriTemplate |
fromTemplate(String templateString)
Creates a new
UriTemplate from the template. |
static UriTemplateBuilder |
fromTemplate(UriTemplate baseTemplate)
Creates a new
UriTemplate from a root UriTemplate. |
Object |
get(String variableName)
FIXME Comment this
|
Collection<UriTemplateComponent> |
getComponents()
Returns the collection of
UriTemplateComponent instances
found in this template. |
Expression[] |
getExpressions()
Returns an array of
Expression instances found in this
template. |
protected Pattern |
getReverseMatchPattern()
Returns the
|
String |
getTemplate()
Returns the original URI template expression.
|
Map<String,Object> |
getValues()
Returns the collection of name/value pairs contained in the instance.
|
String[] |
getVariables()
Returns the list of unique variable names, from all
Expression's, in this template. |
boolean |
hasVariable(String variableName)
Returns true if the
UriTemplate contains the variableName. |
protected void |
parseTemplateString()
Parse the URI template string into the template model.
|
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.
|
UriTemplate |
withDefaultDateFormat(DateFormat dateFormat)
Deprecated.
replaced by
withDefaultDateFormat |
UriTemplate |
withDefaultDateFormat(String dateFormatString) |
public static final String DEFAULT_SEPARATOR
@Deprecated protected DateFormat defaultDateFormat
defaultDateTimeFormatterprotected UriTemplate(LinkedList<UriTemplateComponent> components)
components - public static UriTemplateBuilder createBuilder()
UriTemplateBuilder instance.public static UriTemplateBuilder buildFromTemplate(String template) throws MalformedUriTemplateException
UriTemplateBuilder from the template string.template - MalformedUriTemplateExceptionpublic static UriTemplateBuilder buildFromTemplate(UriTemplate baseTemplate) throws MalformedUriTemplateException
Creates a new UriTemplateBuilder 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.
baseTemplate - MalformedUriTemplateExceptionpublic static final UriTemplate fromTemplate(String templateString) throws MalformedUriTemplateException
UriTemplate from the template.templateString - MalformedUriTemplateExceptionpublic static UriTemplateBuilder fromTemplate(UriTemplate baseTemplate) throws MalformedUriTemplateException
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.
baseTemplate - MalformedUriTemplateExceptionpublic Collection<UriTemplateComponent> getComponents()
UriTemplateComponent instances
found in this template.public int expressionCount()
public Expression[] getExpressions()
Expression instances found in this
template.public String[] getVariables()
Expression's, in this template.protected void parseTemplateString()
throws MalformedUriTemplateException
MalformedUriTemplateExceptionprotected Pattern getReverseMatchPattern()
public static String expand(String templateString, Map<String,Object> values) throws MalformedUriTemplateException, VariableExpansionException
Map.templateString - values - MalformedUriTemplateExceptionVariableExpansionExceptionpublic static String expandPartial(String templateString, Map<String,Object> values) throws MalformedUriTemplateException, VariableExpansionException
Map. Expressions without replacements get
preserved and still exist in the expanded URI string.templateString - URI templatevalues - ReplacementsMalformedUriTemplateExceptionVariableExpansionExceptionpublic String expand(Map<String,Object> vars) throws VariableExpansionException
vars - The values that will be used in the expansionVariableExpansionExceptionpublic String expand() throws VariableExpansionException
VariableExpansionExceptionpublic String expandPartial() throws VariableExpansionException
VariableExpansionExceptionpublic String getTemplate()
public Map<String,Object> getValues()
public UriTemplate withDefaultDateFormat(String dateFormatString)
dateFormatString - @Deprecated public UriTemplate withDefaultDateFormat(DateFormat dateFormat)
withDefaultDateFormatdateFormat - public UriTemplate set(String variableName, Object value)
variableName - value - public boolean hasVariable(String variableName)
UriTemplate contains the variableName.variableName - 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 static boolean containsOperator(String op)
op - Copyright © 2012-04-30–2016 Ryan J. McDonough. All rights reserved.