freemarker.template.expression
Class ExpressionUtils

java.lang.Object
  extended by freemarker.template.expression.ExpressionUtils

public final class ExpressionUtils
extends java.lang.Object

Utility methods for evaluating expressions. These are used in various parts of the freemarker.template.expression package. They are also generally useful when dealing with TemplateModels directly, such as when retrieving parameters from a TemplateMethodModel2 parameter list.

Since:
1.7.5
Version:
$Id: ExpressionUtils.java 1101 2013-04-01 04:17:32Z run2000 $
Author:
Nicholas Cull

Nested Class Summary
static class ExpressionUtils.ExpressionType
          Enumerates the types of results that an expression can generate.
 
Field Summary
static java.util.Set<ExpressionUtils.ExpressionType> HASH
          The expression can be a Hash only.
static java.util.Set<ExpressionUtils.ExpressionType> LIST
          The expression can be a List only.
static java.util.Set<ExpressionUtils.ExpressionType> METHOD
          The expression can be a Method only.
static java.util.Set<ExpressionUtils.ExpressionType> NUMBER
          The expression can be a Number only.
static java.util.Set<ExpressionUtils.ExpressionType> STRING
          The expression can be a String only.
static java.util.Set<ExpressionUtils.ExpressionType> STRING_OR_NUMBER
          The expression can be a String or a Number.
static java.util.Set<ExpressionUtils.ExpressionType> VARIABLE
          The expression is a variable, so can return anything other than a constant.
 
Method Summary
static long compareNumbers(TemplateModel leftModel, TemplateModel rightModel)
          Compares two numeric expressions.
static long getAsNumber(TemplateModel model)
          Determines the given TemplateModel's long value.
static java.lang.String getAsString(TemplateModel model)
          Determines the given TemplateModel's String value.
static java.lang.String getAsStringOrEmpty(TemplateModel model)
          Determines the given TemplateModel's String value.
static boolean isEqual(TemplateModel leftModel, TemplateModel rightModel)
          Determines whether both sides of an expression are equal.
static boolean isTrue(TemplateModel model)
          Determines the "truth" of a given template model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VARIABLE

public static final java.util.Set<ExpressionUtils.ExpressionType> VARIABLE
The expression is a variable, so can return anything other than a constant.


STRING_OR_NUMBER

public static final java.util.Set<ExpressionUtils.ExpressionType> STRING_OR_NUMBER
The expression can be a String or a Number.


STRING

public static final java.util.Set<ExpressionUtils.ExpressionType> STRING
The expression can be a String only.


NUMBER

public static final java.util.Set<ExpressionUtils.ExpressionType> NUMBER
The expression can be a Number only.


HASH

public static final java.util.Set<ExpressionUtils.ExpressionType> HASH
The expression can be a Hash only.


LIST

public static final java.util.Set<ExpressionUtils.ExpressionType> LIST
The expression can be a List only.


METHOD

public static final java.util.Set<ExpressionUtils.ExpressionType> METHOD
The expression can be a Method only.

Method Detail

isTrue

public static boolean isTrue(TemplateModel model)
                      throws TemplateModelException

Determines the "truth" of a given template model. This is determined as follows:

Parameters:
model - the TemplateModel to be tested, possibly null
Returns:
true if the model evaluates to true, otherwise false
Throws:
TemplateModelException - the truth of the template model could not be determined

getAsString

public static java.lang.String getAsString(TemplateModel model)
                                    throws TemplateModelException

Determines the given TemplateModel's String value. This is a simple case of getting the TemplateScalarModel.getAsString() value from the model, assuming it implements TemplateScalarModel. If it's a TemplateNumberModel, we return a base-10 encoding of the number value. If it's empty or null, return null.

Parameters:
model - the TemplateModel to get the String value from, possibly null
Returns:
the String value, or null if the model is empty
Throws:
TemplateModelException - the String value of the template model could not be determined

getAsStringOrEmpty

public static java.lang.String getAsStringOrEmpty(TemplateModel model)
                                           throws TemplateModelException

Determines the given TemplateModel's String value. This is a simple case of getting the TemplateScalarModel.getAsString() value from the model, assuming it implements TemplateScalarModel. If it's a TemplateNumberModel, we return a base-10 encoding of the number value. If it's empty or null, return the empty string.

Parameters:
model - the TemplateModel to get the String value from, possibly null
Returns:
the String value, or the empty String if the model is empty
Throws:
TemplateModelException - the String value of the template model could not be determined
Since:
1.9

getAsNumber

public static long getAsNumber(TemplateModel model)
                        throws TemplateModelException

Determines the given TemplateModel's long value. This is a simple case of getting the TemplateNumberModel.getAsNumber() value from the model, assuming it implements TemplateNumberModel. If it's empty or null, return 0.

Parameters:
model - the TemplateModel to get the long value from, possibly null
Throws:
TemplateModelException - the String value of the template model could not be determined

isEqual

public static boolean isEqual(TemplateModel leftModel,
                              TemplateModel rightModel)
                       throws TemplateModelException
Determines whether both sides of an expression are equal. Can deal with null values on either side of the test.

Parameters:
leftModel - the left-hand TemplateModel to be compared, possibly null
rightModel - the right-hand TemplateModel to be compared, possibly null
Returns:
true if the models are equal in value, otherwise false
Throws:
TemplateModelException - the template models could not be compared

compareNumbers

public static long compareNumbers(TemplateModel leftModel,
                                  TemplateModel rightModel)
                           throws TemplateModelException
Compares two numeric expressions. Can deal with null values on either side of the comparison.

Parameters:
leftModel - the left-hand TemplateModel to be compared, possibly null
rightModel - the right-hand TemplateModel to be compared, possibly null
Returns:
<0 if the left model is less than the right model, >0 if the left model is greater than the right model, or == 0 if the models are the same
Throws:
TemplateModelException - the template models could not be compared