Class MatchXpathCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class MatchXpathCheck
    extends AbstractCheck

    Evaluates Xpath query and report violation on all matching AST nodes. This check allows user to implement custom checks using Xpath. If Xpath query is not specified explicitly, then the check does nothing.

    It is recommended to define custom message for violation to explain what is not allowed and what to use instead, default message might be too abstract. To customize a message you need to add message element with matchxpath.match as key attribute and desired message as value attribute.

    Please read more about Xpath syntax at Xpath Syntax. Information regarding Xpath functions can be found at XSLT/XPath Reference. Note, that @text attribute can be used only with token types that are listed in XpathUtil.

    • Property query - Specify Xpath query. Type is java.lang.String. Default value is "".

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • matchxpath.match
    Since:
    8.39
    • Field Detail

      • MSG_KEY

        public static final java.lang.String MSG_KEY
        A key is pointing to the warning message text provided by user.
        See Also:
        Constant Field Values
      • query

        private java.lang.String query
        Specify Xpath query.
      • xpathExpression

        private net.sf.saxon.sxpath.XPathExpression xpathExpression
        Xpath expression.
    • Method Detail

      • setQuery

        public void setQuery​(java.lang.String query)
        Setter to specify Xpath query.
        Parameters:
        query - Xpath query.
        Throws:
        java.lang.IllegalStateException - if creation of xpath expression fails
        Since:
        8.39
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • beginTree

        public void beginTree​(DetailAST rootAST)
        Description copied from class: AbstractCheck
        Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.
        Overrides:
        beginTree in class AbstractCheck
        Parameters:
        rootAST - the root of the tree
      • findMatchingNodesByXpathQuery

        private java.util.List<DetailASTfindMatchingNodesByXpathQuery​(DetailAST rootAST)
        Find nodes that match query.
        Parameters:
        rootAST - root node
        Returns:
        list of matching nodes
        Throws:
        java.lang.IllegalStateException - if evaluation of xpath query fails