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

This page was automatically generated by Maven