File: XMLComparer.java

package info (click to toggle)
eclipselink 2.7.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,708 kB
  • sloc: java: 476,807; xml: 547; makefile: 21
file content (270 lines) | stat: -rw-r--r-- 9,510 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.platform.xml;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

/**
 * This class is used to compare if two DOM nodes are equal.
 */
public class XMLComparer {

    private boolean ignoreOrder;
    //Useful e.g. for password element where is content encrypted by AES/GCM/NoPadding where is randomly generated input vector for every encrypt(.) call.
    //Control document and test document is not same in this part.
    private String ignoreElementName = null;

    public XMLComparer() {
        super();
        ignoreOrder = false;
    }

    /**
     * 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.
     */
    public boolean isNodeEqual(Node control, Node test) {
        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;
        }
    }

    protected boolean isAttributeEqual(Attr control, 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 boolean isCommentEqual(Comment control, Comment test) {
        if (!isStringEqual(control.getNodeValue(), test.getNodeValue())) {
            return false;
        }
        return true;
    }

    private boolean isDocumentEqual(Document control, 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 boolean isDocumentFragmentEqual(DocumentFragment control, DocumentFragment test) {
        return isNodeListEqual(control.getChildNodes(), test.getChildNodes());
    }

    private boolean isDocumentTypeEqual(DocumentType control, 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 boolean isElementEqual(Element control, Element test) {
        if (ignoreElementName != null && isStringEqual(ignoreElementName, test.getTagName())) {
            return true;
        }
        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();
        int numberOfTestAttributes = testAttributes.getLength();
        if (numberOfControlAttributes != numberOfTestAttributes) {
            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 boolean isEntityReferenceEqual(EntityReference control, EntityReference test) {
        if (!isStringEqual(control.getNodeName(), test.getNodeName())) {
            return false;
        }
        return true;
    }

    private boolean isProcessingInstructionEqual(ProcessingInstruction control, ProcessingInstruction test) {
        if (!isStringEqual(control.getTarget(), test.getTarget())) {
            return false;
        }
        if (!isStringEqual(control.getData(), test.getData())) {
            return false;
        }
        return true;
    }

    private boolean isTextEqual(Text control, Text test) {
        return isStringEqual(control.getNodeValue(), test.getNodeValue());
    }

    private boolean isNodeListEqual(NodeList control, NodeList test) {
        int numberOfControlNodes = control.getLength();
        if (numberOfControlNodes != test.getLength()) {
            return false;
        }
        if(ignoreOrder){
            for (int x = 0; x < numberOfControlNodes; x++) {
                if(!isNodeInNodeList(control.item(x), test)){
                    return false;
                }
            }
            for (int x = 0; x < numberOfControlNodes; x++) {
                if(!isNodeInNodeList(test.item(x), control)){
                    return false;
                }
            }
        }else{
            for (int x = 0; x < numberOfControlNodes; x++) {
                if (!isNodeEqual(control.item(x), test.item(x))) {
                    return false;
                }
            }
        }
        return true;
    }

    private boolean isNodeInNodeList(Node node, NodeList nodeList){
        int length = nodeList.getLength();
        for (int x = 0; x < length; x++) {
            Node nextNode = nodeList.item(x);
            if(isNodeEqual(node, nextNode)){
                return true;
            }
        }
        return false;
    }

    private boolean isStringEqual(String control, String test) {
        if (control == test) {
            return true;
        } else if (null == control) {
            return false;
        } else {
            return control.equals(test);
        }
    }

    public boolean isIgnoreOrder() {
        return ignoreOrder;
    }

    public void setIgnoreOrder(boolean ignoreOrder) {
        this.ignoreOrder = ignoreOrder;
    }

    public String getIgnoreElementName() {
        return ignoreElementName;
    }

    public void setIgnoreElementName(String ignoreElementName) {
        this.ignoreElementName = ignoreElementName;
    }
}