[jdom-interest] deep equals

Bradley S. Huffman hip at cs.okstate.edu
Fri Oct 15 17:51:02 PDT 2004


Elliotte Harold writes:

> Has anyone written any sort of deep equals function for JDOM documents? 
> I hunted around in the contrib package but I couldn't find anything like 
> this. It seems an obvious need. Am I missing something somewhere?

I never finished this, but it might provide a good start for what you need.
Do with it what you want.

Brad

public class JDOMEquivalent {

    public JDOMEquivalent() { }

    public boolean equivalent(Document d1, Document d2) {
        return equivalentContent((Document) d1, (Document) d2);
    }

    public boolean equivalent(Element e1, Element e2) {
        return equivalentName(e1, e2) &&
               equivalentAttributes(e1, e2) &&
               equivalentContent(d1.getDescendants(), d2.getDescendants());
    }

    public boolean equivalent(Text t1, Text t2) {
        String v1 = t1.getValue();
        String v2 = t2.getValue();
        return v1.equals(v2);
    }

    public boolean equivalent(DocType d1, DocType d2) {
        //XXX
    }

    public boolean equivalent(Comment c1, Comment c2) {
        String v1 = c1.getValue();
        String v2 = c2.getValue();
        return v1.equals(v2);
    }

    public boolean equivalent(ProcessingInstruction p1,
                              ProcessingInstruction p2) {
        String t1 = p1.getTarget();
        String t2 = p2.getTarget();
        String d1 = p1.getData();
        String d2 = p2.getData();
        return t1.equals(t2) && d1.equals(d2);
    }

    public boolean equivalent(Attribute a1, Attribute a2) {
        String v1 = a1.getValue();
        String v2 = a2.getValue();
        return equivalentName(a1, a2) && v1.equals(v2);
    }

    public boolean equivalentAttributes(Element e1, Element e2) {
        //XXX
    }

    public boolean equivalentContent(Document d1, Document d2) {
        //XXX short circuit if content size1 != content size2
        return equivalentContent(d1.getDescendants(), d2.getDescendants());
    }

    public boolean equivalentContent(Element e1, Element e2) {
        //XXX short circuit if content size1 != content size2
        return equivalentContent(e1.getDescendants(), e2.getDescendants());
    }

    public boolean equivalentContent(Iterator i1, Iterator i2) {
        boolean result = true;
        while(result && i1.hasNext() && i2.hasNext()) {
            Object o1 = i1.next();        
            Object o2 = i2.next();        
            if ((o1 instanceof Element) && (o2 instanceof Element)) {
                result = equivalent((Element) o1, (Element) o2);
                //XXX Hmm, this should work and avoid much recursion
                //    if we can guarentee i1, i2 are instances of
                //    DescendantIterator
                //
                //    result = equivalentName((Element) o1, (Element) o2) &&
                //             equivalentAttributes((Element) o1, (Element) o2);
            }
            else if ((o1 instanceof Text) && (o2 instanceof Text)) {
                result = equivalent((Text) o1, (Text) o2);
            }
            else if ((o1 instanceof Comment) && (o2 instanceof Comment)) {
                result = equivalent((Comment) o1, (Comment) o2);
            }
            else if ((o1 instanceof ProcessingInstruction) &&
                     (o2 instanceof ProcessingInstruction)) {
                result = equivalent((ProcessingInstruction) o1,
                                    (ProcessingInstruction) o2);
            }
            else if ((o1 instanceof DocType) && (o2 instanceof DocType)) {
                result = equivalent((DocType) o1, (DocType) o2);
            }
            else {
                result = false;
            }
        }
        return result;
    }

    public boolean equivalentName(Element e1, Element e2) {
        Namespace ns1 = e1.getNamespace();
        String localName1 = e1.getName();

        Namespace ns2 = e2.getNamespace();
        String localName1 = e2.getName();

        return (ns1.equals(ns2)) && (localName1.equals(localName2));
    }

    public boolean equivalentName(Attribute a1, Attribute a2) {
        Namespace ns1 = a1.getNamespace();
        String localName1 = a1.getName();

        Namespace ns2 = a2.getNamespace();
        String localName1 = a2.getName();

        return (ns1.equals(ns2)) && (localName1.equals(localName2));
    }
}


More information about the jdom-interest mailing list