Debian Codegraph @

net/sf/saxon/expr/parser/PathMap.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.expr.parser;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.functions.Doc;
12 import net.sf.saxon.functions.DocumentFn;
13 import net.sf.saxon.functions.ResolveURI;
14 import net.sf.saxon.lib.Logger;
15 import net.sf.saxon.om.AxisInfo;
16 import net.sf.saxon.pattern.AnyNodeTest;
17 import net.sf.saxon.pattern.NodeKindTest;
18 import net.sf.saxon.pattern.NodeTest;
19 import net.sf.saxon.trans.XPathException;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.util.*;
24
25 /**
26 * A PathMap is a description of all the paths followed by an expression.
27 * It is a set of trees. Each tree contains as its root an expression that selects
28 * nodes without any dependency on the context. The arcs in the tree are axis steps.
29 * So the expression doc('a.xml')/a[b=2]/c has a single root (the call on doc()), with
30 * a single arc representing child::a, this leads to a node which has two further arcs
31 * representing child::b and child::c. Because element b is atomized, there will also be
32 * an arc for the step descendant::text() indicating the requirement to access the text
33 * nodes of the element.
34 * <p>The current implementation works only for XPath 2.0 expressions (for example, constructs
35 * like xsl:for-each-group are not handled.)</p>
36 * <p>This class, together with the overloaded method
37 * {@link net.sf.saxon.expr.Expression#addToPathMap(PathMap, PathMap.PathMapNodeSet)} can be
38 * seen as an implementation of the static path analysis algorithm given in section 4 of
39 * <a href="http://www-db.research.bell-labs.com/user/simeon/xml_projection.pdf">A. Marian and J. Simeon,
40 * Projecting XML Documents, VLDB 2003</a>.</p>
41 */
42
43 public class PathMap {
44
45 /*@NotNull*/ private List<PathMapRoot> pathMapRoots = new ArrayList<PathMapRoot>();
46 /*@NotNull*/ private HashMap<Binding, PathMapNodeSet> pathsForVariables =
47 new HashMap<Binding, PathMapNodeSet>(); // a map from a variable Binding to a PathMapNodeSet
48
49 /**
50 * A node in the path map. A node holds a set of arcs, each representing a link to another
51 * node in the path map.
52 */
53
54 public static class PathMapNode {
55 List<PathMapArc> arcs;
56 private boolean returnable;
57 private boolean atomized;
58 private boolean hasUnknownDependencies;
59
60 /**
61 * Create a node in the PathMap (initially with no arcs)
62 */
63
64 private PathMapNode() {
65 arcs = new ArrayList<PathMapArc>();
66 }
67
68 /**
69 * Create a new arc
70 *
71 * @param axis the axis of this step
72 * @param test the node test of this step
73 * @return the newly-constructed target of the new arc
74 */
75
76 public PathMapNode createArc(byte axis, /*@NotNull*/ NodeTest test) {
77 for (PathMapArc a : arcs) {
78 if (a.getAxis() == axis && a.getNodeTest().equals(test)) {
79 return a.getTarget();
80 }
81 }
82 PathMapNode target = new PathMapNode();
83 PathMapArc arc = new PathMapArc(axis, test, target);
84 arcs.add(arc);
85 return target;
86 }
87
88 /**
89 * Create a new arc to an existing target
90 *
91 * @param axis the axis of this step
92 * @param test the node test of this step
93 * @param target the target node of the new arc
94 */
95
96 public void createArc(byte axis, /*@NotNull*/ NodeTest test, /*@NotNull*/ PathMapNode target) {
97 for (PathMapArc a : arcs) {
98 if (a.getAxis() == axis && a.getNodeTest().equals(test) && a.getTarget() == target) {
99 // TODO: if it's a different target, then merge the two targets into one. XMark Q8
100 a.getTarget().setReturnable(a.getTarget().isReturnable() || target.isReturnable());
101 if (target.isAtomized()) {
102 a.getTarget().setAtomized();
103 }
104 return;
105 }
106 }
107 PathMapArc arc = new PathMapArc(axis, test, target);
108 arcs.add(arc);
109 }
110
111 /**
112 * Get the arcs emanating from this node in the PathMap
113 *
114 * @return the arcs, each representing an AxisStep. The order of arcs in the array is undefined.
115 */
116
117 public PathMapArc[] getArcs() {
118 return arcs.toArray(new PathMapArc[arcs.size()]);
119 }
120
121 /**
122 * Indicate that the node represents a value that is returnable as the result of the
123 * supplied expression, rather than merely a node that is visited en route
124 *
125 * @param returnable true if the node represents a final result of the expression
126 */
127
128 public void setReturnable(boolean returnable) {
129 this.returnable = returnable;
130 }
131
132 /**
133 * Ask whether the node represents a value that is returnable as the result of the
134 * supplied expression, rather than merely a node that is visited en route
135 *
136 * @return true if the node represents a final result of the expression
137 */
138
139 public boolean isReturnable() {
140 return returnable;
141 }
142
143 /**
144 * Test whether there are any returnable nodes reachable from this node by
145 * zero or more arcs
146 *
147 * @return true if any arcs lead to a pathmap node representing a returnable XDM node
148 */
149
150 public boolean hasReachableReturnables() {
151 if (isReturnable()) {
152 return true;
153 }
154 for (PathMapArc arc : arcs) {
155 if (arc.getTarget().hasReachableReturnables()) {
156 return true;
157 }
158 }
159 return false;
160 }
161
162 /**
163 * Indicate that the typed value or string value of the node reached by this path
164 * will be used. Note that because this is being used only to determine navigation paths,
165 * the property does not need to be set when nodes other than element or document nodes
166 * are atomized.
167 */
168
169 public void setAtomized() {
170 this.atomized = true;
171 }
172
173 /**
174 * Ask whether the typed value (or string value) of the node reached by this path
175 * will be required.
176 *
177 * @return true if the typed value or string value of the node is required
178 */
179
180 public boolean isAtomized() {
181 return atomized;
182 }
183
184 /**
185 * Indicate that the path has unknown dependencies, typically because a node reached
186 * by the path is supplied as an argument to a user-defined function
187 */
188
189 public void setHasUnknownDependencies() {
190 hasUnknownDependencies = true;
191 }
192
193 /**
194 * Ask whether the path has unknown dependencies, typically because a node reached
195 * by the path is supplied as an argument to a user-defined function
196 *
197 * @return true if the path has unknown dependencies
198 */
199
200 public boolean hasUnknownDependencies() {
201 return hasUnknownDependencies;
202 }
203
204 /**
205 * Determine whether the path is entirely within a streamable snapshot of a streamed document:
206 * that is, it must only navigate to ancestors and to attributes of ancestors
207 *
208 * @return true if this path performs navigation other than to ancestors and their attributes
209 */
210
211 public boolean allPathsAreWithinStreamableSnapshot() {
212 if (hasUnknownDependencies() || isReturnable() || isAtomized()) {
213 return false;
214 }
215 for (PathMapArc arc : arcs) {
216 int axis = arc.getAxis();
217 if (axis == AxisInfo.ATTRIBUTE) {
218 PathMapNode next = arc.getTarget();
219 if (next.isReturnable()) {
220 return false;
221 }
222 if (next.getArcs().length != 0 && !next.allPathsAreWithinStreamableSnapshot()) {
223 return false;
224 }
225 } else if (axis == AxisInfo.SELF || axis == AxisInfo.ANCESTOR || axis == AxisInfo.ANCESTOR_OR_SELF ||
226 axis == AxisInfo.PARENT) {
227 PathMapNode next = arc.getTarget();
228 if (next.isAtomized()) {
229 return false;
230 }
231 if (!next.allPathsAreWithinStreamableSnapshot()) {
232 return false;
233 }
234 } else {
235 return false;
236 }
237 }
238 return true;
239 }
240 }
241
242 /**
243 * A root node in the path map. A root node represents either (a) a subexpression that is the first step in
244 * a path expression, or (b) a subexpression that is not the first step in a path, but which returns nodes
245 * (for example, a call on the doc() function).
246 */
247
248 public static class PathMapRoot extends PathMapNode {
249
250 private Expression rootExpression;
251 private boolean isDownwardsOnly;
252
253 /**
254 * Create a PathMapRoot
255 *
256 * @param root the expression at the root of a path
257 */
258 private PathMapRoot(Expression root) {
259 this.rootExpression = root;
260 }
261
262 /**
263 * Get the root expression
264 *
265 * @return the expression at the root of the path
266 */
267 public Expression getRootExpression() {
268 return rootExpression;
269 }
270
271 }
272
273 /**
274 * An arc joining two nodes in the path map. The arc has a target (destination) node, and is
275 * labelled with an AxisExpression representing a step in a path expression
276 */
277
278 public static class PathMapArc {
279 private PathMapNode target;
280 private byte axis;
281 private NodeTest test;
282
283 /**
284 * Create a PathMapArc
285 *
286 * @param axis the axis (a constant from class {@link net.sf.saxon.om.AxisInfo}
287 * @param test the node test
288 * @param target the node reached by following this arc
289 */
290 private PathMapArc(byte axis, /*@NotNull*/ NodeTest test, /*@NotNull*/ PathMapNode target) {
291 this.axis = axis;
292 this.test = test;
293 this.target = target;
294 }
295
296 /**
297 * Get the Axis associated with this arc
298 *
299 * @return the axis, a constant from class {@link net.sf.saxon.om.AxisInfo}
300 */
301
302 public byte getAxis() {
303 return axis;
304 }
305
306 /**
307 * Get the NodeTest associated with this arc
308 *
309 * @return the NodeTest
310 */
311
312 public NodeTest getNodeTest() {
313 return test;
314 }
315
316 /**
317 * Get the target node representing the destination of this arc
318 *
319 * @return the target node
320 */
321
322 public PathMapNode getTarget() {
323 return target;
324 }
325 }
326
327 /**
328 * A (mutable) set of nodes in the path map
329 */
330
331 public static class PathMapNodeSet extends HashSet<PathMapNode> {
332
333 /**
334 * Create an initially-empty set of path map nodes
335 */
336
337 public PathMapNodeSet() {
338 }
339
340 /**
341 * Create a set of path map nodes that initially contains a single node
342 *
343 * @param singleton the single node to act as the initial content
344 */
345
346 public PathMapNodeSet(PathMapNode singleton) {
347 add(singleton);
348 }
349
350 /**
351 * Create an arc from each node in this node set to a corresponding newly-created
352 * target node
353 *
354 * @param axis the axis of the step defining the transition
355 * @param test the node test of the step defining the transition
356 * @return the set of new target nodes
357 */
358
359 /*@NotNull*/
360 public PathMapNodeSet createArc(byte axis, /*@NotNull*/ NodeTest test) {
361 PathMapNodeSet targetSet = new PathMapNodeSet();
362 for (PathMapNode node : this) {
363 targetSet.add(node.createArc(axis, test));
364 }
365 return targetSet;
366 }
367
368 /**
369 * Combine two node sets into one
370 *
371 * @param nodes the set of nodes to be added to this set
372 */
373
374 public void addNodeSet(/*@Nullable*/ PathMapNodeSet nodes) {
375 if (nodes != null) {
376 for (PathMapNode node : nodes) {
377 add(node);
378 }
379 }
380 }
381
382 /**
383 * Set the atomized property on all nodes in this nodeset
384 */
385
386 public void setAtomized() {
387 for (PathMapNode node : this) {
388 node.setAtomized();
389 }
390 }
391
392 /**
393 * Set the returnable property on all nodes in this nodeset
394 */
395
396 public void setReturnable(boolean isReturned) {
397 for (PathMapNode node : this) {
398 node.setReturnable(isReturned);
399 }
400 }
401
402 /**
403 * Test whether there are any returnable nodes reachable from nodes in this nodeset
404 */
405
406 public boolean hasReachableReturnables() {
407 for (PathMapNode node : this) {
408 if (node.hasReachableReturnables()) {
409 return true;
410 }
411 }
412 return false;
413 }
414
415 /**
416 * Determine whether the path is entirely within a streamable snapshot of a streamed document:
417 * that is, it must only navigate to ancestors and to attributes of ancestors
418 */
419
420 public boolean allPathsAreWithinStreamableSnapshot() {
421 for (PathMapNode node : this) {
422 if (!node.allPathsAreWithinStreamableSnapshot()) {
423 return false;
424 }
425 }
426 return true;
427 }
428
429 /**
430 * Indicate that all the descendants of the nodes in this nodeset are required
431 */
432
433 public void addDescendants() {
434 for (PathMapNode node : this) {
435 node.createArc(AxisInfo.DESCENDANT, AnyNodeTest.getInstance());
436 }
437 }
438
439 /**
440 * Indicate that all the nodes have unknown dependencies
441 */
442
443 public void setHasUnknownDependencies() {
444 for (PathMapNode node : this) {
445 node.setHasUnknownDependencies();
446 }
447 }
448
449 }
450
451 /**
452 * Create the PathMap for an expression
453 *
454 * @param exp the expression whose PathMap is required
455 */
456
457 public PathMap(/*@NotNull*/ Expression exp) {
458 PathMapNodeSet finalNodes = exp.addToPathMap(this, null);
459 if (finalNodes != null) {
460 for (PathMapNode node : finalNodes) {
461 node.setReturnable(true);
462 }
463 }
464 }
465
466 /**
467 * Make a new root node in the path map. However, if there is already a root for the same
468 * expression, the existing root for that expression is returned.
469 *
470 * @param exp the expression represented by this root node
471 * @return the new root node
472 */
473
474 public PathMapRoot makeNewRoot(/*@NotNull*/ Expression exp) {
475 for (PathMapRoot r : pathMapRoots) {
476 if (exp.isEqual(r.getRootExpression())) {
477 return r;
478 }
479 }
480 PathMapRoot root = new PathMapRoot(exp);
481 pathMapRoots.add(root);
482 return root;
483 }
484
485 /**
486 * Get all the root expressions from the path map
487 *
488 * @return an array containing the root expressions
489 */
490
491 public PathMapRoot[] getPathMapRoots() {
492 return pathMapRoots.toArray(new PathMapRoot[pathMapRoots.size()]);
493 }
494
495 /**
496 * Register the path used when evaluating a given variable binding
497 *
498 * @param binding the variable binding
499 * @param nodeset the set of PathMap nodes reachable when evaluating that variable
500 */
501
502 public void registerPathForVariable(Binding binding, PathMapNodeSet nodeset) {
503 pathsForVariables.put(binding, nodeset);
504 }
505
506 /**
507 * Get the path used when evaluating a given variable binding
508 *
509 * @param binding the variable binding
510 * @return the set of PathMap nodes reachable when evaluating that variable
511 */
512
513 public PathMapNodeSet getPathForVariable(Binding binding) {
514 return pathsForVariables.get(binding);
515 }
516
517 /**
518 * Get the path map root for the context document
519 *
520 * @return the path map root for the context document if there is one, or null if none is found.
521 * @throws IllegalStateException if there is more than one path map root for the context document
522 */
523
524 /*@Nullable*/
525 public PathMapRoot getContextDocumentRoot() {
526 //System.err.println("BEFORE REDUCTION:");
527 //map.diagnosticDump(System.err);
528 PathMap.PathMapRoot[] roots = getPathMapRoots();
529 PathMapRoot contextRoot = null;
530 for (PathMapRoot root : roots) {
531 PathMapRoot newRoot = reduceToDownwardsAxes(root);
532 if (newRoot.getRootExpression() instanceof RootExpression) {
533 if (contextRoot != null) {
534 throw new IllegalStateException("More than one context document root found in path map");
535 } else {
536 contextRoot = newRoot;
537 }
538 }
539 }
540 //System.err.println("AFTER REDUCTION:");
541 //map.diagnosticDump(System.err);
542 return contextRoot;
543 }
544
545 /**
546 * Get the path map root for the context item
547 *
548 * @return the path map root for the context item if there is one, or null if none is found.
549 * @throws IllegalStateException if there is more than one path map root for the context item
550 */
551
552 /*@Nullable*/
553 public PathMapRoot getContextItemRoot() {
554 //System.err.println("BEFORE REDUCTION:");
555 //map.diagnosticDump(System.err);
556 PathMap.PathMapRoot[] roots = getPathMapRoots();
557 PathMapRoot contextRoot = null;
558 for (PathMapRoot root : roots) {
559 if (root.getRootExpression() instanceof ContextItemExpression) {
560 if (contextRoot != null) {
561 throw new IllegalStateException("More than one context document root found in path map");
562 } else {
563 contextRoot = root;
564 }
565 }
566 }
567 return contextRoot;
568 }
569
570 /**
571 * Get the path map root for a call on the doc() or document() function with a given literal argument
572 *
573 * @param requiredUri the literal argument we are looking for
574 * @return the path map root for the specified document if there is one, or null if none is found.
575 * @throws IllegalStateException if there is more than one path map root for the specified document
576 */
577
578 /*@Nullable*/
579 public PathMapRoot getRootForDocument(/*@NotNull*/ String requiredUri) {
580 //System.err.println("BEFORE REDUCTION:");
581 //map.diagnosticDump(System.err);
582 PathMap.PathMapRoot[] roots = getPathMapRoots();
583 PathMapRoot requiredRoot = null;
584 for (PathMapRoot root : roots) {
585 PathMapRoot newRoot = reduceToDownwardsAxes(root);
586 Expression exp = newRoot.getRootExpression();
587 String baseUri;
588 if (exp.isCallOn(Doc.class)) {
589 baseUri = exp.getStaticBaseURIString();
590 } else if (exp.isCallOn(DocumentFn.class)) {
591 baseUri = exp.getStaticBaseURIString();
592 } else {
593 continue;
594 }
595 Expression arg = ((SystemFunctionCall) exp).getArg(0);
596 String suppliedUri = null;
597 if (arg instanceof Literal) {
598 try {
599 String argValue = ((Literal) arg).getValue().getStringValue();
600 if (baseUri == null) {
601 if (new URI(argValue).isAbsolute()) {
602 suppliedUri = argValue;
603 } else {
604 suppliedUri = null;
605 }
606 } else {
607 suppliedUri = ResolveURI.makeAbsolute(argValue, baseUri).toString();
608 }
609 } catch (URISyntaxException err) {
610 suppliedUri = null;
611 } catch (XPathException err) {
612 suppliedUri = null;
613 }
614 }
615 if (requiredUri.equals(suppliedUri)) {
616 if (requiredRoot != null) {
617 throw new IllegalStateException("More than one document root found in path map for " + requiredUri);
618 } else {
619 requiredRoot = newRoot;
620 }
621 }
622 }
623 //System.err.println("AFTER REDUCTION:");
624 //map.diagnosticDump(System.err);
625 return requiredRoot;
626 }
627
628 /**
629 * Given a PathMapRoot, simplify the tree rooted at this node so that
630 * it only contains downwards selections: specifically, so that the only axes
631 * used are child, attribute, namespace, and descendant. If the root expression
632 * is a ContextItemExpression (that is, the path can start at any node) then it is rebased
633 * to start at a root node, which means in effect that a path such as a/b/c is treated
634 * as //a/b/c.
635 *
636 * @param root the root of the path to be simplified
637 * @return the path map root after converting the tree to use downwards axes only
638 */
639
640 public PathMapRoot reduceToDownwardsAxes(/*@NotNull*/ PathMapRoot root) {
641 // If the path is rooted at an arbitrary context node, we rebase it to be rooted at the
642 // document root. This involves changing the root to a RootExpression, and changing the axis
643 // for initial steps from child to descendant where necessary
644 if (root.isDownwardsOnly) {
645 return root;
646 }
647 PathMapRoot newRoot = root;
648 if (root.getRootExpression() instanceof ContextItemExpression) {
649 RootExpression slash = new RootExpression();
650 //slash.setContainer(root.getRootExpression().getContainer());
651 //root.setRootExpression(slash);
652 newRoot = makeNewRoot(slash);
653 for (int i = root.arcs.size() - 1; i >= 0; i--) {
654 PathMapArc arc = root.arcs.get(i);
655 byte axis = arc.getAxis();
656 switch (axis) {
657 case AxisInfo.ATTRIBUTE:
658 case AxisInfo.NAMESPACE: {
659 PathMapNode newTarget = new PathMapNode();
660 newTarget.arcs.add(arc);
661 newRoot.createArc(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT, newTarget);
662 break;
663 }
664 default: {
665 newRoot.createArc(AxisInfo.DESCENDANT_OR_SELF, arc.getNodeTest(), arc.getTarget());
666 break;
667 }
668 }
669 }
670 for (int i = 0; i < pathMapRoots.size(); i++) {
671 if (pathMapRoots.get(i) == root) {
672 pathMapRoots.remove(i);
673 break;
674 }
675 }
676 }
677 // Now process the tree of paths recursively, rewriting all axes in terms of downwards
678 // selections, if necessary as downward selections from the root
679 Stack<PathMapNode> nodeStack = new Stack<PathMapNode>();
680 nodeStack.push(newRoot);
681 reduceToDownwardsAxes(newRoot, nodeStack);
682 newRoot.isDownwardsOnly = true;
683 return newRoot;
684 }
685
686 /**
687 * Supporting method for {@link #reduceToDownwardsAxes(PathMap.PathMapRoot)}
688 *
689 * @param root the root of the path being simplified
690 * @param nodeStack the sequence of nodes by which the current node in the path map was reached.
691 * The node at the bottom of the stack is the root.
692 */
693
694 private void reduceToDownwardsAxes(/*@NotNull*/ PathMapRoot root, /*@NotNull*/ Stack<PathMapNode> nodeStack) {
695 //PathMapArc lastArc = (PathMapArc)arcStack.peek();
696 //byte lastAxis = lastArc.getStep().getAxis();
697 PathMapNode node = nodeStack.peek();
698 if (node.hasUnknownDependencies()) {
699 root.setHasUnknownDependencies();
700 }
701
702 for (int i = 0; i < node.arcs.size(); i++) {
703 nodeStack.push((node.arcs.get(i)).getTarget());
704 reduceToDownwardsAxes(root, nodeStack);
705 nodeStack.pop();
706 }
707
708 for (int i = node.arcs.size() - 1; i >= 0; i--) {
709 PathMapArc thisArc = node.arcs.get(i);
710 //AxisExpression axisStep = thisArc.getStep();
711 PathMapNode grandParent =
712 (nodeStack.size() < 2 ? null : nodeStack.get(nodeStack.size() - 2));
713 byte lastAxis = -1;
714 if (grandParent != null) {
715 for (PathMapArc arc1 : grandParent.arcs) {
716 PathMapArc arc = (arc1);
717 if (arc.getTarget() == node) {
718 lastAxis = arc.getAxis();
719 }
720 }
721 }
722 switch (thisArc.getAxis()) {
723
724 case AxisInfo.ANCESTOR_OR_SELF:
725 case AxisInfo.DESCENDANT_OR_SELF:
726 if (thisArc.getNodeTest() == NodeKindTest.DOCUMENT) {
727 // This is typically an absolute path expression appearing within a predicate
728 node.arcs.remove(i);
729 for (PathMapArc arc : thisArc.getTarget().arcs) {
730 root.arcs.add(arc);
731 }
732 break;
733 } else {
734 // fall through
735 }
736
737 case AxisInfo.ANCESTOR:
738 case AxisInfo.FOLLOWING:
739 case AxisInfo.PRECEDING: {
740 // replace the axis by a downwards axis from the root
741 if (thisArc.getAxis() != AxisInfo.DESCENDANT_OR_SELF) {
742 root.createArc(AxisInfo.DESCENDANT_OR_SELF, thisArc.getNodeTest(), thisArc.getTarget());
743 node.arcs.remove(i);
744 }
745 break;
746 }
747
748 case AxisInfo.ATTRIBUTE:
749 case AxisInfo.CHILD:
750 case AxisInfo.DESCENDANT:
751 case AxisInfo.NAMESPACE:
752 // no action
753 break;
754
755 case AxisInfo.FOLLOWING_SIBLING:
756 case AxisInfo.PRECEDING_SIBLING: {
757 if (grandParent != null) {
758 grandParent.createArc(lastAxis, thisArc.getNodeTest(), thisArc.getTarget());
759 node.arcs.remove(i);
760 break;
761 } else {
762 root.createArc(AxisInfo.CHILD, thisArc.getNodeTest(), thisArc.getTarget());
763 node.arcs.remove(i);
764 break;
765 }
766 }
767 case AxisInfo.PARENT: {
768
769 if (lastAxis == AxisInfo.CHILD || lastAxis == AxisInfo.ATTRIBUTE || lastAxis == AxisInfo.NAMESPACE) {
770 // ignore the parent step - it leads to somewhere we have already been.
771 // But it might become a returned node
772 if (node.isReturnable()) {
773 grandParent.setReturnable(true);
774 }
775 // any paths after the parent step need to be attached to the grandparent
776
777 PathMapNode target = thisArc.getTarget();
778 for (int a = 0; a < target.arcs.size(); a++) {
779 grandParent.arcs.add(target.arcs.get(a));
780 }
781 node.arcs.remove(i);
782 } else if (lastAxis == AxisInfo.DESCENDANT) {
783 if (thisArc.getTarget().arcs.isEmpty()) {
784 grandParent.createArc(AxisInfo.DESCENDANT_OR_SELF, thisArc.getNodeTest());
785 } else {
786 grandParent.createArc(AxisInfo.DESCENDANT_OR_SELF, thisArc.getNodeTest(), thisArc.getTarget());
787 }
788 node.arcs.remove(i);
789 } else {
790 // don't try to be precise about a/b/../../c
791 if (thisArc.getTarget().arcs.isEmpty()) {
792 root.createArc(AxisInfo.DESCENDANT_OR_SELF, thisArc.getNodeTest());
793 } else {
794 root.createArc(AxisInfo.DESCENDANT_OR_SELF, thisArc.getNodeTest(), thisArc.getTarget());
795 }
796 node.arcs.remove(i);
797 }
798 break;
799 }
800 case AxisInfo.SELF: {
801 // This step can't take us anywhere we haven't been, so delete it
802 node.arcs.remove(i);
803 break;
804 }
805 }
806 }
807
808 }
809
810 /**
811 * Display a printed representation of the path map
812 *
813 * @param out the output stream to which the output will be written
814 */
815
816 public void diagnosticDump(/*@NotNull*/ Logger out) {
817 for (int i = 0; i < pathMapRoots.size(); i++) {
818 out.info("\nROOT EXPRESSION " + i);
819 PathMapRoot mapRoot = pathMapRoots.get(i);
820 if (mapRoot.hasUnknownDependencies()) {
821 out.info(" -- has unknown dependencies --");
822 }
823 Expression exp = mapRoot.rootExpression;
824 exp.explain(out);
825 out.info("\nTREE FOR EXPRESSION " + i);
826 showArcs(out, mapRoot, 2);
827 }
828 }
829
830 /**
831 * Internal helper method called by diagnosticDump, to show the arcs emanating from a node.
832 * Each arc is shown as a representation of the axis step, followed optionally by "@" if the
833 * node reached by the arc is atomized, followed optionally by "#" if the
834 * node reached by the arc is a final returnable node.
835 *
836 * @param out the output stream
837 * @param node the node in the path map whose arcs are to be displayed
838 * @param indent the indentation level in the output
839 */
840
841 private void showArcs(Logger out, PathMapNode node, int indent) {
842 String pad = " ".substring(0, indent);
843 List<PathMapArc> arcs = node.arcs;
844 for (PathMapArc arc : arcs) {
845 out.info(pad + AxisInfo.axisName[arc.axis] +
846 "::" +
847 arc.test.toString() +
848 (arc.target.isAtomized() ? " @" : "") +
849 (arc.target.isReturnable() ? " #" : "") +
850 (arc.target.hasUnknownDependencies() ? " ...??" : ""));
851 showArcs(out, arc.target, indent + 2);
852 }
853 }
854
855 }
856