@Nullable public static <T> T getParentOfType( final DomElement element, final Class<T> requiredClass, final boolean strict) { for (DomElement curElement = strict && element != null ? element.getParent() : element; curElement != null; curElement = curElement.getParent()) { if (requiredClass.isInstance(curElement)) { return (T) curElement; } } return null; }
@Nullable public static List<JavaMethod> getFixedPath(DomElement element) { assert element.isValid(); final LinkedList<JavaMethod> methods = new LinkedList<>(); while (true) { final DomElement parent = element.getParent(); if (parent instanceof DomFileElement) { break; } final JavaMethod method = getGetterMethod(element, parent); if (method == null) { return null; } methods.addFirst(method); element = element.getParent(); } return methods; }
/** * @param domElement DomElement to search root of * @return the topmost valid DomElement being a parent of the given one. May be and may be not * DomFileElement. If root tag has changed, file may lose its domness, so there will be no * DomFileElement, but the inner DomElement's will be still alive because the underlying XML * tags are valid */ @NotNull public static DomElement getRoot(@NotNull DomElement domElement) { while (true) { final DomElement parent = domElement.getParent(); if (parent == null) { return domElement; } domElement = parent; } }
public static <T extends DomElement> DomFileElement<T> getFileElement( @NotNull DomElement element) { if (element instanceof DomFileElement) { return (DomFileElement) element; } DomFileElement fileElement = element.getUserData(FILE_ELEMENT_KEY); if (fileElement == null) { DomElement parent = element.getParent(); if (parent != null) { fileElement = getFileElement(parent); } element.putUserData(FILE_ELEMENT_KEY, fileElement); } return fileElement; }
public static boolean isAncestor( @NotNull DomElement ancestor, @NotNull DomElement descendant, boolean strict) { if (!strict && ancestor.equals(descendant)) return true; final DomElement parent = descendant.getParent(); return parent != null && isAncestor(ancestor, parent, false); }