net.sf.saxon.pattern

## Class Pattern

- java.lang.Object
- - net.sf.saxon.pattern.Pattern

- All Implemented Interfaces:
    Serializable, SourceLocator, Container, PatternFinder
  Direct Known Subclasses:
    AncestorQualifiedPattern, AnchorPattern, BooleanExpressionPattern, ConditionalPattern, GeneralNodePattern, GeneralPositionalPattern, ItemTypePattern, NodeSetPattern, PatternThatSetsCurrent, PatternWithPredicate, SimplePositionalPattern, VennPattern

      public abstract class Pattern
      extends Object
      implements PatternFinder, Serializable, Container

  A Pattern represents the result of parsing an XSLT pattern string.
  Patterns are created by calling the static method Pattern.make(string).
  The pattern is used to test a particular node by calling match().
  See Also:
    Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Pattern()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `allocateSlots(SlotManager slotManager, int nextFree)` Allocate slots to any variables used within the pattern
    `Pattern` | `analyze(ExpressionVisitor visitor, ExpressionVisitor.ContextItemType contextItemType)` Type-check the pattern.
    `void` | `bindCurrent(Binding binding)` Replace any calls on current() by a variable reference bound to the supplied binding
    `int` | `getColumnNumber()` Get the column number (always -1)
    `int` | `getContainerGranularity()` Get the granularity of the container.
    `double` | `getDefaultPriority()` Determine the default priority to use if this pattern appears as a match pattern for a template with no explicit priority attribute.
    `int` | `getDependencies()` Get the dependencies of the pattern.
    `Executable` | `getExecutable()` Get the executable containing this pattern
    `int` | `getFingerprint()` Determine the name fingerprint of nodes to which this pattern applies.
    `int` | `getHostLanguage()` Get the host language (XSLT, XQuery, XPath) used to implement the code in this container
    `abstract ItemType` | `getItemType()` Get an ItemType that all the items matching this pattern must satisfy
    `int` | `getLineNumber()` Get the line number on which the pattern was defined
    `LocationProvider` | `getLocationProvider()` Get the LocationProvider allowing location identifiers to be resolved.
    `int` | `getNodeKind()` Determine the types of nodes to which this pattern applies.
    `String` | `getPublicId()` Get the public ID (always null)
    `String` | `getSystemId()` Get the system id of the entity in which the pattern occurred
    `boolean` | `isMotionless(boolean allowExtensions)` Test whether a pattern is motionless, that is, whether it can be evaluated against a node without repositioning the input stream.
    `Iterator<Expression>` | `iterateSubExpressions()` Iterate over the subexpressions within this pattern
    `static Pattern` | `make(String pattern, StaticContext env, Executable exec)` Static factory method to make a Pattern by parsing a String.
    `abstract boolean` | `matches(Item item, XPathContext context)` Determine whether this Pattern matches the given item.
    `boolean` | `matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context)` Determine whether this pattern matches a given Node within the subtree rooted at a given anchor node.
    `void` | `promote(PromotionOffer offer, Expression parent)` Offer promotion for subexpressions within this pattern.
    `protected static void` | `replaceCurrent(Expression exp, Binding binding)`
    `boolean` | `replaceSubExpression(Expression original, Expression replacement)` Replace a subexpression by a replacement subexpression
    `SequenceIterator<? extends NodeInfo>` | `selectNodes(DocumentInfo doc, XPathContext context)` Select nodes in a document using this PatternFinder.
    `void` | `setExecutable(Executable executable)` Set the executable containing this pattern
    `void` | `setLineNumber(int lineNumber)` Set the line number where the pattern occurred
    `void` | `setOriginalText(String text)` Set the original text of the pattern for use in diagnostics
    `void` | `setSystemId(String systemId)` Set the system ID where the pattern occurred
    `Pattern` | `simplify(ExpressionVisitor visitor)` Simplify the pattern by applying any context-independent optimisations.
    `String` | `toString()` Get the original pattern text

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### Pattern

          public Pattern()

  - ### Method Detail

    - #### make

          public static Pattern make(String pattern,
                     StaticContext env,
                     Executable exec)
                              throws XPathException

      Static factory method to make a Pattern by parsing a String.
      Parameters:
        `pattern` - The pattern text as a String
        `env` - An object defining the compile-time context for the expression
        `exec` - The executable containing this pattern
      Returns:
        The pattern object
      Throws:
        `XPathException` - if the pattern is invalid

    - #### replaceCurrent

          protected static void replaceCurrent(Expression exp,
                            Binding binding)

    - #### bindCurrent

          public void bindCurrent(Binding binding)

      Replace any calls on current() by a variable reference bound to the supplied binding

    - #### getExecutable

          public Executable getExecutable()

      Get the executable containing this pattern
      Specified by:
        `getExecutable` in interface `Container`
      Returns:
        the executable

    - #### setExecutable

          public void setExecutable(Executable executable)

      Set the executable containing this pattern
      Parameters:
        `executable` - the executable

    - #### getLocationProvider

          public LocationProvider getLocationProvider()

      Get the LocationProvider allowing location identifiers to be resolved.
      Specified by:
        `getLocationProvider` in interface `Container`
      Returns:
        the location provider

    - #### getContainerGranularity

          public int getContainerGranularity()

      Get the granularity of the container.
      Specified by:
        `getContainerGranularity` in interface `Container`
      Returns:
        0 for a temporary container created during parsing; 1 for a container that operates at the level of an XPath expression; 2 for a container at the level of a global function or template

    - #### setOriginalText

          public void setOriginalText(String text)

      Set the original text of the pattern for use in diagnostics
      Parameters:
        `text` - the original text of the pattern

    - #### simplify

          public Pattern simplify(ExpressionVisitor visitor)
                           throws XPathException

      Simplify the pattern by applying any context-independent optimisations. Default implementation does nothing.
      Parameters:
        `visitor` - the expression visitor
      Returns:
        the optimised Pattern
      Throws:
        `XPathException`

    - #### analyze

          public Pattern analyze(ExpressionVisitor visitor,
                        ExpressionVisitor.ContextItemType contextItemType)
                          throws XPathException

      Type-check the pattern.
      Parameters:
        `visitor` - the expression visitor
        `contextItemType` - the type of the context item at the point where the pattern is defined. Set to null if it is known that the context item is undefined.
      Returns:
        the optimised Pattern
      Throws:
        `XPathException`

    - #### getDependencies

          public int getDependencies()

      Get the dependencies of the pattern. The only possible dependency for a pattern is on local variables. This is analyzed in those patterns where local variables may appear.
      Returns:
        the dependencies, as a bit-significant mask

    - #### iterateSubExpressions

          public Iterator<Expression> iterateSubExpressions()

      Iterate over the subexpressions within this pattern
      Returns:
        an iterator over the subexpressions. Default implementation returns an empty sequence

    - #### allocateSlots

          public int allocateSlots(SlotManager slotManager,
                          int nextFree)

      Allocate slots to any variables used within the pattern
      Parameters:
        `slotManager` - the slot manager representing the stack frame for local variables
        `nextFree` - the next slot that is free to be allocated
      Returns:
        the next slot that is free to be allocated

    - #### promote

          public void promote(PromotionOffer offer,
                     Expression parent)
                       throws XPathException

      Offer promotion for subexpressions within this pattern. The offer will be accepted if the subexpression is not dependent on the factors (e.g. the context item) identified in the PromotionOffer. By default the offer is not accepted - this is appropriate in the case of simple expressions such as constant values and variable references where promotion would give no performance advantage. This method is always called at compile time.

      Unlike the corresponding method on `Expression`, this method does not return anything: it can make internal changes to the pattern, but cannot return a different pattern. Only certain kinds of promotion are applicable within a pattern: specifically, promotions affecting local variable references within the pattern.

      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
        `parent` -
      Throws:
        `XPathException` - if any error is detected

    - #### setSystemId

          public void setSystemId(String systemId)

      Set the system ID where the pattern occurred
      Parameters:
        `systemId` - the URI of the module containing the pattern

    - #### setLineNumber

          public void setLineNumber(int lineNumber)

      Set the line number where the pattern occurred
      Parameters:
        `lineNumber` - the line number of the pattern in the source module

    - #### isMotionless

          public boolean isMotionless(boolean allowExtensions)

      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.
      Parameters:
        `allowExtensions` - if false, the result is determined strictly according to the W3C "guaranteed streamability rules. If true, Saxon extensions are permitted: that is, constructs may be recognized as motionless by Saxon even if they are not recognized as motionless by the W3C rules.
      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

    - #### matches

          public abstract boolean matches(Item item,
                        XPathContext context)
                                   throws XPathException

      Determine whether this Pattern matches the given item. This is the main external interface for matching patterns: it sets current() to the node being tested
      Parameters:
        `item` - The item to be tested against the Pattern
        `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)

    - #### 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.
      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)

    - #### selectNodes

          public SequenceIterator<? extends NodeInfo> selectNodes(DocumentInfo doc,
                                                         XPathContext context)
                                                           throws XPathException

      Select nodes in a document using this PatternFinder.
      Specified by:
        `selectNodes` in interface `PatternFinder`
      Parameters:
        `doc` - the document node at the root of a tree
        `context` - the dynamic evaluation context
      Returns:
        an iterator over the selected nodes in the document.
      Throws:
        `XPathException` - if a dynamic error is encountered

    - #### getNodeKind

          public int getNodeKind()

      Determine the types of nodes to which this pattern applies. Used for optimisation. For patterns that match nodes of several types, return Type.NODE. For patterns that do not match nodes, return -1.
      Returns:
        the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT

    - #### getFingerprint

          public int getFingerprint()

      Determine the name fingerprint of nodes to which this pattern applies. Used for optimisation.
      Returns:
        A fingerprint that the nodes must match, or -1 if it can match multiple fingerprints, or it if matches atomic values

    - #### getItemType

          public abstract ItemType getItemType()

      Get an ItemType that all the items matching this pattern must satisfy
      Returns:
        an ItemType, as specific as possible, which all the matching items satisfy

    - #### getDefaultPriority

          public double getDefaultPriority()

      Determine the default priority to use if this pattern appears as a match pattern for a template with no explicit priority attribute.
      Returns:
        the default priority for the pattern

    - #### getSystemId

          public String getSystemId()

      Get the system id of the entity in which the pattern occurred
      Specified by:
        `getSystemId` in interface `SourceLocator`

    - #### getLineNumber

          public int getLineNumber()

      Get the line number on which the pattern was defined
      Specified by:
        `getLineNumber` in interface `SourceLocator`

    - #### getColumnNumber

          public int getColumnNumber()

      Get the column number (always -1)
      Specified by:
        `getColumnNumber` in interface `SourceLocator`

    - #### getPublicId

          public String getPublicId()

      Get the public ID (always null)
      Specified by:
        `getPublicId` in interface `SourceLocator`

    - #### toString

          public String toString()

      Get the original pattern text
      Overrides:
        `toString` in class `Object`

    - #### getHostLanguage

          public int getHostLanguage()

      Get the host language (XSLT, XQuery, XPath) used to implement the code in this container
      Specified by:
        `getHostLanguage` in interface `Container`
      Returns:
        typically `Configuration.XSLT` or `Configuration.XQUERY`

    - #### replaceSubExpression

          public boolean replaceSubExpression(Expression original,
                                     Expression replacement)

      Replace a subexpression by a replacement subexpression
      Parameters:
        `original` - the expression to be replaced
        `replacement` - the new expression to be inserted in its place
      Returns:
        true if the replacement was carried out

Copyright (c) 2004-2013 Saxonica Limited. All rights reserved.