Class SelectorTest

java.lang.Object
net.sf.saxon.pattern.SelectorTest
All Implemented Interfaces:
NamedXNodePredicate, NodePredicate, NodeTest, NodeVectorMatchMaker

public class SelectorTest extends Object implements NodeTest, NodeVectorMatchMaker, NamedXNodePredicate
A SelectorTest represents an axis step written as ./NNN or ./axis::NNN where NNN is either an EQName or a wildcard (such as prefix:* or *:prefix). The semantics depend on whether we are selecting JNodes or XNodes, which we don't know in advance (except in the case where the axis is the attribute or namespace axis).

Once it is known whether the nodes to be matched will be JNodes or XNodes, this node test can be replaced by something more specific; but in many cases that isn't known till evaluation time.

The boolean flag asNCName is true if the test was written as a plain NCName. Where JNodes are being matched, this is interpreted as a string-valued key rather than as a QName.

The nodeKind represents the principal node kind for the axis, which is only relevant when selecting XNodes.

  • Constructor Details

    • SelectorTest

      public SelectorTest(QNameTest key, boolean asNCName, int nodeKind)
  • Method Details

    • getNodeKind

      public int getNodeKind()
      Get the kind of nodes that this node predicate matches
      Specified by:
      getNodeKind in interface NamedXNodePredicate
      Returns:
      the kind of nodes, for example Type.ELEMENT or Type.ATTRIBUTE
    • asXNodeTest

      public NodeTest asXNodeTest(Configuration config)
      Get an equivalent node test, knowing that it will only be used to match XNodes
      Specified by:
      asXNodeTest in interface NodeTest
      Returns:
      an equivalent node test for XNodes
    • getUType

      public UType getUType()
      Get the corresponding UType. A UType is a union of primitive item types.
      Specified by:
      getUType in interface NodeTest
      Returns:
      the smallest UType that subsumes this item type
    • getMatcher

      public IntPredicate 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).
      Specified by:
      getMatcher in interface NodeVectorMatchMaker
      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.
    • getRequiredFingerprint

      public int getRequiredFingerprint()
      Get a fingerprint that can be used to match required nodes.
      Specified by:
      getRequiredFingerprint in interface NamedXNodePredicate
      Returns:
      if all nodes selected by the node test are of the required nodeKind, and have the same fingerprint, then return that fingerprint; otherwise return -1. Note that it is acceptable for the node test to impose further constraints, for example on the type annotation.
    • isFingerprintSufficient

      public boolean isFingerprintSufficient()
      Ask whether having the required fingerprint and node kind is a sufficient condition for a node to satisfy the predicate, or whether other conditions (such as type annotation or nillability) must also be satisfied.
      Specified by:
      isFingerprintSufficient in interface NamedXNodePredicate
      Returns:
      true if matching the fingerprint is a sufficient condition.
    • getItemType

      public ItemType getItemType()
      Get an item type that all matching nodes must satisfy
      Specified by:
      getItemType in interface NodeTest
      Returns:
      an item type
    • test

      public boolean test(GNode gnode)
      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.
      Specified by:
      test in interface NodePredicate
      Specified by:
      test in interface NodeTest
      Parameters:
      gnode - the node to be matched
      Returns:
      true if the node test is satisfied by the supplied node, false otherwise
    • getDefaultPriority

      public final double getDefaultPriority()
      Determine the default priority of this node test when used on its own as a Pattern
      Specified by:
      getDefaultPriority in interface NodeTest
      Returns:
      the default priority for the pattern
    • getQNameTest

      public QNameTest getQNameTest()
      Extract a QNameTest (the strongest one possible) that must be satisfied by a node if it is to satisfy this NodeTest
      Specified by:
      getQNameTest in interface NodeTest
      Returns:
      the strongest possible QNameTest
    • 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
      Specified by:
      getMatchingNodeName in interface NamedXNodePredicate
      Returns:
      the name that a node must have if it is to satisfy the predicate
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Returns a hash code value for the object.
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)
      Determines whether two NameTests are equal
      Overrides:
      equals in class Object
    • 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 NodeTest
      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()
      Description copied from interface: NodeTest
      Get a concise string representation of this node test for use in diagnostics
      Specified by:
      toShortString in interface NodeTest
      Returns:
      a suitably abbreviated respresention of the node test
    • isNillable

      public boolean isNillable()
      Determine whether the content type (if present) is nillable
      Specified by:
      isNillable in interface NodeTest
      Returns:
      true if the content test (when present) can match nodes that are nilled
    • export

      public String export()
      Specified by:
      export in interface NodeTest