Class GeneralPositionalPattern

  • All Implemented Interfaces:
    ExportAgent, Locatable, IdentityComparable, Traceable

    public class GeneralPositionalPattern
    extends Pattern
    A GeneralPositionalPattern is a pattern of the form A[P] where A is an axis expression using the child axis and P is an expression that depends on the position.

    This class handles cases where the predicate P is arbitrarily complex. Simple comparisons of position() against an integer value are handled by the class SimplePositionalPattern.

    • Constructor Detail

      • GeneralPositionalPattern

        public GeneralPositionalPattern​(NodeTest base,
                                        Expression positionExpr)
        Create a GeneralPositionalPattern
        Parameters:
        base - the base expression (to be matched independently of position)
        positionExpr - the positional filter which matches only if the position of the node is correct
    • Method Detail

      • operands

        public java.lang.Iterable<Operand> operands()
        Get the immediate sub-expressions of this expression, with information about the relationship of each expression to its parent expression. Default implementation works off the results of iterateSubExpressions()

        If the expression is a Callable, then it is required that the order of the operands returned by this function is the same as the order of arguments supplied to the corresponding call() method.

        Overrides:
        operands in class Expression
        Returns:
        an iterator containing the sub-expressions of this expression
      • getPositionExpr

        public Expression getPositionExpr()
        Get the filter assocated with the pattern
        Returns:
        the filter predicate
      • getNodeTest

        public NodeTest getNodeTest()
        Get the base pattern
        Returns:
        the base pattern before filtering
      • setUsesPosition

        public void setUsesPosition​(boolean usesPosition)
      • simplify

        public Pattern simplify()
                         throws XPathException
        Simplify the pattern: perform any context-independent optimisations
        Overrides:
        simplify in class Pattern
        Returns:
        the simplified expression (or the original if unchanged, or if modified in-situ)
        Throws:
        XPathException - if an error is discovered during expression rewriting
      • typeCheck

        public Pattern typeCheck​(ExpressionVisitor visitor,
                                 ContextItemStaticInfo contextItemType)
                          throws XPathException
        Type-check the pattern, performing any type-dependent optimizations.
        Overrides:
        typeCheck in class Pattern
        Parameters:
        visitor - an expression visitor
        contextItemType - the type of the context item at the point where the pattern appears
        Returns:
        the optimised Pattern
        Throws:
        XPathException - if an error is discovered during this phase (typically a type error)
      • optimize

        public Pattern optimize​(ExpressionVisitor visitor,
                                ContextItemStaticInfo contextInfo)
                         throws XPathException
        Perform optimisation of an expression and its subexpressions. This is the third and final phase of static optimization.

        This method is called after all references to functions and variables have been resolved to the declaration of the function or variable, and after all type checking has been done.

        Overrides:
        optimize in class Pattern
        Parameters:
        visitor - an expression visitor
        contextInfo - 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 to Type.ITEM_TYPE
        Returns:
        the original expression, rewritten if appropriate to optimize execution
        Throws:
        XPathException - if an error is discovered during this phase (typically a type error)
      • getDependencies

        public int getDependencies()
        Get the dependencies of the pattern. The only interesting dependencies for a pattern are dependencies on local variables or on user-defined functions. These are analyzed in those patterns containing predicates.
        Overrides:
        getDependencies in class Pattern
        Returns:
        the dependencies, as a bit-significant mask
      • allocateSlots

        public int allocateSlots​(SlotManager slotManager,
                                 int nextFree)
        Allocate slots to any variables used within the pattern
        Overrides:
        allocateSlots in class Pattern
        Parameters:
        slotManager - manages allocation of slots in a stack frame
        nextFree - the next slot that is free to be allocated @return the next slot that is free to be allocated
        Returns:
        the next slot that is free to be allocated
      • matches

        public boolean matches​(Item item,
                               XPathContext context)
                        throws XPathException
        Determine whether the pattern matches a given item.
        Specified by:
        matches in class Pattern
        Parameters:
        item - the item to be tested
        context - The dynamic context.
        Returns:
        true if the pattern matches, else false
        Throws:
        XPathException - if an error occurs while matching the pattern
      • matchesBeneathAnchor

        public boolean matchesBeneathAnchor​(NodeInfo node,
                                            NodeInfo anchor,
                                            XPathContext context)
                                     throws XPathException
        Determine whether this pattern matches a given Node within the subtree rooted at a given anchor node. This method is used when the pattern is used for streaming.
        Overrides:
        matchesBeneathAnchor in class Pattern
        Parameters:
        node - The NodeInfo representing the Element or other node to be tested against the Pattern
        anchor - The anchor node, which must match any AnchorPattern subpattern
        context - The dynamic context. Only relevant if the pattern uses variables, or contains calls on functions such as document() or key().
        Returns:
        true if the node matches the Pattern, false otherwise
        Throws:
        XPathException - if an error occurs while matching the pattern (the caller will usually treat this the same as a false result)
      • getUType

        public UType getUType()
        Get a UType indicating which kinds of items this Pattern can match.
        Specified by:
        getUType in class Pattern
        Returns:
        a UType indicating all the primitive types of item that the pattern can match.
      • getFingerprint

        public int getFingerprint()
        Determine the fingerprint of nodes to which this pattern applies. Used for optimisation.
        Overrides:
        getFingerprint in class Pattern
        Returns:
        the fingerprint of nodes matched by this pattern.
      • getItemType

        public ItemType getItemType()
        Get an ItemType that all the nodes matching this pattern must satisfy
        Specified by:
        getItemType in class Pattern
        Returns:
        an ItemType, as specific as possible, which all the matching items satisfy
      • equals

        public boolean equals​(java.lang.Object other)
        Determine whether this pattern is the same as another pattern
        Overrides:
        equals in class Expression
        Parameters:
        other - the other object
        Returns:
        true if the other operand is an expression and if it can be determined that the two expressions are equivalent, in the sense that they will always return the same result.
      • computeHashCode

        protected int computeHashCode()
        hashcode supporting equals()
        Overrides:
        computeHashCode in class Expression
        Returns:
        a computed hash code
      • isMotionless

        public boolean isMotionless()
        Test whether a pattern is motionless, that is, whether it can be evaluated against a node without repositioning the input stream. This is a necessary condition for patterns used as the match pattern of a streamed template rule.
        Overrides:
        isMotionless in class Pattern
        Returns:
        true if the pattern is motionless, that is, if it can be evaluated against a streamed node without changing the position in the streamed input file
      • copy

        public Pattern copy​(RebindingMap rebindings)
        Copy a pattern. This makes a deep copy.
        Specified by:
        copy in class Pattern
        Parameters:
        rebindings - variables that need to be re-bound
        Returns:
        the copy of the original pattern
      • reconstruct

        public java.lang.String reconstruct()
        Get a string representation of the pattern. This will be in a form similar to the original pattern text, but not necessarily identical. It is not guaranteed to be in legal pattern syntax.
        Overrides:
        reconstruct in class Pattern
      • export

        public void export​(ExpressionPresenter presenter)
                    throws XPathException
        Description copied from class: Expression
        Diagnostic print of expression structure. The abstract expression tree is written to the supplied output destination.
        Specified by:
        export in interface ExportAgent
        Specified by:
        export in class Pattern
        Parameters:
        presenter - 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.