org.restlet.util
Class Template

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

public class Template
extends Object

String template with a model is based on a request. 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.

Model property Variable name Content type
request.confidential c boolean (true|false)
request.clientInfo.address cia String
request.clientInfo.agent cig String
request.challengeResponse.identifier cri String
request.challengeResponse.scheme crs String
request.entity.characterSet ecs String
response.entity.characterSet ECS String
request.entity.encoding ee String
response.entity.encoding EE String
request.entity.expirationDate eed Date (HTTP format)
response.entity.expirationDate EED Date (HTTP format)
request.entity.language el String
response.entity.language EL String
request.entity.modificationDate emd Date (HTTP format)
response.entity.modificationDate EMD Date (HTTP format)
request.entity.mediaType emt String
response.entity.mediaType EMT String
request.entity.size es Integer
response.entity.size ES Integer
request.entity.tag et String
response.entity.tag ET String
request.referrerRef f* Reference (see table below variable name sub-parts)
request.hostRef h* Reference (see table below variable name sub-parts)
request.method m String
request.rootRef o* Reference (see table below variable name sub-parts)
request.protocol p String
request.resourceRef r* Reference (see table below variable name sub-parts)
response.redirectRef R* Reference (see table below variable name sub-parts)
response.status S Integer
response.serverInfo.address SIA String
response.serverInfo.agent SIG String
response.serverInfo.port SIP Integer

Below is the list of name sub-parts, for Reference variables, that can replace the asterix in the variable names above:

Reference property Sub-part name Content type
authority a String
baseRef b* Reference
relativePart e String
fragment f String
hostIdentifier h String
identifier i String
path p String
query q String
remainingPart r String

Author:
Jerome Louvel (contact@noelios.com)
See Also:
URI Template specification

Field Summary
static int MODE_EQUALS
           
static int MODE_STARTS_WITH
           
 
Constructor Summary
Template(Logger logger, String pattern)
          Default constructor.
Template(Logger logger, String pattern, int matchingMode)
          Constructor.
Template(Logger logger, String pattern, int matchingMode, int defaultType, String defaultDefaultValue, boolean defaultRequired, boolean defaultFixed)
          Constructor.
 
Method Summary
 String format(Map<String,Object> variables)
          Creates a formatted string based on the given request.
 String format(Request request, Response response)
          Creates a formatted string based on the given request.
 Variable getDefaultVariable()
          Returns the default variable.
 Logger getLogger()
          Returns the logger to use.
 int getMatchingMode()
          Returns the matching mode to use when parsing a formatted reference.
 String getPattern()
          Returns the pattern to use for formatting or parsing.
 Map<String,Variable> getVariables()
          Returns the modifiable map of variables.
 int match(String formattedString)
          Indicates if the current pattern matches the given formatted string.
 int parse(String formattedString, Map<String,Object> variables)
          Attempts to parse a formatted reference.
 int parse(String formattedString, Request request)
          Attempts to parse a formatted reference.
 void setMatchingMode(int matchingMode)
          Sets the matching mode to use when parsing a formatted reference.
 void setPattern(String pattern)
          Sets the pattern to use for formatting or parsing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODE_STARTS_WITH

public static final int MODE_STARTS_WITH
See Also:
Constant Field Values

MODE_EQUALS

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

Template

public Template(Logger logger,
                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:
logger - The logger to use.
pattern - The pattern to use for formatting or parsing.

Template

public Template(Logger logger,
                String pattern,
                int matchingMode)
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

public Template(Logger logger,
                String pattern,
                int matchingMode,
                int defaultType,
                String defaultDefaultValue,
                boolean defaultRequired,
                boolean defaultFixed)
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.
Method Detail

format

public String format(Request request,
                     Response response)
Creates a formatted string based on the given request.

Parameters:
request - The request to use as a model.
response - The response to use as a model.
Returns:
The formatted string.

format

public String format(Map<String,Object> variables)
Creates a formatted string based on the given request.

Parameters:
variables - The variables to use when formatting.
Returns:
The formatted string.

getDefaultVariable

public Variable getDefaultVariable()
Returns the default variable.

Returns:
The default variable.

getLogger

public 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 String getPattern()
Returns the pattern to use for formatting or parsing.

Returns:
The pattern to use for formatting or parsing.

getVariables

public Map<String,Variable> getVariables()
Returns the modifiable map of variables.

Returns:
The modifiable map of variables.

match

public int match(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(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)

parse

public int parse(String formattedString,
                 Map<String,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)

setPattern

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

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

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.


Copyright © 2005-2007 Noelios Consulting.