org.weborganic.uri.template
Class Variable

java.lang.Object
  extended by org.weborganic.uri.template.Variable

public class Variable
extends Object

A variable in a URL pattern or template. Expansion rule (4.4.1):

 "In a variable ('var') expansion, if the variable is defined then substitute the value of 
 the variable, otherwise substitute the default value. 
 If no default value is given then substitute with the empty string."
 
Characters (4.2):
 var         = varname [ "=" vardefault ]
 vars        = var [ *("," var) ]
 varname     = (ALPHA / DIGIT)*
              (ALPHA / DIGIT / "." / "_" / "-" )
 vardefault  = *(unreserved / pct-encoded)
 

Version:
6 February 2009
Author:
Christophe Lauret
See Also:
URI Template (Internet Draft 3), RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax

Nested Class Summary
static class Variable.Reserved
          Used for reserved variable names.
 
Constructor Summary
Variable(String name)
          Creates a new variable.
Variable(String name, String def)
          Creates a new variable.
Variable(Variable.Reserved reserved)
          Creates a new variable.
 
Method Summary
 String defaultValue()
          Returns the default value for this variable.
 boolean equals(Object o)
           
 int hashCode()
           
static boolean isValidName(String name)
          Indicates whether the variable has a valid name according to the specifications.
static boolean isValidValue(String value)
          Indicates whether the variable has a valid value according to the specifications.
 String name()
          Returns the name of this variable.
static Variable parse(String exp)
          Parses the given expression and returns the corresponding instance.
 String toString()
           
 String value(Parameters parameters)
          Returns the expanded value of this variable.
 String[] values(Parameters parameters)
          Returns the expanded value of this variable.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Variable

public Variable(Variable.Reserved reserved)
         throws NullPointerException,
                IllegalArgumentException
Creates a new variable.

Parameters:
reserved - The name of the variable.
Throws:
NullPointerException - If the specified name is null.
IllegalArgumentException - If the specified name is an empty string.

Variable

public Variable(String name)
         throws NullPointerException,
                IllegalArgumentException
Creates a new variable.

Parameters:
name - The name of the variable.
Throws:
NullPointerException - If the specified name is null.
IllegalArgumentException - If the specified name is an empty string.

Variable

public Variable(String name,
                String def)
         throws NullPointerException,
                IllegalArgumentException
Creates a new variable.

Parameters:
name - The name of the variable.
def - The default value for the variable.
Throws:
NullPointerException - If the specified name is null.
IllegalArgumentException - If the specified name is an empty string.
Method Detail

name

public String name()
Returns the name of this variable. This method never return null.

Returns:
The name of this variable.

defaultValue

public String defaultValue()
Returns the default value for this variable. This method never return null.

Returns:
The default value for this variable.

value

public String value(Parameters parameters)
Returns the expanded value of this variable. If no value is specified for this variable, the default value is returned instead.

Parameters:
parameters - The parameters.
Returns:
The value.

values

public String[] values(Parameters parameters)
Returns the expanded value of this variable. If no values are specified for this variable, the default value is returned instead.

Parameters:
parameters - The parameters.
Returns:
The values.

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

parse

public static Variable parse(String exp)
                      throws URITemplateSyntaxException
Parses the given expression and returns the corresponding instance.

Parameters:
exp - The expression to parse.
Returns:
the corresponding variable.
Throws:
URITemplateSyntaxException - If the expression cannot be parsed

isValidName

public static boolean isValidName(String name)
Indicates whether the variable has a valid name according to the specifications.

Parameters:
name - The name of the variable.
Returns:
true if the name is valid; false otherwise.

isValidValue

public static boolean isValidValue(String value)
Indicates whether the variable has a valid value according to the specifications.

Parameters:
value - The value of the variable.
Returns:
true if the name is not valid; false otherwise.