Count the DOM nodes of the supplied type (nodeType) between the supplied sibling nodes, not including the nodes themselves. - Java XML

Java examples for XML:DOM Node

Description

Count the DOM nodes of the supplied type (nodeType) between the supplied sibling nodes, not including the nodes themselves.

Demo Code

/*//  w w w.j  ava2  s.  co  m
 * ePUB Corrector - https://github.com/vysokyj/epub-corrector/
 *
 * Copyright (C) 2012 Jiri Vysoky
 *
 * ePUB Corrector is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * ePUB Corrector is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cobertura; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**
     * Count the DOM nodes of the supplied type (nodeType) between the supplied
     * sibling nodes, not including the nodes themselves.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node1    First sibling node.
     * @param node2    Second sibling node.
     * @param nodeType The DOM {@link Node} type of the siblings to be counted.
     * @return The number of siblings of the supplied type between the supplied
     *         sibling nodes.
     * @throws UnsupportedOperationException if the supplied {@link Node Nodes}
     *                                       don't have the same parent node i.e. are not sibling nodes.
     */
    public static int countNodesBetween(Node node1, Node node2,
            short nodeType) {
        Node parent1 = node1.getParentNode();
        if (parent1 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node1
                    + "] has no parent.");
            return 0;
        }

        Node parent2 = node2.getParentNode();
        if (parent2 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node2
                    + "] has no parent.");
            return 0;
        }

        if (parent1 != parent2) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2
                    + "]. These nodes do not share the same sparent.");
            return 0;
        }

        int countBeforeNode1 = countNodesBefore(node1, nodeType);
        int countBeforeNode2 = countNodesBefore(node2, nodeType);
        int count = countBeforeNode2 - countBeforeNode1;

        if (node1.getNodeType() == nodeType) {
            count--;
        }

        return count;
    }

    /**
     * Count the DOM nodes between the supplied sibling nodes, not including
     * the nodes themselves.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node1 First sibling node.
     * @param node2 Second sibling node.
     * @return The number of siblings between the supplied sibling nodes.
     * @throws UnsupportedOperationException if the supplied {@link Node Nodes}
     *                                       don't have the same parent node i.e. are not sibling nodes.
     */
    public static int countNodesBetween(Node node1, Node node2) {
        Node parent1 = node1.getParentNode();
        if (parent1 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node1
                    + "] has no parent.");
            return 0;
        }

        Node parent2 = node2.getParentNode();
        if (parent2 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node2
                    + "] has no parent.");
            return 0;
        }

        if (parent1 != parent2) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2
                    + "]. These nodes do not share the same sparent.");
            return 0;
        }

        int countBeforeNode1 = countNodesBefore(node1);
        int countBeforeNode2 = countNodesBefore(node2);
        int count = countBeforeNode2 - countBeforeNode1 - 1;

        return count;
    }

    /**
     * Count the DOM nodes of the supplied type (nodeType) before the supplied
     * node, not including the node itself.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node     Node whose siblings are to be counted.
     * @param nodeType The DOM {@link Node} type of the siblings to be counted.
     * @return The number of siblings of the supplied type before the supplied node.
     */
    public static int countNodesBefore(Node node, short nodeType) {
        Node parent = node.getParentNode();
        if (parent == null) {
            System.out.println("Cannot count nodes before [" + node
                    + "]. [" + node + "] has no parent.");
            return 0;
        }

        NodeList siblings = parent.getChildNodes();
        int count = 0;
        int siblingCount = siblings.getLength();

        for (int i = 0; i < siblingCount; i++) {
            Node sibling = siblings.item(i);

            if (sibling == node) {
                break;
            }
            if (sibling.getNodeType() == nodeType) {
                count++;
            }
        }

        return count;
    }

    /**
     * Count the DOM nodes before the supplied node, not including the node itself.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node Node whose siblings are to be counted.
     * @return The number of siblings before the supplied node.
     */
    public static int countNodesBefore(Node node) {
        Node parent = node.getParentNode();
        if (parent == null) {
            System.out.println("Cannot count nodes before [" + node
                    + "]. [" + node + "] has no parent.");
            return 0;
        }

        NodeList siblings = parent.getChildNodes();
        int count = 0;
        int siblingCount = siblings.getLength();

        for (int i = 0; i < siblingCount; i++) {
            Node sibling = siblings.item(i);

            if (sibling == node) {
                break;
            }
            count++;
        }

        return count;
    }
}

Related Tutorials