001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     ataillefer
018 */
019package org.nuxeo.ecm.diff.service.impl;
020
021import java.util.Arrays;
022
023import org.custommonkey.xmlunit.Difference;
024import org.custommonkey.xmlunit.DifferenceConstants;
025import org.custommonkey.xmlunit.DifferenceListener;
026import org.custommonkey.xmlunit.NodeDetail;
027import org.w3c.dom.Node;
028
029/**
030 * Custom DifferenceListener for XMLUnit Diff.
031 * <p>
032 * It ignores what we call "structural" differences, ie.:
033 * <ul>
034 * <li>DOCTYPE related</li>
035 * <li>Namespace URI</li>
036 * <li>Attribute value</li>
037 * <li>Attribute name not found</li>
038 * <li>Element tag name</li>
039 * <li>Child node list sequence</li>
040 * <li>Child node list length</li>
041 * </ul>
042 *
043 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
044 */
045public class IgnoreStructuralDifferenceListener implements DifferenceListener {
046
047    private static final String SCHEMA_ELEMENT = "schema";
048
049    /**
050     * Difference types to be ignored.
051     */
052    private static final int[] IGNORE = new int[] { DifferenceConstants.HAS_DOCTYPE_DECLARATION_ID,
053            DifferenceConstants.DOCTYPE_NAME_ID, DifferenceConstants.DOCTYPE_PUBLIC_ID_ID,
054            DifferenceConstants.DOCTYPE_SYSTEM_ID_ID, DifferenceConstants.NAMESPACE_URI_ID,
055            DifferenceConstants.ATTR_VALUE_ID, DifferenceConstants.ATTR_NAME_NOT_FOUND_ID,
056            DifferenceConstants.ELEMENT_TAG_NAME_ID, DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID,
057            DifferenceConstants.CHILD_NODELIST_LENGTH_ID };
058
059    static {
060        Arrays.sort(IGNORE);
061    }
062
063    /**
064     * Here want to:
065     * <ul>
066     * <li>Take into account all difference types to be ignored.</li>
067     * <li>Not consider an unbalanced schema, ie. a schema that exists for a document but not for the other one, as a
068     * difference.</li>
069     * </ul>
070     */
071    public int differenceFound(Difference difference) {
072
073        boolean unBalancedSchema = false;
074
075        NodeDetail controlNodeDetail = difference.getControlNodeDetail();
076        NodeDetail testNodeDetail = difference.getTestNodeDetail();
077
078        if (controlNodeDetail != null && testNodeDetail != null) {
079
080            Node controlNode = controlNodeDetail.getNode();
081            Node testNode = testNodeDetail.getNode();
082
083            if (controlNode != null && SCHEMA_ELEMENT.equals(controlNode.getNodeName()) && testNode == null) {
084                unBalancedSchema = true;
085            }
086
087            if (!unBalancedSchema && testNode != null && SCHEMA_ELEMENT.equals(testNode.getNodeName())
088                    && controlNode == null) {
089                unBalancedSchema = true;
090            }
091        }
092
093        return (Arrays.binarySearch(IGNORE, difference.getId()) >= 0 || unBalancedSchema) ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL
094                : RETURN_ACCEPT_DIFFERENCE;
095    }
096
097    public void skippedComparison(Node control, Node test) {
098    }
099}