[jdom-interest] Moving elements

Rolf Lear rlear at algorithmics.com
Thu May 1 04:38:21 PDT 2003


I have used the following code in the past .....

Basically, because DTD's are cumbersome if the child elements are not in a
particular order, I wrote the following code to re-order the child elements
to conform to the expected order of the DTD. Does both of your criteria,
detatch, re-order. It is memory-hungry in the sense that it is recursive,
and the memory is kept during recursion. The "plus" side for me is that it
reorders just the elements, and any "mixed" content is left in the same
order. This is obviously application specific, so YMMV.

Rolf

    private static void reorderElements (Element element, Comparator comp) {
        if (element.getChildren().isEmpty()) return;
        
        // Try to do the whole thing without a clone... Use detach only.
        
        LinkedList list = new LinkedList();
        LinkedList kids = new LinkedList();
        List content = element.getContent();
        
        while (!content.isEmpty()) {
            // This removes and detaches element.
            Object o = content.remove(0);
            // add the detached object to our list, in same order.
            list.add(o);
            // add to our list of kids, if element
            if (o instanceof Element) kids.add(o);
        }
        Collections.sort(kids, comp);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) instanceof Element) {
                Element rep = (Element)kids.remove(0);
                reorderElements(rep, comp);
                list.set(i,rep);
            }
        }
        element.setContent(list);
    }

-----Original Message-----
From: Bradley S. Huffman [mailto:hip at a.cs.okstate.edu]
Sent: Thursday, May 01, 2003 12:00 AM
To: Jason Hunter
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Moving elements 


Jason Hunter writes:

> 2) We could have a new method like this on Element and Document:
> 
>   List element.getContentDetached()
> 
> It would return a list of the detached content.  Then you could do this:
> 
> heading.setContent(anchorList.getContentDetached());

Definitely +1, but I'd just go ahead and name it removeContent().

> 3) We could change the add semantics.  David Flanagan suggested this,
> and it's how DOM does things.  If you add an element somewhere and the
> element already has a parent, the new parent just trumps it.  Compare
> that to right now we throw an IllegalAddException.  If we changed
> behaviors, it would make nice code:

Better documentation is also a solution :) But if the remove/add sequence is
not convenient enough, I'd prefer a moveContent(...) on Element instead of
changing the add semantics.

    heading.moveContent(textNode); // Move textNode to end of heading's
                                   // content list.

> heading.setContent(anchorList.getContent());

Hmmm, does that a) detach the content from anchorList or b) make
a copy of anchorList before setting heading's content.  It's not clear just
by looking at it. I like

    heading.setContent(anchorList.removeContent());

> Sorting works naturally also.  The big issue is whether you expect this
> sort of auto-detachment.  I guess I wouldn't, but maybe I've used JDOM
> too long.  :-)

I wouldn't either,  and besides

    List list = anchorList.removeContent();
    // do the sort
    heading.setContent(list);

is very clear about what is actual being done.

Brad
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030501/33d7a413/attachment.htm


More information about the jdom-interest mailing list