Class XpathQueryGenerator
java.lang.Object
com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator
Generates xpath queries. Xpath queries are generated based on received
DetailAst element, line number, column number and token type.
Token type parameter is optional.
Example class
public class Main {
public String sayHello(String name) {
return "Hello, " + name;
}
}
Following expression returns list of queries. Each query is the string representing full path to the node inside Xpath tree, whose line number is 3 and column number is 4.
new XpathQueryGenerator(rootAst, 3, 4).generate();
Result list
- /COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='Main']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='sayHello']]
- /COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='Main']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='sayHello']] /MODIFIERS
- /COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='Main']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='sayHello']] /MODIFIERS/LITERAL_PUBLIC
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intThe column number of the element for which the query should be generated.private final FileTextTheFileTextobject, representing content of the file.private final intThe line number of the element for which the query should be generated.private final DetailASTThe root ast.private final intThe distance between tab stop position.private final intThe token type of the element for which the query should be generated. -
Constructor Summary
ConstructorsConstructorDescriptionXpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, int tokenType, FileText fileText, int tabWidth) Creates a newXpathQueryGeneratorinstance.XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, FileText fileText, int tabWidth) Creates a newXpathQueryGeneratorinstance.XpathQueryGenerator(TreeWalkerAuditEvent event, int tabWidth) Creates a newXpathQueryGeneratorinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringEscape <, >, &, ' and " as their entities.private static StringencodeCharacter(char chr) Encodes escape character for Xpath.private intReturns the column number with tabs expanded.private static DetailASTReturns childDetailAstelement of the given root, which has text attribute.private static DetailASTReturns childDetailAstelement of the given root, which has text attribute.private static intFinds position of the ast element among siblings.generate()Returns list of xpath queries of nodes, matching line number, column number and token type.static StringReturns full xpath query for given ast element.Returns list of nodes matching defined line number, column number and token type.private static StringgetXpathQuery(DetailAST root, DetailAST ast) Returns relative xpath query for given ast element from root.private static booleanChecks if the given ast element has uniqueTokenTypesamong siblings.private booleanChecks if the givenDetailASTnode is matching line number, column number and token type.private static booleanChecks if ast element has all requirements to have unique xpath query.
-
Field Details
-
rootAst
The root ast. -
lineNumber
The line number of the element for which the query should be generated. -
columnNumber
The column number of the element for which the query should be generated. -
tokenType
The token type of the element for which the query should be generated. Optional. -
fileText
TheFileTextobject, representing content of the file. -
tabWidth
The distance between tab stop position.
-
-
Constructor Details
-
XpathQueryGenerator
Creates a newXpathQueryGeneratorinstance.- Parameters:
event-TreeWalkerAuditEventobjecttabWidth- distance between tab stop position
-
XpathQueryGenerator
public XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, FileText fileText, int tabWidth) Creates a newXpathQueryGeneratorinstance.- Parameters:
rootAst- root astlineNumber- line number of the element for which the query should be generatedcolumnNumber- column number of the element for which the query should be generatedfileText- theFileTextobjecttabWidth- distance between tab stop position
-
XpathQueryGenerator
public XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, int tokenType, FileText fileText, int tabWidth) Creates a newXpathQueryGeneratorinstance.- Parameters:
rootAst- root astlineNumber- line number of the element for which the query should be generatedcolumnNumber- column number of the element for which the query should be generatedtokenType- token type of the element for which the query should be generatedfileText- theFileTextobjecttabWidth- distance between tab stop position
-
-
Method Details
-
generate
Returns list of xpath queries of nodes, matching line number, column number and token type. This approach uses DetailAST traversal. DetailAST means detail abstract syntax tree.- Returns:
- list of xpath queries of nodes, matching line number, column number and token type
-
findChildWithTextAttribute
Returns childDetailAstelement of the given root, which has text attribute.- Parameters:
root-DetailASTroot ast- Returns:
- child
DetailAstelement of the given root
-
findChildWithTextAttributeRecursively
Returns childDetailAstelement of the given root, which has text attribute. Performs search recursively inside node's subtree.- Parameters:
root-DetailASTroot ast- Returns:
- child
DetailAstelement of the given root
-
generateXpathQuery
Returns full xpath query for given ast element.- Parameters:
ast-DetailASTast element- Returns:
- full xpath query for given ast element
-
findPositionAmongSiblings
Finds position of the ast element among siblings.- Parameters:
ast-DetailASTast element- Returns:
- position of the ast element
-
isXpathQueryForNodeIsAccurateEnough
Checks if ast element has all requirements to have unique xpath query.- Parameters:
ast-DetailASTast element- Returns:
- true if ast element will have unique xpath query, false otherwise
-
getMatchingAstElements
Returns list of nodes matching defined line number, column number and token type.- Returns:
- list of nodes matching defined line number, column number and token type
-
getXpathQuery
Returns relative xpath query for given ast element from root.- Parameters:
root-DetailASTroot elementast-DetailASTast element- Returns:
- relative xpath query for given ast element from root
-
hasAtLeastOneSiblingWithSameTokenType
Checks if the given ast element has uniqueTokenTypesamong siblings.- Parameters:
ast-DetailASTast element- Returns:
- if the given ast element has unique
TokenTypesamong siblings
-
expandedTabColumn
Returns the column number with tabs expanded.- Parameters:
ast-DetailASTroot ast- Returns:
- the column number with tabs expanded
-
isMatchingByLineAndColumnAndTokenType
Checks if the givenDetailASTnode is matching line number, column number and token type.- Parameters:
ast-DetailASTast element- Returns:
- true if the given
DetailASTnode is matching
-
encode
Escape <, >, &, ' and " as their entities. Custom method for Xpath generation to maintain compatibility with Saxon and encoding outside Ascii range characters.According to Saxon documentation:
From Saxon 7.1, string delimiters can be doubled within the string to represent` the delimiter itself: for example select='"He said, ""Go!"""'.Guava cannot as Guava encoding does not meet our requirements like double encoding for apos, removed slashes which are basic requirements for Saxon to decode.
- Parameters:
value- the value to escape.- Returns:
- the escaped value if necessary.
-
encodeCharacter
Encodes escape character for Xpath. Escape characters need '&' before, but it also requires XML 1.1 until #5168.- Parameters:
chr- Character to check.- Returns:
- String, Encoded string.
-