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.
- Since:
- 8.39
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled 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.booleanWhether comment nodes are required or not.voidSetter 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, visitTokenMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods 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:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
isCommentNodesRequired
Description copied from class:AbstractCheckWhether comment nodes are required or not.- Overrides:
isCommentNodesRequiredin classAbstractCheck- Returns:
- false as a default value.
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein 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
-