View Javadoc
1 package net.sourceforge.pmd.util.viewer.model; 2 3 import net.sourceforge.pmd.ast.ASTCompilationUnit; 4 import net.sourceforge.pmd.ast.JavaParser; 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.2 2003/09/23 20:51:06 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 StringReader reader = new StringReader( source ); 63 64 JavaParser parser = new JavaParser( reader ); 65 66 ASTCompilationUnit compilationUnit = parser.CompilationUnit( ); 67 68 rootNode = compilationUnit; 69 70 fireViewerModelEvent( 71 new ViewerModelEvent( this, ViewerModelEvent.CODE_RECOMPILED ) ); 72 } 73 74 /*** 75 * determines wheteher the model has a compiled tree at it's disposal 76 * 77 * @return true if there is an AST, false otherwise 78 */ 79 public boolean hasCompiledTree( ) 80 { 81 return rootNode != null; 82 } 83 84 /*** 85 * evaluates the given XPath expression against the current tree 86 * 87 * @param xPath XPath expression to be evaluated 88 * @param evaluator object which requests the evaluation 89 */ 90 public void evaluateXPathExpression( String xPath, Object evaluator ) 91 throws ParseException, JaxenException 92 { 93 XPath xpath = new BaseXPath( xPath, new DocumentNavigator( ) ); 94 95 evaluationResults = xpath.selectNodes( rootNode ); 96 97 fireViewerModelEvent( 98 new ViewerModelEvent( 99 evaluator, ViewerModelEvent.PATH_EXPRESSION_EVALUATED ) ); 100 } 101 102 /*** 103 * retrieves the results of last evaluation 104 * 105 * @return a list containing the nodes selected by the last XPath expression 106 * evaluation 107 */ 108 public List getLastEvaluationResults( ) 109 { 110 return evaluationResults; 111 } 112 113 /*** 114 * selects the given node in the AST 115 * 116 * @param node node to be selected 117 * @param selector object which requests the selection 118 */ 119 public void selectNode( SimpleNode node, Object selector ) 120 { 121 fireViewerModelEvent( 122 new ViewerModelEvent( selector, ViewerModelEvent.NODE_SELECTED, node ) ); 123 } 124 125 /*** 126 * appends the given fragment to the XPath expression 127 * 128 * @param pathFragment fragment to be added 129 * @param appender object that is trying to append the fragment 130 */ 131 public void appendToXPathExpression( String pathFragment, Object appender ) 132 { 133 fireViewerModelEvent( 134 new ViewerModelEvent( 135 appender, ViewerModelEvent.PATH_EXPRESSION_APPENDED, pathFragment ) ); 136 } 137 138 /*** 139 * adds a listener to the model 140 * 141 * @param l listener to be added 142 */ 143 public void addViewerModelListener( ViewerModelListener l ) 144 { 145 listeners.add( l ); 146 } 147 148 /*** 149 * removes the lisetener from the model 150 * 151 * @param l listener to be removed 152 */ 153 public void removeViewerModelListener( ViewerModelListener l ) 154 { 155 listeners.remove( l ); 156 } 157 158 /*** 159 * notifes all listener of a change in the model 160 * 161 * @param e change's reason 162 */ 163 protected void fireViewerModelEvent( ViewerModelEvent e ) 164 { 165 for ( int i = 0; i < listeners.size( ); i++ ) 166 { 167 ( (ViewerModelListener)listeners.elementAt( i ) ).viewerModelChanged( e ); 168 } 169 } 170 } 171 172 173 /* 174 * $Log: ViewerModel.java,v $ 175 * Revision 1.2 2003/09/23 20:51:06 tomcopeland 176 * Cleaned up imports 177 * 178 * Revision 1.1 2003/09/23 20:32:42 tomcopeland 179 * Added Boris Gruschko's new AST/XPath viewer 180 * 181 * Revision 1.1 2003/09/24 01:33:03 bgr 182 * moved to a new package 183 * 184 * Revision 1.3 2003/09/24 00:40:35 bgr 185 * evaluation results browsing added 186 * 187 * Revision 1.2 2003/09/23 07:52:16 bgr 188 * menus added 189 * 190 * Revision 1.1 2003/09/22 05:21:54 bgr 191 * initial commit 192 * 193 */

This page was automatically generated by Maven