java.lang.Object
org.klojang.templates.TemplateUtils
Utility class extending the functionality of the
Template
class.- Author:
- Ayco Holleman
-
Method Summary
Modifier and TypeMethodDescriptiongetAllVariableFQNames
(Template template) Returns the fully-qualified names of all variables in the specified template and all templates descending from the specified template.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.static List
<VariableOccurrence> getAllVariableOccurrences
(Template template) Returns a depth-first view of all variable occurrences within the specified template.getAllVariables
(Template template) Returns the names of all variables in the specified template and all templates descending from the specified template.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.static String
Returns the fully-qualified name of the specified template, relative to the root template.static String
Returns the fully-qualified name of the specified name.static Template
getNestedTemplate
(Template template, String FQN) Returns the nested template corresponding to the specified fully-qualified name.getTemplateHierarchy
(Template template) Returns the specified template and all templates descending from it.static void
printParts
(Template template) Prints out the constituent parts of the specifiedTemplate
.static void
printParts
(Template template, PrintStream out) Prints out the constituent parts of the specifiedTemplate
.
-
Method Details
-
getFQN
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
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 namename
- the name of a template variable or nested template- Returns:
- its fully-qualified name
-
getAllVariableOccurrences
Returns a depth-first view of all variable occurrences within the specified template. The returnedList
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
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 returnedList
is created on demand and modifiable.- Parameters:
template
- the template- Returns:
- a
List
containing theTemplate
and its descendants
-
getNestedTemplate
Returns the nested template corresponding to the specified fully-qualified name. Contrary toTemplate.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 templateFQN
- the fully qualified name of the nested template- Returns:
- The (possibly deeply) nested template corresponding to the specified fully-qualified name
-
getContainingTemplate
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 takenFQN
- the fully-qualified name- Returns:
- The template containing the variable or nested template denoted by the specified fully-qualified name
-
getAllVariables
Returns the names of all variables in the specified template and all templates descending from the specified template. Each tuple in the returnedList
contains aTemplate
instance and a variable name. TheTemplate
instance is either the specified template itself or one of its descendants. The returnedList
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
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
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 namesrelative
- iftrue
, 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
Prints out the constituent parts of the specifiedTemplate
. Can be used for debugging purposes.- Parameters:
template
- theTemplate
whose parts to print
-
printParts
Prints out the constituent parts of the specifiedTemplate
. Can be used for debugging purposes.- Parameters:
template
- theTemplate
whose parts to printout
- thePrintStream
to which to print
-