Class StringifierRegistry

java.lang.Object
org.klojang.templates.StringifierRegistry

public final class StringifierRegistry extends Object
A registry of stringifiers used by the RenderSession to stringify the values coming back from the data access layer. Stringifiers can be used, for example, to apply non-standard formatting to dates and numbers, or to apply some sort of escaping (e.g. HTML escaping), or to stringify objects whose toString() method does not satisfy your needs. If you need to configure stringifiers, your code might look something like this:

 StringifierRegistry stringifiers = StringifierRegistry.configure()
    .forType(int.class, obj -> String.valueOf((int) obj + 10))
    .freeze();
 Template template = Template.fromString("~%foo%");
 RenderSession session = template.newRenderSession(stringifiers);
 String out = session.set("foo", 32).render(); // 42
 

In practice, you are more likely to create just a single StringifierRegistry instance for your entire application, when it starts up, and pass that instance to all calls to Template.newRenderSession().

This is how a StringifierRegistry decides which stringifier to use for a template variable:

  1. If a stringifier has been registered for a variable group and the variable belongs to that group, then that is the stringifier that is going to be used.
  2. If a stringifier has been registered for that particular variable in that particular template, then that is the stringifier that is going to be used.
  3. If a stringifier has been registered for all variables with that particular name (irrespective of the template they belong to), then that is the stringifier that is going to be used. See StringifierRegistry.Builder.forName(String, Stringifier)
  4. If a stringifier has been registered for the data type of that particular variable, then that is the stringifier that is going to be used.
  5. If you have registered an alternative default stringifier, then that is the stringifier that is going to be used.
  6. Otherwise Stringifier.DEFAULT is going to be used.
Author:
Ayco Holleman
  • Field Details

    • STANDARD_STRINGIFIERS

      public static final StringifierRegistry STANDARD_STRINGIFIERS
      A minimal StringifierRegistry instance. It contains stringifiers for the predefined variable groups. Variables not within these groups are stringified using the default stringifier. This is the StringifierRegistry a RenderSession will use if you called Template.newRenderSession() without the StringifierRegistry argument.
    • ESCAPE_HTML

      public static final Stringifier ESCAPE_HTML
      Applies HTML escaping. This is one of the standard stringifiers. It is the stringifier used by the HTML variable group.
    • ESCAPE_JS

      public static final Stringifier ESCAPE_JS
      Applies Javascript escaping. This is one of the standard stringifiers. It is the stringifier used by the JS variable group.
    • ESCAPE_ATTR

      public static final Stringifier ESCAPE_ATTR
      To be used for escaping HTML attributes. Same as ESCAPE_HTML except that single quotes and double quotes are also escaped. This is one of the standard stringifiers. It is the stringifier used by the ATTR variable group.
    • ESCAPE_JS_ATTR

      public static final Stringifier ESCAPE_JS_ATTR
      To be used for escaping HTML attributes containing Javascript, like onclick. This is one of the standard stringifiers. It is the stringifier used by the JS_ATTR variable group.
    • ESCAPE_QUERY_PARAM

      public static final Stringifier ESCAPE_QUERY_PARAM
      To be used for escaping URL query parameter. Both parameter names and parameter values can be escaped using this stringifier since they are escaped identically. This is one of the standard stringifiers. It is the stringifier used by the PARAM variable group.
    • ESCAPE_PATH

      public static final Stringifier ESCAPE_PATH
      To be used for escaping URL path segments. This is one of the standard stringifiers. It is the stringifier used by the PATH variable group.
  • Method Details

    • configure

      public static StringifierRegistry.Builder configure()
      Returns a Builder instance that lets you configure a StringifierRegistry. The StringifierRegistry will already contain the default stringifier and the stringifiers for the standard variable groups.
      Returns:
      A Builder instance that lets you configure a StringifierRegistry
    • cleanSlate

      public static StringifierRegistry.Builder cleanSlate()
      Returns a Builder instance that lets you configure a StringifierRegistry. The registry will not contain any stringifier except the default stringifier. Useful for non-HTML templates.
      Returns:
      A Builder instance that lets you configure a StringifierRegistry