Class XpathQueryGenerator

java.lang.Object
com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator

public class XpathQueryGenerator extends Object
Generates xpath queries. Xpath queries are generated based on received DetailAst element, line number, column number and token type. Token type parameter is optional.

Example class

 public class Main {

     public String sayHello(String name) {
         return "Hello, " + name;
     }
 }
 

Following expression returns list of queries. Each query is the string representing full path to the node inside Xpath tree, whose line number is 3 and column number is 4.

     new XpathQueryGenerator(rootAst, 3, 4).generate();
 

Result list

  • /CLASS_DEF[./IDENT[@text='Main']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='sayHello']]
  • /CLASS_DEF[./IDENT[@text='Main']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='sayHello']] /MODIFIERS
  • /CLASS_DEF[./IDENT[@text='Main']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='sayHello']] /MODIFIERS/LITERAL_PUBLIC
  • Constructor Details

    • XpathQueryGenerator

      public XpathQueryGenerator(TreeWalkerAuditEvent event, int tabWidth)
      Creates a new XpathQueryGenerator instance.
      Parameters:
      event - TreeWalkerAuditEvent object
      tabWidth - distance between tab stop position
    • XpathQueryGenerator

      public XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, FileText fileText, int tabWidth)
      Creates a new XpathQueryGenerator instance.
      Parameters:
      rootAst - root ast
      lineNumber - line number of the element for which the query should be generated
      columnNumber - column number of the element for which the query should be generated
      fileText - the FileText object
      tabWidth - distance between tab stop position
    • XpathQueryGenerator

      public XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, int tokenType, FileText fileText, int tabWidth)
      Creates a new XpathQueryGenerator instance.
      Parameters:
      rootAst - root ast
      lineNumber - line number of the element for which the query should be generated
      columnNumber - column number of the element for which the query should be generated
      tokenType - token type of the element for which the query should be generated
      fileText - the FileText object
      tabWidth - distance between tab stop position
  • Method Details

    • generate

      public List<String> generate()
      Returns list of xpath queries of nodes, matching line number, column number and token type. This approach uses DetailAST traversal. DetailAST means detail abstract syntax tree.
      Returns:
      list of xpath queries of nodes, matching line number, column number and token type
    • generateXpathQuery

      public static String generateXpathQuery(DetailAST ast)
      Returns full xpath query for given ast element.
      Parameters:
      ast - DetailAST ast element
      Returns:
      full xpath query for given ast element