Java XML Attribute Get getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)

Here you can find the source of getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)

Description

Search upwards through the ancestors of node with name ancestorName, and return the first ancestor for which an attribute named attributeName is present.

License

Open Source License

Declaration

public static Element getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName) 

Method Source Code

//package com.java2s;
/**/*from   ww  w.  java2 s .co  m*/
 * Copyright 2000-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Search upwards through the ancestors of <code>node</code> with
     * name <code>ancestorName</code>,
     * and return the first ancestor for which an attribute named
     * <code>attributeName</code> is present.
     * Return <code>null</code> if no such ancestor exists.
     */
    public static Element getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName) {
        for (Node a = getAncestor(node, ancestorName); a != null; a = getAncestor(a, ancestorName)) {
            if (a.getNodeType() == Node.ELEMENT_NODE && ((Element) a).hasAttribute(attributeName)) {
                // Recursion ends here.
                return (Element) a;
            }
        }
        // No such ancestor.
        return null;
    }

    /**
     * If <code>node</code> has an ancestor
     * with name <code>ancestorName</code>, return that ancestor.
     * Else return <code>null</code>.
     */
    public static Node getAncestor(Node node, String ancestorName) {
        Node p = node;
        while ((p = p.getParentNode()) != null) {
            if (p.getNodeName().equals(ancestorName))
                return p;
        }
        return null;
    }

    /**
     * If <code>node</code> has an ancestor
     * with one of the names in  <code>ancestorNames</code>, return the closest of these ancestors.
     * Else return <code>null</code>.
     */
    public static Node getAncestor(Node node, String[] ancestorNames) {
        if (ancestorNames.length <= 0)
            throw new IllegalArgumentException("No ancestorNames provided.");
        Node closestAncestor = null;
        for (int i = 0; i < ancestorNames.length; i++) {
            Node ancestor = getAncestor(node, ancestorNames[i]);
            if (ancestor != null) {
                if (closestAncestor == null) {
                    closestAncestor = ancestor;
                } else if (isAncestor(closestAncestor, ancestor)) { // new one is closer than closest so far
                    closestAncestor = ancestor;
                } // else leave as is
            }
        }
        return closestAncestor;
    }

    /** Verify if <code>ancestor</code> is an ancestor of <code>node</code> */
    public static boolean isAncestor(Node ancestor, Node node) {
        Node p = node;
        while ((p = p.getParentNode()) != null) {
            if (ancestor == p)
                return true;
        }
        return false;
    }
}

Related

  1. getBooleanAttributeOption(final Element configuration, final String option, boolean defaultValue)
  2. getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)
  3. getBooleanAttributeRequired(Node node, String attributeName)
  4. getBooleanAttributeValue(Node node, String attribute)
  5. getCascadeValue(final Element elem, final String attrName)
  6. getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)
  7. getCurrentLevelAttributeTextValue(Element ele, String attribute)
  8. getDirectAttribute(Node node, String name)
  9. getDivHeadAttr(Element annotU, String attrName)