Class TextTemplate
- java.lang.Object
-
- org.nuxeo.common.utils.TextTemplate
-
public class TextTemplate extends Object
Text template processing.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)
andsetFreemarkerParsingExtensions(String)
allow to set the list of files being processed when usingprocessDirectory(File, File)
, based on their extension; others being simply copied. - its decrypted value by default: "
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
TextTemplate.EscapeVariableFilter
-
Constructor Summary
Constructors Constructor Description TextTemplate()
TextTemplate(Map<String,String> vars)
TextTemplate(Properties)
provides an additional default values behaviorTextTemplate(Properties vars)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getVariable(String name)
Properties
getVariables()
void
initFreeMarker()
Initialize FreeMarker data model from Java properties.boolean
isTrim()
TextTemplate
keepEncryptedAsVar(boolean keepEncryptedAsVar)
Whether to replace or not the variables which value is encrypted.Properties
preprocessVars(Properties unprocessedVars)
List<String>
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.String
processText(InputStream in)
void
processText(InputStream is, OutputStreamWriter os)
String
processText(String content)
String
processText(String text, Function<String,String> transform)
It processes the given text.InputStream
processTextAsStream(InputStream in)
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
unescape(String value)
protected Properties
unescape(Properties props)
unescape variables
-
-
-
Constructor Detail
-
TextTemplate
public TextTemplate()
-
TextTemplate
public TextTemplate(Map<String,String> vars)
TextTemplate(Properties)
provides an additional default values behavior- See Also:
TextTemplate(Properties)
-
TextTemplate
public TextTemplate(Properties vars)
- Parameters:
vars
- Properties containing keys and values for template processing
-
-
Method Detail
-
isTrim
public boolean isTrim()
-
setTrim
public void setTrim(boolean trim)
Set to true in order to trim invisible characters (spaces) from values.
-
setVariable
public void setVariable(String name, String value)
If adding multiple variables, prefer use ofsetVariables(Map)
-
getVariables
public Properties getVariables()
-
processString
protected String processString(CryptoProperties props, String text)
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
protected Properties unescape(Properties props)
unescape variables
-
preprocessVars
public Properties preprocessVars(Properties unprocessedVars)
-
processText
public String processText(String text, Function<String,String> transform)
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
public String processText(InputStream in) throws IOException
- Throws:
IOException
-
processText
public void processText(InputStream is, OutputStreamWriter os) throws IOException
- Throws:
IOException
-
processTextAsStream
public InputStream processTextAsStream(InputStream in) throws IOException
- 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:
processText(InputStream, OutputStreamWriter)
,processFreemarker(File, File)
-
setTextParsingExtensions
public void setTextParsingExtensions(String extensionsList)
- Parameters:
extensionsList
- comma-separated list of files extensions to parse
-
setFreemarkerParsingExtensions
public void setFreemarkerParsingExtensions(String extensionsList)
-
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
public TextTemplate keepEncryptedAsVar(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- Returns:
- this object to ease chaining
- Since:
- 2021.14
-
-