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 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 of NodeTest that implements a single method, matches(int, NodeName, SchemaType)

  • Constructor Details

    • NodeTest

      public NodeTest()
  • Method Details

    • getGenre

      public Genre getGenre()
      Determine the Genre (top-level classification) of this type
      Specified by:
      getGenre in interface ItemType
      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 interface ItemType
      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
      Specified by:
      matches in interface ItemType
      Parameters:
      item - The item to be tested
      th - The type hierarchy cache. Currently used only when matching function items.
      Returns:
      true if the item is an instance of this type; false otherwise
    • 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 interface ItemType
      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 interface ItemType
      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 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 interface ItemType
      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 interface ItemType
      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 interface ItemType
      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 interface ItemType
      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 interface ItemType
      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 calling matchesNode(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 matched
      name - 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 method matches(int, NodeName, SchemaType)
      Specified by:
      test in interface NodePredicate
      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 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 interface ItemTypeWithSequenceTypeCache
      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 interface ItemTypeWithSequenceTypeCache
      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 interface ItemTypeWithSequenceTypeCache
      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 interface ItemTypeWithSequenceTypeCache
      Returns:
      a sequence type representing one or more instances of this atomic type
      Since:
      9.8.0.2
    • explainMismatch

      public Optional<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 interface ItemType
      Parameters:
      item - the item that doesn't match this type
      th - the type hierarchy cache
      Returns:
      optionally, a message explaining why the item does not match the type
    • toShortString

      public String toShortString()