Debian Codegraph @

net/sf/saxon/pattern/AnchorPattern.java

raw symbols SCIP
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.NodeInfo;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.ItemType;
19 import net.sf.saxon.type.UType;
20
21 /**
22 * This is a special pattern that matches the "anchor node". It is used for the selectors
23 * that arise when evaluating XPath expressions in streaming mode; the anchor
24 * node is the context node for the streamed XPath evaluation.
25 *
26 * Given a streamed evaluation of an expression such as ./BOOKS/BOOK/PRICE, the way we evaluate
27 * this is to turn it into a pattern, which is then tested against all descendant nodes.
28 * Conceptually the pattern is $A/BOOKS/BOOK/PRICE, where $A is referred to as the anchor
29 * node. When we evaluate the pattern against (say) a PRICE element, the match will only succeed
30 * if the name of the element is "PRICE" and its ancestors are, in order, a BOOK element, a
31 * BOOKS element, and the anchor node $A.
32 */
33 public class AnchorPattern extends Pattern {
34
35 private static AnchorPattern THE_INSTANCE = new AnchorPattern();
36
37 public static AnchorPattern getInstance() {
38 return THE_INSTANCE;
39 }
40
41 protected AnchorPattern() {
42 }
43
44 /**
45 * Get a UType indicating which kinds of items this Pattern can match.
46 *
47 * @return a UType indicating all the primitive types of item that the pattern can match.
48 */
49 @Override
50 public UType getUType() {
51 return UType.PARENT_NODE_KINDS;
52 }
53
54 /**
55 * Type-check the pattern.
56 *
57 * @param visitor the expression visitor
58 * @param contextItemType the type of the context item at the point where the pattern
59 * is defined. Set to null if it is known that the context item is undefined.
60 * @return the optimised Pattern
61 */
62
63 public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
64 return this;
65 }
66
67 /**
68 * Determine whether this pattern matches a given Node within the subtree rooted at a given
69 * anchor node. This method is used when the pattern is used for streaming.
70 *
71 * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
72 * @param anchor The anchor node, which must match any AnchorPattern subpattern
73 * @param context The dynamic context. Only relevant if the pattern
74 * uses variables, or contains calls on functions such as document() or key().
75 * @return true if the node matches the Pattern, false otherwise
76 */
77
78 public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException {
79 return anchor == null || node == anchor;
80 }
81
82 /**
83 * Determine whether this Pattern matches the given Node. This is the main external interface
84 * for matching patterns: it sets current() to the node being tested
85 *
86 * @param item The NodeInfo representing the Element or other node to be tested against the Pattern
87 * @param context The dynamic context. Only relevant if the pattern
88 * uses variables, or contains calls on functions such as document() or key().
89 * @return true if the node matches the Pattern, false otherwise
90 */
91
92 public boolean matches(Item<?> item, XPathContext context) throws XPathException {
93 throw new AssertionError();
94 //return true;
95 }
96
97 /**
98 * Get a NodeTest that all the nodes matching this pattern must satisfy
99 *
100 * @return a NodeTest, as specific as possible, which all the matching nodes satisfy
101 */
102
103 public ItemType getItemType() {
104 return AnyNodeTest.getInstance();
105 }
106
107 /*@NotNull*/
108 public String toString() {
109 return ".";
110 }
111
112 @Override
113 public void export(ExpressionPresenter presenter) throws XPathException {
114 presenter.startElement("p.anchor");
115 presenter.endElement();
116 }
117
118 /**
119 * Copy an AnchorPattern.
120 * Since there is only one, return the same.
121 *
122 * @param rebindings variables that need to be re-bound
123 * @return the original nodeTest
124 */
125
126 /*@NotNull*/
127 public Pattern copy(RebindingMap rebindings) {
128 return this;
129 }
130 }
131