public class XpathQueryGenerator extends java.lang.Object
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
| Modifier and Type | Field and Description |
|---|---|
private int |
columnNumber
The column number of the element for which the query should be generated.
|
private FileText |
fileText
The
FileText object, representing content of the file. |
private int |
lineNumber
The line number of the element for which the query should be generated.
|
private DetailAST |
rootAst
The root ast.
|
private int |
tabWidth
The distance between tab stop position.
|
private int |
tokenType
The token type of the element for which the query should be generated.
|
| Constructor and Description |
|---|
XpathQueryGenerator(DetailAST rootAst,
int lineNumber,
int columnNumber,
FileText fileText,
int tabWidth)
Creates a new
XpathQueryGenerator instance. |
XpathQueryGenerator(DetailAST rootAst,
int lineNumber,
int columnNumber,
int tokenType,
FileText fileText,
int tabWidth)
Creates a new
XpathQueryGenerator instance. |
XpathQueryGenerator(TreeWalkerAuditEvent event,
int tabWidth)
Creates a new
XpathQueryGenerator instance. |
| Modifier and Type | Method and Description |
|---|---|
private static java.lang.String |
encode(java.lang.String value)
Escape <, >, &, ' and " as their entities.
|
private static java.lang.String |
encodeCharacter(char chr)
Encodes escape character for Xpath.
|
private int |
expandedTabColumn(DetailAST ast)
Returns the column number with tabs expanded.
|
private static DetailAST |
findChildWithTextAttribute(DetailAST root)
Returns child
DetailAst element of the given root, which has text attribute. |
private static DetailAST |
findChildWithTextAttributeRecursively(DetailAST root)
Returns child
DetailAst element of the given root, which has text attribute. |
private static int |
findPositionAmongSiblings(DetailAST ast)
Finds position of the ast element among siblings.
|
java.util.List<java.lang.String> |
generate()
Returns list of xpath queries of nodes, matching line number, column number and token type.
|
static java.lang.String |
generateXpathQuery(DetailAST ast)
Returns full xpath query for given ast element.
|
private java.util.List<DetailAST> |
getMatchingAstElements()
Returns list of nodes matching defined line number, column number and token type.
|
private static java.lang.String |
getXpathQuery(DetailAST root,
DetailAST ast)
Returns relative xpath query for given ast element from root.
|
private static boolean |
hasAtLeastOneSiblingWithSameTokenType(DetailAST ast)
Checks if the given ast element has unique
TokenTypes among siblings. |
private boolean |
isMatchingByLineAndColumnAndTokenType(DetailAST ast)
Checks if the given
DetailAST node is matching line number, column number and token
type. |
private static boolean |
isXpathQueryForNodeIsAccurateEnough(DetailAST ast)
Checks if ast element has all requirements to have unique xpath query.
|
private final int lineNumber
private final int columnNumber
private final int tokenType
private final int tabWidth
public XpathQueryGenerator(TreeWalkerAuditEvent event, int tabWidth)
XpathQueryGenerator instance.event - TreeWalkerAuditEvent objecttabWidth - distance between tab stop positionpublic XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, FileText fileText, int tabWidth)
XpathQueryGenerator instance.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 - the FileText objecttabWidth - distance between tab stop positionpublic XpathQueryGenerator(DetailAST rootAst, int lineNumber, int columnNumber, int tokenType, FileText fileText, int tabWidth)
XpathQueryGenerator instance.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 - the FileText objecttabWidth - distance between tab stop positionpublic java.util.List<java.lang.String> generate()
private static DetailAST findChildWithTextAttribute(DetailAST root)
DetailAst element of the given root, which has text attribute.root - DetailAST root astDetailAst element of the given rootprivate static DetailAST findChildWithTextAttributeRecursively(DetailAST root)
DetailAst element of the given root, which has text attribute.
Performs search recursively inside node's subtree.root - DetailAST root astDetailAst element of the given rootpublic static java.lang.String generateXpathQuery(DetailAST ast)
ast - DetailAST ast elementprivate static int findPositionAmongSiblings(DetailAST ast)
ast - DetailAST ast elementprivate static boolean isXpathQueryForNodeIsAccurateEnough(DetailAST ast)
ast - DetailAST ast elementprivate java.util.List<DetailAST> getMatchingAstElements()
private static java.lang.String getXpathQuery(DetailAST root, DetailAST ast)
root - DetailAST root elementast - DetailAST ast elementprivate static boolean hasAtLeastOneSiblingWithSameTokenType(DetailAST ast)
TokenTypes among siblings.ast - DetailAST ast elementTokenTypes among siblingsprivate int expandedTabColumn(DetailAST ast)
ast - DetailAST root astprivate boolean isMatchingByLineAndColumnAndTokenType(DetailAST ast)
DetailAST node is matching line number, column number and token
type.ast - DetailAST ast elementDetailAST node is matchingprivate static java.lang.String encode(java.lang.String value)
According to Saxon documentation in http://saxon.sourceforge.net/saxon7.1/expressions.html 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.
value - the value to escape.private static java.lang.String encodeCharacter(char chr)
chr - Character to check.Copyright © 2001-2022. All Rights Reserved.