use of com.puppycrawl.tools.checkstyle.xpath.RootNode in project checkstyle by checkstyle.
the class MatchXpathCheck method findMatchingNodesByXpathQuery.
/**
* Find nodes that match query.
*
* @param rootAST root node
* @return list of matching nodes
* @throws IllegalStateException if evaluation of xpath query fails
*/
private List<DetailAST> findMatchingNodesByXpathQuery(DetailAST rootAST) {
try {
final RootNode rootNode = new RootNode(rootAST);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> matchingItems = xpathExpression.evaluate(xpathDynamicContext);
return matchingItems.stream().map(item -> ((AbstractNode) item).getUnderlyingNode()).collect(Collectors.toList());
} catch (XPathException ex) {
throw new IllegalStateException("Evaluation of Xpath query failed: " + query, ex);
}
}
use of com.puppycrawl.tools.checkstyle.xpath.RootNode in project checkstyle by checkstyle.
the class XpathFilterElement method getItems.
/**
* Returns list of nodes matching xpath expression given event.
*
* @param event {@code TreeWalkerAuditEvent} object
* @return list of nodes matching xpath expression given event
* @throws IllegalStateException if the xpath query could not be evaluated.
*/
private List<Item> getItems(TreeWalkerAuditEvent event) {
final RootNode rootNode;
if (event.getRootAst() == null) {
rootNode = null;
} else {
rootNode = new RootNode(event.getRootAst());
}
final List<Item> items;
try {
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
items = xpathExpression.evaluate(xpathDynamicContext);
} catch (XPathException ex) {
throw new IllegalStateException("Cannot initialize context and evaluate query: " + xpathQuery, ex);
}
return items;
}
use of com.puppycrawl.tools.checkstyle.xpath.RootNode in project checkstyle by checkstyle.
the class XpathUtilTest method testCreateChildren.
@Test
public void testCreateChildren() {
final DetailAstImpl rootAst = new DetailAstImpl();
final DetailAstImpl elementAst = new DetailAstImpl();
rootAst.addChild(elementAst);
final RootNode rootNode = new RootNode(rootAst);
final List<AbstractNode> children = XpathUtil.createChildren(rootNode, rootNode, elementAst);
assertWithMessage("Expected one child node").that(children).hasSize(1);
assertWithMessage("Node depth should be 1").that(children.get(0).getDepth()).isEqualTo(1);
}
use of com.puppycrawl.tools.checkstyle.xpath.RootNode in project checkstyle by checkstyle.
the class XpathUtil method printXpathBranch.
/**
* Returns xpath query results on file as string.
*
* @param xpath query to evaluate
* @param file file to run on
* @return all results as string separated by delimiter
* @throws CheckstyleException if some parsing error happens
* @throws IOException if an error occurs
*/
public static String printXpathBranch(String xpath, File file) throws CheckstyleException, IOException {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
try {
final RootNode rootNode = new RootNode(JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS));
final XPathExpression xpathExpression = xpathEvaluator.createExpression(xpath);
final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
final List<Item> matchingItems = xpathExpression.evaluate(xpathDynamicContext);
return matchingItems.stream().map(item -> ((AbstractNode) item).getUnderlyingNode()).map(AstTreeStringPrinter::printBranch).collect(Collectors.joining(DELIMITER));
} catch (XPathException ex) {
final String errMsg = String.format(Locale.ROOT, "Error during evaluation for xpath: %s, file: %s", xpath, file.getCanonicalPath());
throw new CheckstyleException(errMsg, ex);
}
}
Aggregations