Top Description Inners Fields Constructors Methods
com.google.escapevelocity

pack-priv abstract Class ExpressionNode

extends Node
Class Inheritance
Known Direct Subclasses
com.google.escapevelocity.ExpressionNode.BinaryExpressionNode, com.google.escapevelocity.ExpressionNode.NotExpressionNode, com.google.escapevelocity.Parser.RangeLiteralNode, com.google.escapevelocity.Parser.ListLiteralNode, com.google.escapevelocity.Parser.StringLiteralNode, com.google.escapevelocity.ReferenceNode, com.google.escapevelocity.ConstantExpressionNode
Imports
com.google.escapevelocity.Parser.Operator

A node in the parse tree representing an expression. Expressions appear inside directives, specifically #set, #if, #foreach, and macro calls. Expressions can also appear inside indices in references, like $x[$i].

Nontrivial expressions are represented by a tree of ExpressionNode objects. For example, in #if ($foo.bar < 3), the expression $foo.bar < 3 is parsed into a tree that we might describe as

BinaryExpressionNode(
    ReferenceNode.MemberReferenceNode(
        ReferenceNode.PlainReferenceNode("foo"),
        "bar"),
    Operator#LESS,
    ConstantExpressionNode(3))
Author
[email protected] (Éamonn McManus)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
ExpressionNode.BinaryExpressionNode

Represents all binary expressions.

pack-priv static class
ExpressionNode.NotExpressionNode

A node in the parse tree representing an expression like !$a.

Field Summary

Inherited from com.google.escapevelocity.Node:
lineNumberresourceName

Constructor Summary

AccessConstructor and Description
pack-priv
ExpressionNode(String resourceName, int lineNumber)

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract Object
evaluate(EvaluationContext
the context of the evaluation, for example the $variables that are in scope
context
,
boolean
whether an undefined plain reference like $foo is considered to be false. This is the case when evaluating the condition in an #if. Everywhere else, an undefined reference causes an exception.
undefinedIsFalse
)

Returns the result of evaluating this node in the given context.

pack-priv final Object
pack-priv Integer
intValue(EvaluationContext context)

The integer result of evaluating this expression, or null if the expression evaluates to null.

pack-priv boolean
isSilent()

True if a null value for this expression is silently translated to an empty string when substituted into template text.

pack-priv boolean
isTrue(EvaluationContext context, boolean undefinedIsFalse)

True if evaluating this expression yields a value that is considered true by Velocity's rules.

pack-priv final void
render(EvaluationContext context, StringBuilder output)

Implements abstract com.google.escapevelocity.Node.render.

Adds this node's contribution to the output.
private static String
show(Object value)

Returns a string representing the given value, for use in error messages.

public abstract String
toString()

Overrides java.lang.Object.toString.

Returns the source form of this node.
Inherited from com.google.escapevelocity.Node:
consemptyNodeevaluationExceptionevaluationExceptionisHorizontalWhitespace

Constructor Detail

ExpressionNodeback to summary
pack-priv ExpressionNode(String resourceName, int lineNumber)

Method Detail

evaluateback to summary
pack-priv abstract Object evaluate(EvaluationContext context, boolean undefinedIsFalse)

Returns the result of evaluating this node in the given context. This result may be used as part of a further operation, for example evaluating 2 + 3 to 5 in order to set $x to 5 in #set ($x = 2 + 3). Or it may be used directly as part of the template output, for example evaluating replacing name by Fred in My name is $name..

This has to be an Object rather than a String (or rather than appending to a StringBuilder) because it can potentially participate in other operations. In the preceding example, the nodes representing 2 and 3 and the node representing 2 + 3 all return Integer. As another example, in #if ($foo.bar < 3) the value $foo is itself an ExpressionNode which evaluates to the object referenced by the foo variable, and $foo.bar is another ExpressionNode that takes the value from foo and looks for the property bar in it.

Parameters
context:EvaluationContext

the context of the evaluation, for example the $variables that are in scope

undefinedIsFalse:boolean

whether an undefined plain reference like $foo is considered to be false. This is the case when evaluating the condition in an #if. Everywhere else, an undefined reference causes an exception.

evaluateback to summary
pack-priv final Object evaluate(EvaluationContext context)
intValueback to summary
pack-priv Integer intValue(EvaluationContext context)

The integer result of evaluating this expression, or null if the expression evaluates to null.

Exceptions
EvaluationException:
if evaluating the expression produces an exception, or if it yields a value that is neither an integer nor null.
isSilentback to summary
pack-priv boolean isSilent()

True if a null value for this expression is silently translated to an empty string when substituted into template text. Otherwise it results in an exception.

isTrueback to summary
pack-priv boolean isTrue(EvaluationContext context, boolean undefinedIsFalse)

True if evaluating this expression yields a value that is considered true by Velocity's rules. A value is false if it is null or equal to Boolean.FALSE. Every other value is true.

Note that the text at the similar link here states that empty collections and empty strings are also considered false, but that is not what Velocity actually implements.

renderback to summary
pack-priv final void render(EvaluationContext context, StringBuilder output)

Implements abstract com.google.escapevelocity.Node.render.

Doc from com.google.escapevelocity.Node.render.

Adds this node's contribution to the output. Depending on the node type, this might be plain text from the template, or one branch of an #if, or the value of a $reference, etc.

Annotations
@Override
showback to summary
private static String show(Object value)

Returns a string representing the given value, for use in error messages. The string includes both the value's toString() and its type.

toStringback to summary
public abstract String toString()

Overrides java.lang.Object.toString.

Returns the source form of this node. This may not be exactly how it appears in the template. For example both $x and ${x} end up being the same kind of node, and its toString() is "$x".

This method is used in error messages. It is not invoked in normal template evaluation.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
com.google.escapevelocity back to summary

pack-priv Class ExpressionNode.BinaryExpressionNode

extends ExpressionNode
Class Inheritance

Represents all binary expressions. In #set ($a = $b + $c), this will be the type of the node representing $b + $c.

Field Summary

Modifier and TypeField and Description
pack-priv final ExpressionNode
pack-priv final Parser.Operator
pack-priv final ExpressionNode

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private boolean
equal(EvaluationContext context)

Returns true if lhs and rhs are equal according to Velocity.

pack-priv Object
evaluate(EvaluationContext
the context of the evaluation, for example the $variables that are in scope
context
,
boolean
whether an undefined plain reference like $foo is considered to be false. This is the case when evaluating the condition in an #if. Everywhere else, an undefined reference causes an exception.
undefinedIsFalse
)

Implements abstract com.google.escapevelocity.ExpressionNode.evaluate.

Returns the result of evaluating this node in the given context.
private Void
nullOperand(boolean leftIsNull)

private String
private Object
public String
toString()

Implements abstract com.google.escapevelocity.ExpressionNode.toString.

Returns the source form of this node.
Inherited from com.google.escapevelocity.ExpressionNode:
evaluateintValueisSilentisTruerender

Field Detail

lhsback to summary
pack-priv final ExpressionNode lhs
opback to summary
pack-priv final Parser.Operator op
rhsback to summary
pack-priv final ExpressionNode rhs

Constructor Detail

BinaryExpressionNodeback to summary
pack-priv BinaryExpressionNode(ExpressionNode lhs, Parser.Operator op, ExpressionNode rhs)

Method Detail

equalback to summary
private boolean equal(EvaluationContext context)

Returns true if lhs and rhs are equal according to Velocity.

Velocity's definition of equality differs depending on whether the objects being compared are of the same class. If so, equality comes from Object.equals as you would expect. But if they are not of the same class, they are considered equal if their toString() values are equal. This means that integer 123 equals long 123L and also string "123". It also means that equality isn't always transitive. For example, two StringBuilder objects each containing "123" will not compare equal, even though the string "123" compares equal to each of them.

evaluateback to summary
pack-priv Object evaluate(EvaluationContext context, boolean undefinedIsFalse)

Implements abstract com.google.escapevelocity.ExpressionNode.evaluate.

Doc from com.google.escapevelocity.ExpressionNode.evaluate.

Returns the result of evaluating this node in the given context. This result may be used as part of a further operation, for example evaluating 2 + 3 to 5 in order to set $x to 5 in #set ($x = 2 + 3). Or it may be used directly as part of the template output, for example evaluating replacing name by Fred in My name is $name..

This has to be an Object rather than a String (or rather than appending to a StringBuilder) because it can potentially participate in other operations. In the preceding example, the nodes representing 2 and 3 and the node representing 2 + 3 all return Integer. As another example, in #if ($foo.bar < 3) the value $foo is itself an ExpressionNode which evaluates to the object referenced by the foo variable, and $foo.bar is another ExpressionNode that takes the value from foo and looks for the property bar in it.

Parameters
context:EvaluationContext

the context of the evaluation, for example the $variables that are in scope

undefinedIsFalse:boolean

whether an undefined plain reference like $foo is considered to be false. This is the case when evaluating the condition in an #if. Everywhere else, an undefined reference causes an exception.

Annotations
@Override
nullOperandback to summary
private Void nullOperand(boolean leftIsNull)
operandStringback to summary
private String operandString(ExpressionNode operand)
plusback to summary
private Object plus(EvaluationContext context)
toStringback to summary
public String toString()

Implements abstract com.google.escapevelocity.ExpressionNode.toString.

Doc from com.google.escapevelocity.ExpressionNode.toString.

Returns the source form of this node. This may not be exactly how it appears in the template. For example both $x and ${x} end up being the same kind of node, and its toString() is "$x".

This method is used in error messages. It is not invoked in normal template evaluation.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
com.google.escapevelocity back to summary

pack-priv Class ExpressionNode.NotExpressionNode

extends ExpressionNode
Class Inheritance

A node in the parse tree representing an expression like !$a.

Field Summary

Modifier and TypeField and Description
private final ExpressionNode

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv Object
evaluate(EvaluationContext
the context of the evaluation, for example the $variables that are in scope
context
,
boolean
whether an undefined plain reference like $foo is considered to be false. This is the case when evaluating the condition in an #if. Everywhere else, an undefined reference causes an exception.
undefinedIsFalse
)

Implements abstract com.google.escapevelocity.ExpressionNode.evaluate.

Returns the result of evaluating this node in the given context.
public String
toString()

Implements abstract com.google.escapevelocity.ExpressionNode.toString.

Returns the source form of this node.
Inherited from com.google.escapevelocity.ExpressionNode:
evaluateintValueisSilentisTruerender

Field Detail

exprback to summary
private final ExpressionNode expr

Constructor Detail

NotExpressionNodeback to summary
pack-priv NotExpressionNode(ExpressionNode expr)

Method Detail

evaluateback to summary
pack-priv Object evaluate(EvaluationContext context, boolean undefinedIsFalse)

Implements abstract com.google.escapevelocity.ExpressionNode.evaluate.

Doc from com.google.escapevelocity.ExpressionNode.evaluate.

Returns the result of evaluating this node in the given context. This result may be used as part of a further operation, for example evaluating 2 + 3 to 5 in order to set $x to 5 in #set ($x = 2 + 3). Or it may be used directly as part of the template output, for example evaluating replacing name by Fred in My name is $name..

This has to be an Object rather than a String (or rather than appending to a StringBuilder) because it can potentially participate in other operations. In the preceding example, the nodes representing 2 and 3 and the node representing 2 + 3 all return Integer. As another example, in #if ($foo.bar < 3) the value $foo is itself an ExpressionNode which evaluates to the object referenced by the foo variable, and $foo.bar is another ExpressionNode that takes the value from foo and looks for the property bar in it.

Parameters
context:EvaluationContext

the context of the evaluation, for example the $variables that are in scope

undefinedIsFalse:boolean

whether an undefined plain reference like $foo is considered to be false. This is the case when evaluating the condition in an #if. Everywhere else, an undefined reference causes an exception.

Annotations
@Override
toStringback to summary
public String toString()

Implements abstract com.google.escapevelocity.ExpressionNode.toString.

Doc from com.google.escapevelocity.ExpressionNode.toString.

Returns the source form of this node. This may not be exactly how it appears in the template. For example both $x and ${x} end up being the same kind of node, and its toString() is "$x".

This method is used in error messages. It is not invoked in normal template evaluation.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override