Debian Codegraph @

net/sf/saxon/pattern/SimplePositionalPattern.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.ExpressionTool;
12 import net.sf.saxon.expr.parser.RebindingMap;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.trace.ExpressionPresenter;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.util.Navigator;
18 import net.sf.saxon.type.AnyItemType;
19 import net.sf.saxon.type.ItemType;
20 import net.sf.saxon.type.UType;
21
22 /**
23 * A SimplePositionalPattern is a pattern of the form A[N] where A is an axis expression using the child axis
24 * and P is a numeric literal.
25 */
26
27
28 public final class SimplePositionalPattern extends Pattern {
29
30 private NodeTest nodeTest;
31 private int position;
32
33 /**
34 * Create a SimplePositionalPattern
35 *
36 * @param nodeTest the test that the node must satisfy
37 * @param position the required position of the node
38 */
39
40 public SimplePositionalPattern(NodeTest nodeTest, int position) {
41 this.nodeTest = nodeTest;
42 this.position = position;
43 }
44
45 /**
46 * Get the required position
47 *
48 * @return the integer appearing as the filter predicate
49 */
50
51 public int getPosition() {
52 return position;
53 }
54
55 /**
56 * Get the node test
57 * @return the node test used
58 */
59 public NodeTest getNodeTest() {
60 return nodeTest;
61 }
62 /**
63 * Determine whether the pattern matches a given item.
64 *
65 * @param item the item to be tested
66 * @return true if the pattern matches, else false
67 */
68
69 public boolean matches(Item<?> item, XPathContext context) {
70 return item instanceof NodeInfo && matchesBeneathAnchor((NodeInfo) item, null, context);
71 }
72
73 /**
74 * Get a UType indicating which kinds of items this Pattern can match.
75 *
76 * @return a UType indicating all the primitive types of item that the pattern can match.
77 */
78 @Override
79 public UType getUType() {
80 return nodeTest.getUType();
81 }
82
83 /**
84 * Determine the fingerprint of nodes to which this pattern applies.
85 * Used for optimisation.
86 *
87 * @return the fingerprint of nodes matched by this pattern.
88 */
89
90 public int getFingerprint() {
91 return nodeTest.getFingerprint();
92 }
93
94 /**
95 * Get an ItemType that all the nodes matching this pattern must satisfy
96 */
97
98 public ItemType getItemType() {
99 return nodeTest.getPrimitiveItemType();
100 }
101
102 /**
103 * Determine whether this pattern is the same as another pattern
104 *
105 * @param other the other object
106 */
107
108 public boolean equals(Object other) {
109 if (other instanceof SimplePositionalPattern) {
110 SimplePositionalPattern fp = (SimplePositionalPattern) other;
111 return nodeTest.equals(fp.nodeTest) && position == fp.position;
112 } else {
113 return false;
114 }
115 }
116
117 /**
118 * hashcode supporting equals()
119 */
120
121 public int computeHashCode() {
122 return nodeTest.hashCode() ^ (position<<3);
123 }
124
125 /**
126 * Test whether a pattern is motionless, that is, whether it can be evaluated against a node
127 * without repositioning the input stream. This is a necessary condition for patterns used
128 * as the match pattern of a streamed template rule.
129 *
130 * @return true if the pattern is motionless, that is, if it can be evaluated against a streamed
131 * node without changing the position in the streamed input file
132 */
133
134 public boolean isMotionless() {
135 return false;
136 }
137
138 /**
139 * Determine whether this pattern matches a given Node within the subtree rooted at a given
140 * anchor node. This method is used when the pattern is used for streaming.
141 *
142 * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
143 * @param anchor The anchor node, which must match any AnchorPattern subpattern
144 * @param context The dynamic context. Only relevant if the pattern
145 * uses variables, or contains calls on functions such as document() or key().
146 * @return true if the node matches the Pattern, false otherwise
147 */
148
149 public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) {
150 if (!nodeTest.matchesNode(node)) {
151 return false;
152 }
153
154 if (anchor != null && node.getParent() != anchor) {
155 return false;
156 }
157
158 return position == Navigator.getSiblingPosition(node, nodeTest, position);
159
160 }
161
162 /**
163 * Copy a pattern. This makes a deep copy.
164 *
165 * @return the copy of the original pattern
166 * @param rebindings variables that need to be re-bound
167 */
168
169 /*@NotNull*/
170 public Pattern copy(RebindingMap rebindings) {
171 SimplePositionalPattern n = new SimplePositionalPattern(nodeTest.copy(), position);
172 ExpressionTool.copyLocationInfo(this, n);
173 return n;
174 }
175
176 /**
177 * Get the original pattern text
178 */
179 @Override
180 public String toString() {
181 return nodeTest + "[" + position + "]";
182 }
183
184 public void export(ExpressionPresenter presenter) throws XPathException {
185 presenter.startElement("p.simPos");
186 presenter.emitAttribute("test", nodeTest.toString());
187 if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) {
188 int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion;
189 presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion));
190 }
191 presenter.emitAttribute("pos", position + "");
192 presenter.endElement();
193 }
194
195 }
196