Debian Codegraph @

net/sf/saxon/pattern/AncestorQualifiedPattern.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.*;
11 import net.sf.saxon.expr.instruct.SlotManager;
12 import net.sf.saxon.expr.parser.*;
13 import net.sf.saxon.om.AxisInfo;
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.Type;
20 import net.sf.saxon.type.UType;
21
22 /**
23 * An AncestorQualifiedPattern represents a path of the form A/B or A//B, where nodes must match the
24 * pattern B and also have a parent/ancestor (respectively) that matches A.
25 */
26
27 public final class AncestorQualifiedPattern extends Pattern {
28
29 private Pattern basePattern;
30 private Pattern upperPattern;
31 private byte upwardsAxis = AxisInfo.PARENT;
32 private ItemType refinedItemType;
33 private boolean testUpperPatternFirst = false;
34
35 public AncestorQualifiedPattern(Pattern base, Pattern upper, byte axis) {
36 this.basePattern = base;
37 this.upperPattern = upper;
38 this.upwardsAxis = axis;
39 adoptChildExpression(base);
40 adoptChildExpression(upper);
41 }
42
43 /**
44 * Get the immediate sub-expressions of this expression, with information about the relationship
45 * of each expression to its parent expression. Default implementation
46 * works off the results of iterateSubExpressions()
47 * <p>If the expression is a Callable, then it is required that the order of the operands
48 * returned by this function is the same as the order of arguments supplied to the corresponding
49 * call() method.</p>
50 *
51 * @return an iterator containing the sub-expressions of this expression
52 */
53 @Override
54 public Iterable<Operand> operands() {
55 return operandList(
56 new Operand(this, upperPattern, OperandRole.SAME_FOCUS_ACTION),
57 new Operand(this, basePattern, OperandRole.SAME_FOCUS_ACTION));
58 }
59
60 /**
61 * Replace any calls on current() by a variable reference bound to the supplied binding
62 */
63 @Override
64 public void bindCurrent(LocalBinding binding) {
65 basePattern.bindCurrent(binding);
66 upperPattern.bindCurrent(binding);
67 }
68
69 /**
70 * Get the base pattern, the pattern applying to the node itself
71 *
72 * @return the base pattern
73 */
74 public Pattern getBasePattern() {
75 return basePattern;
76 }
77
78 /**
79 * Get the pattern applying to the parent node, if there is one
80 *
81 * @return the parent pattern, for example if the pattern is a/b[1]/c then the parent
82 * pattern is a/b[1]
83 */
84
85 public Pattern getUpperPattern() {
86 return upperPattern;
87 }
88
89 /**
90 * Get the upwards axis, that is, the axis by which the upper pattern is reached.
91 * Typically Axis.PARENT or Axis.ANCESTOR
92 *
93 * @return the relevant axis
94 */
95
96 public byte getUpwardsAxis() {
97 return upwardsAxis;
98 }
99
100 /**
101 * Test whether a pattern is motionless, that is, whether it can be evaluated against a node
102 * without repositioning the input stream. This is a necessary condition for patterns used
103 * as the match pattern of a streamed template rule.
104 *
105 * @return true if the pattern is motionless, that is, if it can be evaluated against a streamed
106 * node without changing the position in the streamed input file
107 */
108
109 public boolean isMotionless() {
110 return basePattern.isMotionless() && upperPattern.isMotionless();
111 }
112
113 @Override
114 public boolean matchesCurrentGroup() {
115 return upperPattern.matchesCurrentGroup();
116 }
117
118 /**
119 * Simplify the pattern: perform any context-independent optimisations
120 *
121 */
122
123 public Pattern simplify() throws XPathException {
124 upperPattern = upperPattern.simplify();
125 basePattern = basePattern.simplify();
126 return this;
127 }
128
129 /**
130 * Type-check the pattern, performing any type-dependent optimizations.
131 *
132 * @param visitor an expression visitor
133 * @param contextItemType the type of the context item at the point where the pattern appears
134 * @return the optimised Pattern
135 */
136
137 public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
138 basePattern = basePattern.typeCheck(visitor, contextItemType);
139 upperPattern = upperPattern.typeCheck(visitor, contextItemType);
140 if (upwardsAxis == AxisInfo.PARENT) {
141 ItemType type = basePattern.getItemType();
142 if (type instanceof NodeTest) {
143 // Check that this step in the pattern makes sense in the context of the parent step
144 AxisExpression step;
145 if (type.getPrimitiveType() == Type.ATTRIBUTE) {
146 step = new AxisExpression(AxisInfo.ATTRIBUTE, (NodeTest) type);
147 } else {
148 step = new AxisExpression(AxisInfo.CHILD, (NodeTest) type);
149 }
150 ExpressionTool.copyLocationInfo(this, step);
151 Expression exp = step.typeCheck(visitor, visitor.getConfiguration().makeContextItemStaticInfo(upperPattern.getItemType(), false));
152 refinedItemType = exp.getItemType();
153 }
154 }
155 testUpperPatternFirst = upperPattern.getCost() < basePattern.getCost();
156 return this;
157 }
158
159
160 /**
161 * Get the dependencies of the pattern. The only possible dependency for a pattern is
162 * on local variables. This is analyzed in those patterns where local variables may appear.
163 */
164
165 public int getDependencies() {
166 return basePattern.getDependencies() | upperPattern.getDependencies();
167 }
168
169 /**
170 * Allocate slots to any variables used within the pattern
171 *
172 * @param slotManager keeps track of slots
173 * @param nextFree the next slot that is free to be allocated @return the next slot that is free to be allocated
174 */
175
176 public int allocateSlots(SlotManager slotManager, int nextFree) {
177 // See tests cnfr23, idky239, match54
178 // SlotManager slotManager = env.getStyleElement().getContainingSlotManager();
179 nextFree = upperPattern.allocateSlots(slotManager, nextFree);
180 nextFree = basePattern.allocateSlots(slotManager, nextFree);
181 return nextFree;
182 }
183
184
185 /**
186 * Determine whether the pattern matches a given item.
187 *
188 * @param item the item to be tested
189 * @return true if the pattern matches, else false
190 */
191
192 public boolean matches(Item<?> item, XPathContext context) throws XPathException {
193 return item instanceof NodeInfo && matchesBeneathAnchor((NodeInfo) item, null, context);
194 }
195
196 /**
197 * Determine whether this pattern matches a given Node within the subtree rooted at a given
198 * anchor node. This method is used when the pattern is used for streaming.
199 *
200 * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
201 * @param anchor The anchor node, which must match any AnchorPattern subpattern
202 * @param context The dynamic context. Only relevant if the pattern
203 * uses variables, or contains calls on functions such as document() or key().
204 * @return true if the node matches the Pattern, false otherwise
205 */
206
207 public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException {
208 if (testUpperPatternFirst) {
209 return matchesUpperPattern(node, anchor, context) && basePattern.matches(node, context);
210 } else {
211 return basePattern.matchesBeneathAnchor(node, anchor, context) && matchesUpperPattern(node, anchor, context);
212 }
213 }
214
215 private boolean matchesUpperPattern(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException {
216 switch (upwardsAxis) {
217 case AxisInfo.SELF:
218 return upperPattern.matchesBeneathAnchor(node, anchor, context);
219
220 case AxisInfo.PARENT:
221 NodeInfo par = node.getParent();
222 return par != null && upperPattern.matchesBeneathAnchor(par, anchor, context);
223
224 case AxisInfo.ANCESTOR: {
225 NodeInfo anc = node.getParent();
226 return hasMatchingAncestor(anchor, anc, context);
227 }
228 case AxisInfo.ANCESTOR_OR_SELF: {
229 return hasMatchingAncestor(anchor, node, context);
230 }
231 default:
232 throw new XPathException("Unsupported axis " + AxisInfo.axisName[upwardsAxis] + " in pattern");
233 }
234
235 }
236
237 private boolean hasMatchingAncestor(NodeInfo anchor, NodeInfo anc, XPathContext context) throws XPathException {
238 while (anc != null) {
239 if (upperPattern.matchesBeneathAnchor(anc, anchor, context)) {
240 return true;
241 }
242 if (anc.equals(anchor)) {
243 return false;
244 }
245 anc = anc.getParent();
246 }
247 return false;
248 }
249
250 /**
251 * Get a UType indicating which kinds of items this Pattern can match.
252 *
253 * @return a UType indicating all the primitive types of item that the pattern can match.
254 */
255 @Override
256 public UType getUType() {
257 return basePattern.getUType();
258 }
259
260 /**
261 * Determine the fingerprint of nodes to which this pattern applies.
262 * Used for optimisation.
263 *
264 * @return the fingerprint of nodes matched by this pattern.
265 */
266
267 public int getFingerprint() {
268 return basePattern.getFingerprint();
269 }
270
271 /**
272 * Get a NodeTest that all the nodes matching this pattern must satisfy
273 */
274
275 public ItemType getItemType() {
276 if (refinedItemType != null) {
277 return refinedItemType;
278 }
279 return basePattern.getItemType();
280 }
281
282 /**
283 * Convert the pattern to a typed pattern, in which an element name is treated as
284 * schema-element(N)
285 *
286 * @param val either "strict" or "lax" depending on the value of xsl:mode/@typed
287 * @return either the original pattern unchanged, or a new pattern as the result of the
288 * conversion
289 * @throws net.sf.saxon.trans.XPathException if the pattern cannot be converted
290 */
291 @Override
292 public Pattern convertToTypedPattern(String val) throws XPathException {
293 if (upperPattern.getUType().equals(UType.DOCUMENT)) {
294 // suggests a pattern starting with a leading slash
295 Pattern b2 = basePattern.convertToTypedPattern(val);
296 if (b2 == basePattern) {
297 return this;
298 } else {
299 return new AncestorQualifiedPattern(b2, upperPattern, upwardsAxis);
300 }
301 } else {
302 Pattern u2 = upperPattern.convertToTypedPattern(val);
303 if (u2 == upperPattern) {
304 return this;
305 } else {
306 return new AncestorQualifiedPattern(basePattern, u2, upwardsAxis);
307 }
308 }
309 }
310
311 /**
312 * Get the original pattern text
313 */
314 @Override
315 public String toString() {
316 return upperPattern + (upwardsAxis == AxisInfo.PARENT ? "/" : "//") + basePattern;
317 }
318
319 /**
320 * Determine whether this pattern is the same as another pattern
321 *
322 * @param other the other object
323 */
324
325 public boolean equals(Object other) {
326 if (other instanceof AncestorQualifiedPattern) {
327 AncestorQualifiedPattern aqp = (AncestorQualifiedPattern) other;
328 return basePattern.isEqual(aqp.basePattern) && upperPattern.isEqual(aqp.upperPattern) && upwardsAxis == aqp.upwardsAxis;
329 } else {
330 return false;
331 }
332 }
333
334 /**
335 * hashcode supporting equals()
336 */
337
338 public int computeHashCode() {
339 return 88267 ^ basePattern.hashCode() ^ upperPattern.hashCode() ^ (upwardsAxis << 22);
340 }
341
342 public void export(ExpressionPresenter presenter) throws XPathException {
343 presenter.startElement("p.withUpper");
344 presenter.emitAttribute("axis", AxisInfo.axisName[getUpwardsAxis()]);
345 presenter.emitAttribute("upFirst", ""+ testUpperPatternFirst);
346 basePattern.export(presenter);
347 upperPattern.export(presenter);
348 presenter.endElement();
349 }
350
351 /**
352 * Copy a pattern. This makes a deep copy.
353 *
354 * @return the copy of the original pattern
355 * @param rebindings variables that need to be re-bound
356 */
357
358 /*@NotNull*/
359 public Pattern copy(RebindingMap rebindings) {
360 AncestorQualifiedPattern n = new AncestorQualifiedPattern(basePattern.copy(rebindings),
361 upperPattern.copy(rebindings), upwardsAxis);
362 ExpressionTool.copyLocationInfo(this, n);
363 return n;
364 }
365
366 }
367