Class TextTemplate
Copy files or directories replacing parameters matching pattern '${[a-zA-Z_0-9\-\.]+}' with values from a
CryptoProperties
.
If the value of a variable is encrypted:
setVariable("var", Crypto.encrypt(value.getBytes))then "
${var}
" will be replaced with:
- its decrypted value by default: "
value
" - "
${var}
" after a call to "setKeepEncryptedAsVar(true)}
"
${#var}
" will always be replaced with its decrypted value.
Since 5.7.2, variables can have a default value using syntax ${parameter:=defaultValue}. The default value will be used if parameter is null or unset.
Methods setTextParsingExtensions(String)
and setFreemarkerParsingExtensions(String)
allow to set
the list of files being processed when using processDirectory(File, File)
, based on their extension; others
being simply copied.
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionTextTemplate
(Map<String, String> vars) TextTemplate(Properties)
provides an additional default values behaviorTextTemplate
(Properties vars) -
Method Summary
Modifier and TypeMethodDescriptiongetVariable
(String name) void
Initialize FreeMarker data model from Java properties.boolean
isTrim()
keepEncryptedAsVar
(boolean keepEncryptedAsVar) Whether to replace or not the variables which value is encrypted.preprocessVars
(Properties unprocessedVars) processDirectory
(File in, File out) Recursively process each file from "in" directory to "out" directory.void
processFreemarker
(File in, File out) protected String
processString
(CryptoProperties props, String text) That method is not recursive.protected String
processString
(CryptoProperties props, String text, Function<String, String> transform) That method is not recursive.void
processText
(InputStream is, OutputStreamWriter os) processText
(String content) processText
(String text, Function<String, String> transform) It processes the given text.void
setFreemarkerParsingExtensions
(String extensionsList) void
setKeepEncryptedAsVar
(boolean keepEncryptedAsVar) Whether to replace or not the variables which value is encrypted.void
setTextParsingExtensions
(String extensionsList) void
setTrim
(boolean trim) Set to true in order to trim invisible characters (spaces) from values.void
setVariable
(String name, String value) If adding multiple variables, prefer use ofsetVariables(Map)
void
setVariables
(Map<String, String> vars) protected String
protected Properties
unescape
(Properties props) unescape variables
-
Constructor Details
-
TextTemplate
public TextTemplate() -
TextTemplate
TextTemplate(Properties)
provides an additional default values behavior- See Also:
-
TextTemplate
- Parameters:
vars
- Properties containing keys and values for template processing
-
-
Method Details
-
isTrim
public boolean isTrim() -
setTrim
public void setTrim(boolean trim) Set to true in order to trim invisible characters (spaces) from values. -
setVariables
-
setVariable
If adding multiple variables, prefer use ofsetVariables(Map)
-
getVariable
-
getVariables
-
processString
That method is not recursive. It processes the given text only once.- Parameters:
props
- CryptoProperties containing the variable valuestext
- Text to process- Returns:
- the processed text
- Since:
- 7.4
-
processString
protected String processString(CryptoProperties props, String text, Function<String, String> transform) That method is not recursive. It processes the given text only once.- Parameters:
props
- CryptoProperties containing the variable valuestext
- Text to processtransform
- Function to transform (quote, escape, etc.) processed vars- Returns:
- the processed text
- Since:
- 2021.22
-
unescape
unescape variables -
unescape
-
preprocessVars
-
processText
- Since:
- 7.4
-
processText
It processes the given text.- Parameters:
text
- Text to processtransform
- Function to transform (quote, escape, etc.) processed variables within text- Returns:
- the processed text
- Since:
- 2021.22
-
processText
- Throws:
IOException
-
processText
- Throws:
IOException
-
processTextAsStream
- Throws:
IOException
-
initFreeMarker
public void initFreeMarker()Initialize FreeMarker data model from Java properties.Variables in the form "
foo.bar
" (String with dots) are transformed to "foo[bar]
" (arrays).
So there will be conflicts if a variable name is equal to the prefix of another variable. For instance, "foo.bar
" and "foo.bar.qux
" will conflict.
When a conflict occurs, the conflicting variable is ignored and a warning is logged. The ignored variable will usually be the shortest one (without any contract on this behavior). -
processFreemarker
public void processFreemarker(File in, File out) throws IOException, freemarker.template.TemplateException - Throws:
IOException
freemarker.template.TemplateException
-
processDirectory
public List<String> processDirectory(File in, File out) throws IOException, freemarker.template.TemplateException Recursively process each file from "in" directory to "out" directory.- Parameters:
in
- Directory to read files fromout
- Directory to write files to- Returns:
- copied files list
- Throws:
IOException
freemarker.template.TemplateException
- See Also:
-
setTextParsingExtensions
- Parameters:
extensionsList
- comma-separated list of files extensions to parse
-
setFreemarkerParsingExtensions
-
setKeepEncryptedAsVar
public void setKeepEncryptedAsVar(boolean keepEncryptedAsVar) Whether to replace or not the variables which value is encrypted.- Parameters:
keepEncryptedAsVar
- iftrue
, the variables which value is encrypted won't be expanded- Since:
- 7.4
-
keepEncryptedAsVar
Whether to replace or not the variables which value is encrypted.- Parameters:
keepEncryptedAsVar
- iftrue
, the variables which value is encrypted won't be expanded- Returns:
- this object to ease chaining
- Since:
- 2021.14
-