Class XpathUtil


  • public final class XpathUtil
    extends java.lang.Object
    Contains utility methods for xpath.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.regex.Pattern CARRIAGE_RETURN_TO_TAG
      This regexp is used to convert carriage return to carriage-return tag.
      private static java.lang.String DELIMITER
      Delimiter to separate xpath results.
      private static java.util.regex.Pattern NEWLINE_TO_TAG
      This regexp is used to convert new line to newline tag.
      private static java.util.BitSet TOKEN_TYPES_WITH_TEXT_ATTRIBUTE
      Token types which support text attribute.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private XpathUtil()
      Stop instances being created.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<AbstractNode> createChildren​(AbstractNode root, AbstractNode parent, DetailAST firstChild)
      Iterates siblings of the given node and creates new Xpath-nodes.
      static java.lang.String getTextAttributeValue​(DetailAST ast)
      Returns content of the text attribute of the ast element.
      static java.util.List<net.sf.saxon.om.NodeInfo> getXpathItems​(java.lang.String xpath, AbstractNode rootNode)
      Returns list of nodes matching xpath expression given node context.
      static java.lang.String printXpathBranch​(java.lang.String xpath, java.io.File file)
      Returns xpath query results on file as string.
      static boolean supportsTextAttribute​(DetailAST ast)
      Checks, if specified node can have @text attribute.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TOKEN_TYPES_WITH_TEXT_ATTRIBUTE

        private static final java.util.BitSet TOKEN_TYPES_WITH_TEXT_ATTRIBUTE
        Token types which support text attribute. These token types were selected based on analysis that all others do not match required criteria - text attribute of the token must be useful and help to retrieve more precise results. There are three types of AST tokens: 1. Tokens for which the texts are equal to the name of the token. Or in other words, nodes for which the following expression is always true:
             detailAst.getText().equals(TokenUtil.getTokenName(detailAst.getType()))
         
        For example:
             //MODIFIERS[@text='MODIFIERS']
             //OBJBLOCK[@text='OBJBLOCK']
         
        These tokens do not match required criteria because their texts do not carry any additional information, they do not affect the xpath requests and do not help to get more accurate results. The texts of these nodes are useless. No matter what code you analyze, these texts are always the same. In addition, they make xpath queries more complex, less readable and verbose. 2. Tokens for which the texts differ from token names, but texts are always constant. For example:
             //LITERAL_VOID[@text='void']
             //RCURLY[@text='}']
         
        These tokens are not used for the same reasons as were described in the previous part. 3. Tokens for which texts are not constant. The texts of these nodes are closely related to a concrete class, method, variable and so on. For example:
             String greeting = "HelloWorld";
             //STRING_LITERAL[@text='HelloWorld']
         
             int year = 2017;
             //NUM_INT[@text=2017]
         
             int age = 23;
             //NUM_INT[@text=23]
         
        As you can see same NUM_INT token type can have different texts, depending on context.
             public class MyClass {}
             //IDENT[@text='MyClass']
         
        Only these tokens support text attribute because they make our xpath queries more accurate. These token types are listed below.
      • NEWLINE_TO_TAG

        private static final java.util.regex.Pattern NEWLINE_TO_TAG
        This regexp is used to convert new line to newline tag.
      • CARRIAGE_RETURN_TO_TAG

        private static final java.util.regex.Pattern CARRIAGE_RETURN_TO_TAG
        This regexp is used to convert carriage return to carriage-return tag.
      • DELIMITER

        private static final java.lang.String DELIMITER
        Delimiter to separate xpath results.
    • Constructor Detail

      • XpathUtil

        private XpathUtil()
        Stop instances being created.
    • Method Detail

      • createChildren

        public static java.util.List<AbstractNodecreateChildren​(AbstractNode root,
                                                                  AbstractNode parent,
                                                                  DetailAST firstChild)
        Iterates siblings of the given node and creates new Xpath-nodes.
        Parameters:
        root - the root node
        parent - the parent node
        firstChild - the first DetailAST
        Returns:
        children list
      • supportsTextAttribute

        public static boolean supportsTextAttribute​(DetailAST ast)
        Checks, if specified node can have @text attribute.
        Parameters:
        ast - DetailAst element
        Returns:
        true if element supports @text attribute, false otherwise
      • getTextAttributeValue

        public static java.lang.String getTextAttributeValue​(DetailAST ast)
        Returns content of the text attribute of the ast element.
        Parameters:
        ast - DetailAst element
        Returns:
        text attribute of the ast element
      • printXpathBranch

        public static java.lang.String printXpathBranch​(java.lang.String xpath,
                                                        java.io.File file)
                                                 throws CheckstyleException,
                                                        java.io.IOException
        Returns xpath query results on file as string.
        Parameters:
        xpath - query to evaluate
        file - file to run on
        Returns:
        all results as string separated by delimiter
        Throws:
        CheckstyleException - if some parsing error happens
        java.io.IOException - if an error occurs
      • getXpathItems

        public static java.util.List<net.sf.saxon.om.NodeInfo> getXpathItems​(java.lang.String xpath,
                                                                             AbstractNode rootNode)
                                                                      throws net.sf.saxon.trans.XPathException
        Returns list of nodes matching xpath expression given node context.
        Parameters:
        xpath - Xpath expression
        rootNode - NodeInfo node context
        Returns:
        list of nodes matching xpath expression given node context
        Throws:
        net.sf.saxon.trans.XPathException - if Xpath cannot be parsed