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)}"
and "${#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.

Author:
Bogdan Stefanescu
See Also:
  • Constructor Details

  • 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

      public void setVariables(Map<String,String> vars)
    • setVariable

      public void setVariable(String name, String value)
      If adding multiple variables, prefer use of setVariables(Map)
    • getVariable

      public String getVariable(String name)
    • 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 values
      text - 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 values
      text - Text to process
      transform - Function to transform (quote, escape, etc.) processed vars
      Returns:
      the processed text
      Since:
      2021.22
    • unescape

      protected Properties unescape(Properties props)
      unescape variables
    • unescape

      protected String unescape(String value)
    • preprocessVars

      public Properties preprocessVars(Properties unprocessedVars)
    • processText

      public String processText(String content)
      Since:
      7.4
    • processText

      public String processText(String text, Function<String,String> transform)
      It processes the given text.
      Parameters:
      text - Text to process
      transform - 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 from
      out - Directory to write files to
      Returns:
      copied files list
      Throws:
      IOException
      freemarker.template.TemplateException
      See Also:
    • 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 - if true, 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 - if true, the variables which value is encrypted won't be expanded
      Returns:
      this object to ease chaining
      Since:
      2021.14