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 listeners; |
20 |
| private SimpleNode rootNode; |
21 |
| private List evaluationResults; |
22 |
| |
23 |
0
| public ViewerModel() {
|
24 |
0
| listeners = new ArrayList(5);
|
25 |
| } |
26 |
| |
27 |
0
| public SimpleNode getRootNode() {
|
28 |
0
| return rootNode;
|
29 |
| } |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
0
| public void commitSource(String source, TargetJDKVersion jdk) {
|
36 |
0
| ASTCompilationUnit compilationUnit = jdk.createParser(new StringReader(source)).CompilationUnit();
|
37 |
0
| rootNode = compilationUnit;
|
38 |
0
| fireViewerModelEvent(new ViewerModelEvent(this, ViewerModelEvent.CODE_RECOMPILED));
|
39 |
| } |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
0
| public boolean hasCompiledTree() {
|
47 |
0
| return rootNode != null;
|
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
0
| public void evaluateXPathExpression(String xPath, Object evaluator)
|
57 |
| throws ParseException, JaxenException { |
58 |
0
| XPath xpath = new BaseXPath(xPath, new DocumentNavigator());
|
59 |
0
| evaluationResults = xpath.selectNodes(rootNode);
|
60 |
0
| fireViewerModelEvent(new ViewerModelEvent(evaluator, ViewerModelEvent.PATH_EXPRESSION_EVALUATED));
|
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
0
| public List getLastEvaluationResults() {
|
71 |
0
| return evaluationResults;
|
72 |
| } |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
0
| public void selectNode(SimpleNode node, Object selector) {
|
81 |
0
| fireViewerModelEvent(new ViewerModelEvent(selector, ViewerModelEvent.NODE_SELECTED, node));
|
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
0
| public void appendToXPathExpression(String pathFragment, Object appender) {
|
91 |
0
| fireViewerModelEvent(new ViewerModelEvent(appender, ViewerModelEvent.PATH_EXPRESSION_APPENDED, pathFragment));
|
92 |
| } |
93 |
| |
94 |
0
| public void addViewerModelListener(ViewerModelListener l) {
|
95 |
0
| listeners.add(l);
|
96 |
| } |
97 |
| |
98 |
0
| public void removeViewerModelListener(ViewerModelListener l) {
|
99 |
0
| listeners.remove(l);
|
100 |
| } |
101 |
| |
102 |
0
| protected void fireViewerModelEvent(ViewerModelEvent e) {
|
103 |
0
| for (int i = 0; i < listeners.size(); i++) {
|
104 |
0
| ((ViewerModelListener) listeners.get(i)).viewerModelChanged(e);
|
105 |
| } |
106 |
| } |
107 |
| } |