Class MatchXpathCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.MatchXpathCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
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 isjava.lang.String
. Default value is""
.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
matchxpath.match
- Since:
- 8.39
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Called before the starting to process a tree.findMatchingNodesByXpathQuery
(DetailAST rootAST) Find nodes that match query.int[]
The configurable token set.int[]
Returns the default token a check is interested in.int[]
The tokens that this check must be registered for.boolean
Whether comment nodes are required or not.void
Setter to specify Xpath query.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens, visitToken
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text provided by user.- See Also:
-
query
Specify Xpath query. -
xpathExpression
Xpath expression.
-
-
Constructor Details
-
MatchXpathCheck
public MatchXpathCheck()
-
-
Method Details
-
setQuery
Setter to specify Xpath query.- Parameters:
query
- Xpath query.- Throws:
IllegalStateException
- if creation of xpath expression fails- Since:
- 8.39
-
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 classAbstractCheck
- Returns:
- the default tokens
- See Also:
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
-
isCommentNodesRequired
Description copied from class:AbstractCheck
Whether comment nodes are required or not.- Overrides:
isCommentNodesRequired
in classAbstractCheck
- Returns:
- false as a default value.
-
beginTree
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 classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
findMatchingNodesByXpathQuery
Find nodes that match query.- Parameters:
rootAST
- root node- Returns:
- list of matching nodes
- Throws:
IllegalStateException
- if evaluation of xpath query fails
-