org.restlet.util
Class Template

java.lang.Object
  extended by org.restlet.util.Template

public class Template
extends java.lang.Object

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 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:
Resolver, URI Template specification

Field Summary
static int MODE_EQUALS
           
static int MODE_STARTS_WITH
           
 
Constructor Summary
Template(java.util.logging.Logger logger, java.lang.String pattern)
          Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.
Template(java.util.logging.Logger logger, java.lang.String pattern, int matchingMode)
          Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.
Template(java.util.logging.Logger logger, java.lang.String pattern, int matchingMode, int defaultType, java.lang.String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed)
          Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.
Template(java.util.logging.Logger logger, java.lang.String pattern, int matchingMode, int defaultType, java.lang.String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed, boolean encodeVariables)
          Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.
Template(java.lang.String pattern)
          Default constructor.
Template(java.lang.String pattern, int matchingMode)
          Constructor.
Template(java.lang.String pattern, int matchingMode, int defaultType, java.lang.String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed)
          Constructor.
 
Method Summary
 java.lang.String format(java.util.Map<java.lang.String,?> values)
          Creates a formatted string based on the given map of values.
 java.lang.String format(Request request, Response response)
          Creates a formatted string based on the given request and response.
 java.lang.String format(Resolver<java.lang.String> resolver)
          Creates a formatted string based on the given variable resolver.
 Variable getDefaultVariable()
          Returns the default variable.
 java.util.logging.Logger getLogger()
          Returns the logger to use.
 int getMatchingMode()
          Returns the matching mode to use when parsing a formatted reference.
 java.lang.String getPattern()
          Returns the pattern to use for formatting or parsing.
 java.util.List<java.lang.String> getVariableNames()
          Returns the list of variable names in the template.
 java.util.Map<java.lang.String,Variable> getVariables()
          Returns the modifiable map of variable descriptors.
 boolean isEncodeVariables()
          Indicates if the variables must be encoded when formatting the template.
 int match(java.lang.String formattedString)
          Indicates if the current pattern matches the given formatted string.
 int parse(java.lang.String formattedString, java.util.Map<java.lang.String,java.lang.Object> variables)
          Attempts to parse a formatted reference.
 int parse(java.lang.String formattedString, Request request)
          Attempts to parse a formatted reference.
 void setDefaultVariable(Variable defaultVariable)
          Sets the variable to use, if no variable is given.
 void setEncodeVariables(boolean encodeVariables)
          Indicates if the variables must be encoded when formatting the template.
 void setLogger(java.util.logging.Logger logger)
          Sets the logger to use.
 void setMatchingMode(int matchingMode)
          Sets the matching mode to use when parsing a formatted reference.
 void setPattern(java.lang.String pattern)
          Sets the pattern to use for formatting or parsing.
 void setVariables(java.util.Map<java.lang.String,Variable> variables)
          Sets the modifiable map of variables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODE_EQUALS

public static final int MODE_EQUALS
See Also:
Constant Field Values

MODE_STARTS_WITH

public static final int MODE_STARTS_WITH
See Also:
Constant Field Values
Constructor Detail

Template

@Deprecated
public Template(java.util.logging.Logger logger,
                           java.lang.String pattern)
Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.

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:
logger - The logger to use.
pattern - The pattern to use for formatting or parsing.

Template

@Deprecated
public Template(java.util.logging.Logger logger,
                           java.lang.String pattern,
                           int matchingMode)
Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.

Constructor.

Parameters:
logger - The logger to use.
pattern - The pattern to use for formatting or parsing.
matchingMode - The matching mode to use when parsing a formatted reference.

Template

@Deprecated
public Template(java.util.logging.Logger logger,
                           java.lang.String pattern,
                           int matchingMode,
                           int defaultType,
                           java.lang.String defaultDefaultValue,
                           boolean defaultRequired,
                           boolean defaultFixed)
Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.

Constructor.

Parameters:
logger - The logger to use.
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

@Deprecated
public Template(java.util.logging.Logger logger,
                           java.lang.String pattern,
                           int matchingMode,
                           int defaultType,
                           java.lang.String defaultDefaultValue,
                           boolean defaultRequired,
                           boolean defaultFixed,
                           boolean encodeVariables)
Deprecated. Use the constructor with logger instead. The logger can still be set using the setLogger(Logger) method.

Constructor.

Parameters:
logger - The logger to use.
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.
encodeVariables - True if the variables must be encoded when formatting the template.

Template

public Template(java.lang.String pattern)
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

public Template(java.lang.String pattern,
                int matchingMode)
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(java.lang.String pattern,
                int matchingMode,
                int defaultType,
                java.lang.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.
Method Detail

format

public java.lang.String format(java.util.Map<java.lang.String,?> values)
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:
Resolver.createResolver(Map)

format

public java.lang.String format(Request request,
                               Response response)
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:
Resolver.createResolver(Request, Response)

format

public java.lang.String format(Resolver<java.lang.String> resolver)
Creates a formatted string based on the given variable resolver.

Parameters:
resolver - The variable resolver to use.
Returns:
The formatted string.

getDefaultVariable

public Variable getDefaultVariable()
Returns the default variable.

Returns:
The default variable.

getLogger

public java.util.logging.Logger 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

public java.lang.String getPattern()
Returns the pattern to use for formatting or parsing.

Returns:
The pattern to use for formatting or parsing.

getVariableNames

public java.util.List<java.lang.String> getVariableNames()
Returns the list of variable names in the template.

Returns:
The list of variable names.

getVariables

public java.util.Map<java.lang.String,Variable> 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.

isEncodeVariables

public boolean isEncodeVariables()
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

public int match(java.lang.String formattedString)
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

public int parse(java.lang.String formattedString,
                 java.util.Map<java.lang.String,java.lang.Object> variables)
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:
Reference.decode(String)

parse

public int parse(java.lang.String formattedString,
                 Request request)
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:
Reference.decode(String)

setDefaultVariable

public void setDefaultVariable(Variable defaultVariable)
Sets the variable to use, if no variable is given.

Parameters:
defaultVariable -

setEncodeVariables

public void setEncodeVariables(boolean encodeVariables)
Indicates if the variables must be encoded when formatting the template.

Parameters:
encodeVariables - True if the variables must be encoded when formatting the template.

setLogger

public void setLogger(java.util.logging.Logger logger)
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

public void setPattern(java.lang.String pattern)
Sets the pattern to use for formatting or parsing.

Parameters:
pattern - The pattern to use for formatting or parsing.

setVariables

public void setVariables(java.util.Map<java.lang.String,Variable> variables)
Sets the modifiable map of variables.

Parameters:
variables - The modifiable map of variables.


Copyright © 2005-2008 Noelios Technologies.