FileDocCategorySizeDatePackage
XMLComparer.javaAPI DocGlassfish v2 API9238Tue May 22 16:54:48 BST 2007oracle.toplink.essentials.platform.xml

XMLComparer

public class XMLComparer extends Object
This class is used to compare if two DOM nodes are equal.

Fields Summary
Constructors Summary
public XMLComparer()

        super();
    
Methods Summary
private booleanisAttributeEqual(org.w3c.dom.Attr control, org.w3c.dom.Attr test)

        if (!isStringEqual(control.getNamespaceURI(), test.getNamespaceURI())) {
            return false;
        }
        if (!isStringEqual(control.getName(), test.getName())) {
            return false;
        }
        if (!isStringEqual(control.getNodeValue(), test.getNodeValue())) {
            return false;
        }
        return true;
    
private booleanisCommentEqual(org.w3c.dom.Comment control, org.w3c.dom.Comment test)

        if (!isStringEqual(control.getNodeValue(), test.getNodeValue())) {
            return false;
        }
        return true;
    
private booleanisDocumentEqual(org.w3c.dom.Document control, org.w3c.dom.Document test)

        if (!isDocumentTypeEqual(control.getDoctype(), test.getDoctype())) {
            return false;
        }

        Element controlRootElement = control.getDocumentElement();
        Element testRootElement = test.getDocumentElement();
        if (controlRootElement == testRootElement) {
            return true;
        } else if ((null == controlRootElement) || (null == testRootElement)) {
            return false;
        }
        return isElementEqual(controlRootElement, testRootElement);
    
private booleanisDocumentFragmentEqual(org.w3c.dom.DocumentFragment control, org.w3c.dom.DocumentFragment test)

        return isNodeListEqual(control.getChildNodes(), test.getChildNodes());
    
private booleanisDocumentTypeEqual(org.w3c.dom.DocumentType control, org.w3c.dom.DocumentType test)

        if (control == test) {
            return true;
        } else if ((null == control) || (null == test)) {
            return false;
        }

        if (!isStringEqual(control.getName(), test.getName())) {
            return false;
        }
        if (!isStringEqual(control.getPublicId(), test.getPublicId())) {
            return false;
        }
        if (!isStringEqual(control.getSystemId(), test.getSystemId())) {
            return false;
        }

        return true;
    
private booleanisElementEqual(org.w3c.dom.Element control, org.w3c.dom.Element test)

        if (!isStringEqual(control.getNamespaceURI(), test.getNamespaceURI())) {
            return false;
        }
        if (!isStringEqual(control.getTagName(), test.getTagName())) {
            return false;
        }

        // COMPARE ATTRIBUTES    
        NamedNodeMap controlAttributes = control.getAttributes();
        NamedNodeMap testAttributes = test.getAttributes();
        int numberOfControlAttributes = controlAttributes.getLength();
        if (numberOfControlAttributes != testAttributes.getLength()) {
            return false;
        }
        Attr controlAttribute;
        Attr testAttribute;
        for (int x = 0; x < numberOfControlAttributes; x++) {
            controlAttribute = (Attr)controlAttributes.item(x);
            if (null == controlAttribute.getNamespaceURI()) {
                testAttribute = (Attr)testAttributes.getNamedItem(controlAttribute.getNodeName());
            } else {
                testAttribute = (Attr)testAttributes.getNamedItemNS(controlAttribute.getNamespaceURI(), controlAttribute.getLocalName());
            }
            if (null == testAttribute) {
                return false;
            } else if (!isAttributeEqual(controlAttribute, testAttribute)) {
                return false;
            }
        }

        // COMPARE CHILD NODES
        return isNodeListEqual(control.getChildNodes(), test.getChildNodes());
    
private booleanisEntityReferenceEqual(org.w3c.dom.EntityReference control, org.w3c.dom.EntityReference test)

        if (!isStringEqual(control.getNodeName(), test.getNodeName())) {
            return false;
        }
        return true;
    
public booleanisNodeEqual(org.w3c.dom.Node control, org.w3c.dom.Node test)
Compare two DOM nodes.

param
control The first node in the comparison.
param
test The second node in the comparison.
return
Return true if the nodes are equal, else false.

        if (control == test) {
            return true;
        } else if ((null == control) || (null == test)) {
            return false;
        } else if (control.getNodeType() != test.getNodeType()) {
            return false;
        }
        switch (control.getNodeType()) {
        case (Node.ATTRIBUTE_NODE):
            return isAttributeEqual((Attr)control, (Attr)test);
        case (Node.CDATA_SECTION_NODE):
            return isTextEqual((Text)control, (Text)test);
        case (Node.COMMENT_NODE):
            return isCommentEqual((Comment)control, (Comment)test);
        case (Node.DOCUMENT_FRAGMENT_NODE):
            return isDocumentFragmentEqual((DocumentFragment)control, (DocumentFragment)test);
        case (Node.DOCUMENT_NODE):
            return isDocumentEqual((Document)control, (Document)test);
        case (Node.DOCUMENT_TYPE_NODE):
            return isDocumentTypeEqual((DocumentType)control, (DocumentType)test);
        case (Node.ELEMENT_NODE):
            return isElementEqual((Element)control, (Element)test);
        case (Node.ENTITY_NODE):
            return false;
        case (Node.ENTITY_REFERENCE_NODE):
            return isEntityReferenceEqual((EntityReference)control, (EntityReference)test);
        case (Node.NOTATION_NODE):
            return false;
        case (Node.PROCESSING_INSTRUCTION_NODE):
            return isProcessingInstructionEqual((ProcessingInstruction)control, (ProcessingInstruction)test);
        case (Node.TEXT_NODE):
            return isTextEqual((Text)control, (Text)test);
        default:
            return true;
        }
    
private booleanisNodeListEqual(org.w3c.dom.NodeList control, org.w3c.dom.NodeList test)

        int numberOfControlNodes = control.getLength();
        if (numberOfControlNodes != test.getLength()) {
            return false;
        }
        for (int x = 0; x < numberOfControlNodes; x++) {
            if (!isNodeEqual(control.item(x), test.item(x))) {
                return false;
            }
        }
        return true;
    
private booleanisProcessingInstructionEqual(org.w3c.dom.ProcessingInstruction control, org.w3c.dom.ProcessingInstruction test)

        if (!isStringEqual(control.getTarget(), test.getTarget())) {
            return false;
        }
        if (!isStringEqual(control.getData(), test.getData())) {
            return false;
        }
        return true;
    
private booleanisStringEqual(java.lang.String control, java.lang.String test)

        if (control == test) {
            return true;
        } else if (null == control) {
            return false;
        } else {
            return control.equals(test);
        }
    
private booleanisTextEqual(org.w3c.dom.Text control, org.w3c.dom.Text test)

        return isStringEqual(control.getNodeValue(), test.getNodeValue());