[jdom-interest] isRootElement issue

Jason Hunter jhunter at collab.net
Tue Oct 24 14:12:58 PDT 2000


> I found some intersting behaviour(misbehaviour) of isRootElement.  
> The weared situation is like this.
> 
> content.set(content.indexOf(tmp), doc1.getRootElement());
>  //Replacing temp node with child(No errors)

Yep, I believe you found a bug.  Looks like Element won't let you add an
Element with a parent, but PartialList enforces no such guarantee.

Here's Element:

    public Element addContent(Element element) {
        if (element.isRootElement()) {
            throw new IllegalAddException(this, element,
                "The element already has an existing parent " +
                "(the document root)");
        } else if (element.getParent() != null) {
            throw new IllegalAddException(this, element,
                "The element already has an existing parent \"" +
                element.getParent().getQualifiedName() + "\"");
        }

        if (content == null) {
            content = new LinkedList();
        }

        element.setParent(this);
        content.add(element);
        return this;
    }

Here's PartialList:

    public Object set(int index, Object current) {
        Object old = get(index);
        int backingIndex = backingList.indexOf(old);
        if (backingIndex != -1) {
            backingList.set(backingIndex, current);
        }

        if (old instanceof Element) {
            ((Element)old).setParent(null);
        }
        if (current instanceof Element) {
            ((Element)current).setParent(parent);  // null is OK
        }

        return super.set(index, current);
    }

In my opinion, PartialList needs to check for parentage, in this and all
other methods.  Brett, any reason it's not?

-jh-



More information about the jdom-interest mailing list