|
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 |
12373
| public NodeIterator(Node contextNode) {
|
|
19 |
12373
| this.node = getFirstNode(contextNode);
|
|
20 |
| } |
|
21 |
| |
|
22 |
36001
| public boolean hasNext() {
|
|
23 |
36001
| return node != null;
|
|
24 |
| } |
|
25 |
| |
|
26 |
12706
| public Object next() {
|
|
27 |
12706
| if (node == null)
|
|
28 |
0
| throw new NoSuchElementException();
|
|
29 |
12706
| Node ret = node;
|
|
30 |
12706
| node = getNextNode(node);
|
|
31 |
12706
| 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 |
12776
| private int getPositionFromParent(Node contextNode) {
|
|
54 |
12776
| Node parentNode = contextNode.jjtGetParent();
|
|
55 |
16403
| for (int i = 0; i < parentNode.jjtGetNumChildren(); i++) {
|
|
56 |
16403
| if (parentNode.jjtGetChild(i) == contextNode) {
|
|
57 |
12776
| return i;
|
|
58 |
| } |
|
59 |
| } |
|
60 |
0
| throw new RuntimeException("Node was not a child of it's parent ???");
|
|
61 |
| } |
|
62 |
| |
|
63 |
12757
| protected Node getNextSibling(Node contextNode) {
|
|
64 |
12757
| Node parentNode = contextNode.jjtGetParent();
|
|
65 |
12757
| if (parentNode != null) {
|
|
66 |
12757
| int nextPosition = getPositionFromParent(contextNode) + 1;
|
|
67 |
12757
| if (nextPosition < parentNode.jjtGetNumChildren()) {
|
|
68 |
2635
| return parentNode.jjtGetChild(nextPosition);
|
|
69 |
| } |
|
70 |
| } |
|
71 |
10122
| return null;
|
|
72 |
| } |
|
73 |
| |
|
74 |
12306
| protected Node getFirstChild(Node contextNode) {
|
|
75 |
12306
| if (contextNode.jjtGetNumChildren() > 0) {
|
|
76 |
10082
| return contextNode.jjtGetChild(0);
|
|
77 |
| } else { |
|
78 |
2224
| 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 |
| } |