Package org.restlet.routing
Class Template
java.lang.Object
org.restlet.routing.Template
String template with a pluggable model. Supports both formatting and parsing.
The template variables can be inserted using the "{name}" syntax and
described using the modifiable map of variable descriptors. When no
descriptor is found for a given variable, the template logic uses its default
variable property initialized using the default
Note that the variable descriptors can be changed before the first parsing or matching call. After that point, changes won't be taken into account.
Format and parsing methods are specially available to deal with requests and response. See
Variable
constructor.Note that the variable descriptors can be changed before the first parsing or matching call. After that point, changes won't be taken into account.
Format and parsing methods are specially available to deal with requests and response. See
format(Request, Response) and
parse(String, Request).- Author:
- Jerome Louvel
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMode where all characters must match the template and size be identical.static final intMode where characters at the beginning must match the template. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.Constructor.Template(String pattern, int matchingMode, int defaultType, String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed) Constructor.Template(String pattern, int matchingMode, int defaultType, String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed, boolean encodingVariables) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionCreates a formatted string based on the given map of values.Creates a formatted string based on the given request and response.Creates a formatted string based on the given variable resolver.Returns the default variable.Returns the logger to use.intReturns the matching mode to use when parsing a formatted reference.Returns the pattern to use for formatting or parsing.Returns the list of variable names in the template.Returns the modifiable map of variable descriptors.booleanIndicates if the variables must be encoded when formatting the template.intIndicates if the current pattern matches the given formatted string.intAttempts to parse a formatted reference.intAttempts to parse a formatted reference.intAttempts to parse a formatted reference.voidsetDefaultVariable(Variable defaultVariable) Sets the variable to use, if no variable is given.voidsetEncodingVariables(boolean encodingVariables) Indicates if the variables must be encoded when formatting the template.voidSets the logger to use.voidsetMatchingMode(int matchingMode) Sets the matching mode to use when parsing a formatted reference.voidsetPattern(String pattern) Sets the pattern to use for formatting or parsing.voidsetVariables(Map<String, Variable> variables) Sets the modifiable map of variables.
-
Field Details
-
MODE_EQUALS
public static final int MODE_EQUALSMode where all characters must match the template and size be identical.- See Also:
-
MODE_STARTS_WITH
public static final int MODE_STARTS_WITHMode where characters at the beginning must match the template.- See Also:
-
-
Constructor Details
-
Template
Default constructor. Each variable matches any sequence of characters by default. When parsing, the template will attempt to match the whole template. When formatting, the variable are replaced by an empty string if they don't exist in the model.- Parameters:
pattern- The pattern to use for formatting or parsing.
-
Template
Constructor.- Parameters:
pattern- The pattern to use for formatting or parsing.matchingMode- The matching mode to use when parsing a formatted reference.
-
Template
public Template(String pattern, int matchingMode, int defaultType, String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed) Constructor.- Parameters:
pattern- The pattern to use for formatting or parsing.matchingMode- The matching mode to use when parsing a formatted reference.defaultType- The default type of variables with no descriptor.defaultDefaultValue- The default value for null variables with no descriptor.defaultRequired- The default required flag for variables with no descriptor.defaultFixed- The default fixed value for variables with no descriptor.
-
Template
public Template(String pattern, int matchingMode, int defaultType, String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed, boolean encodingVariables) Constructor.- Parameters:
pattern- The pattern to use for formatting or parsing.matchingMode- The matching mode to use when parsing a formatted reference.defaultType- The default type of variables with no descriptor.defaultDefaultValue- The default value for null variables with no descriptor.defaultRequired- The default required flag for variables with no descriptor.defaultFixed- The default fixed value for variables with no descriptor.encodingVariables- True if the variables must be encoded when formatting the template.
-
-
Method Details
-
format
Creates a formatted string based on the given map of values.- Parameters:
values- The values to use when formatting.- Returns:
- The formatted string.
- See Also:
-
format
Creates a formatted string based on the given request and response.- Parameters:
request- The request to use as a model.response- The response to use as a model.- Returns:
- The formatted string.
- See Also:
-
format
Creates a formatted string based on the given variable resolver.- Parameters:
resolver- The variable resolver to use.- Returns:
- The formatted string.
-
getDefaultVariable
Returns the default variable.- Returns:
- The default variable.
-
getLogger
Returns the logger to use.- Returns:
- The logger to use.
-
getMatchingMode
public int getMatchingMode()Returns the matching mode to use when parsing a formatted reference.- Returns:
- The matching mode to use when parsing a formatted reference.
-
getPattern
Returns the pattern to use for formatting or parsing.- Returns:
- The pattern to use for formatting or parsing.
-
getVariableNames
Returns the list of variable names in the template.- Returns:
- The list of variable names.
-
getVariables
Returns the modifiable map of variable descriptors. Creates a new instance if no one has been set. Note that those variables are only descriptors that can influence the way parsing and formatting is done, they don't contain the actual value parsed.- Returns:
- The modifiable map of variables.
-
isEncodingVariables
public boolean isEncodingVariables()Indicates if the variables must be encoded when formatting the template.- Returns:
- True if the variables must be encoded when formatting the template, false otherwise.
-
match
Indicates if the current pattern matches the given formatted string.- Parameters:
formattedString- The formatted string to match.- Returns:
- The number of matched characters or -1 if the match failed.
-
parse
Attempts to parse a formatted reference. If the parsing succeeds, the given request's attributes are updated.
Note that the values parsed are directly extracted from the formatted reference and are therefore not percent-decoded.- Parameters:
formattedString- The string to parse.variables- The map of variables to update.- Returns:
- The number of matched characters or -1 if no character matched.
- See Also:
-
parse
Attempts to parse a formatted reference. If the parsing succeeds, the given request's attributes are updated.
Note that the values parsed are directly extracted from the formatted reference and are therefore not percent-decoded.- Parameters:
formattedString- The string to parse.variables- The map of variables to update.loggable- True if the parsing should be logged.- Returns:
- The number of matched characters or -1 if no character matched.
- See Also:
-
parse
Attempts to parse a formatted reference. If the parsing succeeds, the given request's attributes are updated.
Note that the values parsed are directly extracted from the formatted reference and are therefore not percent-decoded.- Parameters:
formattedString- The string to parse.request- The request to update.- Returns:
- The number of matched characters or -1 if no character matched.
- See Also:
-
setDefaultVariable
Sets the variable to use, if no variable is given.- Parameters:
defaultVariable-
-
setEncodingVariables
public void setEncodingVariables(boolean encodingVariables) Indicates if the variables must be encoded when formatting the template.- Parameters:
encodingVariables- True if the variables must be encoded when formatting the template.
-
setLogger
Sets the logger to use.- Parameters:
logger- The logger to use.
-
setMatchingMode
public void setMatchingMode(int matchingMode) Sets the matching mode to use when parsing a formatted reference.- Parameters:
matchingMode- The matching mode to use when parsing a formatted reference.
-
setPattern
Sets the pattern to use for formatting or parsing.- Parameters:
pattern- The pattern to use for formatting or parsing.
-
setVariables
Sets the modifiable map of variables.- Parameters:
variables- The modifiable map of variables.
-