Class FlexibleStringExpander

  • All Implemented Interfaces:
    java.io.Serializable, IsEmpty
    Direct Known Subclasses:
    FlexibleStringExpander.ArrayOffsetString, FlexibleStringExpander.ConstSimpleElem

    public abstract class FlexibleStringExpander
    extends java.lang.Object
    implements java.io.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:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CLOSE_BRACKET  
      static java.lang.String OPEN_BRACKET  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected FlexibleStringExpander​(char[] chars)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsConstant​(FlexibleStringExpander fse)
      Returns true if fse contains a String constant.
      static boolean containsExpression​(FlexibleStringExpander fse)
      Returns true if fse contains an expression.
      static boolean containsScript​(FlexibleStringExpander fse)
      Returns true if fse contains a script.
      java.lang.Object expand​(java.util.Map<java.lang.String,​? extends java.lang.Object> context)
      Evaluate this object's expression and return the result as an Object.
      java.lang.Object expand​(java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.Locale locale)
      Evaluate this object's expression and return the result as an Object.
      java.lang.Object expand​(java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.TimeZone timeZone, java.util.Locale locale)
      Evaluate this object's expression and return the result as an Object.
      static java.lang.String expandString​(java.lang.String expression, java.util.Map<java.lang.String,​? extends java.lang.Object> context)
      Evaluate an expression and return the result as a String.
      static java.lang.String expandString​(java.lang.String expression, java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.Locale locale)
      Evaluate an expression and return the result as a String.
      static java.lang.String expandString​(java.lang.String expression, java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.TimeZone timeZone, java.util.Locale locale)
      Evaluate an expression and return the result as a String.
      java.lang.String expandString​(java.util.Map<java.lang.String,​? extends java.lang.Object> context)
      Evaluate this object's expression and return the result as a String.
      java.lang.String expandString​(java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.Locale locale)
      Evaluate this object's expression and return the result as a String.
      java.lang.String expandString​(java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.TimeZone timeZone, java.util.Locale locale)
      Evaluate this object's expression and return the result as a String.
      protected abstract java.lang.Object get​(java.util.Map<java.lang.String,​? extends java.lang.Object> context, java.util.TimeZone timeZone, java.util.Locale locale)  
      char[] getChars()
      Get chars char [ ].
      int getHint()
      Gets hint.
      static FlexibleStringExpander getInstance​(java.lang.String expression)
      Returns a FlexibleStringExpander object.
      static FlexibleStringExpander getInstance​(java.lang.String expression, boolean useCache)  
      abstract java.lang.String getOriginal()
      Returns a copy of the original expression.
      protected static FlexibleStringExpander[] getStrElems​(char[] chars, int offset, int length)  
      abstract boolean isEmpty()
      Returns true if the original expression is empty or null.
      void setHint​(int hint)
      Sets hint.
      java.lang.String toString()
      Returns a copy of the original expression.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • FlexibleStringExpander

        protected FlexibleStringExpander​(char[] chars)
    • Method Detail

      • 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 java.lang.String expandString​(java.lang.String expression,
                                                    java.util.Map<java.lang.String,​? extends java.lang.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 java.lang.String expandString​(java.lang.String expression,
                                                    java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                                    java.util.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 java.lang.String expandString​(java.lang.String expression,
                                                    java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                                    java.util.TimeZone timeZone,
                                                    java.util.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​(java.lang.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​(java.lang.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 java.lang.Object get​(java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                                java.util.TimeZone timeZone,
                                                java.util.Locale locale)
      • expandString

        public java.lang.String expandString​(java.util.Map<java.lang.String,​? extends java.lang.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 java.lang.String expandString​(java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                             java.util.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 java.lang.String expandString​(java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                             java.util.TimeZone timeZone,
                                             java.util.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 java.lang.Object expand​(java.util.Map<java.lang.String,​? extends java.lang.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 java.lang.Object expand​(java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                       java.util.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 java.lang.Object expand​(java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                       java.util.TimeZone timeZone,
                                       java.util.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 java.lang.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 java.lang.String toString()
        Returns a copy of the original expression.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The original expression