Java XML Node Parent isAncestorOf(Node candidateAncestor, Node candidateSibling, boolean strict)

Here you can find the source of isAncestorOf(Node candidateAncestor, Node candidateSibling, boolean strict)

Description

Checks whether a node is ancestor or same of another node.

License

Apache License

Parameter

Parameter Description
candidateAncestor the candidate ancestor node.
candidateSibling the candidate sibling node.
strict if <code>true</code> is not allowed that the ancestor and sibling can be the same node.

Return

true if candidateSibling is ancestor of candidateSibling, false otherwise.

Declaration

public static boolean isAncestorOf(Node candidateAncestor, Node candidateSibling, boolean strict) 

Method Source Code

//package com.java2s;
/*//from w w w. j a v a  2 s . c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Checks whether a node is ancestor or same of another node.
     *
     * @param candidateAncestor the candidate ancestor node.
     * @param candidateSibling the candidate sibling node.
     * @param strict if <code>true</code> is not allowed that the ancestor and sibling can be the same node.
     * @return <code>true</code> if <code>candidateSibling</code> is ancestor of <code>candidateSibling</code>,
     *         <code>false</code> otherwise.
     */
    public static boolean isAncestorOf(Node candidateAncestor, Node candidateSibling, boolean strict) {
        if (candidateAncestor == null)
            throw new NullPointerException("candidate ancestor cannot be null null.");
        if (candidateSibling == null)
            throw new NullPointerException("candidate sibling cannot be null null.");
        if (strict && candidateAncestor.equals(candidateSibling))
            return false;
        Node parent = candidateSibling;
        while (parent != null) {
            if (parent.equals(candidateAncestor))
                return true;
            parent = parent.getParentNode();
        }
        return false;
    }

    /**
     * Checks whether a node is ancestor or same of another node. As
     * {@link #isAncestorOf(org.w3c.dom.Node, org.w3c.dom.Node, boolean)} with <code>strict=false</code>.
     *
     * @param candidateAncestor the candidate ancestor node.
     * @param candidateSibling the candidate sibling node.
     * @return <code>true</code> if <code>candidateSibling</code> is ancestor of <code>candidateSibling</code>,
     *         <code>false</code> otherwise.
     */
    public static boolean isAncestorOf(Node candidateAncestor, Node candidateSibling) {
        return isAncestorOf(candidateAncestor, candidateSibling, false);
    }
}

Related

  1. getParentOfNode(Node node)
  2. getParentOfNode(Node node)
  3. getParents(Node node)
  4. isAncestor(Node ancestor, Node node)
  5. isAncestorOf(Node ancestor, Node node)
  6. isAncestorOf(Node node, Node descendant)
  7. isAnyNodeAncestorOf(ArrayList ancestorNodes, Node node)
  8. isDescendant(Node test, Node target)
  9. isDescendantOrSelf(Node ctx, Node descendantOrSelf)