@Nullable private SimpleNode visit(SimpleNode simpleNode, DomElement domElement) { boolean validCandidate = false; if (simpleNode instanceof AbstractDomElementNode) { final DomElement nodeElement = ((AbstractDomElementNode) simpleNode).getDomElement(); if (nodeElement != null) { validCandidate = !(simpleNode instanceof DomElementsGroupNode); if (validCandidate && nodeElement.equals(domElement)) { return simpleNode; } if (!(nodeElement instanceof MergedObject) && !isParent(nodeElement, domElement)) { return null; } } } final Object[] childElements = myBuilder.getTreeStructure().getChildElements(simpleNode); if (childElements.length == 0 && validCandidate) { // leaf return simpleNode; } for (Object child : childElements) { SimpleNode result = visit((SimpleNode) child, domElement); if (result != null) { return result; } } return validCandidate ? simpleNode : null; }
private static boolean isParent(final DomElement potentialParent, final DomElement domElement) { DomElement currParent = domElement; while (currParent != null) { if (currParent.equals(potentialParent)) return true; currParent = currParent.getParent(); } return false; }