public class StringTemplate extends Object
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().| Modifier and Type | Field and Description |
|---|---|
static String |
EXPRESSION_END_MARKER |
static String |
EXPRESSION_START_MARKER |
| Constructor and Description |
|---|
StringTemplate(String template) |
| Modifier and Type | Method and Description |
|---|---|
StringTemplate |
bind(String key,
Object value) |
StringTemplate |
ignoreMissingBindings() |
boolean |
matches(String string) |
String |
solve() |
public static final String EXPRESSION_START_MARKER
public static final String EXPRESSION_END_MARKER
public StringTemplate(String template)
public boolean matches(String string)
public StringTemplate bind(String key, Object value)
public StringTemplate ignoreMissingBindings()
public String solve()
Copyright © 2024. All rights reserved.