Class StringTemplate


  • public class StringTemplate
    extends Object
    String template engine allowing easy string regex matching and usual template engine resolution.

    This engine uses a syntax inspired in mustache using {{}} as basic indicator of special treatment/replacement.

    To check fo string regex matching you can have a template like

    <root version="1.2">{{\d+}}</root>
    and use matches(String). No need to escape regex special characters outside of {{}} .

    To use regular string template resolution you can use templates like

    <root version="1.2">{{value}}</root>
    and use bind(String, Object) and solve() to get the resulting string of replacing each occurrence of {{}} with the bound value. Additionally, you can define default values for each replacement expression. In this example
    <root version="1.2">{{value:3}}</root>
    it will solve "value" to string "3" if no value is bound to "value" or if bound value is null. If a replacement has no binding value different from null and no default value is specified, then an exception will be generated. You can always specify an empty default value (like {{value:}}) which avoids the exception and generates an empty string instead.

    You can even use one template for both regex matching or string template solving. Eg:

    <root version="1.2">{{value:3~\d+}}</root>
    can be used with matches(String) or with bind(String, Object) and solve().