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 * The model for the viewer gui
19 *
20 * <p>
21 * This is the model part of MVC
22 * </p>
23 *
24 * @author Boris Gruschko ( boris at gruschko.org )
25 * @version $Id: ViewerModel.java,v 1.4 2004/04/12 17:35:09 tomcopeland Exp $
26 */
27 public class ViewerModel
28 {
29 private Vector listeners;
30 private SimpleNode rootNode;
31 private List evaluationResults;
32
33 /***
34 * constructs the model
35 */
36 public ViewerModel( )
37 {
38 listeners = new Vector( 5 );
39 }
40
41 /***
42 * Retrieves AST's root node
43 *
44 * @return AST's root node
45 */
46 public SimpleNode getRootNode( )
47 {
48 return rootNode;
49 }
50
51 /***
52 * commits source code to the model.
53 *
54 * <p>
55 * all existing source will be replaced
56 * </p>
57 *
58 * @param source source to be commited
59 */
60 public void commitSource( String source )
61 {
62 ASTCompilationUnit compilationUnit = new TargetJDK1_4().createParser(new StringReader( source )).CompilationUnit( );
63 rootNode = compilationUnit;
64 fireViewerModelEvent(
65 new ViewerModelEvent( this, ViewerModelEvent.CODE_RECOMPILED ) );
66 }
67
68 /***
69 * determines wheteher the model has a compiled tree at it's disposal
70 *
71 * @return true if there is an AST, false otherwise
72 */
73 public boolean hasCompiledTree( )
74 {
75 return rootNode != null;
76 }
77
78 /***
79 * evaluates the given XPath expression against the current tree
80 *
81 * @param xPath XPath expression to be evaluated
82 * @param evaluator object which requests the evaluation
83 */
84 public void evaluateXPathExpression( String xPath, Object evaluator )
85 throws ParseException, JaxenException
86 {
87 XPath xpath = new BaseXPath( xPath, new DocumentNavigator( ) );
88
89 evaluationResults = xpath.selectNodes( rootNode );
90
91 fireViewerModelEvent(
92 new ViewerModelEvent(
93 evaluator, ViewerModelEvent.PATH_EXPRESSION_EVALUATED ) );
94 }
95
96 /***
97 * retrieves the results of last evaluation
98 *
99 * @return a list containing the nodes selected by the last XPath expression
100 * evaluation
101 */
102 public List getLastEvaluationResults( )
103 {
104 return evaluationResults;
105 }
106
107 /***
108 * selects the given node in the AST
109 *
110 * @param node node to be selected
111 * @param selector object which requests the selection
112 */
113 public void selectNode( SimpleNode node, Object selector )
114 {
115 fireViewerModelEvent(
116 new ViewerModelEvent( selector, ViewerModelEvent.NODE_SELECTED, node ) );
117 }
118
119 /***
120 * appends the given fragment to the XPath expression
121 *
122 * @param pathFragment fragment to be added
123 * @param appender object that is trying to append the fragment
124 */
125 public void appendToXPathExpression( String pathFragment, Object appender )
126 {
127 fireViewerModelEvent(
128 new ViewerModelEvent(
129 appender, ViewerModelEvent.PATH_EXPRESSION_APPENDED, pathFragment ) );
130 }
131
132 /***
133 * adds a listener to the model
134 *
135 * @param l listener to be added
136 */
137 public void addViewerModelListener( ViewerModelListener l )
138 {
139 listeners.add( l );
140 }
141
142 /***
143 * removes the lisetener from the model
144 *
145 * @param l listener to be removed
146 */
147 public void removeViewerModelListener( ViewerModelListener l )
148 {
149 listeners.remove( l );
150 }
151
152 /***
153 * notifes all listener of a change in the model
154 *
155 * @param e change's reason
156 */
157 protected void fireViewerModelEvent( ViewerModelEvent e )
158 {
159 for ( int i = 0; i < listeners.size( ); i++ )
160 {
161 ( (ViewerModelListener)listeners.elementAt( i ) ).viewerModelChanged( e );
162 }
163 }
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193