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 public ViewerModel() { 24 listeners = new ArrayList (5); 25 } 26 27 public SimpleNode getRootNode() { 28 return rootNode; 29 } 30 31 35 public void commitSource(String source, TargetJDKVersion jdk) { 36 ASTCompilationUnit compilationUnit = jdk.createParser(new StringReader (source)).CompilationUnit(); 37 rootNode = compilationUnit; 38 fireViewerModelEvent(new ViewerModelEvent(this, ViewerModelEvent.CODE_RECOMPILED)); 39 } 40 41 46 public boolean hasCompiledTree() { 47 return rootNode != null; 48 } 49 50 56 public void evaluateXPathExpression(String xPath, Object 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 70 public List getLastEvaluationResults() { 71 return evaluationResults; 72 } 73 74 80 public void selectNode(SimpleNode node, Object selector) { 81 fireViewerModelEvent(new ViewerModelEvent(selector, ViewerModelEvent.NODE_SELECTED, node)); 82 } 83 84 90 public void appendToXPathExpression(String pathFragment, Object 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 |