[jdom-interest] Moving Subtrees using JDOM

Noam Tamim noamt at yahoo.com
Sun Feb 24 01:34:43 PST 2002

--- Jason Hunter <jhunter at acm.org> wrote:
> > Therefore, a utility method that gets a live FilterList will look
> > something like this one:
> > public static void moveContent(List content, Element to) {
> > The remaining problem is handling document fragments that are not
> > stored in one List (and may have different parents). It is not really
> > a problem - it can be solved quite easily in fact - the problem is
> > that one method can't deal with both cases. A method that, unlike
> > the above moveContent(), is given a random list of nodes, must cast
> > and call detach() on every item in it - but the detach must NOT be
> > from the same list (see point a above).
> Ah, filters will soon come to the rescue...

I'm not sure they will solve everything. The Filter.match(Object) 
method, as I understand, works like the FileFilter.accept(File) 
method. So if all objects (strings, elements, etc.) come from
the same FilterList, it'll work fine - for example, if I want
to move all elements that contain "foo" in their name, it'll be
easy to do it;  
But in the case mentioned above, it won't work - 
I want (do I? maybe nobody needs to do it anyway) to be able 
to pass a List of objects from different sources, detach them 
from their parents, and have them adopted by another. If the List
passed to the method is NOT a FilterList, this method will do:

public static void moveObjects(List objects, Element to) {
  Iterator i=objects.iterator();
  List list=to.getContent();
  while (i.hasNext()) {
    // detach(Object) is yet another simple utility method,
    // that does instanceof checks, casts, and detaches.

Thing is, it is very important that the first moveContent() will
be passed a FilterList, while the second moveObjects() is passed
a (non-Filter)List. A way of enforcing it is asking if the list
is a ContentList.FilterList, but that can only be done by members
of org.jdom, not by external utility methods.


Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games

More information about the jdom-interest mailing list