Class PropertiesSubstitutor

java.lang.Object
scriptella.expression.PropertiesSubstitutor

public class PropertiesSubstitutor extends Object
Substitutes properties(or expressions) in strings.

$ symbol indicate property or expression to evaluate and substitute.

The following properties/expression syntax is used:

Property reference

References named property.
Examples:

 $foo
 

Expression

. Expression is wrapped by braces and evaluated by Expression engine. Examples:

 ${name+' '+surname} etc.
 

This class is not thread safe

Version:
1.0
Author:
Fyodor Kupolov
  • Field Details

    • PROP_PTR

      public static final Pattern PROP_PTR
      Simple property patterns, e.g. $property
    • EXPR_PTR

      public static final Pattern EXPR_PTR
      Expression pattern, e.g. ${property} etc.
  • Constructor Details

  • Method Details

    • substitute

      public String substitute(String s)
      Substitutes properties/expressions in s and returns the result string.

      If result of evaluation is null or the property being substitued doesn't have value in callback - the whole expressions is copied into result string as is.

      Parameters:
      s - string to substitute. Null strings allowed.
      Returns:
      substituted string.
    • substitute

      public void substitute(Reader reader, Writer writer) throws IOException
      Copies content from reader to writer and expands properties.
      Parameters:
      reader - reader to process.
      writer - writer to output substituted content to.
      Throws:
      IOException - if I/O error occurs.
    • substitute

      public String substitute(Reader reader) throws IOException
      Reads content from reader and expands properties.

      Note: For performance reasons use substitute(java.io.Reader,java.io.Writer) if possible.

      Parameters:
      reader - reader to process.
      Returns:
      reader's content with properties expanded.
      Throws:
      IOException - if I/O error occurs.
      See Also:
    • getParameters

      public ParametersCallback getParameters()
      Returns:
      parameter callback used for substitution.
    • setParameters

      public void setParameters(ParametersCallback parameters)
      Sets parameters callback used for substitution.
      Parameters:
      parameters - not null parameters callback.
    • getNullString

      public String getNullString()
      Returns string literal representing null value.

      Used when converting objects toString(Object).

      Returns:
      string representing null value.
    • setNullString

      public void setNullString(String nullString)
      Sets string literal representing null.

      Used when converting objects toString(Object).

      Parameters:
      nullString - string literal representing null
    • toString

      protected String toString(Object o)
      Converts specified object to string.

      getNullString() represents null values.

      Subclasses may provide custom conversion strategy here.

      Parameters:
      o - object to convert to String.
      Returns:
      string representation of object.
    • hasProperties

      public static boolean hasProperties(String string)
      Tests if the given string contains properties/expressions.
      Parameters:
      string - string to check.
      Returns:
      true if a given string contains properties/expressions.