Package net.sf.saxon.expr
Class Literal
java.lang.Object
net.sf.saxon.expr.Expression
net.sf.saxon.expr.Literal
- All Implemented Interfaces:
ExportAgent
,Locatable
,IdentityComparable
,Traceable
- Direct Known Subclasses:
FunctionLiteral
,StringLiteral
A Literal is an expression whose value is constant: it is a class that implements the
Expression
interface as a wrapper around a GroundedValue
. 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')-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Elaborator for a literal. -
Field Summary
Fields inherited from class net.sf.saxon.expr.Expression
EFFECTIVE_BOOLEAN_VALUE, EVALUATE_METHOD, ITEM_FEED_METHOD, ITERATE_METHOD, MAX_COST, MAX_SEQUENCE_LENGTH, MAX_STRING_LENGTH, PROCESS_METHOD, staticProperties, UNBOUNDED_LOWER, UNBOUNDED_UPPER, UPDATE_METHOD, WATCH_METHOD
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Literal
(GroundedValue value) Create a literal as a wrapper around a Value -
Method Summary
Modifier and TypeMethodDescriptionaddToPathMap
(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) Add a representation of this expression to a PathMap.protected int
Determine the cardinalityprotected int
Return a hash code to support the equals() functionprotected int
Compute the static properties of this expression (other than its type).copy
(RebindingMap rebindings) 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
Diagnostic print of expression structure.static void
exportAtomicValue
(AtomicValue value, ExpressionPresenter out) static void
exportValue
(Sequence value, ExpressionPresenter out) final int
Determine which aspects of the context the expression depends on.Make an elaborator for this expressionGet a name identifying the kind of expression, in terms meaningful to a user.Get the value represented by this Literalint
An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().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 possibleint
Return the estimated cost of evaluating an expression.getStaticUType
(UType contextItemType) Get the static type of the expression as a UType, following precisely the type inference rules defined in the XSLT 3.0 specification.Get the (partial) name of a class that supports streaming of this kind of expressionstatic boolean
hasEffectiveBooleanValue
(Expression exp, boolean value) Test if a literal has the effective boolean value true (or false)static 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
isConstantZero
(Expression exp) Test if a literal represents the integer value 1static boolean
Test whether the literal explicitly wraps an empty sequence.boolean
isInstance
(SequenceType req, TypeHierarchy th) 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 specificationiterate()
Return an Iterator to iterate over the values of a sequence.iterate
(XPathContext context) Return an Iterator to iterate over the values of a sequence.static Literal
Factory method to make an empty-sequence literalstatic Literal
makeLiteral
(GroundedValue value) Factory method to create a literal as a wrapper around a Value (factory method)static Literal
makeLiteral
(GroundedValue value, Expression origin) Make a literal, taking the retained static context and location information from another expression which is being simplified/optimizedoptimize
(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) Optimize an expressionvoid
process
(Outputter output, XPathContext context) Process the value as an instruction, without returning any tail callsboolean
Ask whether the expression supports lazy evaluation.toPattern
(Configuration config) Convert this expression to an equivalent XSLT patternProduce a short string identifying the expression for use in error messagestoString()
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, ContextItemStaticInfo contextInfo) TypeCheck an expressionMethods inherited from class net.sf.saxon.expr.Expression
adoptChildExpression, allowExtractingCommonSubexpressions, checkedOperands, checkForUpdatingSubexpressions, checkPermittedContents, computeDependencies, computeStaticProperties, dispatchTailCall, dynamicError, explain, getCardinality, getConfiguration, getCost, getEvaluationMethod, getExtraProperty, getIntrinsicDependencies, getLocalRetainedStaticContext, getLocation, getObjectName, getPackageData, getParentExpression, getProperties, getProperty, getRetainedStaticContext, getScopingExpression, getSlotsUsed, getSpecialProperties, getStaticBaseURI, getStaticBaseURIString, getStaticType, getTracingTag, hasCompatibleStaticContext, hashCode, hasSpecialProperty, hasVariableBinding, identityHashCode, implementsStaticTypeCheck, isCallOn, isEqual, isIdentical, isInstruction, isLiftable, isMultiThreaded, isStaticPropertiesKnown, isUpdatingExpression, makeElaborator, markTailFunctionCalls, operandList, operands, operandSparseList, optimizeChildren, prepareForStreaming, resetLocalStaticProperties, restoreParentPointers, setEvaluationMethod, setExtraProperty, setFiltered, setFlattened, setLocation, setParentExpression, setRetainedStaticContext, setRetainedStaticContextLocally, setRetainedStaticContextThoroughly, setStaticProperty, simplify, simplifyChildren, staticTypeCheck, suppressValidation, typeCheckChildren, typeError, unordered, verifyParentPointers, withLocation
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.saxon.trace.Traceable
gatherProperties
-
Constructor Details
-
Literal
Create a literal as a wrapper around a Value- Parameters:
value
- the value of this literal
-
-
Method Details
-
getGroundedValue
Get the value represented by this Literal- Returns:
- the constant value
-
typeCheck
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException TypeCheck an expression- Overrides:
typeCheck
in classExpression
- Parameters:
visitor
- an expression visitorcontextInfo
- Information available statically about the context item: whether it is (possibly) absent; its static type; its streaming posture.- Returns:
- for a Value, this always returns the value unchanged
- Throws:
XPathException
- if an error is discovered during this phase (typically a type error)
-
optimize
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException 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
- Throws:
XPathException
- if an error is discovered during this phase (typically a type error)
-
getNetCost
public int getNetCost()Return the estimated cost of evaluating an expression. This is a very crude measure based on the syntactic form of the expression (we have no knowledge of data values). We take the cost of evaluating a simple scalar comparison or arithmetic expression as 1 (one), and we assume that a sequence has length 5. The resulting estimates may be used, for example, to reorder the predicates in a filter expression so cheaper predicates are evaluated first.- Overrides:
getNetCost
in classExpression
- Returns:
- the intrinsic cost of this operation, excluding the costs of evaluating the operands
-
getItemType
Determine the data type of the items in the expression, if possible- Specified by:
getItemType
in classExpression
- Returns:
- for the default implementation: AnyItemType (not known)
-
isInstance
-
getStaticUType
Get the static type of the expression as a UType, following precisely the type inference rules defined in the XSLT 3.0 specification.- Overrides:
getStaticUType
in classExpression
- Parameters:
contextItemType
- the static context item type- Returns:
- the static item type of the expression according to the XSLT 3.0 defined rules
-
computeCardinality
protected 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
. May also returnStaticProperty.ALLOWS_ZERO
if the result is known to be an empty sequence, orStaticProperty.ALLOWS_MANY
if if is known to return a sequence of length two or more.
-
computeSpecialProperties
protected int computeSpecialProperties()Compute the static properties of this expression (other than its type). For a Value, the only special property isStaticProperty.NO_NODES_NEWLY_CREATED
.- Overrides:
computeSpecialProperties
in classExpression
- Returns:
- the value
StaticProperty.NO_NODES_NEWLY_CREATED
-
supportsLazyEvaluation
public boolean supportsLazyEvaluation()Ask whether the expression supports lazy evaluation.- Overrides:
supportsLazyEvaluation
in classExpression
- Returns:
- false either if the expression cannot be evaluated lazily because it has dependencies that cannot be saved in the context, or because lazy evaluation is pointless (for example, for literals and variable references).
-
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
- Parameters:
rebindings
- variables that need to be re-bound- Returns:
- a copy of the original literal. Note that the underlying value is not copied; the code relies on the caller treating the underlying value as immutable.
-
toPattern
Convert this expression to an equivalent XSLT pattern- Overrides:
toPattern
in classExpression
- Parameters:
config
- the Saxon configuration- Returns:
- the equivalent pattern
- Throws:
XPathException
- if conversion is not possible
-
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:
output
- the destination for the resultcontext
- The dynamic context, giving access to the current node,- Throws:
XPathException
- if a dynamic error occurs
-
getImplementationMethod
public int getImplementationMethod()Description copied from class:Expression
An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). This method indicates which of these methods is provided directly. The other methods will always be available indirectly, using an implementation that relies on one of the other methods.- Specified by:
getImplementationMethod
in classExpression
- Returns:
- the implementation method, for example
Expression.ITERATE_METHOD
orExpression.EVALUATE_METHOD
orExpression.PROCESS_METHOD
-
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
-
equals
Determine whether two literals are equal, when considered as expressions.- Overrides:
equals
in classExpression
- 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.
-
computeHashCode
protected int computeHashCode()Return a hash code to support the equals() function- Overrides:
computeHashCode
in classExpression
- Returns:
- a computed hash code
-
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
-
export
Diagnostic print of expression structure. The abstract expression tree is written to the supplied output destination.- Specified by:
export
in interfaceExportAgent
- Specified by:
export
in classExpression
- Parameters:
out
- the expression presenter used to display the structure- Throws:
XPathException
- if the export fails, for example if an expression is found that won't work in the target environment.
-
exportValue
- Throws:
XPathException
-
getExpressionName
Get a name identifying the kind of expression, in terms meaningful to a user.- Overrides:
getExpressionName
in classExpression
- Returns:
- a name identifying the kind of expression, in terms meaningful to a user. The name will always be in the form of a lexical XML QName, and should match the name used in export() output displaying the expression.
-
exportAtomicValue
public static void exportAtomicValue(AtomicValue value, ExpressionPresenter out) throws XPathException - Throws:
XPathException
-
toShortString
Produce a short string identifying the expression for use in error messages- Overrides:
toShortString
in classExpression
- Returns:
- a short string, sufficient to identify the expression
-
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:
- true 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:
- true if the expression is a literal and the value of the literal is an empty sequence
-
isConstantBoolean
Test if a literal represents the boolean value true- Parameters:
exp
- an expressionvalue
- true or false- Returns:
- true if the expression is a literal and the literal represents the boolean value given in the second argument
-
hasEffectiveBooleanValue
Test if a literal has the effective boolean value true (or false)- Parameters:
exp
- an expressionvalue
- true or false- Returns:
- true 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:
- true if the expression is a literal and the literal represents the integer value 1
-
isConstantZero
Test if a literal represents the integer value 1- Parameters:
exp
- an expression- Returns:
- true 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.
-
makeEmptySequence
Factory method to make an empty-sequence literal- Returns:
- a literal whose value is the empty sequence
-
makeLiteral
Factory method to create a literal as a wrapper around a Value (factory method)- Parameters:
value
- the value of this literal- Returns:
- the Literal
-
makeLiteral
Make a literal, taking the retained static context and location information from another expression which is being simplified/optimized- Parameters:
value
- the literal valueorigin
- the expression whose context and location information is to be retained- Returns:
- the Literal
-
getStreamerName
Get the (partial) name of a class that supports streaming of this kind of expression- Overrides:
getStreamerName
in classExpression
- Returns:
- the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, or null if there is no such class
-
getElaborator
Make an elaborator for this expression- Overrides:
getElaborator
in classExpression
- Returns:
- a suitable elaborator
-