public class TextTemplate extends Object
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:
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.
Modifier and Type | Class and Description |
---|---|
protected static class |
TextTemplate.EscapeVariableFilter |
Constructor and Description |
---|
TextTemplate() |
TextTemplate(Map<String,String> vars)
TextTemplate(Properties) provides an additional default values behavior |
TextTemplate(Properties vars) |
Modifier and Type | Method and Description |
---|---|
String |
getVariable(String name) |
Properties |
getVariables() |
void |
initFreeMarker()
Initialize FreeMarker data model from Java properties.
|
boolean |
isTrim() |
Properties |
preprocessVars(Properties unprocessedVars) |
String |
process(CharSequence text)
Deprecated.
Since 7.4. Use
processText(CharSequence) instead. |
String |
process(InputStream in)
Deprecated.
Since 7.4. Use
processText(InputStream) instead. |
void |
process(InputStream in,
OutputStream out)
Deprecated.
Since 7.4. Use
processText(InputStream, OutputStream) instead. |
void |
process(InputStream is,
OutputStream os,
boolean processText)
Deprecated.
Since 7.4. Use
processText(InputStream, OutputStream) (if processText ) or
IOUtils.copy(InputStream, OutputStream) |
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.
|
String |
processText(CharSequence text)
Deprecated.
Since 7.4. Use
processText(String) |
String |
processText(InputStream in) |
void |
processText(InputStream is,
OutputStream os) |
String |
processText(String text) |
void |
setFreemarkerParsingExtensions(String extensionsList) |
void |
setKeepEncryptedAsVar(boolean keepEncryptedAsVar)
Whether to replace or not the variables which value is encrypted.
|
void |
setParsingExtensions(String extensionsList)
Deprecated.
Since 7.4. Use
setTextParsingExtensions(String) instead. |
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 of
setVariables(Map) |
void |
setVariables(Map<String,String> vars) |
protected Properties |
unescape(Properties props)
unescape variables
|
protected String |
unescape(String value) |
public TextTemplate()
public TextTemplate(Map<String,String> vars)
TextTemplate(Properties)
provides an additional default values behaviorTextTemplate(Properties)
public TextTemplate(Properties vars)
vars
- Properties containing keys and values for template processingpublic boolean isTrim()
public void setTrim(boolean trim)
public void setVariables(Map<String,String> vars)
public void setVariable(String name, String value)
setVariables(Map)
public String getVariable(String name)
public Properties getVariables()
@Deprecated public String process(CharSequence text)
processText(CharSequence)
instead.@Deprecated public String process(InputStream in) throws IOException
processText(InputStream)
instead.IOException
@Deprecated public void process(InputStream in, OutputStream out) throws IOException
processText(InputStream, OutputStream)
instead.IOException
@Deprecated public void process(InputStream is, OutputStream os, boolean processText) throws IOException
processText(InputStream, OutputStream)
(if processText
) or
IOUtils.copy(InputStream, OutputStream)
processText
- if true, text is processed for parameters replacementIOException
protected String processString(CryptoProperties props, String text)
props
- CryptoProperties containing the variable valuestext
- Text to processprotected Properties unescape(Properties props)
public Properties preprocessVars(Properties unprocessedVars)
@Deprecated public String processText(CharSequence text)
processText(String)
public String processText(String text)
public String processText(InputStream in) throws IOException
IOException
public void processText(InputStream is, OutputStream os) throws IOException
IOException
public void initFreeMarker()
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).
public void processFreemarker(File in, File out) throws IOException, freemarker.template.TemplateException
IOException
freemarker.template.TemplateException
public List<String> processDirectory(File in, File out) throws FileNotFoundException, IOException, freemarker.template.TemplateException
in
- Directory to read files fromout
- Directory to write files toFileNotFoundException
IOException
freemarker.template.TemplateException
processText(InputStream, OutputStream)
,
processFreemarker(File, File)
@Deprecated public void setParsingExtensions(String extensionsList)
setTextParsingExtensions(String)
instead.extensionsList
- comma-separated list of files extensions to parsesetTextParsingExtensions(String)
,
setFreemarkerParsingExtensions(String)
public void setTextParsingExtensions(String extensionsList)
extensionsList
- comma-separated list of files extensions to parsepublic void setFreemarkerParsingExtensions(String extensionsList)
public void setKeepEncryptedAsVar(boolean keepEncryptedAsVar)
keepEncryptedAsVar
- if true
, the variables which value is encrypted won't be expandedCopyright © 2015 Nuxeo SA. All rights reserved.