1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.jaxen; |
5 |
| |
6 |
| import net.sourceforge.pmd.ast.Node; |
7 |
| |
8 |
| import java.util.Iterator; |
9 |
| import java.util.NoSuchElementException; |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| public abstract class NodeIterator implements Iterator { |
15 |
| |
16 |
| private Node node; |
17 |
| |
18 |
36770
| public NodeIterator(Node contextNode) {
|
19 |
36770
| this.node = getFirstNode(contextNode);
|
20 |
| } |
21 |
| |
22 |
104805
| public boolean hasNext() {
|
23 |
104805
| return node != null;
|
24 |
| } |
25 |
| |
26 |
36015
| public Object next() {
|
27 |
36015
| if (node == null)
|
28 |
0
| throw new NoSuchElementException();
|
29 |
36015
| Node ret = node;
|
30 |
36015
| node = getNextNode(node);
|
31 |
36015
| return ret;
|
32 |
| } |
33 |
| |
34 |
0
| public void remove() {
|
35 |
0
| throw new UnsupportedOperationException();
|
36 |
| } |
37 |
| |
38 |
| protected abstract Node getFirstNode(Node contextNode); |
39 |
| |
40 |
| protected abstract Node getNextNode(Node contextNode); |
41 |
| |
42 |
22
| protected Node getPreviousSibling(Node contextNode) {
|
43 |
22
| Node parentNode = contextNode.jjtGetParent();
|
44 |
22
| if (parentNode != null) {
|
45 |
19
| int prevPosition = getPositionFromParent(contextNode) - 1;
|
46 |
19
| if (prevPosition >= 0) {
|
47 |
6
| return parentNode.jjtGetChild(prevPosition);
|
48 |
| } |
49 |
| } |
50 |
16
| return null;
|
51 |
| } |
52 |
| |
53 |
36105
| private int getPositionFromParent(Node contextNode) {
|
54 |
36105
| Node parentNode = contextNode.jjtGetParent();
|
55 |
45334
| for (int i = 0; i < parentNode.jjtGetNumChildren(); i++) {
|
56 |
45334
| if (parentNode.jjtGetChild(i) == contextNode) {
|
57 |
36105
| return i;
|
58 |
| } |
59 |
| } |
60 |
0
| throw new RuntimeException("Node was not a child of it's parent ???");
|
61 |
| } |
62 |
| |
63 |
36086
| protected Node getNextSibling(Node contextNode) {
|
64 |
36086
| Node parentNode = contextNode.jjtGetParent();
|
65 |
36086
| if (parentNode != null) {
|
66 |
36086
| int nextPosition = getPositionFromParent(contextNode) + 1;
|
67 |
36086
| if (nextPosition < parentNode.jjtGetNumChildren()) {
|
68 |
7035
| return parentNode.jjtGetChild(nextPosition);
|
69 |
| } |
70 |
| } |
71 |
29051
| return null;
|
72 |
| } |
73 |
| |
74 |
36683
| protected Node getFirstChild(Node contextNode) {
|
75 |
36683
| if (contextNode.jjtGetNumChildren() > 0) {
|
76 |
28992
| return contextNode.jjtGetChild(0);
|
77 |
| } else { |
78 |
7691
| return null;
|
79 |
| } |
80 |
| } |
81 |
| |
82 |
4
| protected Node getLastChild(Node contextNode) {
|
83 |
4
| if (contextNode.jjtGetNumChildren() > 0) {
|
84 |
2
| return contextNode.jjtGetChild(contextNode.jjtGetNumChildren() - 1);
|
85 |
| } else { |
86 |
2
| return null;
|
87 |
| } |
88 |
| } |
89 |
| } |