com.gentics.api.lib.expressionparser
Class ExpressionEvaluator

java.lang.Object
  extended by com.gentics.api.lib.expressionparser.ExpressionEvaluator
All Implemented Interfaces:
Changeable, Resolvable

public class ExpressionEvaluator
extends Object
implements Changeable

Class for expression evaluation. Implements the basic functionality to evaluate an expression. Also provides static methods for type conversions.


Nested Class Summary
static class ExpressionEvaluator.TypeSafeComparator
          Comparator class for doing typesafe comparisons and sorting.
 
Field Summary
protected  Map baseObjects
          base objects of the expression evaluator
static int OBJECTTYPE_ANY
          constant for any value type
static int OBJECTTYPE_ASSIGNMENT
          constant for the value type "assignment (result)"
static int OBJECTTYPE_BINARY
          constant for binary value type
static int OBJECTTYPE_BOOLEAN
          constant for the value type "boolean"
static int OBJECTTYPE_COLLECTION
          constant for the value type "collection"
static int OBJECTTYPE_DATE
          constant for the value type "date"
static int OBJECTTYPE_NULL
          constant for the value type "null" (object is expected to be null)
static int OBJECTTYPE_NUMBER
          constant for the value type "number"
static int OBJECTTYPE_STRING
          constant for the value type "string"
static int OBJECTTYPE_UNKNOWN
          constant for unknown value type
static int OBJECTTYPE_WILDCARDSTRING
          constant for the value type "string"
protected  PropertyResolver resolver
          PropertyResolver
 
Constructor Summary
ExpressionEvaluator()
          Create a new instance of the expression evaluator
 
Method Summary
 boolean canResolve()
          Check whether the resolvable is capable of resolving properties right now.
 void clearProperties()
          Clear all resolvable properties
static boolean containsAll(Collection leftCollection, Collection rightCollection)
          Check whether the left collection contains all objects from the right collection
static boolean containsNone(Collection leftCollection, Collection rightCollection)
          Check whether the collections have no object in common
static boolean containsOneOf(Collection leftCollection, Collection rightCollection)
          Check whether the left collection contains at least one object of the right collection
 void filter(Expression expression, List<Resolvable> resolvables)
          Filter the given list of resolvables with the given expression.
 Object get(String key)
          Get the property named by key or null if the property does not exist or is not set.
static byte[] getAsBinary(Object object)
          Get the given object as binary.
static Boolean getAsBoolean(Object object)
          Get the given object as boolean.
static Collection getAsCollection(Object object)
          Get the given object as Collection.
static Date getAsDate(Object object)
          Get the given object as Date.
static Number getAsNumber(ExpressionQueryRequest request, Object object, Class numberClass)
           
static Number getAsNumber(Object object)
          Get the given object as number.
static Number getAsNumber(Object object, Class numberClass)
          Get the given object as number of the given class.
static String getAsString(Object object)
          Get the given object as String.
static Object getAsType(Object object, int type)
          Get the given object as object of specified type.
protected static int getObjectType(Object object)
          Analyze the given object and get the objecttype
 Object getProperty(String key)
          Get the property named by key or null if the property does not exist or is not set.
protected static String getTypeName(Object object)
          Get the type of the object as string
static String getValuetypeName(int valueType)
          Get the name of the given value type
protected static boolean incompatibleComparisonTypes(Object object1, Object object2)
          Helper method to throw an Exception "Incompatible types found in comparison..."
static boolean isInteger(Number number)
          Check whether the given number is an integer.
static boolean isTypeSafeEqual(Object object1, Object object2)
          Check whether the objects are typesafe equal.
static boolean isTypeSafeUnequal(Object object1, Object object2)
          Check whether the objects are typesafe unequal.
 boolean match(Expression expression)
          Try to match the given expression (against the added resolvables)
 boolean match(Expression expression, Resolvable matchedObject)
          Try to match the given expression against the given object (and the added resolvables)
 boolean setProperty(String name, Object value)
          Set (modify) the property name to resolve to the given value
 void setRequestParameters(Map parameters)
          Allows you to set parameters which will be passed to ExpressionQueryRequest
 void setResolver(PropertyResolver resolver)
          Set a property resolver, which will be used to resolve paths in expressions
static boolean typeSafeComparison(ExpressionQueryRequest request, Object object1, Object object2, int type)
          Perform a typesafe comparison between the given objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OBJECTTYPE_ANY

public static final int OBJECTTYPE_ANY
constant for any value type

See Also:
Constant Field Values

OBJECTTYPE_NULL

public static final int OBJECTTYPE_NULL
constant for the value type "null" (object is expected to be null)

See Also:
Constant Field Values

OBJECTTYPE_BOOLEAN

public static final int OBJECTTYPE_BOOLEAN
constant for the value type "boolean"

See Also:
Constant Field Values

OBJECTTYPE_NUMBER

public static final int OBJECTTYPE_NUMBER
constant for the value type "number"

See Also:
Constant Field Values

OBJECTTYPE_DATE

public static final int OBJECTTYPE_DATE
constant for the value type "date"

See Also:
Constant Field Values

OBJECTTYPE_STRING

public static final int OBJECTTYPE_STRING
constant for the value type "string"

See Also:
Constant Field Values

OBJECTTYPE_WILDCARDSTRING

public static final int OBJECTTYPE_WILDCARDSTRING
constant for the value type "string"

See Also:
Constant Field Values

OBJECTTYPE_COLLECTION

public static final int OBJECTTYPE_COLLECTION
constant for the value type "collection"

See Also:
Constant Field Values

OBJECTTYPE_ASSIGNMENT

public static final int OBJECTTYPE_ASSIGNMENT
constant for the value type "assignment (result)"

See Also:
Constant Field Values

OBJECTTYPE_UNKNOWN

public static final int OBJECTTYPE_UNKNOWN
constant for unknown value type

See Also:
Constant Field Values

OBJECTTYPE_BINARY

public static final int OBJECTTYPE_BINARY
constant for binary value type

See Also:
Constant Field Values

baseObjects

protected Map baseObjects
base objects of the expression evaluator


resolver

protected PropertyResolver resolver
PropertyResolver

Constructor Detail

ExpressionEvaluator

public ExpressionEvaluator()
Create a new instance of the expression evaluator

Method Detail

setResolver

public void setResolver(PropertyResolver resolver)
Set a property resolver, which will be used to resolve paths in expressions

Parameters:
resolver - property resolver

getValuetypeName

public static String getValuetypeName(int valueType)
Get the name of the given value type

Parameters:
valueType - value type
Returns:
name of the value type as string

setRequestParameters

public void setRequestParameters(Map parameters)
Allows you to set parameters which will be passed to ExpressionQueryRequest

Parameters:
parameters -

match

public boolean match(Expression expression)
              throws ExpressionParserException
Try to match the given expression (against the added resolvables)

Parameters:
expression - expression to match
Returns:
true when the expression matches, false if not
Throws:
ExpressionParserException - when the expression cannot be matched (is not boolean or contains errors)

match

public boolean match(Expression expression,
                     Resolvable matchedObject)
              throws ExpressionParserException
Try to match the given expression against the given object (and the added resolvables)

Parameters:
expression - expression to match
matchedObject - object the expression is matched against
Returns:
true when the expression matches, false if not
Throws:
ExpressionParserException - when the expression cannot be matched (is not boolean or contains errors)

filter

public void filter(Expression expression,
                   List<Resolvable> resolvables)
            throws ExpressionParserException
Filter the given list of resolvables with the given expression. The list will be modified to only contain the objects that match the given expression. If the expression contains the filter() function (PostProcessors), they will also be called with the remaining list.

Parameters:
expression - expression
resolvables - list of resolvables, must not be null and must be modifiable
Throws:
ExpressionParserException

getAsNumber

public static Number getAsNumber(Object object,
                                 Class numberClass)
                          throws EvaluationException
Get the given object as number of the given class. When the object is no instance of the given class, its string representation is parsed as number.

Parameters:
object - object to interpret as number
numberClass - desired class, must be a subclass of Number
Returns:
number representation of the object
Throws:
EvaluationException - when the object cannot be interpreted as number or the given numberClass is not derived from class Number

getAsNumber

public static Number getAsNumber(ExpressionQueryRequest request,
                                 Object object,
                                 Class numberClass)
                          throws EvaluationException
Throws:
EvaluationException

getAsNumber

public static Number getAsNumber(Object object)
                          throws EvaluationException
Get the given object as number. When the object is no instance of class Number, its string representation is parsed as number.

Parameters:
object - object to interpret as number
Returns:
number representation of the object
Throws:
EvaluationException - when the object cannot be interpreted as number

getAsBoolean

public static Boolean getAsBoolean(Object object)
                            throws EvaluationException
Get the given object as boolean. When the object is not instance of class Boolean, its string representation is parsed as boolean. "true", "false", "1" or "0" can be interpreted as boolean.

Parameters:
object - object to interpret as boolean
Returns:
boolean representation of the object
Throws:
EvaluationException - when the object cannot be interpreted as boolean

getAsDate

public static Date getAsDate(Object object)
                      throws EvaluationException
Get the given object as Date. This fails when the object is no instance of class Date.

Parameters:
object - object to interpret as Date
Returns:
Date representation of the object
Throws:
EvaluationException - when the object cannot be interpreted as Date

getAsCollection

public static Collection getAsCollection(Object object)
                                  throws EvaluationException
Get the given object as Collection. When the object is no instance of class Collection, a new collection containing just the object is returned.

Parameters:
object - object to interpret as collection
Returns:
collection representation of the object
Throws:
EvaluationException

getAsString

public static String getAsString(Object object)
                          throws EvaluationException
Get the given object as String. This only fails when the object is a Collection.

Parameters:
object - object to interpret as string
Returns:
string representation of the object
Throws:
EvaluationException - when the object cannot be interpreted as string

getAsBinary

public static byte[] getAsBinary(Object object)
                          throws EvaluationException
Get the given object as binary. This will succeed, if the object either already is binary data, or is a String

Parameters:
object - object to get as binary data
Returns:
binary data
Throws:
EvaluationException

getAsType

public static Object getAsType(Object object,
                               int type)
                        throws EvaluationException
Get the given object as object of specified type. This calls one of the getAsXXX() methods. The type may be one of (OBJECTTYPE_ANY, OBJECTTYPE_BOOLEAN, OBJECTTYPE_COLLECTION, OBJECTTYPE_DATE, OBJECTTYPE_NULL, OBJECTTYPE_NUMBER, OBJECTTYPE_STRING).

Parameters:
object - object to interpret in the given type
type - requested type for the object
Returns:
type-representation of the object
Throws:
EvaluationException - when the object cannot be interpreted in the given type

isInteger

public static boolean isInteger(Number number)
Check whether the given number is an integer. This is true when the object is an instance of one of the following classes: Long, Integer, Short, Byte, BigInteger

Parameters:
number - number to check
Returns:
true when the number is an integer, false if not

getTypeName

protected static String getTypeName(Object object)
Get the type of the object as string

Parameters:
object - object to analyze
Returns:
type of the object in readable form

incompatibleComparisonTypes

protected static boolean incompatibleComparisonTypes(Object object1,
                                                     Object object2)
                                              throws EvaluationException
Helper method to throw an Exception "Incompatible types found in comparison..."

Parameters:
object1 - first object in comparison
object2 - second object in comparison
Returns:
nothing, since this method always throws an exception
Throws:
EvaluationException - since this is the purpose of this method

typeSafeComparison

public static boolean typeSafeComparison(ExpressionQueryRequest request,
                                         Object object1,
                                         Object object2,
                                         int type)
                                  throws EvaluationException
Perform a typesafe comparison between the given objects. The type may be one of (Function.TYPE_SMALLER, Function.TYPE_SMALLEROREQUAL, Function.TYPE_GREATEROREQUAL, Function.TYPE_GREATER).

Parameters:
request - object.
object1 - lefthand-side object
object2 - righthand-side object
type - type of the comparison
Returns:
result of the comparison
Throws:
EvaluationException - when the objects cannot be converted such that the comparison can be done

isTypeSafeUnequal

public static boolean isTypeSafeUnequal(Object object1,
                                        Object object2)
                                 throws EvaluationException
Check whether the objects are typesafe unequal. This is not the converse as isTypeSafeEqual(Object, Object), when object1 is an instance of NestedCollection. In this case, the comparison is interpreted as: "true when object1 contains at least one object that is unequal object2"

Parameters:
object1 - lefthand-side object
object2 - righthand-side object
Returns:
true when the objects are unequal, false if they are equal
Throws:
EvaluationException - when the objects cannot be compared

isTypeSafeEqual

public static boolean isTypeSafeEqual(Object object1,
                                      Object object2)
                               throws EvaluationException
Check whether the objects are typesafe equal.

Parameters:
object1 - lefthand-side object
object2 - righthand-side object
Returns:
true when the objects are equal, false if not
Throws:
EvaluationException - when the objects cannot be compared

getObjectType

protected static int getObjectType(Object object)
                            throws EvaluationException
Analyze the given object and get the objecttype

Parameters:
object - object to analyze
Returns:
objecttype of the object
Throws:
EvaluationException

containsOneOf

public static boolean containsOneOf(Collection leftCollection,
                                    Collection rightCollection)
                             throws EvaluationException
Check whether the left collection contains at least one object of the right collection

Parameters:
leftCollection - left collection
rightCollection - right collection
Returns:
true when the collections contain at least one common object, false if not
Throws:
EvaluationException

containsNone

public static boolean containsNone(Collection leftCollection,
                                   Collection rightCollection)
                            throws EvaluationException
Check whether the collections have no object in common

Parameters:
leftCollection - left collection
rightCollection - right collection
Returns:
true when the collections have no object in common, false if they have at least one common object
Throws:
EvaluationException

containsAll

public static boolean containsAll(Collection leftCollection,
                                  Collection rightCollection)
Check whether the left collection contains all objects from the right collection

Parameters:
leftCollection - left collection
rightCollection - right collection
Returns:
true when the left collection contains all objects from the right collection (or the right collection is empty), false if not

setProperty

public boolean setProperty(String name,
                           Object value)
                    throws InsufficientPrivilegesException
Description copied from interface: Changeable
Set (modify) the property name to resolve to the given value

Specified by:
setProperty in interface Changeable
Parameters:
name - name of the property to set/modify
value - (new) value to set
Returns:
true if property was set successful, false otherwise.
Throws:
InsufficientPrivilegesException - when the property may not be changed

getProperty

public Object getProperty(String key)
Description copied from interface: Resolvable
Get the property named by key or null if the property does not exist or is not set. Alias for Resolvable.get(String).

Specified by:
getProperty in interface Resolvable
Parameters:
key - key of the property
Returns:
value of the property or null

get

public Object get(String key)
Description copied from interface: Resolvable
Get the property named by key or null if the property does not exist or is not set.

Specified by:
get in interface Resolvable
Parameters:
key - key of the property
Returns:
value of the property or null

canResolve

public boolean canResolve()
Description copied from interface: Resolvable
Check whether the resolvable is capable of resolving properties right now.
if this method returns false all calls to Resolvable.getProperty(String) or Resolvable.get(String) will return null.

Specified by:
canResolve in interface Resolvable
Returns:
true when the resolvable can resolve properties. false if not

clearProperties

public void clearProperties()
Clear all resolvable properties



Copyright © 2013 Gentics Software GmbH. All Rights Reserved.