2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (c) 2019 IBM
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.sdnc.config.audit.node;
29 import java.io.IOException;
30 import java.io.StringReader;
32 import javax.xml.XMLConstants;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
37 import org.custommonkey.xmlunit.Diff;
38 import org.custommonkey.xmlunit.Difference;
39 import org.custommonkey.xmlunit.DifferenceConstants;
40 import org.custommonkey.xmlunit.DifferenceListener;
41 import org.custommonkey.xmlunit.ElementNameQualifier;
42 import org.custommonkey.xmlunit.XMLUnit;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Node;
45 import org.xml.sax.InputSource;
46 import org.xml.sax.SAXException;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
52 public class CompareXmlData implements CompareDataInterface
54 private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
62 public CompareXmlData(String controlXml, String testXml) {
64 this.controlXml = controlXml;
65 this.testXml = testXml;
69 public boolean compare() throws Exception
72 log.debug("controlXml : " + controlXml);
73 log.debug("testXml : " + testXml);
78 Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
79 diff.overrideElementQualifier(new ElementNameQualifier() {
81 protected boolean equalsNamespace(Node control, Node test) {
85 diff.overrideDifferenceListener(new DifferenceListener() {
87 public int differenceFound(Difference diff) {
88 if (diff.getId() == DifferenceConstants.ATTR_VALUE_ID) {
89 return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
91 return RETURN_ACCEPT_DIFFERENCE;
94 public void skippedComparison(Node arg0, Node arg1) { }
101 catch(SAXException se)
103 log.error("Exception caught", se);
104 throw new Exception(se.getMessage());
108 log.error("Exception caught", e);
109 throw new Exception(e.getMessage());
113 private void doSetup() throws ParserConfigurationException, SAXException, IOException
116 XMLUnit.setIgnoreAttributeOrder(true);
117 XMLUnit.setIgnoreComments(true);
118 XMLUnit.setIgnoreWhitespace(true);
122 public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
124 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
125 dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
126 dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
127 dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
129 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
130 StringReader reader = new StringReader(inXml);
131 InputSource inputSource = new InputSource(reader);
132 Document doc = dBuilder.parse(inputSource);
133 doc.getDocumentElement().normalize();