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