View Javadoc

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