View Javadoc

    1   package net.sourceforge.pmd.util.viewer.model;
    2
    3   import net.sourceforge.pmd.TargetJDKVersion;
    4   import net.sourceforge.pmd.ast.ASTCompilationUnit;
    5   import net.sourceforge.pmd.ast.ParseException;
    6   import net.sourceforge.pmd.ast.SimpleNode;
    7   import net.sourceforge.pmd.jaxen.DocumentNavigator;
    8   import org.jaxen.BaseXPath;
    9   import org.jaxen.JaxenException;
    10  import org.jaxen.XPath;
    11
    12  import java.io.StringReader;
    13  import java.util.List;
    14  import java.util.Vector;
    15
    16  public class ViewerModel {
    17      private Vector listeners;
    18      private SimpleNode rootNode;
    19      private List evaluationResults;
    20
    21      public ViewerModel() {
    22          listeners = new Vector(5);
    23      }
    24
    25      public SimpleNode getRootNode() {
    26          return rootNode;
    27      }
    28
    29      /***
    30       * commits source code to the model.
    31       * all existing source will be replaced
    32       */
    33      public void commitSource(String source, TargetJDKVersion jdk) {
    34          ASTCompilationUnit compilationUnit = jdk.createParser(new StringReader(source)).CompilationUnit();
    35          rootNode = compilationUnit;
    36          fireViewerModelEvent(new ViewerModelEvent(this, ViewerModelEvent.CODE_RECOMPILED));
    37      }
    38
    39      /***
    40       * determines whether the model has a compiled tree at it's disposal
    41       *
    42       * @return true if there is an AST, false otherwise
    43       */
    44      public boolean hasCompiledTree() {
    45          return rootNode != null;
    46      }
    47
    48      /***
    49       * evaluates the given XPath expression against the current tree
    50       *
    51       * @param xPath     XPath expression to be evaluated
    52       * @param evaluator object which requests the evaluation
    53       */
    54      public void evaluateXPathExpression(String xPath, Object evaluator)
    55              throws ParseException, JaxenException {
    56          XPath xpath = new BaseXPath(xPath, new DocumentNavigator());
    57          evaluationResults = xpath.selectNodes(rootNode);
    58          fireViewerModelEvent(new ViewerModelEvent(evaluator, ViewerModelEvent.PATH_EXPRESSION_EVALUATED));
    59      }
    60
    61      /***
    62       * retrieves the results of last evaluation
    63       *
    64       * @return a list containing the nodes selected by the last XPath expression
    65       *         <p/>
    66       *         evaluation
    67       */
    68      public List getLastEvaluationResults() {
    69          return evaluationResults;
    70      }
    71
    72      /***
    73       * selects the given node in the AST
    74       *
    75       * @param node     node to be selected
    76       * @param selector object which requests the selection
    77       */
    78      public void selectNode(SimpleNode node, Object selector) {
    79          fireViewerModelEvent(new ViewerModelEvent(selector, ViewerModelEvent.NODE_SELECTED, node));
    80      }
    81
    82      /***
    83       * appends the given fragment to the XPath expression
    84       *
    85       * @param pathFragment fragment to be added
    86       * @param appender     object that is trying to append the fragment
    87       */
    88      public void appendToXPathExpression(String pathFragment, Object appender) {
    89          fireViewerModelEvent(new ViewerModelEvent(appender, ViewerModelEvent.PATH_EXPRESSION_APPENDED, pathFragment));
    90      }
    91
    92      public void addViewerModelListener(ViewerModelListener l) {
    93          listeners.add(l);
    94      }
    95
    96      public void removeViewerModelListener(ViewerModelListener l) {
    97          listeners.remove(l);
    98      }
    99
    100     protected void fireViewerModelEvent(ViewerModelEvent e) {
    101         for (int i = 0; i < listeners.size(); i++) {
    102             ((ViewerModelListener) listeners.elementAt(i)).viewerModelChanged(e);
    103         }
    104     }
    105 }