KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > viewer > model > ViewerModel


1 package net.sourceforge.pmd.util.viewer.model;
2
3 import java.io.StringReader JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
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 JavaDoc listeners;
20     private SimpleNode rootNode;
21     private List JavaDoc evaluationResults;
22
23     public ViewerModel() {
24         listeners = new ArrayList JavaDoc(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 JavaDoc source, TargetJDKVersion jdk) {
36         ASTCompilationUnit compilationUnit = jdk.createParser(new StringReader JavaDoc(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 JavaDoc xPath, Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc pathFragment, Object JavaDoc 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             ((ViewerModelListener) listeners.get(i)).viewerModelChanged(e);
105         }
106     }
107 }
108
Popular Tags