Search in sources :

Example 1 with AbstractNode

use of com.puppycrawl.tools.checkstyle.xpath.AbstractNode 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 AbstractNode

use of com.puppycrawl.tools.checkstyle.xpath.AbstractNode in project checkstyle by checkstyle.

the class XpathFilterElement method isXpathQueryMatching.

/**
 * Is matching by xpath query.
 *
 * @param event event
 * @return true if it is matching or not set.
 */
private boolean isXpathQueryMatching(TreeWalkerAuditEvent event) {
    boolean isMatching;
    if (xpathExpression == null) {
        isMatching = true;
    } else {
        isMatching = false;
        final List<AbstractNode> nodes = getItems(event).stream().map(AbstractNode.class::cast).collect(Collectors.toList());
        for (AbstractNode abstractNode : nodes) {
            isMatching = abstractNode.getTokenType() == event.getTokenType() && abstractNode.getLineNumber() == event.getLine() && abstractNode.getColumnNumber() == event.getColumnCharIndex();
            if (isMatching) {
                break;
            }
        }
    }
    return isMatching;
}
Also used : AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode)

Example 3 with AbstractNode

use of com.puppycrawl.tools.checkstyle.xpath.AbstractNode in project checkstyle by checkstyle.

the class XpathUtil method getXpathItems.

/**
 * Returns list of nodes matching xpath expression given node context.
 *
 * @param xpath Xpath expression
 * @param rootNode {@code NodeInfo} node context
 * @return list of nodes matching xpath expression given node context
 */
public static List<NodeInfo> getXpathItems(String xpath, AbstractNode rootNode) throws XPathException {
    final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
    final XPathExpression xpathExpression = xpathEvaluator.createExpression(xpath);
    final XPathDynamicContext xpathDynamicContext = xpathExpression.createDynamicContext(rootNode);
    final List<Item> items = xpathExpression.evaluate(xpathDynamicContext);
    return items.stream().map(item -> (NodeInfo) item).collect(Collectors.toList());
}
Also used : XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) List(java.util.List) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext) AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) Configuration(net.sf.saxon.Configuration) XPathException(net.sf.saxon.trans.XPathException) Collectors(java.util.stream.Collectors) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) NodeInfo(net.sf.saxon.om.NodeInfo) XPathExpression(net.sf.saxon.sxpath.XPathExpression) Item(net.sf.saxon.om.Item) NodeInfo(net.sf.saxon.om.NodeInfo) XPathEvaluator(net.sf.saxon.sxpath.XPathEvaluator) XPathDynamicContext(net.sf.saxon.sxpath.XPathDynamicContext)

Example 4 with AbstractNode

use of com.puppycrawl.tools.checkstyle.xpath.AbstractNode 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 5 with AbstractNode

use of com.puppycrawl.tools.checkstyle.xpath.AbstractNode in project checkstyle by checkstyle.

the class XpathUtil method createChildren.

/**
 * Iterates siblings of the given node and creates new Xpath-nodes.
 *
 * @param root the root node
 * @param parent the parent node
 * @param firstChild the first DetailAST
 * @return children list
 */
public static List<AbstractNode> createChildren(AbstractNode root, AbstractNode parent, DetailAST firstChild) {
    DetailAST currentChild = firstChild;
    final int depth = parent.getDepth() + 1;
    final List<AbstractNode> result = new ArrayList<>();
    while (currentChild != null) {
        final int index = result.size();
        final ElementNode child = new ElementNode(root, parent, currentChild, depth, index);
        result.add(child);
        currentChild = currentChild.getNextSibling();
    }
    return result;
}
Also used : AbstractNode(com.puppycrawl.tools.checkstyle.xpath.AbstractNode) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayList(java.util.ArrayList) ElementNode(com.puppycrawl.tools.checkstyle.xpath.ElementNode)

Aggregations

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