001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * ataillefer 016 */ 017package org.nuxeo.ecm.diff.service.impl; 018 019import java.util.Arrays; 020 021import org.custommonkey.xmlunit.Difference; 022import org.custommonkey.xmlunit.DifferenceConstants; 023import org.custommonkey.xmlunit.DifferenceListener; 024import org.custommonkey.xmlunit.NodeDetail; 025import org.w3c.dom.Node; 026 027/** 028 * Custom DifferenceListener for XMLUnit Diff. 029 * <p> 030 * It ignores what we call "structural" differences, ie.: 031 * <ul> 032 * <li>DOCTYPE related</li> 033 * <li>Namespace URI</li> 034 * <li>Attribute value</li> 035 * <li>Attribute name not found</li> 036 * <li>Element tag name</li> 037 * <li>Child node list sequence</li> 038 * <li>Child node list length</li> 039 * </ul> 040 * 041 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 042 */ 043public class IgnoreStructuralDifferenceListener implements DifferenceListener { 044 045 private static final String SCHEMA_ELEMENT = "schema"; 046 047 /** 048 * Difference types to be ignored. 049 */ 050 private static final int[] IGNORE = new int[] { DifferenceConstants.HAS_DOCTYPE_DECLARATION_ID, 051 DifferenceConstants.DOCTYPE_NAME_ID, DifferenceConstants.DOCTYPE_PUBLIC_ID_ID, 052 DifferenceConstants.DOCTYPE_SYSTEM_ID_ID, DifferenceConstants.NAMESPACE_URI_ID, 053 DifferenceConstants.ATTR_VALUE_ID, DifferenceConstants.ATTR_NAME_NOT_FOUND_ID, 054 DifferenceConstants.ELEMENT_TAG_NAME_ID, DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID, 055 DifferenceConstants.CHILD_NODELIST_LENGTH_ID }; 056 057 static { 058 Arrays.sort(IGNORE); 059 } 060 061 /** 062 * Here want to: 063 * <ul> 064 * <li>Take into account all difference types to be ignored.</li> 065 * <li>Not consider an unbalanced schema, ie. a schema that exists for a document but not for the other one, as a 066 * difference.</li> 067 * </ul> 068 */ 069 public int differenceFound(Difference difference) { 070 071 boolean unBalancedSchema = false; 072 073 NodeDetail controlNodeDetail = difference.getControlNodeDetail(); 074 NodeDetail testNodeDetail = difference.getTestNodeDetail(); 075 076 if (controlNodeDetail != null && testNodeDetail != null) { 077 078 Node controlNode = controlNodeDetail.getNode(); 079 Node testNode = testNodeDetail.getNode(); 080 081 if (controlNode != null && SCHEMA_ELEMENT.equals(controlNode.getNodeName()) && testNode == null) { 082 unBalancedSchema = true; 083 } 084 085 if (!unBalancedSchema && testNode != null && SCHEMA_ELEMENT.equals(testNode.getNodeName()) 086 && controlNode == null) { 087 unBalancedSchema = true; 088 } 089 } 090 091 return (Arrays.binarySearch(IGNORE, difference.getId()) >= 0 || unBalancedSchema) ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL 092 : RETURN_ACCEPT_DIFFERENCE; 093 } 094 095 public void skippedComparison(Node control, Node test) { 096 } 097}