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 int
The column number of the element for which the query should be generated.private final FileText
TheFileText
object, representing content of the file.private final int
The line number of the element for which the query should be generated.private final DetailAST
The root ast.private final int
The distance between tab stop position.private final int
The 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 newXpathQueryGenerator
instance.XpathQueryGenerator
(DetailAST rootAst, int lineNumber, int columnNumber, FileText fileText, int tabWidth) Creates a newXpathQueryGenerator
instance.XpathQueryGenerator
(TreeWalkerAuditEvent event, int tabWidth) Creates a newXpathQueryGenerator
instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static String
Escape <, >, &, ' and " as their entities.private static String
encodeCharacter
(char chr) Encodes escape character for Xpath.private int
Returns the column number with tabs expanded.private static DetailAST
Returns childDetailAst
element of the given root, which has text attribute.private static DetailAST
Returns childDetailAst
element of the given root, which has text attribute.private static int
Finds position of the ast element among siblings.generate()
Returns list of xpath queries of nodes, matching line number, column number and token type.static String
Returns full xpath query for given ast element.Returns list of nodes matching defined line number, column number and token type.private static String
getXpathQuery
(DetailAST root, DetailAST ast) Returns relative xpath query for given ast element from root.private static boolean
Checks if the given ast element has uniqueTokenTypes
among siblings.private boolean
Checks if the givenDetailAST
node is matching line number, column number and token type.private static boolean
Checks 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
TheFileText
object, representing content of the file. -
tabWidth
The distance between tab stop position.
-
-
Constructor Details
-
XpathQueryGenerator
Creates a newXpathQueryGenerator
instance.- Parameters:
event
-TreeWalkerAuditEvent
objecttabWidth
- distance between tab stop position
-
XpathQueryGenerator
public XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, FileText fileText, int tabWidth) Creates a newXpathQueryGenerator
instance.- 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
- theFileText
objecttabWidth
- distance between tab stop position
-
XpathQueryGenerator
public XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, int tokenType, FileText fileText, int tabWidth) Creates a newXpathQueryGenerator
instance.- 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
- theFileText
objecttabWidth
- 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 childDetailAst
element of the given root, which has text attribute.- Parameters:
root
-DetailAST
root ast- Returns:
- child
DetailAst
element of the given root
-
findChildWithTextAttributeRecursively
Returns childDetailAst
element of the given root, which has text attribute. Performs search recursively inside node's subtree.- Parameters:
root
-DetailAST
root ast- Returns:
- child
DetailAst
element of the given root
-
generateXpathQuery
Returns full xpath query for given ast element.- Parameters:
ast
-DetailAST
ast element- Returns:
- full xpath query for given ast element
-
findPositionAmongSiblings
Finds position of the ast element among siblings.- Parameters:
ast
-DetailAST
ast element- Returns:
- position of the ast element
-
isXpathQueryForNodeIsAccurateEnough
Checks if ast element has all requirements to have unique xpath query.- Parameters:
ast
-DetailAST
ast 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
-DetailAST
root elementast
-DetailAST
ast element- Returns:
- relative xpath query for given ast element from root
-
hasAtLeastOneSiblingWithSameTokenType
Checks if the given ast element has uniqueTokenTypes
among siblings.- Parameters:
ast
-DetailAST
ast element- Returns:
- if the given ast element has unique
TokenTypes
among siblings
-
expandedTabColumn
Returns the column number with tabs expanded.- Parameters:
ast
-DetailAST
root ast- Returns:
- the column number with tabs expanded
-
isMatchingByLineAndColumnAndTokenType
Checks if the givenDetailAST
node is matching line number, column number and token type.- Parameters:
ast
-DetailAST
ast element- Returns:
- true if the given
DetailAST
node 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.
-