Package net.sf.saxon.pattern
Class NodeTest
- java.lang.Object
-
- net.sf.saxon.pattern.NodeTest
-
- All Implemented Interfaces:
NodePredicate
,ItemType
,ItemTypeWithSequenceTypeCache
- Direct Known Subclasses:
AnyNodeTest
,CombinedNodeTest
,ContentTypeTest
,DocumentNodeTest
,ErrorType
,LocalNameTest
,MultipleNodeKindTest
,NamespaceTest
,NameTest
,NameTestUnion
,NodeKindTest
,NodeSelector
,SameNameTest
,SchemaAttributeTest
,SchemaElementTest
public abstract class NodeTest extends java.lang.Object implements NodePredicate, ItemTypeWithSequenceTypeCache
A NodeTest is a simple kind of pattern that enables a context-free test of whether a node matches a given node kind and name. There are several kinds of node test: a full name test, a prefix test, and an "any node of a given type" test, an "any node of any type" test, a "no nodes" test (used, e.g. for "@comment()").As well as being used to support XSLT pattern matching, NodeTests act as predicates in axis steps, and also act as item types for type matching.
For use in user-written application calling
NodeInfo.iterateAxis(int)
, it is possible to write a user-defined subclass ofNodeTest
that implements a single method,matches(int, NodeName, SchemaType)
-
-
Constructor Summary
Constructors Constructor Description NodeTest()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description NodeTest
copy()
Copy a NodeTest.java.util.Optional<java.lang.String>
explainMismatch(Item item, TypeHierarchy th)
Get extra diagnostic information about why a supplied item does not conform to this item type, if available.AtomicType
getAtomizedItemType()
Get the item type of the atomic values that will be produced when an item of this type is atomized (assuming that atomization succeeds)java.lang.String
getBasicAlphaCode()
Get an alphabetic code representing the type, or at any rate, the nearest built-in type from which this type is derived.SchemaType
getContentType()
Get the content type allowed by this NodeTest (that is, the type annotation of the matched nodes).abstract double
getDefaultPriority()
Determine the default priority to use if this node-test appears as a match pattern for a template with no explicit priority attribute.int
getFingerprint()
Get the name of the nodes matched by this nodetest, if it matches a specific name.Genre
getGenre()
Determine the Genre (top-level classification) of this typeIntPredicateProxy
getMatcher(NodeVectorTree tree)
Get a matching function that can be used to test whether numbered nodes in a TinyTree or DominoTree satisfy the node test.StructuredQName
getMatchingNodeName()
Get the name of the nodes matched by this nodetest, if it matches a specific name.ItemType
getPrimitiveItemType()
Get the primitive item type corresponding to this item type.int
getPrimitiveType()
Get the basic kind of object that this ItemType matches: for a NodeTest, this is the kind of node, or Type.Node if it matches different kinds of nodes.java.util.Optional<IntSet>
getRequiredNodeNames()
Get the set of node names allowed by this NodeTest.boolean
isAtomicType()
Determine whether this item type is an atomic typeboolean
isAtomizable(TypeHierarchy th)
Ask whether values of this type are atomizableboolean
isNillable()
Determine whether the content type (if present) is nillableboolean
isPlainType()
Determine whether this item type is atomic (that is, whether it can ONLY match atomic values)abstract boolean
matches(int nodeKind, NodeName name, SchemaType annotation)
Test whether this node test is satisfied by a given node.boolean
matches(Item item, TypeHierarchy th)
Test whether a given item conforms to this typeSequenceType
one()
Get a sequence type representing exactly one instance of this atomic typeSequenceType
oneOrMore()
Get a sequence type representing one or more instances of this atomic typeboolean
test(NodeInfo node)
Test whether this node test is satisfied by a given node.java.lang.String
toShortString()
SequenceType
zeroOrMore()
Get a sequence type representing one or more instances of this atomic typeSequenceType
zeroOrOne()
Get a sequence type representing zero or one instances of this atomic type-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sf.saxon.type.ItemType
getFullAlphaCode, getNormalizedDefaultPriority, getUType, toExportString
-
-
-
-
Method Detail
-
getGenre
public Genre getGenre()
Determine the Genre (top-level classification) of this type- Specified by:
getGenre
in interfaceItemType
- Returns:
- the Genre to which this type belongs, specifically
Genre.NODE
-
getDefaultPriority
public abstract double getDefaultPriority()
Determine the default priority to use if this node-test appears as a match pattern for a template with no explicit priority attribute.- Specified by:
getDefaultPriority
in interfaceItemType
- Returns:
- the default priority for the pattern
-
matches
public boolean matches(Item item, TypeHierarchy th)
Description copied from interface:ItemType
Test whether a given item conforms to this type
-
getPrimitiveItemType
public ItemType getPrimitiveItemType()
Get the primitive item type corresponding to this item type. For item(), this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds, it is the value representing the node kind, for example Type.ELEMENT. For anyAtomicValue it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER. For other atomic types it is the primitive type as defined in XML Schema, except that INTEGER is considered to be a primitive type.- Specified by:
getPrimitiveItemType
in interfaceItemType
- Returns:
- the corresponding primitive type
-
getPrimitiveType
public int getPrimitiveType()
Get the basic kind of object that this ItemType matches: for a NodeTest, this is the kind of node, or Type.Node if it matches different kinds of nodes.- Specified by:
getPrimitiveType
in interfaceItemType
- Returns:
- the node kind matched by this node test
-
getFingerprint
public int getFingerprint()
Get the name of the nodes matched by this nodetest, if it matches a specific name. Return -1 if the node test matches nodes of more than one name
-
getMatchingNodeName
public StructuredQName getMatchingNodeName()
Get the name of the nodes matched by this nodetest, if it matches a specific name. Return null if the node test matches nodes of more than one name
-
getBasicAlphaCode
public java.lang.String getBasicAlphaCode()
Get an alphabetic code representing the type, or at any rate, the nearest built-in type from which this type is derived. The codes are designed so that for any two built-in types A and B, alphaCode(A) is a prefix of alphaCode(B) if and only if A is a supertype of B.- Specified by:
getBasicAlphaCode
in interfaceItemType
- Returns:
- the alphacode for the nearest containing built-in type
-
isAtomicType
public boolean isAtomicType()
Determine whether this item type is an atomic type- Specified by:
isAtomicType
in interfaceItemType
- Returns:
- true if this is ANY_ATOMIC_TYPE or a subtype thereof
-
isPlainType
public boolean isPlainType()
Determine whether this item type is atomic (that is, whether it can ONLY match atomic values)- Specified by:
isPlainType
in interfaceItemType
- Returns:
- false: this is not ANY_ATOMIC_TYPE or a subtype thereof
-
getAtomizedItemType
public AtomicType getAtomizedItemType()
Get the item type of the atomic values that will be produced when an item of this type is atomized (assuming that atomization succeeds)- Specified by:
getAtomizedItemType
in interfaceItemType
- Returns:
- the best available item type of the atomic values that will be produced when an item of this type is atomized, or null if it is known that atomization will throw an error.
-
isAtomizable
public boolean isAtomizable(TypeHierarchy th)
Ask whether values of this type are atomizable- Specified by:
isAtomizable
in interfaceItemType
- Parameters:
th
- The type hierarchy cache- Returns:
- true unless it is known that these items will be elements with element-only content, in which case return false
-
getMatcher
public IntPredicateProxy getMatcher(NodeVectorTree tree)
Get a matching function that can be used to test whether numbered nodes in a TinyTree or DominoTree satisfy the node test. (Calling this matcher must give the same result as callingmatchesNode(tree.getNode(nodeNr))
, but it may well be faster).- Parameters:
tree
- the tree against which the returned function will operate- Returns:
- an IntPredicate; the matches() method of this predicate takes a node number as input, and returns true if and only if the node identified by this node number matches the node test.
-
matches
public abstract boolean matches(int nodeKind, NodeName name, SchemaType annotation)
Test whether this node test is satisfied by a given node. This method is only fully supported for a subset of NodeTests, because it doesn't provide all the information needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node test of the form element(N,T) or schema-element(E) where it is necessary to know whether the node is nilled, and (b) it can't be used to evaluate a node test of the form document-node(element(X)). This in practice means that it is used (a) to evaluate the simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate node tests where the node kind is known to be an attribute.- Parameters:
nodeKind
- The kind of node to be matchedname
- identifies the expanded name of the node to be matched. The value should be null for a node with no name.annotation
- The actual content type of the node. Null means no constraint.- Returns:
- true if the node matches this node test
-
test
public boolean test(NodeInfo node)
Test whether this node test is satisfied by a given node. This alternative method is used in the case of nodes where calculating the fingerprint is expensive, for example DOM or JDOM nodes. The default implementation calls the methodmatches(int, NodeName, SchemaType)
- Specified by:
test
in interfaceNodePredicate
- Parameters:
node
- the node to be matched- Returns:
- true if the node test is satisfied by the supplied node, false otherwise
-
getContentType
public SchemaType getContentType()
Get the content type allowed by this NodeTest (that is, the type annotation of the matched nodes). Return AnyType if there are no restrictions. The default implementation returns AnyType.- Returns:
- the type annotation that all nodes matching this NodeTest must satisfy
-
getRequiredNodeNames
public java.util.Optional<IntSet> getRequiredNodeNames()
Get the set of node names allowed by this NodeTest. This is returned as a set of Integer fingerprints. If all names are permitted (i.e. there are no constraints on the node name), returns IntUniversalSet.getInstance(). The default implementation returns the universal set.- Returns:
- the set of integer fingerprints of the node names that this node test can match; or absent if the set of names cannot be represented (for example, with the name tests *:xxx or xxx:*)
-
isNillable
public boolean isNillable()
Determine whether the content type (if present) is nillable- Returns:
- true if the content test (when present) can match nodes that are nilled
-
copy
public NodeTest copy()
Copy a NodeTest. Since they are never written to except in their constructors, returns the same.- Returns:
- the original nodeTest
-
one
public SequenceType one()
Get a sequence type representing exactly one instance of this atomic type- Specified by:
one
in interfaceItemTypeWithSequenceTypeCache
- Returns:
- a sequence type representing exactly one instance of this atomic type
- Since:
- 9.8.0.2
-
zeroOrOne
public SequenceType zeroOrOne()
Get a sequence type representing zero or one instances of this atomic type- Specified by:
zeroOrOne
in interfaceItemTypeWithSequenceTypeCache
- Returns:
- a sequence type representing zero or one instances of this atomic type
- Since:
- 9.8.0.2
-
oneOrMore
public SequenceType oneOrMore()
Get a sequence type representing one or more instances of this atomic type- Specified by:
oneOrMore
in interfaceItemTypeWithSequenceTypeCache
- Returns:
- a sequence type representing one or more instances of this atomic type
- Since:
- 9.8.0.2
-
zeroOrMore
public SequenceType zeroOrMore()
Get a sequence type representing one or more instances of this atomic type- Specified by:
zeroOrMore
in interfaceItemTypeWithSequenceTypeCache
- Returns:
- a sequence type representing one or more instances of this atomic type
- Since:
- 9.8.0.2
-
explainMismatch
public java.util.Optional<java.lang.String> explainMismatch(Item item, TypeHierarchy th)
Get extra diagnostic information about why a supplied item does not conform to this item type, if available. If extra information is returned, it should be in the form of a complete sentence, minus the closing full stop. No information should be returned for obvious cases.- Specified by:
explainMismatch
in interfaceItemType
- Parameters:
item
- the item that doesn't match this typeth
- the type hierarchy cache- Returns:
- optionally, a message explaining why the item does not match the type
-
toShortString
public java.lang.String toShortString()
-
-