public class Template
extends java.lang.Object
Templates are just defined by strings which contain placeholders. Placeholders may contain defaults which are used when no user-defined value is provided.
%{name}
where name is at
least one character long and consists of ASCII letters, decimal numbers,
colons, double colons, underscores, or plus and minus signs.
%{name|@default@}
, where
the rules for name are the same as for normal placeholders. The vertical bar
indicates a default, which itself is enclosed in any letter of your choice as a marker
(here for example @
) is used. This allows the default text to contain any letters,
you just have to take care to enclose it in a letter which is not contained
in combination with a directly following closing brace. So above example could also
contain email addresses without problems. The substitution test extent over
multiple lines.
For nicer optics there are a few markers which expect a different end marker (here
...
stands for the fallback text):
%{name|<...>}
%{name|(...)}
%{name|[...]}
%{name|{...}}
%{name|>...<}
.
Modifier and Type | Field and Description |
---|---|
(package private) static java.lang.String |
FALLBACK_SEPARATOR
The separator used to include a fallback.
|
(package private) static java.lang.String |
PLACEHOLDER_PREFIX
Prefix of a text holder mark.
|
(package private) static java.lang.String |
PLACEHOLDER_SUFFIX
Suffix of a text holder mark.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
apply(Dict<java.lang.String,java.lang.String> variableMapping)
Apply the given variable mapping and return the result.
|
java.lang.String |
apply(java.util.Map<java.lang.String,java.lang.String> variableMapping)
Apply the given variable mapping and return the result.
|
java.lang.String |
apply(java.lang.String name1,
java.lang.Object value1,
java.lang.Object... moreNameValuePairs)
Apply the given name-variable mapping and return the result.
|
static void |
checkName(java.lang.String name)
Check a name that it does not contain invalid characters.
|
static Template |
from(java.lang.String templateText)
Create a template.
|
Countable<java.lang.String> |
getPlaceholders()
Get the set of placeholder names which are used in this template.
|
static final java.lang.String PLACEHOLDER_PREFIX
static final java.lang.String PLACEHOLDER_SUFFIX
static final java.lang.String FALLBACK_SEPARATOR
@NotNull public Countable<java.lang.String> getPlaceholders()
@NotNull public static Template from(@NotNull java.lang.String templateText) throws java.text.ParseException
templateText
- template textjava.text.ParseException
- if text does contain invalid placeholder definitions@NotNull public java.lang.String apply(@NotNull Dict<java.lang.String,java.lang.String> variableMapping)
variableMapping
- variable mapping, has to contain a key for each namevariableMapping
@NotNull public java.lang.String apply(@NotNull java.util.Map<java.lang.String,java.lang.String> variableMapping)
variableMapping
- variable mapping, has to contain a key for each namevariableMapping
@NotNull public java.lang.String apply(@NotNull java.lang.String name1, @NotNull java.lang.Object value1, @NotNull java.lang.Object... moreNameValuePairs)
name1
- name of first placeholdervalue1
- substitution value of first placeholdermoreNameValuePairs
- further name-value pairs, each even entry has to be a non-null string, and
each following odd entry has to be something non-null, for which its string
representation will be used as substitution valuepublic static void checkName(@NotNull java.lang.String name)
name
- name to checkjava.lang.IllegalArgumentException
- if name contains invalid characters