org.sakaiproject.entitybroker.util
Class TemplateParseUtil

java.lang.Object
  extended by org.sakaiproject.entitybroker.util.TemplateParseUtil

public class TemplateParseUtil
extends Object

Utility class to handle the URL template parsing (entity template parsing)

Author:
Aaron Zeckoski (aaron@caret.cam.ac.uk)

Nested Class Summary
static class TemplateParseUtil.PreProcessedTemplate
          Contains the data for templates, each template must have a template key and the template itself
static class TemplateParseUtil.ProcessedTemplate
          Contains the processed template with the values from the processed input string that was determined to be related to this template
static class TemplateParseUtil.Template
          Represents a parseable template (which is basically a key and the template string), the array which defines the set of template keys is PARSE_TEMPLATE_KEYS
Rules for parse templates: 1) "{","}", and SEPARATOR are special characters and must be used as indicated only 2) Must begin with a SEPARATOR, must not end with a SEPARATOR 3) must begin with "/{prefix}" (use the SEPARATOR and PREFIX constants) 3) each {var} can only be used once in a template 4) {var} can never touch each other (i.e /{var1}{var2}/{id} is invalid) 5) each {var} can only have the chars from VALID_VAR_CHARS 6) parse templates can only have the chars from VALID_TEMPLATE_CHARS 7) Empty braces ({}) cannot appear in the template
 
Field Summary
static String BRACES
           
static List<TemplateParseUtil.PreProcessedTemplate> defaultPreprocessedTemplates
          Stores the preloaded processed default templates
static List<TemplateParseUtil.Template> defaultTemplates
          Stores the preloaded default templates
static String DIRECT_PREFIX
           
static String DIRECT_PREFIX_SLASH
           
static String DOT_EXTENSION
          The extension with a period in front marker (Example value: ".xml")
static String EXTENSION
          The entity extension (format) marker (Example value: "xml")
static String ID
          The entity ID marker (Example value: "123")
static String[] PARSE_TEMPLATE_KEYS
          Defines the order that parse templates will be processed in and the set of parse template types (keys) which must be defined, the first one to match will be used when parsing in a path
static char PERIOD
           
static String PREFIX
          The entity prefix marker (Example value: "myprefix")
static String PREFIX_VARIABLE
           
static String QUERY_STRING
          The value in the query string (without a leading ?)
static String QUESTION_QUERY_STRING
          The value in the query string (with a leading ?)
static char SEPARATOR
           
static String TEMPLATE_DELETE
          Defines the parse template for the "delete" operation, access the data to remove a record, typically /{prefix}/{id}/delete
static String TEMPLATE_EDIT
          Defines the parse template for the "edit" operation, access the data to modify a record, typically /{prefix}/{id}/edit
static String TEMPLATE_LIST
          Defines the parse template for the "list" operation, return a list of all records, typically /{prefix}
static String TEMPLATE_NEW
          Defines the parse template for the "new" operation, return a form for creating a new record, typically /{prefix}/new
static String TEMPLATE_PREFIX
           
static String TEMPLATE_SHOW
          Defines the parse template for the "show" operation, access a record OR POST operations related to a record, typically /{prefix}/{id}
static String VALID_INPUT_CHARS
          Defines the valid chars for a parser input (e.g. entity reference)
static String VALID_TEMPLATE_CHARS
          Defines the valid chars for a template
static String VALID_TEMPLATE_CHARS_OUTGOING
          Defines the valid template chars for an outgoing template (allows ?)
static String VALID_VAR_CHARS
          Defines the valid chars for a replacement variable
 
Constructor Summary
TemplateParseUtil()
           
 
Method Summary
static String[] findExtension(String input)
          Find the extension from a string and return the string without the extension and the extension, an extension is a period (".") followed by any number of non-periods, the original input is returned as the 0th item in the array
returned array contains 3 strings:
0 = the original input string
1 = the string without the extension or the original if it has none
2 = the extension OR null if there is no extension
static String getDefaultTemplate(String templateKey)
          Get a default template for a specific template key
static String mergeTemplate(String template, Map<String,String> segments)
          Takes a template and replaces the segment keys with the segment values, keys should not have {} around them yet as these will be added around each key in the segments map
static TemplateParseUtil.ProcessedTemplate parseTemplate(String input, List<TemplateParseUtil.PreProcessedTemplate> preprocessed)
          Parse a string and attempt to match it to a template and then return the match information along with all the parsed out keys and values
static TemplateParseUtil.PreProcessedTemplate preprocessTemplate(TemplateParseUtil.Template t)
          process a template into a preprocessed template which can be cached
static List<TemplateParseUtil.PreProcessedTemplate> preprocessTemplates(List<TemplateParseUtil.Template> templates)
          Process the templates before attempting to match them, this is here so we can reduce the load of reprocessing the same templates over and over
static String validateOutgoingTemplate(String template)
          Validates an outgoing template to make sure it is valid
static void validateTemplate(String template)
          Validate a template, if invalid then an exception is thrown
static void validateTemplateKey(String templateKey)
          Check if a templateKey is valid, if not then throws IllegalArgumentException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEPARATOR

public static final char SEPARATOR
See Also:
Constant Field Values

PERIOD

public static final char PERIOD
See Also:
Constant Field Values

BRACES

public static final String BRACES
See Also:
Constant Field Values

PREFIX

public static final String PREFIX
The entity prefix marker (Example value: "myprefix")

See Also:
Constant Field Values

ID

public static final String ID
The entity ID marker (Example value: "123")

See Also:
Constant Field Values

EXTENSION

public static final String EXTENSION
The entity extension (format) marker (Example value: "xml")

See Also:
Constant Field Values

DOT_EXTENSION

public static final String DOT_EXTENSION
The extension with a period in front marker (Example value: ".xml")

See Also:
Constant Field Values

QUERY_STRING

public static final String QUERY_STRING
The value in the query string (without a leading ?), '' if non available (Example value: "auto=true")

See Also:
Constant Field Values

QUESTION_QUERY_STRING

public static final String QUESTION_QUERY_STRING
The value in the query string (with a leading ?), '' if non available (Example value: "?auto=true")

See Also:
Constant Field Values

PREFIX_VARIABLE

public static final String PREFIX_VARIABLE
See Also:
Constant Field Values

TEMPLATE_PREFIX

public static final String TEMPLATE_PREFIX
See Also:
Constant Field Values

DIRECT_PREFIX

public static final String DIRECT_PREFIX
See Also:
Constant Field Values

DIRECT_PREFIX_SLASH

public static final String DIRECT_PREFIX_SLASH
See Also:
Constant Field Values

VALID_VAR_CHARS

public static final String VALID_VAR_CHARS
Defines the valid chars for a replacement variable

See Also:
Constant Field Values

VALID_INPUT_CHARS

public static final String VALID_INPUT_CHARS
Defines the valid chars for a parser input (e.g. entity reference)

See Also:
Constant Field Values

VALID_TEMPLATE_CHARS

public static final String VALID_TEMPLATE_CHARS
Defines the valid chars for a template

See Also:
Constant Field Values

VALID_TEMPLATE_CHARS_OUTGOING

public static final String VALID_TEMPLATE_CHARS_OUTGOING
Defines the valid template chars for an outgoing template (allows ?)

See Also:
Constant Field Values

TEMPLATE_LIST

public static final String TEMPLATE_LIST
Defines the parse template for the "list" operation, return a list of all records, typically /{prefix}

See Also:
Constant Field Values

TEMPLATE_SHOW

public static final String TEMPLATE_SHOW
Defines the parse template for the "show" operation, access a record OR POST operations related to a record, typically /{prefix}/{id}

See Also:
Constant Field Values

TEMPLATE_NEW

public static final String TEMPLATE_NEW
Defines the parse template for the "new" operation, return a form for creating a new record, typically /{prefix}/new

See Also:
Constant Field Values

TEMPLATE_EDIT

public static final String TEMPLATE_EDIT
Defines the parse template for the "edit" operation, access the data to modify a record, typically /{prefix}/{id}/edit

See Also:
Constant Field Values

TEMPLATE_DELETE

public static final String TEMPLATE_DELETE
Defines the parse template for the "delete" operation, access the data to remove a record, typically /{prefix}/{id}/delete

See Also:
Constant Field Values

PARSE_TEMPLATE_KEYS

public static final String[] PARSE_TEMPLATE_KEYS
Defines the order that parse templates will be processed in and the set of parse template types (keys) which must be defined, the first one to match will be used when parsing in a path


defaultTemplates

public static final List<TemplateParseUtil.Template> defaultTemplates
Stores the preloaded default templates


defaultPreprocessedTemplates

public static final List<TemplateParseUtil.PreProcessedTemplate> defaultPreprocessedTemplates
Stores the preloaded processed default templates

Constructor Detail

TemplateParseUtil

public TemplateParseUtil()
Method Detail

validateTemplateKey

public static void validateTemplateKey(String templateKey)
Check if a templateKey is valid, if not then throws IllegalArgumentException

Parameters:
templateKey - a key from the set of template keys PARSE_TEMPLATE_KEYS

getDefaultTemplate

public static String getDefaultTemplate(String templateKey)
Get a default template for a specific template key

Parameters:
templateKey - a key from the set of template keys PARSE_TEMPLATE_KEYS
Returns:
the template
Throws:
IllegalArgumentException - if the template key is invalid

validateTemplate

public static void validateTemplate(String template)
Validate a template, if invalid then an exception is thrown

Parameters:
template - a parse template

validateOutgoingTemplate

public static String validateOutgoingTemplate(String template)
Validates an outgoing template to make sure it is valid

Parameters:
template - an outgoing template, if starts with / then it will be used as is and redirected to, otherwise it will have the direct URL prefixed and will be forwarded
Returns:
the template which should be completely valid

mergeTemplate

public static String mergeTemplate(String template,
                                   Map<String,String> segments)
Takes a template and replaces the segment keys with the segment values, keys should not have {} around them yet as these will be added around each key in the segments map

Parameters:
template - a parse template with {variables} in it
segments - a map of all possible segment keys and values, unused keys will be ignored
Returns:
the template with replacement values filled in
Throws:
IllegalArgumentException - if all template variables cannot be replaced or template is empty/null

findExtension

public static String[] findExtension(String input)
Find the extension from a string and return the string without the extension and the extension, an extension is a period (".") followed by any number of non-periods, the original input is returned as the 0th item in the array
returned array contains 3 strings:
0 = the original input string
1 = the string without the extension or the original if it has none
2 = the extension OR null if there is no extension

Parameters:
input - any string
Returns:
an array with the string without the extension or the original if it has none in position 1 and the extension in the position 2 (or null if no extension), position 0 holds the original input string

parseTemplate

public static TemplateParseUtil.ProcessedTemplate parseTemplate(String input,
                                                                List<TemplateParseUtil.PreProcessedTemplate> preprocessed)
Parse a string and attempt to match it to a template and then return the match information along with all the parsed out keys and values

Parameters:
input - a string which we want to attempt to match to one of the templates
preprocessed - the analyzed templates to attempt to match in the order they should attempt the match, can be a single template or multiples, use preprocessTemplates(List) to create this (recommend caching the preprocessed templates to avoid reprocessing them over and over)
Returns:
a the processed template analysis object OR null if no matches

preprocessTemplates

public static List<TemplateParseUtil.PreProcessedTemplate> preprocessTemplates(List<TemplateParseUtil.Template> templates)
Process the templates before attempting to match them, this is here so we can reduce the load of reprocessing the same templates over and over

Parameters:
templates - the templates to attempt to preprocess, can be a single template or multiples
Returns:
the list of preprocessed templates (in the same order as input)

preprocessTemplate

public static TemplateParseUtil.PreProcessedTemplate preprocessTemplate(TemplateParseUtil.Template t)
process a template into a preprocessed template which can be cached

Parameters:
t - the template
Returns:
the preprocessed template


Copyright © 2007-2013 Sakai Project. All Rights Reserved.