Java XML Node Compare equalNode(Node nodeA, Node nodeB)

Here you can find the source of equalNode(Node nodeA, Node nodeB)

Description

equal Node

License

Apache License

Declaration

private static boolean equalNode(Node nodeA, Node nodeB) 

Method Source Code

//package com.java2s;
/**/*from   ww  w .  j av a2 s .  c  o  m*/
 *
 *  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 java.util.ArrayList;
import java.util.List;

import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static boolean equalNode(Node nodeA, Node nodeB) {
        if (nodeA == null) {
            if (nodeB == null) {
                return true;
            }
            return false;
        }
        // following is intended to provide same function as 1.5 isEqualNode()
        if (nodeA.getNodeType() != nodeB.getNodeType()) {
            return false;
        }
        if (!equalString(nodeA.getNodeName(), nodeB.getNodeName())) {
            return false;
        }
        if (!equalString(nodeA.getLocalName(), nodeB.getLocalName())) {
            return false;
        }
        if (!equalString(nodeA.getNamespaceURI(), nodeB.getNamespaceURI())) {
            return false;
        }
        if (!equalString(nodeA.getNamespaceURI(), nodeB.getNamespaceURI())) {
            return false;
        }
        if (!equalString(nodeA.getPrefix(), nodeB.getPrefix())) {
            return false;
        }
        if (!equalString(nodeA.getNodeValue(), nodeB.getNodeValue())) {
            return false;
        }
        if (!equalNamedNodeMap(nodeA.getAttributes(), nodeB.getAttributes())) {
            return false;
        }
        if (!equalNodeList(nodeA.getChildNodes(), nodeB.getChildNodes())) {
            return false;
        }
        if (nodeA.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
            DocumentType documentTypeA = (DocumentType) nodeA;
            DocumentType documentTypeB = (DocumentType) nodeB;
            if (!equalString(documentTypeA.getPublicId(), documentTypeB.getPublicId())) {
                return false;
            }
            if (!equalString(documentTypeA.getSystemId(), documentTypeB.getSystemId())) {
                return false;
            }
            if (!equalString(documentTypeA.getInternalSubset(), documentTypeB.getInternalSubset())) {
                return false;
            }
            if (!equalNamedNodeMap(documentTypeA.getEntities(), documentTypeB.getEntities())) {
                return false;
            }
            if (!equalNamedNodeMap(documentTypeA.getNotations(), documentTypeB.getNotations())) {
                return false;
            }
        }
        return true;
    }

    private static boolean equalString(String stringA, String stringB) {
        if (stringA == null) {
            if (stringB == null) {
                return true;
            }
            return false;
        }
        return stringA.equals(stringB);
    }

    private static boolean equalNamedNodeMap(NamedNodeMap mapA, NamedNodeMap mapB) {
        if (mapA == null) {
            if (mapB == null) {
                return true;
            }
            return false;
        }
        if (mapA.getLength() != mapB.getLength()) {
            return false;
        }
        for (int i = 0; i < mapA.getLength(); i++) {
            Node trialNode = mapA.item(i);
            if (trialNode == null) {
                return false;
            }
            Node checkNode = mapB.getNamedItem(trialNode.getNodeName());
            if (checkNode == null) {
                return false;
            }
            if (!equalNode(trialNode, checkNode)) {
                return false;
            }
        }
        return true;
    }

    private static boolean equalNodeList(NodeList nodeListA, NodeList nodeListB) {
        if (nodeListA == null) {
            if (nodeListB == null) {
                return true;
            }
            return false;
        }
        return equalNodes(nodeListA, nodeListB);
    }

    private static boolean equalNodes(NodeList sourceNodeList, NodeList targetNodeList) {
        ArrayList sourceNodes = new ArrayList();
        ArrayList targetNodes = new ArrayList();

        getAllNodes(sourceNodeList, sourceNodes);
        getAllNodes(targetNodeList, targetNodes);

        int sourceLength = sourceNodes.size();
        int targetLength = targetNodes.size();

        if (sourceLength != targetLength) {
            return false;
        }

        for (int i = 0; i < sourceLength; i++) {
            Node sourceNode = (Node) sourceNodes.get(i);
            Node targetNode = (Node) targetNodes.get(i);

            /* remove comment when migrated to Java 1.5
            if (!sourceNode.isEqualNode(targetNode))
            {
              return false;
            }
            */
            // following is intended as 1.4 equivalent of isEqualNode()
            if (!equalNode(sourceNode, targetNode)) {
                return false;
            }
        } // for

        return true;
    }

    private static void getAllNodes(NodeList nodeList, List nodes) {
        int length = nodeList.getLength();
        if (length == 0) {
            return;
        }

        for (int i = 0; i < length; i++) {
            Node node = nodeList.item(i);
            nodes.add(node);
            getAllNodes(node.getChildNodes(), nodes);
        } // for
    }
}

Related

  1. compareLocations(Node node1, int offset1, Node node2, int offset2)
  2. compareNode(Node nQ, Node nN, Boolean considerLength, Map qvars)
  3. compareNodes(Node expected, Node actual)
  4. compareStringNode(Node node, String tag)
  5. compareTwoNodes(Node m, Node n)
  6. equals(Node n1, Node n2)
  7. isDescendant(Node testNode, Node compareToNode)
  8. isNodeInNS(Node nodeToCompare, String nsuri, String tagName)
  9. isNodeSame(org.w3c.dom.Node node1, org.w3c.dom.Node node2)