Class 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)
    • Field Detail

      • 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
      • 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
      • commonExtensions

        public static Set<String> commonExtensions
        Contains a set of all the common extensions
    • 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)