View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    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   * @author daniels
    13   */
    14  public abstract class NodeIterator implements Iterator {
    15
    16      private Node node;
    17
    18      public NodeIterator(Node contextNode) {
    19          this.node = getFirstNode(contextNode);
    20      }
    21
    22      public boolean hasNext() {
    23          return node != null;
    24      }
    25
    26      public Object next() {
    27          if (node == null)
    28              throw new NoSuchElementException();
    29          Node ret = node;
    30          node = getNextNode(node);
    31          return ret;
    32      }
    33
    34      public void remove() {
    35          throw new UnsupportedOperationException();
    36      }
    37
    38      protected abstract Node getFirstNode(Node contextNode);
    39
    40      protected abstract Node getNextNode(Node contextNode);
    41
    42      protected Node getPreviousSibling(Node contextNode) {
    43          Node parentNode = contextNode.jjtGetParent();
    44          if (parentNode != null) {
    45              int prevPosition = getPositionFromParent(contextNode) - 1;
    46              if (prevPosition >= 0) {
    47                  return parentNode.jjtGetChild(prevPosition);
    48              }
    49          }
    50          return null;
    51      }
    52
    53      private int getPositionFromParent(Node contextNode) {
    54          Node parentNode = contextNode.jjtGetParent();
    55          for (int i = 0; i < parentNode.jjtGetNumChildren(); i++) {
    56              if (parentNode.jjtGetChild(i) == contextNode) {
    57                  return i;
    58              }
    59          }
    60          throw new RuntimeException("Node was not a child of it's parent ???");
    61      }
    62
    63      protected Node getNextSibling(Node contextNode) {
    64          Node parentNode = contextNode.jjtGetParent();
    65          if (parentNode != null) {
    66              int nextPosition = getPositionFromParent(contextNode) + 1;
    67              if (nextPosition < parentNode.jjtGetNumChildren()) {
    68                  return parentNode.jjtGetChild(nextPosition);
    69              }
    70          }
    71          return null;
    72      }
    73
    74      protected Node getFirstChild(Node contextNode) {
    75          if (contextNode.jjtGetNumChildren() > 0) {
    76              return contextNode.jjtGetChild(0);
    77          } else {
    78              return null;
    79          }
    80      }
    81
    82      protected Node getLastChild(Node contextNode) {
    83          if (contextNode.jjtGetNumChildren() > 0) {
    84              return contextNode.jjtGetChild(contextNode.jjtGetNumChildren() - 1);
    85          } else {
    86              return null;
    87          }
    88      }
    89  }