Java XML Node Parent isDescendantOrSelf(Node ctx, Node descendantOrSelf)

Here you can find the source of isDescendantOrSelf(Node ctx, Node descendantOrSelf)

Description

Returns true if the descendantOrSelf is on the descendant-or-self axis of the context node.

License

Apache License

Parameter

Parameter Description
ctx a parameter
descendantOrSelf a parameter

Return

true if the node is descendant

Declaration

public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) 

Method Source Code

//package com.java2s;
/* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
 * The package name has been changed; there may also be other changes.
 * /*from   www .  j a  v  a 2  s .  c  o  m*/
 * This notice is included to meet the condition in clause 4(b) of the License. 
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Node;

public class Main {
    /**
     * Returns true if the descendantOrSelf is on the descendant-or-self axis
     * of the context node.
     *
     * @param ctx
     * @param descendantOrSelf
     * @return true if the node is descendant
     */
    public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) {
        if (ctx == descendantOrSelf) {
            return true;
        }

        Node parent = descendantOrSelf;

        while (true) {
            if (parent == null) {
                return false;
            }

            if (parent == ctx) {
                return true;
            }

            if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
                parent = ((Attr) parent).getOwnerElement();
            } else {
                parent = parent.getParentNode();
            }
        }
    }
}

Related

  1. isAncestorOf(Node ancestor, Node node)
  2. isAncestorOf(Node candidateAncestor, Node candidateSibling, boolean strict)
  3. isAncestorOf(Node node, Node descendant)
  4. isAnyNodeAncestorOf(ArrayList ancestorNodes, Node node)
  5. isDescendant(Node test, Node target)
  6. isDescendantOrSelf(Node ctx, Node descendantOrSelf)
  7. previousSiblingElement(Node node)
  8. resolveLeafNodeValue(Node node)