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
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 askey
attribute and desired message asvalue
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
Fields Modifier and Type Field Description static String
MSG_KEY
A key is pointing to the warning message text provided by user.private String
query
Specify Xpath query.private net.sf.saxon.sxpath.XPathExpression
xpathExpression
Xpath expression.
-
Constructor Summary
Constructors Constructor Description MatchXpathCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginTree(DetailAST rootAST)
Called before the starting to process a tree.private List<DetailAST>
findMatchingNodesByXpathQuery(DetailAST rootAST)
Find nodes that match query.int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.boolean
isCommentNodesRequired()
Whether comment nodes are required or not.void
setQuery(String query)
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 Detail
-
MSG_KEY
public static final String MSG_KEY
A key is pointing to the warning message text provided by user.- See Also:
- Constant Field Values
-
xpathExpression
private net.sf.saxon.sxpath.XPathExpression xpathExpression
Xpath expression.
-
-
Constructor Detail
-
MatchXpathCheck
public MatchXpathCheck()
-
-
Method Detail
-
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 classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
isCommentNodesRequired
public boolean isCommentNodesRequired()
Description copied from class:AbstractCheck
Whether comment nodes are required or not.- Overrides:
isCommentNodesRequired
in classAbstractCheck
- 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 classAbstractCheck
- 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
-
-