Package net.sf.saxon.expr
Class Literal
java.lang.Object
net.sf.saxon.expr.Expression
net.sf.saxon.expr.Literal
- All Implemented Interfaces:
Serializable
,SourceLocator
,LocationProvider
,SaxonLocator
,InstructionInfo
,Locator
- Direct Known Subclasses:
StringLiteral
A Literal is an expression whose value is constant: it is a class that implements the
Expression
interface as a wrapper around a Value
. This may derive from an actual literal in an XPath expression
or query, or it may be the result of evaluating a constant subexpression such as true() or xs:date('2007-01-16')- See Also:
-
Field Summary
Fields inherited from class net.sf.saxon.expr.Expression
EFFECTIVE_BOOLEAN_VALUE, EVALUATE_METHOD, EVENT_FEED_METHOD, ITEM_FEED_METHOD, ITERATE_METHOD, locationId, MAX_SEQUENCE_LENGTH, MAX_STRING_LENGTH, PROCESS_METHOD, PUSH_SELECTION, staticProperties, UNBOUNDED_LOWER, UNBOUNDED_UPPER, WATCH_METHOD
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddToPathMap
(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) Add a representation of this expression to a PathMap.int
Determine the cardinalityint
Compute the static properties of this expression (other than its type).copy()
Copy an expression.boolean
effectiveBooleanValue
(XPathContext context) Get the effective boolean value of the expression.boolean
Determine whether two literals are equal, when considered as expressions.evaluateAsString
(XPathContext context) Evaluate an expression as a String.evaluateItem
(XPathContext context) Evaluate as a singleton item (or empty sequence).void
evaluatePendingUpdates
(XPathContext context, PendingUpdateList pul) Evaluate an updating expression, adding the results to a Pending Update List.void
Diagnostic print of expression structure.final int
Determine which aspects of the context the expression depends on.For an expression that returns an integer or a sequence of integers, get a lower and upper bound on the values of the integers that may be returned, from static analysis.Determine the data type of the items in the expression, if possiblegetValue()
Get the value represented by this Literalint
hashCode()
Return a hash code to support the equals() functionstatic boolean
isAtomic
(Expression exp) Test whether the literal wraps an atomic value.static boolean
isConstantBoolean
(Expression exp, boolean value) Test if a literal represents the boolean value truestatic boolean
isConstantOne
(Expression exp) Test if a literal represents the integer value 1static boolean
Test whether the literal explicitly wraps an empty sequence.boolean
Determine whether the expression can be evaluated without reference to the part of the context document outside the subtree rooted at the context node.boolean
Determine whether this is a vacuous expression as defined in the XQuery update specificationSequenceIterator<? extends Item>
iterate()
Return an Iterator to iterate over the values of a sequence.SequenceIterator<? extends Item>
iterate
(XPathContext context) Return an Iterator to iterate over the values of a sequence.static Literal
Make an empty-sequence literalstatic Literal
makeLiteral
(Value value) Create a literal as a wrapper around a Value (factory method)final Expression
optimize
(ExpressionVisitor visitor, ExpressionVisitor.ContextItemType contextItemType) Optimize an expressionvoid
process
(XPathContext context) Process the value as an instruction, without returning any tail callssimplify
(ExpressionVisitor visitor) Simplify an expressiontoString()
The toString() method for an expression attempts to give a representation of the expression in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.typeCheck
(ExpressionVisitor visitor, ExpressionVisitor.ContextItemType contextItemType) TypeCheck an expressionMethods inherited from class net.sf.saxon.expr.Expression
adoptChildExpression, checkForUpdatingSubexpressions, checkPermittedContents, computeDependencies, computeStaticProperties, doPromotion, dynamicError, explain, getCardinality, getColumnNumber, getColumnNumber, getConstructType, getContainer, getEvaluationMethod, getExecutable, getExpressionName, getHostLanguage, getImplementationMethod, getIntrinsicDependencies, getLineNumber, getLineNumber, getLocationId, getLocationProvider, getObjectName, getProperties, getProperty, getPublicId, getSlotsUsed, getSpecialProperties, getSystemId, getSystemId, hasLoopingSubexpression, hasVariableBinding, implementsStaticTypeCheck, isUpdatingExpression, iterateEvents, iterateSameFocusSubExpressions, iterateSubExpressions, markTailFunctionCalls, promote, replaceSubExpression, resetLocalStaticProperties, setContainer, setEvaluationMethod, setFiltered, setFlattened, setLocationId, staticTypeCheck, suppressValidation, typeError
-
Constructor Details
-
Literal
Create a literal as a wrapper around a Value- Parameters:
value
- the value of this literal
-
-
Method Details
-
makeLiteral
Create a literal as a wrapper around a Value (factory method)- Parameters:
value
- the value of this literal- Returns:
- the Literal
-
makeEmptySequence
Make an empty-sequence literal- Returns:
- a literal whose value is the empty sequence
-
getValue
Get the value represented by this Literal- Returns:
- the constant value
-
simplify
Simplify an expression- Overrides:
simplify
in classExpression
- Parameters:
visitor
- an expression visitor- Returns:
- for a Value, this always returns the value unchanged
-
typeCheck
public Expression typeCheck(ExpressionVisitor visitor, ExpressionVisitor.ContextItemType contextItemType) TypeCheck an expression- Overrides:
typeCheck
in classExpression
- Parameters:
visitor
- an expression visitorcontextItemType
- the static type of "." at the point where this expression is invoked. The parameter is set to null if it is known statically that the context item will be undefined. If the type of the context item is not known statically, the argument is set toType.ITEM_TYPE
- Returns:
- for a Value, this always returns the value unchanged
-
optimize
public final Expression optimize(ExpressionVisitor visitor, ExpressionVisitor.ContextItemType contextItemType) Optimize an expression- Overrides:
optimize
in classExpression
- Parameters:
visitor
- an expression visitorcontextItemType
- the static type of "." at the point where this expression is invoked. The parameter is set to null if it is known statically that the context item will be undefined. If the type of the context item is not known statically, the argument is set toType.ITEM_TYPE
- Returns:
- for a Value, this always returns the value unchanged
-
getItemType
Determine the data type of the items in the expression, if possible- Specified by:
getItemType
in classExpression
- Parameters:
th
- The TypeHierarchy. Can be null if the target is an AtomicValue.- Returns:
- for the default implementation: AnyItemType (not known)
-
computeCardinality
public int computeCardinality()Determine the cardinality- Specified by:
computeCardinality
in classExpression
- Returns:
- the computed cardinality, as one of the values
StaticProperty.ALLOWS_ZERO_OR_ONE
,StaticProperty.EXACTLY_ONE
,StaticProperty.ALLOWS_ONE_OR_MORE
,StaticProperty.ALLOWS_ZERO_OR_MORE
-
computeSpecialProperties
public int computeSpecialProperties()Compute the static properties of this expression (other than its type). For a Value, the only special property isStaticProperty.NON_CREATIVE
.- Overrides:
computeSpecialProperties
in classExpression
- Returns:
- the value
StaticProperty.NON_CREATIVE
-
getIntegerBounds
For an expression that returns an integer or a sequence of integers, get a lower and upper bound on the values of the integers that may be returned, from static analysis. The default implementation returns null, meaning "unknown" or "not applicable". Other implementations return an array of two IntegerValue objects, representing the lower and upper bounds respectively. The values UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH are used to indicate values limited by the size of a string or the size of a sequence.- Overrides:
getIntegerBounds
in classExpression
- Returns:
- the lower and upper bounds of integer values in the result, or null to indicate unknown or not applicable.
-
isVacuousExpression
public boolean isVacuousExpression()Determine whether this is a vacuous expression as defined in the XQuery update specification- Overrides:
isVacuousExpression
in classExpression
- Returns:
- true if this expression is vacuous
-
copy
Copy an expression. This makes a deep copy.- Specified by:
copy
in classExpression
- Returns:
- the copy of the original expression
-
addToPathMap
Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited by an expression in a source tree.- Overrides:
addToPathMap
in classExpression
- Parameters:
pathMap
- the PathMap to which the expression should be addedpathMapNodeSet
- the set of nodes within the path map- Returns:
- the pathMapNode representing the focus established by this expression, in the case where this expression is the first operand of a path expression or filter expression
-
getDependencies
public final int getDependencies()Determine which aspects of the context the expression depends on. The result is a bitwise-or'ed value composed from constants such as StaticProperty.VARIABLES and StaticProperty.CURRENT_NODE- Overrides:
getDependencies
in classExpression
- Returns:
- for a Value, this always returns zero.
-
iterate
Return an Iterator to iterate over the values of a sequence. The value of every expression can be regarded as a sequence, so this method is supported for all expressions. This default implementation handles iteration for expressions that return singleton values: for non-singleton expressions, the subclass must provide its own implementation.- Overrides:
iterate
in classExpression
- Parameters:
context
- supplies the context for evaluation- Returns:
- a SequenceIterator that can be used to iterate over the result of the expression
- Throws:
XPathException
- if any dynamic error occurs evaluating the expression
-
iterate
Return an Iterator to iterate over the values of a sequence. The value of every expression can be regarded as a sequence, so this method is supported for all expressions. This default implementation handles iteration for expressions that return singleton values: for non-singleton expressions, the subclass must provide its own implementation.- Returns:
- a SequenceIterator that can be used to iterate over the result of the expression
- Throws:
XPathException
- if any dynamic error occurs evaluating the expression
-
evaluateItem
Evaluate as a singleton item (or empty sequence). Note: this implementation returns the first item in the sequence. The method should not be used unless appropriate type-checking has been done to ensure that the value will be a singleton.- Overrides:
evaluateItem
in classExpression
- Parameters:
context
- The context in which the expression is to be evaluated- Returns:
- the node or atomic value that results from evaluating the expression; or null to indicate that the result is an empty sequence
- Throws:
XPathException
- if any dynamic error occurs evaluating the expression
-
process
Process the value as an instruction, without returning any tail calls- Overrides:
process
in classExpression
- Parameters:
context
- The dynamic context, giving access to the current node, the current variables, etc.- Throws:
XPathException
- if a dynamic error occurs
-
evaluateAsString
Description copied from class:Expression
Evaluate an expression as a String. This function must only be called in contexts where it is known that the expression will return a single string (or where an empty sequence is to be treated as a zero-length string). Implementations should not attempt to convert the result to a string, other than converting () to "". This method is used mainly to evaluate expressions produced by compiling an attribute value template.- Overrides:
evaluateAsString
in classExpression
- Parameters:
context
- The context in which the expression is to be evaluated- Returns:
- the value of the expression, evaluated in the current context. The expression must return a string or (); if the value of the expression is (), this method returns "".
- Throws:
XPathException
- if any dynamic error occurs evaluating the expression
-
effectiveBooleanValue
Get the effective boolean value of the expression. This returns false if the value is the empty sequence, a zero-length string, a number equal to zero, or the boolean false. Otherwise it returns true.- Overrides:
effectiveBooleanValue
in classExpression
- Parameters:
context
- The context in which the expression is to be evaluated- Returns:
- the effective boolean value
- Throws:
XPathException
- if any dynamic error occurs evaluating the expression
-
evaluatePendingUpdates
public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException Evaluate an updating expression, adding the results to a Pending Update List. The default implementation of this method, which is used for non-updating expressions, throws an UnsupportedOperationException. The implementation for a literal representing an empty sequence, however, is a no-op.- Overrides:
evaluatePendingUpdates
in classExpression
- Parameters:
context
- the XPath dynamic evaluation contextpul
- the pending update list to which the results should be written- Throws:
XPathException
- if evaluation fails
-
equals
Determine whether two literals are equal, when considered as expressions.- Overrides:
equals
in classObject
- Parameters:
obj
- the other expression- Returns:
- true if the two literals are equal. The test here requires (a) identity in the sense defined by XML Schema (same value in the same value space), and (b) identical type annotations. For example the literal xs:int(3) is not equal (as an expression) to xs:short(3), because the two expressions are not interchangeable.
-
hashCode
public int hashCode()Return a hash code to support the equals() function -
toString
The toString() method for an expression attempts to give a representation of the expression in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath. In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax- Overrides:
toString
in classExpression
- Returns:
- a representation of the expression as a string
-
explain
Diagnostic print of expression structure. The abstract expression tree is written to the supplied output destination.- Specified by:
explain
in classExpression
- Parameters:
out
- the expression presenter used to display the structure
-
isAtomic
Test whether the literal wraps an atomic value. (Note, if this method returns false, this still leaves the possibility that the literal wraps a sequence that happens to contain a single atomic value).- Parameters:
exp
- an expression- Returns:
- if the expression is a literal and the literal wraps an AtomicValue
-
isEmptySequence
Test whether the literal explicitly wraps an empty sequence. (Note, if this method returns false, this still leaves the possibility that the literal wraps a sequence that happens to be empty).- Parameters:
exp
- an expression- Returns:
- if the expression is a literal and the literal wraps an AtomicValue
-
isConstantBoolean
Test if a literal represents the boolean value true- Parameters:
exp
- an expressionvalue
- true or false- Returns:
- if the expression is a literal and the literal represents the boolean value given in the second argument
-
isConstantOne
Test if a literal represents the integer value 1- Parameters:
exp
- an expression- Returns:
- if the expression is a literal and the literal represents the integer value 1
-
isSubtreeExpression
public boolean isSubtreeExpression()Determine whether the expression can be evaluated without reference to the part of the context document outside the subtree rooted at the context node.- Overrides:
isSubtreeExpression
in classExpression
- Returns:
- true if the expression has no dependencies on the context node, or if the only dependencies on the context node are downward selections using the self, child, descendant, attribute, and namespace axes.
-