Class TemplateUtils

java.lang.Object
org.klojang.templates.TemplateUtils

public final class TemplateUtils extends Object
Utility class extending the functionality of the Template class.
Author:
Ayco Holleman
  • Method Details

    • getFQN

      public static String getFQN(Template template)
      Returns the fully-qualified name of the specified template, relative to the root template. If the template is the root template, Template.ROOT_TEMPLATE_NAME is returned. Otherwise the fully-qualified name is the path from the specified template to the root template, in reverse direction.
      
       String src = """
            ~%%begin:companies%
                ~%foo%
                ~%%begin:departments%
                    ~%bar%
                ~%%end:departments%
            ~%%end:companies%
            """;
       Template t = Template.fromString(src);
       System.out.println(TemplateUtils.getFQN(t); // "{root}"
       t = t.getNestedTemplate("companies");
       System.out.println(TemplateUtils.getFQN(t); // "companies"
       t = t.getNestedTemplate("departments");
       System.out.println(TemplateUtils.getFQN(t); // "companies.departments"
       
      Parameters:
      template - the template for which to retrieve the fully-qualified name
      Returns:
      the fully-qualified name of the template
    • getFQN

      public static String getFQN(Template template, String name)
      Returns the fully-qualified name of the specified name. The provided name supposedly is the name of a variable or nested template inside the specified template. If the specified template is the root template, this method simply returns the name as-is. Otherwise it prefixes the template's FQN (and a dot character) to the name.
      Parameters:
      template - the template for which to retrieve the fully-qualified name
      name - the name of a template variable or nested template
      Returns:
      its fully-qualified name
    • getAllVariableOccurrences

      public static List<VariableOccurrence> getAllVariableOccurrences(Template template)
      Returns a depth-first view of all variable occurrences within the specified template. The returned List is created on demand and modifiable.
      Parameters:
      template - the template to collect the variable occurrences from
      Returns:
      a depth-first view of all variable occurrences within the specified template
    • getTemplateHierarchy

      public static List<Template> getTemplateHierarchy(Template template)
      Returns the specified template and all templates descending from it. The specified template will come first in de returned list and the descendant templates are retrieved in breadth-first order. The returned List is created on demand and modifiable.
      Parameters:
      template - the template
      Returns:
      a List containing the Template and its descendants
    • getNestedTemplate

      public static Template getNestedTemplate(Template template, String FQN)
      Returns the nested template corresponding to the specified fully-qualified name. Contrary to Template.getNestedTemplate() this method lets you retrieve deeply nested templates. The fully-qualified name must be relative to the specified template and must not start with the specified template's name itself.
      Parameters:
      template - the template containing the (deeply) nested template
      FQN - the fully qualified name of the nested template
      Returns:
      The (possibly deeply) nested template corresponding to the specified fully-qualified name
    • getContainingTemplate

      public static Template getContainingTemplate(Template template, String FQN)
      Returns the template that directly contains the variable or nested template denoted by the specified fully-qualified name. The specified template is supposed to be the root template, or a template at some higher level than the template containing the variable. The FQName must be relative to the specified template and it must not include the specified template's name.
      Parameters:
      template - the template relative to which the fully-qualified name should be taken
      FQN - the fully-qualified name
      Returns:
      The template containing the variable or nested template denoted by the specified fully-qualified name
    • getAllVariables

      public static List<Tuple2<Template,String>> getAllVariables(Template template)
      Returns the names of all variables in the specified template and all templates descending from the specified template. Each tuple in the returned List contains a Template instance and a variable name. The Template instance is either the specified template itself or one of its descendants. The returned List is created on demand and modifiable.
      Parameters:
      template - the template for which to retrieve the variable names
      Returns:
      all variable names in this Template and the templates nested inside it
    • getAllVariableFQNames

      public static List<String> getAllVariableFQNames(Template template)
      Returns the fully-qualified names of all variables in the specified template and all templates descending from the specified template. The names are fully-qualified relative to the root template of the specified template (not to the specified template itself).
      Parameters:
      template - the template for which to retrieve the variable names
      Returns:
      the fully-qualified variable names in this Template and the templates nested inside it
    • getAllVariableFQNames

      public static List<String> getAllVariableFQNames(Template template, boolean relative)
      Returns the fully-qualified names of all variables in the specified template and all templates descending from the specified template.
      Parameters:
      template - the template for which to retrieve the variable names
      relative - if true, the names are fully-qualified relative to the specified template, otherwise relative to the root template of the specified template
      Returns:
      the fully-qualified variable names in this Template and the templates nested inside it
    • printParts

      public static void printParts(Template template)
      Prints out the constituent parts of the specified Template. Can be used for debugging purposes.
      Parameters:
      template - the Template whose parts to print
    • printParts

      public static void printParts(Template template, PrintStream out)
      Prints out the constituent parts of the specified Template. Can be used for debugging purposes.
      Parameters:
      template - the Template whose parts to print
      out - the PrintStream to which to print