org.ofbiz.base.util.string
Class FlexibleStringExpander

java.lang.Object
  extended by org.ofbiz.base.util.string.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 bsh scripts by using the 'bsh:' prefix, and 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

Nested Class Summary
protected static class FlexibleStringExpander.ArrayOffsetString
           
protected static class FlexibleStringExpander.ConstOffsetElem
          An object that represents a String constant portion of an expression.
protected static class FlexibleStringExpander.ConstSimpleElem
          An object that represents a String constant portion of an expression.
protected static class FlexibleStringExpander.CurrElem
          An object that represents a currency portion of an expression.
protected static class FlexibleStringExpander.Elements
          A container object that contains expression fragments.
protected static class FlexibleStringExpander.NestedVarElem
          An object that represents a nested expression.
protected static class FlexibleStringExpander.ScriptElem
          An object that represents a ${[groovy|bsh]:} expression.
protected static class FlexibleStringExpander.VarElem
          An object that represents a simple, non-nested expression.
 
Field Summary
protected  char[] chars
           
static java.lang.String closeBracket
           
protected static UtilCache<org.ofbiz.base.util.string.FlexibleStringExpander.Key,FlexibleStringExpander> exprCache
           
protected  int hint
           
static java.lang.String module
           
protected static FlexibleStringExpander nullExpr
           
static java.lang.String openBracket
           
 
Constructor Summary
protected FlexibleStringExpander(char[] chars)
           
 
Method Summary
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.
 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.
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.
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)
           
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.
 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
 

Field Detail

module

public static final java.lang.String module

openBracket

public static final java.lang.String openBracket
See Also:
Constant Field Values

closeBracket

public static final java.lang.String closeBracket
See Also:
Constant Field Values

exprCache

protected static final UtilCache<org.ofbiz.base.util.string.FlexibleStringExpander.Key,FlexibleStringExpander> exprCache

nullExpr

protected static final FlexibleStringExpander nullExpr

chars

protected final char[] chars

hint

protected int hint
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)

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