1 package com.puppycrawl.tools.checkstyle.checks.xpath; 20 21 import java.util.Iterator ; 22 23 import org.jaxen.BaseXPath; 24 import org.jaxen.JaxenException; 25 import org.jaxen.XPath; 26 27 import antlr.ASTFactory; 28 29 import com.puppycrawl.tools.checkstyle.api.Check; 30 import com.puppycrawl.tools.checkstyle.api.DetailAST; 31 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 32 33 42 public class XPathCheck extends Check 43 { 44 45 private XPath mXPath; 46 47 48 private String mMessage = "illegal.xpath"; 49 50 51 public int[] getDefaultTokens() 52 { 53 return new int[0]; 54 } 55 56 57 public void beginTree(DetailAST aAST) 58 { 59 if (mXPath != null) { 60 final ASTFactory factory = new ASTFactory(); 61 factory.setASTNodeType(DetailAST.class.getName()); 62 final DetailAST root = 64 (DetailAST) factory.create(TokenTypes.EOF, "ROOT"); 65 root.setFirstChild(aAST); 66 try { 67 final Iterator it = mXPath.selectNodes(aAST).iterator(); 68 while (it.hasNext()) { 69 final DetailAST node = (DetailAST) it.next(); 70 log( 71 node.getLineNo(), 72 node.getColumnNo(), 73 mMessage, 74 new String [] {node.getText()}); 75 } 76 } 77 catch (JaxenException e) { 78 e.printStackTrace(); 80 } 81 } 82 } 83 84 88 public void setMessage(String aMessage) 89 { 90 mMessage = aMessage; 91 } 92 93 98 public void setXPath(String aXPath) 99 throws JaxenException 100 { 101 mXPath = new BaseXPath(aXPath, new DocumentNavigator()); 102 } 103 } 104 | Popular Tags |