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.

Since:
8.39
  • Field Details

    • MSG_KEY

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

      private String query
      Specify Xpath query.
    • xpathExpression

      private net.sf.saxon.sxpath.XPathExpression xpathExpression
      Xpath expression.
  • Constructor Details

    • MatchXpathCheck

      public MatchXpathCheck()
  • Method Details

    • setQuery

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

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • 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:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • isCommentNodesRequired

      public boolean isCommentNodesRequired()
      Description copied from class: AbstractCheck
      Whether comment nodes are required or not.
      Overrides:
      isCommentNodesRequired in class AbstractCheck
      Returns:
      false as a default value.
    • 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 List<DetailAST> findMatchingNodesByXpathQuery(DetailAST rootAST)
      Find nodes that match query.
      Parameters:
      rootAST - root node
      Returns:
      list of matching nodes
      Throws:
      IllegalStateException - if evaluation of xpath query fails