View Javadoc

    1   package net.sourceforge.pmd.dfa.report;
    2
    3   import java.util.ArrayList;
    4   import java.util.List;
    5
    6   public abstract class AbstractReportNode {
    7       private List childNodes = new ArrayList();
    8       private AbstractReportNode parentNode = null;
    9
    10      /*
    11      * Number of all RuleViolations down to this node. At the moment it will
    12      * only be calculated by running the ReportHTMLPrintVisitor.
    13      * */
    14      private int numberOfViolations;
    15
    16      /***
    17       * Should compare to nodes of the tree.
    18       */
    19      public abstract boolean equalsNode(AbstractReportNode arg0);
    20
    21      /***
    22       * @return null If there isn't any child.
    23       */
    24      public AbstractReportNode getFirstChild() {
    25          if (this.isLeaf()) {
    26              return null;
    27          }
    28          return (AbstractReportNode) this.childNodes.get(0);
    29      }
    30
    31      /***
    32       * @return null If there isn't any sibling.
    33       */
    34      public AbstractReportNode getNextSibling() {
    35          if (this.parentNode == null) {
    36              return null;
    37          }
    38          int index = this.parentNode.getChildIndex(this);
    39          if (index < 0) {
    40              return null;
    41          }
    42          if (index >= this.parentNode.childNodes.size() - 1) {
    43              return null;
    44          }
    45          return (AbstractReportNode) this.parentNode.childNodes.get(index + 1);
    46      }
    47
    48      /***
    49       * @return index The index of the x-th child of his parent.
    50       */
    51      private int getChildIndex(AbstractReportNode child) {
    52          for (int i = 0; i < this.childNodes.size(); i++) {
    53              if (this.childNodes.get(i).equals(child)) {
    54                  return i;
    55              }
    56          }
    57          return -1;
    58      }
    59
    60      /***
    61       * Adds the child in front of any other childs.
    62       */
    63      public void addFirst(AbstractReportNode child) {
    64          this.childNodes.add(0, child);
    65          child.parentNode = this;
    66      }
    67
    68      /***
    69       * Adds the child at the end.
    70       */
    71      public void add(AbstractReportNode child) {
    72          this.childNodes.add(child);
    73          child.parentNode = this;
    74      }
    75
    76      public void addNumberOfViolation(int number) {
    77          this.numberOfViolations += number;
    78      }
    79
    80      /***
    81       * @return The number of all violations downside the node.
    82       */
    83      public int getNumberOfViolations() {
    84          return numberOfViolations;
    85      }
    86
    87      // ----------------------------------------------------------------------------
    88      // visitor methods
    89      public void childrenAccept(ReportVisitor visitor) {
    90          for (int i = 0; i < childNodes.size(); i++) {
    91              AbstractReportNode node = (AbstractReportNode) childNodes.get(i);
    92              node.accept(visitor);
    93          }
    94      }
    95
    96      public void accept(ReportVisitor visitor) {
    97          visitor.visit(this);
    98      }
    99
    100     public AbstractReportNode getChildAt(int arg0) {
    101         if (arg0 >= 0 && arg0 <= this.childNodes.size() - 1) {
    102             return (AbstractReportNode) this.childNodes.get(arg0);
    103         }
    104         return null;
    105     }
    106
    107     public int getChildCount() {
    108         return this.childNodes.size();
    109     }
    110
    111     public AbstractReportNode getParent() {
    112         return this.parentNode;
    113     }
    114
    115     public boolean isLeaf() {
    116         return this.childNodes.isEmpty();
    117     }
    118
    119 }