| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.pattern; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 | import net.sf.saxon.expr.parser.RebindingMap; |
| 14 | import net.sf.saxon.om.Item; |
| 15 | import net.sf.saxon.om.Sequence; |
| 16 | import net.sf.saxon.om.SequenceIterator; |
| 17 | import net.sf.saxon.trace.ExpressionPresenter; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.type.ItemType; |
| 20 | import net.sf.saxon.type.UType; |
| 21 | |
| 22 | /** |
| 23 | * This is a special pattern that matches the node supplied as the first argument of a call to |
| 24 | * a streamable stylesheet function; it corresponds to the |
| 25 | * pattern match="$arg" where $arg is the first argument of the function. |
| 26 | */ |
| 27 | public class StreamingFunctionArgumentPattern extends Pattern { |
| 28 | |
| 29 | private static StreamingFunctionArgumentPattern THE_INSTANCE = new StreamingFunctionArgumentPattern(); |
| 30 | |
| 31 | public static StreamingFunctionArgumentPattern getInstance() { |
| 32 | return THE_INSTANCE; |
| 33 | } |
| 34 | |
| 35 | protected StreamingFunctionArgumentPattern() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get a UType indicating which kinds of items this Pattern can match. |
| 40 | * |
| 41 | * @return a UType indicating all the primitive types of item that the pattern can match. |
| 42 | */ |
| 43 | @Override |
| 44 | public UType getUType() { |
| 45 | return UType.ANY_NODE; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Type-check the pattern. |
| 50 | * |
| 51 | * @param visitor the expression visitor |
| 52 | * @param contextItemType the type of the context item at the point where the pattern |
| 53 | * is defined. Set to null if it is known that the context item is undefined. |
| 54 | * @return the optimised Pattern |
| 55 | */ |
| 56 | |
| 57 | public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Determine whether this Pattern matches the given Node. This is the main external interface |
| 63 | * for matching patterns: it sets current() to the node being tested |
| 64 | * |
| 65 | * @param item The NodeInfo representing the Element or other node to be tested against the Pattern |
| 66 | * @param context The dynamic context. Only relevant if the pattern |
| 67 | * uses variables, or contains calls on functions such as document() or key(). |
| 68 | * @return true if the node matches the Pattern, false otherwise |
| 69 | */ |
| 70 | |
| 71 | public boolean matches(Item<?> item, XPathContext context) throws XPathException { |
| 72 | Sequence<?> arg = context.getStackFrame().getStackFrameValues()[0]; |
| 73 | SequenceIterator<?> iter = arg.iterate(); |
| 74 | Item j; |
| 75 | while ((j = iter.next()) != null) { |
| 76 | if (j == item) { |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get a NodeTest that all the nodes matching this pattern must satisfy |
| 85 | * |
| 86 | * @return a NodeTest, as specific as possible, which all the matching nodes satisfy |
| 87 | */ |
| 88 | |
| 89 | public ItemType getItemType() { |
| 90 | return AnyNodeTest.getInstance(); |
| 91 | } |
| 92 | |
| 93 | /*@NotNull*/ |
| 94 | public String toString() { |
| 95 | return "$$"; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void export(ExpressionPresenter presenter) throws XPathException { |
| 100 | presenter.startElement("p.streamingArg"); |
| 101 | presenter.endElement(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Copy an AnchorPattern. |
| 106 | * Since there is only one, return the same. |
| 107 | * |
| 108 | * @param rebindings variables that need to be rebound |
| 109 | * @return the original pattern unchanged |
| 110 | */ |
| 111 | |
| 112 | /*@NotNull*/ |
| 113 | public Pattern copy(RebindingMap rebindings) { |
| 114 | return this; |
| 115 | } |
| 116 | } |
| 117 |