org.apache.beehive.netui.core.urltemplates
Class URLTemplate

Object
  extended by URLTemplate
All Implemented Interfaces:
Serializable

public class URLTemplate
extends Object
implements Serializable

The class to format a URL defined by url-template-config template given by values for a set of tokens.

See Also:
Serialized Form

Nested Class Summary
protected  class URLTemplate.TemplateItem
           
 
Constructor Summary
URLTemplate(String template)
          Create a URLTemplate from a url-template-config template.
URLTemplate(String template, String name)
          Create a URLTemplate from a url-template-config template.
URLTemplate(URLTemplate template)
          Copy constructor to create a URLTemplate from an existing URLTemplate.
 
Method Summary
protected  void appendToResult(InternalStringBuilder result, String value)
           
 String format()
          Return the String representation of the URL after replacing the tokens in the template with their associated values.
 String format(boolean removeUnsetTokens)
          Return the String representation of the URL after replacing the tokens in the template with their associated values.
 String getName()
           
 String getTemplate()
          Retrieve the String form of the template.
 void setName(String name)
           
 void setTemplate(String template)
          Reset the String form of the template.
 void substitute(Map tokensAndValues)
          Replace a set of tokens in the template with a corresponding set of values.
 void substitute(String token, int value)
          Replace a single token in the template with a corresponding int value.
 void substitute(String token, String value)
          Replace a single token in the template with a corresponding String value.
 String toString()
          Return the String representation of the URL after replacing the tokens in the template with their associated values.
 boolean verify(Collection knownTokens, Collection requiredTokens)
          Verification will ensure the URL template conforms to a valid format for known tokens and contains the required tokens.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

URLTemplate

public URLTemplate(String template)
Create a URLTemplate from a url-template-config template.

Parameters:
template - the string form of the template from url-template-config.

URLTemplate

public URLTemplate(String template,
                   String name)
Create a URLTemplate from a url-template-config template.

Parameters:
template - the string form of the template from url-template-config.
name - the name of the template

URLTemplate

public URLTemplate(URLTemplate template)
Copy constructor to create a URLTemplate from an existing URLTemplate.

Note that this is not truly a complete copy because the Map of the replacement values for the given tokens is not copied. This copy will just have an empty map of token values so that it is "cleared" and ready to format another URL.

Parameters:
template - the URLTemplate to copy.
Method Detail

setTemplate

public void setTemplate(String template)
Reset the String form of the template.

Should call verify after setting a new template.

Parameters:
template - the string form of the template from url-template-config.

getTemplate

public String getTemplate()
Retrieve the String form of the template.

Returns:
the string form of the template.

setName

public void setName(String name)

getName

public String getName()

verify

public boolean verify(Collection knownTokens,
                      Collection requiredTokens)
Verification will ensure the URL template conforms to a valid format for known tokens and contains the required tokens. It will also parse the tokens and literal data into a list to improve the replacement performance when constructing the final URL string.

Allow clients to define a set of required and known tokens for the template verification. Tokens are expected to be qualified in braces. E.g. {url:path}

If the template does not contain the required tokens or if the format of a known token is incorrect, this method will log the error and return false.

Should call verify after creating a new template.

Parameters:
knownTokens - the collection of known tokens (Strings) for a valid template.
requiredTokens - the collection of required tokens (Strings) in a valid template.
Returns:
true if the template conforms to a valid format, otherwise return false.

substitute

public void substitute(Map tokensAndValues)
Replace a set of tokens in the template with a corresponding set of values. This assumes that there is an ordered one-to-one relationship. Tokens are expected to be qualified in braces. E.g. {url:path}


substitute

public void substitute(String token,
                       String value)
Replace a single token in the template with a corresponding String value. Tokens are expected to be qualified in braces. E.g. {url:path}


substitute

public void substitute(String token,
                       int value)
Replace a single token in the template with a corresponding int value. Tokens are expected to be qualified in braces. E.g. {url:port}


toString

public String toString()
Return the String representation of the URL after replacing the tokens in the template with their associated values. If there is no value for a token, the token is discarded/removed. I.E. It will not be part of the returned String.

Overrides:
toString in class Object
Returns:
the url

format

public String format()
Return the String representation of the URL after replacing the tokens in the template with their associated values. If there is no value for a token, the token is discarded/removed. I.E. It will not be part of the returned String.

Returns:
the url

format

public String format(boolean removeUnsetTokens)
Return the String representation of the URL after replacing the tokens in the template with their associated values. If the boolean argument is true, then the unset template tokens are removed. Otherwise, do not cleanup the unset tokens.

Parameters:
removeUnsetTokens - flag to tell URLTemplate to remove or leave the unset tokens in the URL.
Returns:
the url

appendToResult

protected void appendToResult(InternalStringBuilder result,
                              String value)