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);
}
}
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;
}
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());
}
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);
}
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;
}
Aggregations