|
1 |
| package net.sourceforge.pmd.util.viewer.model; |
|
2 |
| |
|
3 |
| import net.sourceforge.pmd.TargetJDK1_4; |
|
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 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| public class ViewerModel { |
|
31 |
| private Vector listeners; |
|
32 |
| private SimpleNode rootNode; |
|
33 |
| private List evaluationResults; |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
0
| public ViewerModel() {
|
|
39 |
0
| listeners = new Vector(5);
|
|
40 |
| } |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
0
| public SimpleNode getRootNode() {
|
|
48 |
0
| return rootNode;
|
|
49 |
| } |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
0
| public void commitSource(String source) {
|
|
65 |
0
| ASTCompilationUnit compilationUnit = new TargetJDK1_4().createParser(new StringReader(source)).CompilationUnit();
|
|
66 |
0
| rootNode = compilationUnit;
|
|
67 |
0
| fireViewerModelEvent(new ViewerModelEvent(this, ViewerModelEvent.CODE_RECOMPILED));
|
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
0
| public boolean hasCompiledTree() {
|
|
76 |
0
| return rootNode != null;
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
0
| public void evaluateXPathExpression(String xPath, Object evaluator)
|
|
86 |
| throws ParseException, JaxenException { |
|
87 |
0
| XPath xpath = new BaseXPath(xPath, new DocumentNavigator());
|
|
88 |
0
| evaluationResults = xpath.selectNodes(rootNode);
|
|
89 |
0
| fireViewerModelEvent(new ViewerModelEvent(evaluator, ViewerModelEvent.PATH_EXPRESSION_EVALUATED));
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
0
| public List getLastEvaluationResults() {
|
|
100 |
0
| return evaluationResults;
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
0
| public void selectNode(SimpleNode node, Object selector) {
|
|
110 |
0
| fireViewerModelEvent(new ViewerModelEvent(selector, ViewerModelEvent.NODE_SELECTED, node));
|
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
0
| public void appendToXPathExpression(String pathFragment, Object appender) {
|
|
120 |
0
| fireViewerModelEvent(new ViewerModelEvent(appender, ViewerModelEvent.PATH_EXPRESSION_APPENDED, pathFragment));
|
|
121 |
| } |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
0
| public void addViewerModelListener(ViewerModelListener l) {
|
|
129 |
0
| listeners.add(l);
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
0
| public void removeViewerModelListener(ViewerModelListener l) {
|
|
138 |
0
| listeners.remove(l);
|
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
0
| protected void fireViewerModelEvent(ViewerModelEvent e) {
|
|
147 |
0
| for (int i = 0; i < listeners.size(); i++) {
|
|
148 |
0
| ((ViewerModelListener) listeners.elementAt(i)).viewerModelChanged(e);
|
|
149 |
| } |
|
150 |
| } |
|
151 |
| } |