Class FlexibleStringExpander

java.lang.Object
org.apache.ofbiz.base.util.string.FlexibleStringExpander
All Implemented Interfaces:
Serializable, IsEmpty
Direct Known Subclasses:
FlexibleStringExpander.ArrayOffsetString, FlexibleStringExpander.ConstSimpleElem

public abstract class FlexibleStringExpander extends Object implements Serializable, IsEmpty
Expands String values that contain Unified Expression Language (JSR 245) syntax. This class also supports the execution of Groovy scripts by using the 'groovy:' prefix. Further it is possible to control the output by specifying the suffix '?currency(XXX)' to format the output according to the supplied locale and specified (XXX) currency.

This class extends the UEL by allowing nested expressions.

See Also:
  • Field Details

  • Constructor Details

    • FlexibleStringExpander

      protected FlexibleStringExpander(char[] chars)
  • Method Details

    • containsConstant

      public static boolean containsConstant(FlexibleStringExpander fse)
      Returns true if fse contains a String constant.
      Parameters:
      fse - The FlexibleStringExpander to test
      Returns:
      true if fse contains a String constant
    • containsExpression

      public static boolean containsExpression(FlexibleStringExpander fse)
      Returns true if fse contains an expression.
      Parameters:
      fse - The FlexibleStringExpander to test
      Returns:
      true if fse contains an expression
    • containsScript

      public static boolean containsScript(FlexibleStringExpander fse)
      Returns true if fse contains a script.
      Parameters:
      fse - The FlexibleStringExpander to test
      Returns:
      true if fse contains a script
    • expandString

      public static String expandString(String expression, Map<String,? extends Object> context)
      Evaluate an expression and return the result as a String. Null expressions return null. A null context argument will return the original expression.

      Note that the behavior of this method is not the same as using FlexibleStringExpander.getInstance(expression).expandString(context) because it returns null when given a null expression argument, and FlexibleStringExpander.getInstance(expression).expandString(context) returns an empty String.

      Parameters:
      expression - The original expression
      context - The evaluation context
      Returns:
      The original expression's evaluation result as a String
    • expandString

      public static String expandString(String expression, Map<String,? extends Object> context, Locale locale)
      Evaluate an expression and return the result as a String. Null expressions return null. A null context argument will return the original expression.

      Note that the behavior of this method is not the same as using FlexibleStringExpander.getInstance(expression).expandString(context, locale) because it returns null when given a null expression argument, and FlexibleStringExpander.getInstance(expression).expandString(context, locale) returns an empty String.

      Parameters:
      expression - The original expression
      context - The evaluation context
      locale - The locale to be used for localization
      Returns:
      The original expression's evaluation result as a String
    • expandString

      public static String expandString(String expression, Map<String,? extends Object> context, TimeZone timeZone, Locale locale)
      Evaluate an expression and return the result as a String. Null expressions return null. A null context argument will return the original expression.

      Note that the behavior of this method is not the same as using FlexibleStringExpander.getInstance(expression).expandString(context, timeZone locale) because it returns null when given a null expression argument, and FlexibleStringExpander.getInstance(expression).expandString(context, timeZone, locale) returns an empty String.

      Parameters:
      expression - The original expression
      context - The evaluation context
      timeZone - The time zone to be used for localization
      locale - The locale to be used for localization
      Returns:
      The original expression's evaluation result as a String
    • getInstance

      public static FlexibleStringExpander getInstance(String expression)
      Returns a FlexibleStringExpander object.

      A null or empty argument will return a FlexibleStringExpander object that represents an empty expression. That object is a shared singleton, so there is no memory or performance penalty in using it.

      If the method is passed a String argument that doesn't contain an expression, the FlexibleStringExpander object that is returned does not perform any evaluations on the original String - any methods that return a String will return the original String. The object returned by this method is very compact - taking less memory than the original String.

      Parameters:
      expression - The original expression
      Returns:
      A FlexibleStringExpander instance
    • getInstance

      public static FlexibleStringExpander getInstance(String expression, boolean useCache)
    • getStrElems

      protected static FlexibleStringExpander[] getStrElems(char[] chars, int offset, int length)
    • getChars

      public char[] getChars()
      Get chars char [ ].
      Returns:
      the char [ ]
    • getHint

      public int getHint()
      Gets hint.
      Returns:
      the hint
    • setHint

      public void setHint(int hint)
      Sets hint.
      Parameters:
      hint - the hint
    • get

      protected abstract Object get(Map<String,? extends Object> context, TimeZone timeZone, Locale locale)
    • expandString

      public String expandString(Map<String,? extends Object> context)
      Evaluate this object's expression and return the result as a String. Null or empty expressions return an empty String. A null context argument will return the original expression.
      Parameters:
      context - The evaluation context
      Returns:
      This object's expression result as a String
    • expandString

      public String expandString(Map<String,? extends Object> context, Locale locale)
      Evaluate this object's expression and return the result as a String. Null or empty expressions return an empty String. A null context argument will return the original expression.
      Parameters:
      context - The evaluation context
      locale - The locale to be used for localization
      Returns:
      This object's expression result as a String
    • expandString

      public String expandString(Map<String,? extends Object> context, TimeZone timeZone, Locale locale)
      Evaluate this object's expression and return the result as a String. Null or empty expressions return an empty String. A null context argument will return the original expression.
      Parameters:
      context - The evaluation context
      timeZone - The time zone to be used for localization
      locale - The locale to be used for localization
      Returns:
      This object's expression result as a String
    • expand

      public Object expand(Map<String,? extends Object> context)
      Evaluate this object's expression and return the result as an Object. Null or empty expressions return an empty String. A null context argument will return the original expression.
      Parameters:
      context - The evaluation context
      Returns:
      This object's expression result as a String
    • expand

      public Object expand(Map<String,? extends Object> context, Locale locale)
      Evaluate this object's expression and return the result as an Object. Null or empty expressions return an empty String. A null context argument will return the original expression.
      Parameters:
      context - The evaluation context
      locale - The locale to be used for localization
      Returns:
      This object's expression result as a String
    • expand

      public Object expand(Map<String,? extends Object> context, TimeZone timeZone, Locale locale)
      Evaluate this object's expression and return the result as an Object. Null or empty expressions return an empty String. A null context argument will return the original expression.
      Parameters:
      context - The evaluation context
      timeZone - The time zone to be used for localization
      locale - The locale to be used for localization
      Returns:
      This object's expression result as a String
    • getOriginal

      public abstract String getOriginal()
      Returns a copy of the original expression.
      Returns:
      The original expression
    • isEmpty

      public abstract boolean isEmpty()
      Returns true if the original expression is empty or null.
      Specified by:
      isEmpty in interface IsEmpty
      Returns:
      true if the original expression is empty or null
    • toString

      public String toString()
      Returns a copy of the original expression.
      Overrides:
      toString in class Object
      Returns:
      The original expression