Class StringifierRegistry.Builder

java.lang.Object
org.klojang.templates.StringifierRegistry.Builder
Enclosing class:
StringifierRegistry

public static class StringifierRegistry.Builder extends Object
A builder class for StringifierRegistry instances.
Author:
Ayco Holleman
  • Method Details

    • setDefaultStringifier

      public StringifierRegistry.Builder setDefaultStringifier(Stringifier stringifier)
      Lets you specify an alternative default stringifier, replacing Stringifier.DEFAULT. For example, you might want the default stringifier to be StringifierRegistry.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 variables
      stringifier - the stringifier
      varNames - 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 template
      nestedTemplateName - the name of a template descending from the root template, or null if you want to target the variables in the root template itself
      stringifier - the stringifier
      varNames - 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

      public StringifierRegistry.Builder forVarGroup(String groupName, Stringifier stringifier)
      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 stringifier
      stringifier - the stringifier
      Returns:
      this Builder
    • forName

      public StringifierRegistry.Builder forName(String name, Stringifier stringifier)
      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

      public StringifierRegistry.Builder forType(Class<?> type, Stringifier stringifier)
      Assigns the specified stringifier to the specified type. Internally, type-based stringifiers are stored into, and looked up in a TypeMap. This means that if there is no stringifier defined for, say, Short.class, but there is a stringifier for Number.class, then that is the stringifier that is going to be used for Short values. This saves you from having to specify a stringifier for each and every subclass of Number 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

      public StringifierRegistry.Builder setType(Class<?> type, Template template, String... varNames)
      Explicitly sets the data type of the specified variables. This enables the StringifierRegistry to find a type-based stringifier for a value even if the value is null (in which case Object.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 variables
      template - the template containing the variables
      varNames - the fully-qualified names of the variables
      Returns:
      this Builder
    • freeze

      public StringifierRegistry freeze()
      Returns a new, immutable StringifierRegistry instance.
      Returns:
      A new, immutable StringifierRegistry instance