Class UriTemplate
- java.lang.Object
-
- com.damnhandy.uri.template.UriTemplate
-
- All Implemented Interfaces:
Serializable
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
MapandListvalues as composite types, the library also supports the use of Java objects as well. Please see theVarExploderandDefaultVarExploderfor more info.Basic Usage:
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&apm;lang=en
- Since:
- 1.0
- Version:
- $Revision: 1.1 $
- Author:
- Ryan J. McDonough
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classUriTemplate.Encoding
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_SEPARATORprotected DateFormatdefaultDateFormatDeprecated.Replaced bydefaultDateTimeFormatter
-
Constructor Summary
Constructors Modifier Constructor Description protectedUriTemplate(LinkedList<UriTemplateComponent> components)Create a new UriTemplate.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static UriTemplateBuilderbuildFromTemplate(UriTemplate baseTemplate)Creates a newUriTemplateBuilderfrom a rootUriTemplate.static UriTemplateBuilderbuildFromTemplate(String template)Creates a newUriTemplateBuilderfrom the template string.static booleancontainsOperator(String op)static UriTemplateBuildercreateBuilder()Creates a newUriTemplateBuilderinstance.Stringexpand()Applies variable substitution the URI Template and returns the expanded URI.static Stringexpand(String templateString, Map<String,Object> values)Expands the given template string using the variable replacements in the suppliedMap.Stringexpand(Map<String,Object> vars)Expand the URI template using the supplied valuesStringexpandPartial()static StringexpandPartial(String templateString, Map<String,Object> values)Expands the given template string using the variable replacements in the suppliedMap.intexpressionCount()Returns the number of expressions found in this templatestatic UriTemplateBuilderfromTemplate(UriTemplate baseTemplate)Deprecated.static UriTemplatefromTemplate(String templateString)Creates a newUriTemplatefrom the template string.Objectget(String variableName)FIXME Comment thisCollection<UriTemplateComponent>getComponents()Returns the collection ofUriTemplateComponentinstances found in this template.Expression[]getExpressions()Returns an array ofExpressioninstances found in this template.protected PatterngetReverseMatchPattern()Returns theStringgetTemplate()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 allExpression's, in this template.booleanhasVariable(String variableName)Returns true if theUriTemplatecontains the variableName.protected voidparseTemplateString()Parse the URI template string into the template model.UriTemplateset(String variableName, Object value)Sets a value on the URI template expression variable.UriTemplateset(String variableName, Date value)Sets a Date value into the list of variable substitutions using the defaultDateFormat.UriTemplateset(Map<String,Object> values)Adds the name/value pairs in the suppliedMapto the collection of values within this URI template instance.UriTemplatewithDefaultDateFormat(String dateFormatString)UriTemplatewithDefaultDateFormat(DateFormat dateFormat)Deprecated.replaced bywithDefaultDateFormat
-
-
-
Field Detail
-
DEFAULT_SEPARATOR
public static final String DEFAULT_SEPARATOR
- See Also:
- Constant Field Values
-
defaultDateFormat
@Deprecated protected DateFormat defaultDateFormat
Deprecated.Replaced bydefaultDateTimeFormatter
-
-
Constructor Detail
-
UriTemplate
protected UriTemplate(LinkedList<UriTemplateComponent> components)
Create a new UriTemplate.- Parameters:
components-
-
-
Method Detail
-
createBuilder
public static UriTemplateBuilder createBuilder()
Creates a newUriTemplateBuilderinstance.- Returns:
- the new UriTemplateBuilder
- Since:
- 2.1.2
-
buildFromTemplate
public static UriTemplateBuilder buildFromTemplate(String template) throws MalformedUriTemplateException
Creates a newUriTemplateBuilderfrom the template string.- Parameters:
template-- Returns:
- Throws:
MalformedUriTemplateException- Since:
- 2.0
-
buildFromTemplate
public static UriTemplateBuilder buildFromTemplate(UriTemplate baseTemplate) throws MalformedUriTemplateException
Creates a new
UriTemplateBuilderfrom a rootUriTemplate. This method will create a newUriTemplatefrom the base and copy the variables from the base template to the newUriTemplate.This method is useful when the base template is less volatile than the child expression and you want to merge the two.
- Parameters:
baseTemplate-- Returns:
- Throws:
MalformedUriTemplateException- Since:
- 2.0
-
fromTemplate
public static final UriTemplate fromTemplate(String templateString) throws MalformedUriTemplateException
Creates a new
UriTemplatefrom the template string.- Parameters:
templateString-- Returns:
- Throws:
MalformedUriTemplateException- Since:
- 2.0
-
fromTemplate
@Deprecated public static UriTemplateBuilder fromTemplate(UriTemplate baseTemplate) throws MalformedUriTemplateException
Deprecated.This method is now deprecated. Use
buildFromTemplate(UriTemplate)instead.- Parameters:
baseTemplate-- Returns:
- Throws:
MalformedUriTemplateException- Since:
- 1.0
-
getComponents
public Collection<UriTemplateComponent> getComponents()
Returns the collection of
UriTemplateComponentinstances found in this template.- Returns:
- the collection of
UriTemplateComponents
-
expressionCount
public int expressionCount()
Returns the number of expressions found in this template- Returns:
-
getExpressions
public Expression[] getExpressions()
Returns an array ofExpressioninstances found in this template.- Returns:
-
getVariables
public String[] getVariables()
Returns the list of unique variable names, from allExpression's, in this template.- Returns:
-
parseTemplateString
protected void parseTemplateString() throws MalformedUriTemplateExceptionParse the URI template string into the template model.- Throws:
MalformedUriTemplateException
-
getReverseMatchPattern
protected Pattern getReverseMatchPattern()
Returns the- Returns:
-
expand
public static String expand(String templateString, Map<String,Object> values) throws MalformedUriTemplateException, VariableExpansionException
Expands the given template string using the variable replacements in the suppliedMap.- Parameters:
templateString-values-- Returns:
- the expanded URI as a String
- Throws:
MalformedUriTemplateExceptionVariableExpansionException- Since:
- 1.0
-
expandPartial
public static String expandPartial(String templateString, Map<String,Object> values) throws MalformedUriTemplateException, VariableExpansionException
Expands the given template string using the variable replacements in the suppliedMap. Expressions without replacements get preserved and still exist in the expanded URI string.- Parameters:
templateString- URI templatevalues- Replacements- Returns:
- The expanded URI as a String
- Throws:
MalformedUriTemplateExceptionVariableExpansionException
-
expand
public String expand(Map<String,Object> vars) throws VariableExpansionException
Expand the URI template using the supplied values- Parameters:
vars- The values that will be used in the expansion- Returns:
- the expanded URI as a String
- Throws:
VariableExpansionException- Since:
- 1.0
-
expand
public String expand() throws VariableExpansionException
Applies variable substitution the URI Template and returns the expanded URI.- Returns:
- the expanded URI as a String
- Throws:
VariableExpansionException- Since:
- 1.0
-
expandPartial
public String expandPartial() throws VariableExpansionException
- Returns:
- Throws:
VariableExpansionException
-
getTemplate
public String getTemplate()
Returns the original URI template expression.- Returns:
- the template string
- Since:
- 1.1.4
-
getValues
public Map<String,Object> getValues()
Returns the collection of name/value pairs contained in the instance.- Returns:
- the name value pairs
- Since:
- 1.0
-
withDefaultDateFormat
public UriTemplate withDefaultDateFormat(String dateFormatString)
- Parameters:
dateFormatString-- Returns:
- the date format used to render dates
- Since:
- 1.0
-
withDefaultDateFormat
@Deprecated public UriTemplate withDefaultDateFormat(DateFormat dateFormat)
Deprecated.replaced bywithDefaultDateFormat- Parameters:
dateFormat-- Returns:
- the date format used to render dates
- Since:
- 1.0
-
set
public UriTemplate set(String variableName, Object value)
Sets a value on the URI template expression variable.- Parameters:
variableName-value-- Returns:
- Since:
- 1.0
-
hasVariable
public boolean hasVariable(String variableName)
Returns true if theUriTemplatecontains the variableName.- Parameters:
variableName-- Returns:
-
set
public UriTemplate set(String variableName, Date value)
Sets a Date value into the list of variable substitutions using the defaultDateFormat.- Parameters:
variableName-value-- Returns:
- Since:
- 1.0
-
set
public UriTemplate set(Map<String,Object> values)
Adds the name/value pairs in the suppliedMapto the collection of values within this URI template instance.- Parameters:
values-- Returns:
- Since:
- 1.0
-
containsOperator
public static boolean containsOperator(String op)
- Parameters:
op-- Returns:
-
-