Search in sources :

Example 1 with RootNode

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);
    }
}
Also used : AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) StatelessCheck(com.puppycrawl.tools.checkstyle.StatelessCheck) CommonUtil(com.puppycrawl.tools.checkstyle.utils.CommonUtil) Configuration(net.sf.saxon.Configuration) Collectors(java.util.stream.Collectors) RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) Item(net.sf.saxon.om.Item) List(java.util.List) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext) AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) XPathExpression(net.sf.saxon.sxpath.XPathExpression) RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) Item(net.sf.saxon.om.Item) AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) XPathException(net.sf.saxon.trans.XPathException) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 2 with RootNode

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;
}
Also used : RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) Item(net.sf.saxon.om.Item) XPathException(net.sf.saxon.trans.XPathException) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 3 with RootNode

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);
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) Test(org.junit.jupiter.api.Test)

Example 4 with RootNode

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);
    }
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) RootNode(com.puppycrawl.tools.checkstyle.xpath.RootNode) Item(net.sf.saxon.om.Item) XPathException(net.sf.saxon.trans.XPathException) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Aggregations

RootNode (com.puppycrawl.tools.checkstyle.xpath.RootNode)4 Item (net.sf.saxon.om.Item)3 XPathDynamicContext (net.sf.saxon.sxpath.XPathDynamicContext)3 XPathException (net.sf.saxon.trans.XPathException)3 AbstractNode (com.puppycrawl.tools.checkstyle.xpath.AbstractNode)2 XPathEvaluator (net.sf.saxon.sxpath.XPathEvaluator)2 XPathExpression (net.sf.saxon.sxpath.XPathExpression)2 DetailAstImpl (com.puppycrawl.tools.checkstyle.DetailAstImpl)1 StatelessCheck (com.puppycrawl.tools.checkstyle.StatelessCheck)1 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)1 CommonUtil (com.puppycrawl.tools.checkstyle.utils.CommonUtil)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Configuration (net.sf.saxon.Configuration)1 Test (org.junit.jupiter.api.Test)1