2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * =============================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.sdnc.config.audit.node;
 
  28 import java.io.IOException;
 
  29 import java.io.StringReader;
 
  31 import javax.xml.parsers.DocumentBuilder;
 
  32 import javax.xml.parsers.DocumentBuilderFactory;
 
  33 import javax.xml.parsers.ParserConfigurationException;
 
  35 import org.custommonkey.xmlunit.Diff;
 
  36 import org.custommonkey.xmlunit.Difference;
 
  37 import org.custommonkey.xmlunit.DifferenceConstants;
 
  38 import org.custommonkey.xmlunit.DifferenceListener;
 
  39 import org.custommonkey.xmlunit.ElementNameQualifier;
 
  40 import org.custommonkey.xmlunit.XMLUnit;
 
  41 import org.w3c.dom.Document;
 
  42 import org.w3c.dom.Node;
 
  43 import org.xml.sax.InputSource;
 
  44 import org.xml.sax.SAXException;
 
  46 import com.att.eelf.configuration.EELFLogger;
 
  47 import com.att.eelf.configuration.EELFManager;
 
  50 public class CompareXmlData implements CompareDataInterface
 
  52     private static final EELFLogger log = EELFManager.getInstance().getLogger(CompareXmlData.class);
 
  60     public CompareXmlData(String controlXml, String testXml) {
 
  62         this.controlXml = controlXml;
 
  63         this.testXml = testXml;
 
  67     public boolean compare() throws Exception
 
  70         log.debug("controlXml : " + controlXml);
 
  71         log.debug("testXml : " + testXml);
 
  76              Diff diff = new Diff(getCompareDoc(controlXml), getCompareDoc(testXml));
 
  77              diff.overrideElementQualifier(new ElementNameQualifier() {
 
  79                     protected boolean equalsNamespace(Node control, Node test) {
 
  83              diff.overrideDifferenceListener(new DifferenceListener() {
 
  85                     public int differenceFound(Difference diff) {
 
  86                         if (diff.getId() == DifferenceConstants.ATTR_VALUE_ID) {
 
  87                             return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
 
  89                         return RETURN_ACCEPT_DIFFERENCE;
 
  92                     public void skippedComparison(Node arg0, Node arg1) { }
 
  99         catch(SAXException se)
 
 101             se.printStackTrace();
 
 102             throw new Exception(se.getMessage());
 
 107             throw new Exception(e.getMessage());
 
 111     private void doSetup() throws ParserConfigurationException, SAXException, IOException
 
 114         XMLUnit.setIgnoreAttributeOrder(true);
 
 115         XMLUnit.setIgnoreComments(true);
 
 116         XMLUnit.setIgnoreWhitespace(true);
 
 120     public Document getCompareDoc(String inXml) throws ParserConfigurationException, SAXException, IOException
 
 122         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 
 123         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 
 124         StringReader reader = new StringReader(inXml);
 
 125         InputSource inputSource = new InputSource(reader);
 
 126         Document doc = dBuilder.parse(inputSource);
 
 127         doc.getDocumentElement().normalize();