- All Implemented Interfaces:
Serializable
An instance of this class is basically a wrapper around a
String that contains placeholders ("Variables") in the form
${<name>}, where <name is the variable
name.
The variables names are case-sensitive.
Valid variable names may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
When the system data is added as source (see
replaceVariableFromSystemData()
and
replaceVariable()
with addSystemData set to true), some additional variables
are available:
- "org.tquadrat.ipaddress"
- One of the outbound IP addresses of the machine that executes the current program, if network is configured at all.
- "org.tquadrat.macaddress"
- The MAC address of the machine that executes the current program; if no network is configured, a dummy address is used.
- "org.tquadrat.nodeid"
- The node id of the machines that executes the current program; if no network is configured, a pseudo node id is used.
- "org.tquadrat.now"
- The current data and time in UTC time zone.
- "org.tquadrat.pid"
- The process id of the current program.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: Template.java 1080 2024-01-03 11:05:21Z tquadrat $
- Since:
- 0.1.0
- See Also:
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.util.Template"
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringThe template text.private static final PatternThe pattern that is used to identify a variable in a char sequence.static final StringThe regular expression to identify a variable in a char sequence: "\\$\\{((?:_)|(?:[~/=%:&]?\\p{IsAlphabetic}(?:\\p{IsAlphabetic}|\\d|_|.)*?))}".static final StringThe template for variables: "${%1$s}".static final StringThe variable name for the IP address of the executing machine: "org.tquadrat.ipaddress".static final StringThe variable name for the MAC address of the executing machine: "org.tquadrat.macaddress".static final StringThe variable name for the node id of the executing machine: "org.tquadrat.nodeid".static final StringThe variable name for the current date and time: "org.tquadrat.now".static final StringThe variable name for the id of the current process, executing this program: "org.tquadrat.pid". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringadjustTemplate(String templateText) The mount point for template manipulations in derived classes.Builds the source map with the additional data.private static StringEscapes backslash ('\') and dollar sign ('$') for regex replacements.Collects all the variables of the form${<name>}in the adjusted template.findVariables(CharSequence text) Collects all the variables of the form${<name>}in the given String.protected StringformatResult(String text) Mountpoint for the formatting of the result after the variables have been replaced.protected final StringReturns the template text after it has been processed byadjustTemplate(String).final booleanhasVariable(String name) Checks whether the adjusted template contains the variable of the form${<name>}(matching the pattern given inVARIABLE_PATTERN) with the given name.final booleanChecks whether the adjusted template contains at least one variable of the form${<name>}(matching the pattern given inVARIABLE_PATTERN).static final booleanhasVariables(CharSequence input) Checks whether the given String contains at least one variable of the form${<name>}(matching the pattern given inVARIABLE_PATTERN).static final booleanTest whether the given String is a valid variable name.static final booleanisVariable(CharSequence input) Checks whether the given String is a variable in the form${<name>}, according to the pattern provided inVARIABLE_PATTERN.final StringreplaceVariable(boolean addSystemData, Map<String, ? extends Object>... sources) Replaces the variables of the form${<name>}in the adjusted template with values from the given maps and returns it after formatting the result.static final StringreplaceVariable(CharSequence text, Function<? super String, Optional<String>> retriever) Replaces the variables of the form${<name>}in the given String with values returned by the given retriever function for the variable name.static final StringreplaceVariable(CharSequence text, Map<String, ? extends Object>... sources) Replaces the variables of the form${<name>}in the given String with values from the given maps.final StringreplaceVariable(Function<? super String, Optional<String>> retriever) Replaces the variables of the form${<name>}in the adjusted template with values returned by the given retriever function for the variable name, and returns it after formatting the result.final StringreplaceVariable(Map<String, ? extends Object>... sources) Replaces the variables of the form${<name>}in the adjusted template with values from the given maps and returns it after formatting the result.static final StringreplaceVariableFromSystemData(CharSequence text, Map<String, ? extends Object>... additionalSources) Replaces the variables of the form${<name>}in the given String with values from the system properties, the system environment and the given maps.retrieveVariableValue(String name, Map<String, ? extends Object>... sources) Tries to obtain a value for the given key from one of the given sources that will be searched in the given sequence order.
-
Field Details
-
VARNAME_IPAddress
The variable name for the IP address of the executing machine: "org.tquadrat.ipaddress".- Since:
- 0.1.0
- See Also:
-
VARNAME_MACAddress
The variable name for the MAC address of the executing machine: "org.tquadrat.macaddress".- Since:
- 0.1.0
- See Also:
-
VARNAME_NodeId
The variable name for the node id of the executing machine: "org.tquadrat.nodeid".- Since:
- 0.1.0
- See Also:
-
VARNAME_Now
The variable name for the current date and time: "org.tquadrat.now".- Since:
- 0.1.0
- See Also:
-
VARNAME_pid
The variable name for the id of the current process, executing this program: "org.tquadrat.pid".- Since:
- 0.1.0
- See Also:
-
VARIABLE_PATTERN
The regular expression to identify a variable in a char sequence: "\\$\\{((?:_)|(?:[~/=%:&]?\\p{IsAlphabetic}(?:\\p{IsAlphabetic}|\\d|_|.)*?))}".- Since:
- 0.1.0
- See Also:
-
VARIABLE_TEMPLATE
The template for variables: "${%1$s}". The argument is the name of the variable itself; after an optional prefix character, it may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.').
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
- Since:
- 0.1.0
- See Also:
-
m_TemplateText
The template text. -
m_VariablePattern
The pattern that is used to identify a variable in a char sequence.- See Also:
-
-
Constructor Details
-
Template
Creates a new instance ofTemplate.- Parameters:
templateText- The template text, containing variable in the form${<name>}.
-
-
Method Details
-
adjustTemplate
The mount point for template manipulations in derived classes.
The default implementation will just return the argument.
- Parameters:
templateText- The template text, as it was given to the constructor on creation of the object instance.- Returns:
- The adjusted template text.
-
createAdditionalSource
Builds the source map with the additional data.- Returns:
- The source map.
- See Also:
-
escapeRegexReplacement
@API(status=INTERNAL, since="0.1.0") private static String escapeRegexReplacement(CharSequence input) Escapes backslash ('\') and dollar sign ('$') for regex replacements.- Parameters:
input- The source string.- Returns:
- The string with the escaped characters.
- Since:
- 0.1.0
- See Also:
-
findVariables
Collects all the variables of the form
${<name>}in the given String.If there are not any variables in the given String, an empty
Setwill be returned.A valid variable name may not contain any other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
Finally, there is the single underscore that is allowed as a special variable.
- Parameters:
text- The text with the variables; may benull.- Returns:
- A
Collectionwith the variable (names). - Since:
- 0.0.5
- See Also:
-
findVariables
Collects all the variables of the form
${<name>}in the adjusted template.If there are not any variables in there, an empty
Collectionwill be returned.A valid variable name may not contain any other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
Finally, there is the single underscore that is allowed as a special variable.
- Returns:
- A
Collectionwith the variable (names). - See Also:
-
formatResult
Mountpoint for the formatting of the result after the variables have been replaced.
The default implementation just returns the result.
- Parameters:
text- The result from replacing the variables in the template text.- Returns:
- The reformatted result.
-
getTemplateText
Returns the template text after it has been processed byadjustTemplate(String).- Returns:
- The adjusted template text.
-
hasVariable
Checks whether the adjusted template contains the variable of the form${<name>}(matching the pattern given inVARIABLE_PATTERN) with the given name.- Parameters:
name- The name of the variable to look for.- Returns:
trueif the template contains the variable,falseotherwise.- Throws:
IllegalArgumentException- The given argument is not valid as a variable name.- See Also:
-
hasVariables
Checks whether the given String contains at least one variable of the form${<name>}(matching the pattern given inVARIABLE_PATTERN).- Parameters:
input- The String to test; can benull.- Returns:
trueif the String contains at least one variable,falseotherwise.- Since:
- 0.1.0
- See Also:
-
hasVariables
Checks whether the adjusted template contains at least one variable of the form${<name>}(matching the pattern given inVARIABLE_PATTERN).- Returns:
trueif the template contains at least one variable,falseotherwise.- See Also:
-
isValidVariableName
@API(status=STABLE, since="0.1.0") public static final boolean isValidVariableName(CharSequence name) Test whether the given String is a valid variable name.- Parameters:
name- The bare variable name, without the surrounding "${…}".- Returns:
trueif the given name is valid for a variable name,falseotherwise.- Since:
- 0.1.0
- See Also:
-
isVariable
Checks whether the given String is a variable in the form${<name>}, according to the pattern provided inVARIABLE_PATTERN.- Parameters:
input- The String to test; can benull.- Returns:
trueif the given String is notnull, not the empty String, and it matches the given pattern,falseotherwise.- Since:
- 0.1.0
-
replaceVariable
@SafeVarargs @API(status=STABLE, since="0.1.0") public static final String replaceVariable(CharSequence text, Map<String, ? extends Object>... sources) Replaces the variables of the form
${<name>}in the given String with values from the given maps. The method will try the maps in the given sequence, it stops after the first match.If no replacement value could be found, the variable will not be replaced at all.
If a value from one of the maps contains a variable itself, this will not be replaced.
The variables names are case-sensitive.
Valid variable names may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
- Parameters:
text- The text with the variables; may benull.sources- The maps with the replacement values.- Returns:
- The new text, or
nullif the provided value fortextwas alreadynull. - Since:
- 0.1.0
- See Also:
-
replaceVariable
Replaces the variables of the form
${<name>}in the adjusted template with values from the given maps and returns it after formatting the result. The method will try the maps in the given sequence, it stops after the first match.If no replacement value could be found, the variable will not be replaced at all.
If a value from one of the maps contains a variable itself, this will not be replaced.
The variables names are case-sensitive.
Valid variable names may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
- Parameters:
sources- The maps with the replacement values.- Returns:
- The new text, or
nullif the provided value fortextwas alreadynull. - See Also:
-
replaceVariable
@SafeVarargs public final String replaceVariable(boolean addSystemData, Map<String, ? extends Object>... sources) Replaces the variables of the form
${<name>}in the adjusted template with values from the given maps and returns it after formatting the result. The method will try the maps in the given sequence, it stops after the first match.If
addSystemDatais provided astrue, the system properties and System.getenv() system environment} will be searched for replacement values before any other source.If no replacement value could be found, the variable will not be replaced at all.
If a value from one of the maps contains a variable itself, this will not be replaced.
The variables names are case-sensitive.
Valid variable names may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
- Parameters:
addSystemData-trueif the system properties and the system environment should be searched for replacement values, too, otherwisefalse.sources- The maps with the replacement values.- Returns:
- The new text, or
nullif the provided value fortextwas alreadynull. - See Also:
-
replaceVariable
Replaces the variables of the form
${<name>}in the adjusted template with values returned by the given retriever function for the variable name, and returns it after formatting the result.If no replacement value could be found, the variable will not be replaced at all.
If the retriever function returns a value that contains a variable itself, this will not be replaced.
The retriever function will be called only once for each variable name; if the text contains the same variable multiple times, it will always be replaced with the same value.
The variables names are case-sensitive.
Valid variable names may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
- Parameters:
retriever- The function that will retrieve the replacement values for the given variable names.- Returns:
- The new text, or
nullif the provided value fortextwas alreadynull. - See Also:
-
replaceVariable
@API(status=STABLE, since="0.1.0") public static final String replaceVariable(CharSequence text, Function<? super String, Optional<String>> retriever) Replaces the variables of the form
${<name>}in the given String with values returned by the given retriever function for the variable name.If no replacement value could be found, the variable will not be replaced at all.
If the retriever function returns a value that contains a variable itself, this will not be replaced.
The retriever function will be called only once for each variable name; if the text contains the same variable multiple times, it will always be replaced with the same value.
The variables names are case-sensitive.
Valid variable name may not contain other characters than the letters from 'a' to 'z' (upper case and lower case), the digits from '0' to '9' and the special characters underscore ('_') and dot ('.'), after an optional prefix character.
Allowed prefixes are the tilde ('~'), the slash ('/'), the equal sign ('='), the colon (':'), the percent sign ('%'), and the ampersand ('&').
The prefix character is part of the name.
Finally, there is the single underscore that is allowed as a special variable.
- Parameters:
text- The text with the variables; may benull.retriever- The function that will retrieve the replacement values for the given variable names.- Returns:
- The new text, or
nullif the provided value fortextwas alreadynull. - Since:
- 0.1.0
- See Also:
-
replaceVariableFromSystemData
@SafeVarargs @API(status=STABLE, since="0.1.0") public static final String replaceVariableFromSystemData(CharSequence text, Map<String, ? extends Object>... additionalSources) Replaces the variables of the form
${<name>}in the given String with values from the system properties, the system environment and the given maps. The method will try the maps in the given sequence, it stops after the first match.In addition, five more variables are recognised:
"org.tquadrat.ipaddress"- The first IP address for the machine that executes this Java virtual machine.
"org.tquadrat.macaddress"- The MAC address of the first NIC in this machine.
"org.tquadrat.nodeid"- The node id from the first NIC in this machine.
"org.tquadrat.now"- The current date and time as returned by
Instant.now(). "org.tquadrat.pid"- The process id of this Java virtual machine.
If no replacement value could be found, the variable will not be replaced at all; no exception will be thrown.
If a value from one of the maps contains a variable itself, this will not be replaced.
The variables names are case-sensitive.
- Parameters:
text- The text with the variables; can benull.additionalSources- The maps with additional replacement values.- Returns:
- The new text, or
nullif the provided value fortextwas alreadynull. - See Also:
-
retrieveVariableValue
@SafeVarargs private static final Optional<String> retrieveVariableValue(String name, Map<String, ? extends Object>... sources) Tries to obtain a value for the given key from one of the given sources that will be searched in the given sequence order.- Parameters:
name- The name of the value.sources- The maps with the values.- Returns:
- An instance of
Optionalthat holds the value from one of the sources.
-
