A DifferenceListener That Consider text nodes and CDATA sections to be equal

import org.custommonkey.xmlunit.*;
import org.w3c.dom.Node;

public class CDATAEqualsText implements DifferenceListener {

    public int differenceFound(Difference diff) {

        Node expected = diff.getControlNodeDetail().getNode();
        Node actual = diff.getTestNodeDetail().getNode();
        
        if ((expected.getNodeType() == Node.CDATA_SECTION_NODE 
             && actual.getNodeType() == Node.TEXT_NODE)
             ||
             (actual.getNodeType() == Node.CDATA_SECTION_NODE 
             && expected.getNodeType() == Node.TEXT_NODE)) {
         
            if (expected.getNodeValue().equals(actual.getNodeValue())) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
            
        }
        
        return RETURN_ACCEPT_DIFFERENCE;
        
        // We could really use something like DOM's NodeFilter
        // to indicate whether to process or skip the children
        
    }


    public void skippedComparison(Node expected, Node actual) {}

}

Previous | Next | Top | Cafe con Leche

Copyright 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 19, 2005