[jdom-commits] Follow-on to FilterList

Jason Hunter jhunter at acm.org
Tue Dec 11 01:46:23 PST 2001

FYI, for those looking at the FilterList code that went into the last
check-in, we have two areas that can still be cleaned up a little.  Here
are two questions I asked Alex Rosen and his answers...


> In Document.java there are many checks regarding parentage and type.
> Shouldn't these be offloaded into FilterList now?

Element.java also, right?. That was my idea behind the "live list"
Currently, Element and Document store their children in regular
which means that there two list-modification entry points - the Element
Document methods like addContent() and removeContent(), and the
methods. Both must do the same checking, resulting in duplicate code.

If, instead E and D stored their children in a "live list" (e.g. a
FilterList kind of thing), then the E and D methods could just call
to the list methods, which would do the checking. This live list would
exactly the same one as would be returned by getContent().

Hmmm... actually, since AttributeFilter and ElementContentFilter and
DocumentContentFilter have their important code in static methods, I
Element and Document can call these static methods directly, to
most of the duplicate code.

> In FilterList.java there are two comments like this:
>         // XXX leaves list in an inconsistent state if checkAdd() fails
>         Iterator iter = c.iterator();
>         while (iter.hasNext()) {
>             checkAdd(iter.next());
>         }
> But isn't that no longer true since it looks like the data is
> pre-checked?

The comment isn't very clear. The problem is that checkAdd() both checks
it's OK, and adjusts the parentage. So if the last item is not OK, the
before it will have their parent set, but will not actually be added as
child of the parent object. The solution is to either separate these two
functions into two methods, and pre-check all objects before making any
changes; or to do some (kinda ugly) post-failure cleanup, like
Element.setContent() currently does.

More information about the jdom-commits mailing list