net.sf.saxon.value

Class Value

public abstract class Value extends Object implements Expression, Serializable, ValueRepresentation

A value is the result of an expression but it is also an expression in its own right. Note that every value can be regarded as a sequence - in many cases, a sequence of length one.
Field Summary
static Class[]EMPTY_CLASS_ARRAY
Method Summary
static ItemasItem(ValueRepresentation value)
Static method to make an Item from a Value
static SequenceIteratorasIterator(ValueRepresentation val, XPathContext context)
Static method to get an Iterator over any ValueRepresentation (which may be either a Value or a NodeInfo
static ValueasValue(ValueRepresentation val)
Static method to make a Value from a given Item (which may be either an AtomicValue or a NodeInfo
voidcheckPermittedContents(SchemaType parentType, StaticContext env, boolean whole)
Check statically that the results of the expression are capable of constructing the content of a given schema type.
static CharSequencecollapseWhitespace(CharSequence in)
Collapse whitespace as defined in XML Schema
static Objectconvert(Item item)
Internal method to convert an XPath value to a Java object.
static ValueconvertJavaObjectToXPath(Object object, SequenceType requiredType, Configuration config)
Convert a Java object to an XPath value.
ObjectconvertToJava(Class target, XPathContext context)
Convert to Java object (for passing to external functions)
voiddisplay(int level, NamePool pool, PrintStream out)
Diagnostic display of the expression
booleaneffectiveBooleanValue(XPathContext context)
Get the effective boolean value of the expression.
booleanequals(Object obj)
Compare two (sequence) values for equality.
StringevaluateAsString(XPathContext context)
Evaluate an expression as a String.
ItemevaluateItem(XPathContext context)
Evaluate as a singleton item (or empty sequence).
intgetCardinality()
Determine the cardinality
intgetDependencies()
Determine which aspects of the context the expression depends on.
ItemTypegetItemType(TypeHierarchy th)
Determine the data type of the items in the expression, if possible
static SequenceIteratorgetIterator(ValueRepresentation val)
Get a SequenceIterator over a ValueRepresentation
intgetLength()
Get the length of the sequence
ContainergetParentExpression()
Get the expression that immediately contains this expression.
intgetSpecialProperties()
Get the static properties of this expression (other than its type).
StringgetStringValue()
Convert the value to a string, using the serialization rules.
inthashCode()
Return a hash code to support the equals() function
ItemitemAt(int n)
Get the n'th item in the sequence (starting from 0).
IteratoriterateSubExpressions()
Get the sub-expressions of this expression.
static QNameValuemakeQNameValue(Object object, Configuration config)
Temporary method to make a QNameValue from a JAXP 1.3 QName, without creating a compile-time link to the JDK 1.5 QName class
static CharSequencenormalizeWhitespace(CharSequence in)
Normalize whitespace as defined in XML Schema
Expressionoptimize(Optimizer opt, StaticContext env, ItemType contextItemType)
Optimize an expression
voidprocess(XPathContext context)
Process the value as an instruction, without returning any tail calls
Expressionpromote(PromotionOffer offer)
Offer promotion for this subexpression.
Valuereduce()
Reduce a value to its simplest form.
booleanschemaEquals(Value obj)
Compare two (sequence) values for equality.
Expressionsimplify(StaticContext env)
Simplify an expression
static doublestringToNumber(CharSequence s)
Static method to convert strings to numbers.
StringtoString()
Convert to a string for diagnostic output
static CharSequencetrimWhitespace(CharSequence in)
Remove leading and trailing whitespace.
ExpressiontypeCheck(StaticContext env, ItemType contextItemType)
TypeCheck an expression

Field Detail

EMPTY_CLASS_ARRAY

public static final Class[] EMPTY_CLASS_ARRAY

Method Detail

asItem

public static Item asItem(ValueRepresentation value)
Static method to make an Item from a Value

Parameters: value the value to be converted

Returns: null if the value is an empty sequence; or the only item in the value if it is a singleton sequence

Throws: XPathException if the Value contains multiple items

asIterator

public static SequenceIterator asIterator(ValueRepresentation val, XPathContext context)
Static method to get an Iterator over any ValueRepresentation (which may be either a Value or a NodeInfo

Parameters: val The supplied value, or null, indicating the empty sequence. context The evaluation context. This may be null. It should always be possible to iterate over a value without supplying a context, but sometimes the context can provide access to better error information

Returns: The supplied value, if it is a value, or a SingletonNode that wraps the item, if it is a node. If the supplied value was null, return an EmptySequence

asValue

public static Value asValue(ValueRepresentation val)
Static method to make a Value from a given Item (which may be either an AtomicValue or a NodeInfo

Parameters: val The supplied value, or null, indicating the empty sequence.

Returns: The supplied value, if it is a value, or a SingletonNode that wraps the item, if it is a node. If the supplied value was null, return an EmptySequence

checkPermittedContents

public void checkPermittedContents(SchemaType parentType, StaticContext env, boolean whole)
Check statically that the results of the expression are capable of constructing the content of a given schema type.

Parameters: parentType The schema type env the static context whole

Throws: XPathException if the expression doesn't match the required content type

collapseWhitespace

public static CharSequence collapseWhitespace(CharSequence in)
Collapse whitespace as defined in XML Schema

convert

public static Object convert(Item item)
Internal method to convert an XPath value to a Java object. An atomic value is returned as an instance of the best available Java class. If the item is a node, the node is "unwrapped", to return the underlying node in the original model (which might be, for example, a DOM or JDOM node).

convertJavaObjectToXPath

public static Value convertJavaObjectToXPath(Object object, SequenceType requiredType, Configuration config)
Convert a Java object to an XPath value. This method is called to handle the result of an external function call (but only if the required type is not known), and also to process global parameters passed to the stylesheet or query.

Parameters: object The Java object to be converted requiredType The required type of the result (if known) config The Configuration: may be null, in which case certain kinds of object (eg. DOM nodes) cannot be handled

Returns: the result of converting the value. If the value is null, returns null.

convertToJava

public Object convertToJava(Class target, XPathContext context)
Convert to Java object (for passing to external functions)

display

public void display(int level, NamePool pool, PrintStream out)
Diagnostic display of the expression

effectiveBooleanValue

public boolean effectiveBooleanValue(XPathContext context)
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.

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

public boolean equals(Object obj)
Compare two (sequence) values for equality. This supports identity constraints in XML Schema, which allow list-valued elements and attributes to participate in key and uniqueness constraints. This method returns false if any error occurs during the comparison, or if any of the items in either sequence is a node rather than an atomic value.

evaluateAsString

public String evaluateAsString(XPathContext context)
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.

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 ClassCastException if the result type of the expression is not xs:string?

evaluateItem

public Item evaluateItem(XPathContext context)
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.

getCardinality

public int getCardinality()
Determine the cardinality

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

Returns: for a Value, this always returns zero.

getItemType

public ItemType getItemType(TypeHierarchy th)
Determine the data type of the items in the expression, if possible

Parameters: th The TypeHierarchy. Can be null if the target is an AtomicValue.

Returns: for the default implementation: AnyItemType (not known)

getIterator

public static SequenceIterator getIterator(ValueRepresentation val)
Get a SequenceIterator over a ValueRepresentation

getLength

public int getLength()
Get the length of the sequence

getParentExpression

public final Container getParentExpression()
Get the expression that immediately contains this expression. This method returns null for an outermost expression; it also return null in the case of literal values. For an XPath expression occurring within an XSLT stylesheet, this method returns the XSLT instruction containing the XPath expression.

Returns: the expression that contains this expression, if known; return null if there is no containing expression or if the containing expression is unknown.

getSpecialProperties

public int getSpecialProperties()
Get the static properties of this expression (other than its type). For a Value, the only special property is NON_CREATIVE.

Returns: NON_CREATIVE

getStringValue

public String getStringValue()
Convert the value to a string, using the serialization rules. For atomic values this is the same as a cast; for sequence values it gives a space-separated list.

Throws: XPathException The method can fail if evaluation of the value has been deferred, and if a failure occurs during the deferred evaluation. No failure is possible in the case of an AtomicValue.

hashCode

public int hashCode()
Return a hash code to support the equals() function

itemAt

public Item itemAt(int n)
Get the n'th item in the sequence (starting from 0). This is defined for all Values, but its real benefits come for a sequence Value stored extensionally (or for a MemoClosure, once all the values have been read)

iterateSubExpressions

public final Iterator iterateSubExpressions()
Get the sub-expressions of this expression.

Returns: for a Value, this always returns an empty array

makeQNameValue

public static QNameValue makeQNameValue(Object object, Configuration config)
Temporary method to make a QNameValue from a JAXP 1.3 QName, without creating a compile-time link to the JDK 1.5 QName class

Parameters: object an instance of javax.xml.namespace.QName

Returns: a corresponding Saxon QNameValue, or null if any error occurs performing the conversion

normalizeWhitespace

public static CharSequence normalizeWhitespace(CharSequence in)
Normalize whitespace as defined in XML Schema

optimize

public final Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType)
Optimize an expression

Returns: for a Value, this always returns the value unchanged

process

public void process(XPathContext context)
Process the value as an instruction, without returning any tail calls

Parameters: context The dynamic context, giving access to the current node, the current variables, etc.

promote

public final Expression promote(PromotionOffer offer)
Offer promotion for this subexpression. Values (constant expressions) are never promoted

Parameters: offer details of the offer, for example the offer to move expressions that don't depend on the context to an outer level in the containing expression

Returns: For a Value, this always returns the value unchanged

reduce

public Value reduce()
Reduce a value to its simplest form. If the value is a closure or some other form of deferred value such as a FunctionCallPackage, then it is reduced to a SequenceExtent. If it is a SequenceExtent containing a single item, then it is reduced to that item. One consequence that is exploited by class FilterExpression is that if the value is a singleton numeric value, then the result will be an instance of NumericValue

schemaEquals

public boolean schemaEquals(Value obj)
Compare two (sequence) values for equality. This supports identity constraints in XML Schema, which allow list-valued elements and attributes to participate in key and uniqueness constraints. This method returns false if any error occurs during the comparison, or if any of the items in either sequence is a node rather than an atomic value.

simplify

public final Expression simplify(StaticContext env)
Simplify an expression

Returns: for a Value, this always returns the value unchanged

stringToNumber

public static double stringToNumber(CharSequence s)
Static method to convert strings to numbers. Might as well go here as anywhere else.

Parameters: s the String to be converted

Returns: a double representing the value of the String

Throws: NumberFormatException if the value cannot be converted

toString

public String toString()
Convert to a string for diagnostic output

trimWhitespace

public static CharSequence trimWhitespace(CharSequence in)
Remove leading and trailing whitespace. This has the same effect as collapseWhitespace, but is cheaper, for use by data types that do not allow internal whitespace.

Parameters: in the input string whose whitespace is to be removed

Returns: the result of removing excess whitespace

typeCheck

public final Expression typeCheck(StaticContext env, ItemType contextItemType)
TypeCheck an expression

Returns: for a Value, this always returns the value unchanged