java.lang.Object
org.klojang.templates.StringifierRegistry.Builder
- Enclosing class:
StringifierRegistry
A builder class for
StringifierRegistry
instances.- Author:
- Ayco Holleman
-
Method Summary
Modifier and TypeMethodDescriptionforName
(String name, Stringifier stringifier) Assigns the specified stringifier to all variables with the specified name.forTemplate
(Template root, String nestedTemplateName, Stringifier stringifier, String... varNames) Assigns the specified stringifier to one or more variables in a nested template.forType
(Class<?> type, Stringifier stringifier) Assigns the specified stringifier to the specified type.forVarGroup
(String groupName, Stringifier stringifier) Assigns the specified stringifier to the specified variable group.freeze()
Returns a new, immutableStringifierRegistry
instance.register
(Template template, Stringifier stringifier, String... varNames) Assigns the specified stringifier to one or more variables in the specified template.setDefaultStringifier
(Stringifier stringifier) Lets you specify an alternative default stringifier, replacingStringifier.DEFAULT
.Explicitly sets the data type of the specified variables.
-
Method Details
-
setDefaultStringifier
Lets you specify an alternative default stringifier, replacingStringifier.DEFAULT
. For example, you might want the default stringifier to beStringifierRegistry.ESCAPE_HTML
.- Parameters:
stringifier
- the stringifier to use as the default stringifier- Returns:
- this
Builder
-
register
public StringifierRegistry.Builder register(Template template, Stringifier stringifier, String... varNames) Assigns the specified stringifier to one or more variables in the specified template. The variable names are taken to be fully-qualified names, relative to the specified template. For example:Template template = Template.fromResource(getClass(), "/html/company.html"); StringifierRegistry stringifiers = StringifierRegistry.configure() .register(template, new ZipCodeFormatter(), "zipCode" "departments.employees.address.zipCode", "departments.manager.address.zipCode") .freeze();
To assign the stringifier to all variables in the specified template (non-recursively), specify an empty string array.
- Parameters:
template
- the template containing the variablesstringifier
- the stringifiervarNames
- any array of fully-qualified variable names- Returns:
- this
Builder
- See Also:
-
forTemplate
public StringifierRegistry.Builder forTemplate(Template root, String nestedTemplateName, Stringifier stringifier, String... varNames) Assigns the specified stringifier to one or more variables in a nested template.nestedTemplateName
must be the fully-qualified name of the nested template, relative to the root template. The variable names must be simple names. For example:Template template = Template.fromResource(getClass(), "/html/company.html"); NameFormatter nameFormatter = new NameFormatter(); StringifierRegistry stringifiers = StringifierRegistry.configure() .forTemplate(template, "departments.employees", nameFormatter, "firstName", "lastName") .forTemplate( template, "departments.manager", nameFormatter, "firstName", "lastName") .freeze();
To assign the stringifier to all variables in the nested template, specify an empty string array.
- Parameters:
root
- the root templatenestedTemplateName
- the name of a template descending from the root template, ornull
if you want to target the variables in the root template itselfstringifier
- the stringifiervarNames
- the names of the variables to which to assign the stringifier, or an empty string array if you want to assign the stringifier to all variables within the target template- Returns:
- this
Builder
- See Also:
-
forVarGroup
Assigns the specified stringifier to the specified variable group. Note that different instances of the same variable within the same template can be assigned to different variable groups (for example:~%html:fullName%
and~%js:fullName%
).- Parameters:
groupName
- the name of the variable group to which to assign the stringifierstringifier
- the stringifier- Returns:
- this
Builder
-
forName
Assigns the specified stringifier to all variables with the specified name. This works across all templates within the application, so be careful when registering a stringifier this way. You may specify a wildcard '*' character at the beginning or end of the variable name. For example to assign a number formatter to all variables whose name ends with "Price", specify*Price
as the variable name.- Parameters:
name
- the variable name to associate the stringifier with.stringifier
- the stringifier- Returns:
- this
Builder
-
forType
Assigns the specified stringifier to the specified type. Internally, type-based stringifiers are stored into, and looked up in aTypeMap
. This means that if there is no stringifier defined for, say,Short.class
, but there is a stringifier forNumber.class
, then that is the stringifier that is going to be used forShort
values. This saves you from having to specify a stringifier for each and every subclass ofNumber
if they can all be stringified in the same way.- Parameters:
type
- the type to associate the stringifier with.stringifier
- the stringifier- Returns:
- this
Builder
-
setType
Explicitly sets the data type of the specified variables. This enables theStringifierRegistry
to find a type-based stringifier for a value even if the value isnull
(in which caseObject.getClass()
is not available to determine the variable's type). The variable names are taken to be fully-qualified names, relative to the specified template.- Parameters:
type
- the data type to set for the specified variablestemplate
- the template containing the variablesvarNames
- the fully-qualified names of the variables- Returns:
- this
Builder
-
freeze
Returns a new, immutableStringifierRegistry
instance.- Returns:
- A new, immutable
StringifierRegistry
instance
-