Class TemplateParseUtil
- java.lang.Object
-
- 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
Nested Classes Modifier and Type Class Description static classTemplateParseUtil.PreProcessedTemplateContains the data for templates, each template must have a template key and the template itselfstatic classTemplateParseUtil.ProcessedTemplateContains the processed template with the values from the processed input string that was determined to be related to this templatestatic classTemplateParseUtil.TemplateRepresents a parseable template (which is basically a key and the template string), the array which defines the set of template keys isPARSE_TEMPLATE_KEYS
Rules for parse templates: 1) "{","}", andSEPARATORare special characters and must be used as indicated only 2) Must begin with aSEPARATOR, must not end with aSEPARATOR3) must begin with "/{prefix}" (use theSEPARATORandPREFIXconstants) 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 fromVALID_VAR_CHARS6) parse templates can only have the chars fromVALID_TEMPLATE_CHARS7) Empty braces ({}) cannot appear in the template
-
Field Summary
Fields Modifier and Type Field Description static StringBRACESstatic Set<String>commonExtensionsContains a set of all the common extensionsstatic List<TemplateParseUtil.PreProcessedTemplate>defaultPreprocessedTemplatesStores the preloaded processed default templatesstatic List<TemplateParseUtil.Template>defaultTemplatesStores the preloaded default templatesstatic StringDIRECT_PREFIXstatic StringDIRECT_PREFIX_SLASHstatic StringDOT_EXTENSIONThe extension with a period in front marker (Example value: ".xml")static StringEXTENSIONThe entity extension (format) marker (Example value: "xml")static StringIDThe entity ID marker (Example value: "123")static String[]PARSE_TEMPLATE_KEYSDefines 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 pathstatic charPERIODstatic StringPREFIXThe entity prefix marker (Example value: "myprefix")static StringPREFIX_VARIABLEstatic StringQUERY_STRINGThe value in the query string (without a leading ?)static StringQUESTION_QUERY_STRINGThe value in the query string (with a leading ?)static charSEPARATORstatic StringTEMPLATE_DELETEDefines the parse template for the "delete" operation, access the data to remove a record, typically /{prefix}/{id}/deletestatic StringTEMPLATE_EDITDefines the parse template for the "edit" operation, access the data to modify a record, typically /{prefix}/{id}/editstatic StringTEMPLATE_LISTDefines the parse template for the "list" operation, return a list of all records, typically /{prefix}static StringTEMPLATE_NEWDefines the parse template for the "new" operation, return a form for creating a new record, typically /{prefix}/newstatic StringTEMPLATE_PREFIXstatic StringTEMPLATE_SHOWDefines the parse template for the "show" operation, access a record OR POST operations related to a record, typically /{prefix}/{id}static StringVALID_INPUT_CHARSDefines the valid chars for a parser input (e.g. entity reference)static StringVALID_TEMPLATE_CHARSDefines the valid chars for a templatestatic StringVALID_TEMPLATE_CHARS_OUTGOINGDefines the valid template chars for an outgoing template (allows ?)static StringVALID_VAR_CHARSDefines the valid chars for a replacement variable
-
Constructor Summary
Constructors Constructor Description TemplateParseUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 extensionstatic StringgetDefaultTemplate(String templateKey)Get a default template for a specific template keystatic StringmergeTemplate(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 mapstatic TemplateParseUtil.ProcessedTemplateparseTemplate(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 valuesstatic TemplateParseUtil.PreProcessedTemplatepreprocessTemplate(TemplateParseUtil.Template t)process a template into a preprocessed template which can be cachedstatic 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 overstatic StringvalidateOutgoingTemplate(String template)Validates an outgoing template to make sure it is validstatic voidvalidateTemplate(String template)Validate a template, if invalid then an exception is thrownstatic voidvalidateTemplateKey(String templateKey)Check if a templateKey is valid, if not then throwsIllegalArgumentException
-
-
-
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
-
-
Method Detail
-
validateTemplateKey
public static void validateTemplateKey(String templateKey)
Check if a templateKey is valid, if not then throwsIllegalArgumentException- Parameters:
templateKey- a key from the set of template keysPARSE_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 keysPARSE_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 itsegments- 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 templatespreprocessed- the analyzed templates to attempt to match in the order they should attempt the match, can be a single template or multiples, usepreprocessTemplates(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
-
-