Class XpathUtil
java.lang.Object
com.puppycrawl.tools.checkstyle.utils.XpathUtil
Contains utility methods for xpath.
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final Pattern
This regexp is used to convert carriage return to carriage-return tag.private static final String
Delimiter to separate xpath results.private static final Pattern
This regexp is used to convert new line to newline tag.private static final BitSet
Token types which support text attribute. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<AbstractNode>
createChildren
(AbstractNode root, AbstractNode parent, DetailAST firstChild) Iterates siblings of the given node and creates new Xpath-nodes.static String
Returns content of the text attribute of the ast element.static List<net.sf.saxon.om.NodeInfo>
getXpathItems
(String xpath, AbstractNode rootNode) Returns list of nodes matching xpath expression given node context.static String
printXpathBranch
(String xpath, File file) Returns xpath query results on file as string.static boolean
Checks, if specified node can have@text
attribute.
-
Field Details
-
TOKEN_TYPES_WITH_TEXT_ATTRIBUTE
Token types which support text attribute. These token types were selected based on analysis that all others do not match required criteria - text attribute of the token must be useful and help to retrieve more precise results. There are three types of AST tokens: 1. Tokens for which the texts are equal to the name of the token. Or in other words, nodes for which the following expression is always true:detailAst.getText().equals(TokenUtil.getTokenName(detailAst.getType()))
For example://MODIFIERS[@text='MODIFIERS'] //OBJBLOCK[@text='OBJBLOCK']
These tokens do not match required criteria because their texts do not carry any additional information, they do not affect the xpath requests and do not help to get more accurate results. The texts of these nodes are useless. No matter what code you analyze, these texts are always the same. In addition, they make xpath queries more complex, less readable and verbose. 2. Tokens for which the texts differ from token names, but texts are always constant. For example://LITERAL_VOID[@text='void'] //RCURLY[@text='}']
These tokens are not used for the same reasons as were described in the previous part. 3. Tokens for which texts are not constant. The texts of these nodes are closely related to a concrete class, method, variable and so on. For example:String greeting = "HelloWorld"; //STRING_LITERAL[@text='HelloWorld']
int year = 2017; //NUM_INT[@text=2017]
int age = 23; //NUM_INT[@text=23]
As you can see sameNUM_INT
token type can have different texts, depending on context.public class MyClass {} //IDENT[@text='MyClass']
Only these tokens support text attribute because they make our xpath queries more accurate. These token types are listed below. -
NEWLINE_TO_TAG
This regexp is used to convert new line to newline tag. -
CARRIAGE_RETURN_TO_TAG
This regexp is used to convert carriage return to carriage-return tag. -
DELIMITER
Delimiter to separate xpath results.
-
-
Constructor Details
-
XpathUtil
private XpathUtil()Stop instances being created.
-
-
Method Details
-
createChildren
public static List<AbstractNode> createChildren(AbstractNode root, AbstractNode parent, DetailAST firstChild) Iterates siblings of the given node and creates new Xpath-nodes.- Parameters:
root
- the root nodeparent
- the parent nodefirstChild
- the first DetailAST- Returns:
- children list
-
supportsTextAttribute
Checks, if specified node can have@text
attribute.- Parameters:
ast
-DetailAst
element- Returns:
- true if element supports
@text
attribute, false otherwise
-
getTextAttributeValue
Returns content of the text attribute of the ast element.- Parameters:
ast
-DetailAst
element- Returns:
- text attribute of the ast element
-
printXpathBranch
public static String printXpathBranch(String xpath, File file) throws CheckstyleException, IOException Returns xpath query results on file as string.- Parameters:
xpath
- query to evaluatefile
- file to run on- Returns:
- all results as string separated by delimiter
- Throws:
CheckstyleException
- if some parsing error happensIOException
- if an error occurs
-
getXpathItems
public static List<net.sf.saxon.om.NodeInfo> getXpathItems(String xpath, AbstractNode rootNode) throws net.sf.saxon.trans.XPathException Returns list of nodes matching xpath expression given node context.- Parameters:
xpath
- Xpath expressionrootNode
-NodeInfo
node context- Returns:
- list of nodes matching xpath expression given node context
- Throws:
net.sf.saxon.trans.XPathException
- if Xpath cannot be parsed
-