Class XpathUtil


  • public final class XpathUtil
    extends Object
    Contains utility methods for xpath.
    • Field Detail

      • TOKEN_TYPES_WITH_TEXT_ATTRIBUTE

        private static final Set<Integer> TOKEN_TYPES_WITH_TEXT_ATTRIBUTE
        List of 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 Pattern NEWLINE_TO_TAG
        This regexp is used to convert new line to newline tag.
      • CARRIAGE_RETURN_TO_TAG

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

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

      • XpathUtil

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

      • 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 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
      • getXpathItems

        public static List<net.sf.saxon.om.NodeInfo> getXpathItems​(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