[jdom-interest] detach() [eg]
Elliotte Rusty Harold
elharo at metalab.unc.edu
Sun Apr 22 15:05:20 PDT 2001
>Elliotte Rusty Harold wrote:
>>
>> I propose the following compromise:
>>
>> 1. If detach() is called on the root element, then an exception is thrown.
>
>Current mneumonic:
>addContent(elt.clone()); // copy
>addContent(elt.detach()); // move
>
>Proposed mneumonic:
>addContent(elt.clone()); // copy
>try {
> addContent(elt.detach()); // try a move
>} catch (IllegalXXXException e) {
> elt.getDocument().setRootElement(new Element("filler"));
> addContent(elt);
>}
>
>I prefer the former.
>
I question the use case. How many times do you want to move the root
element of a document? In any case, it seems to me that all we're
saying is that when an element is used as the root the Document has
locked it and won't let it be moved without a replacement. That seems
perfectly reasonable to me.
However, this is not the problem. The problem is the phantom element
JDOM attaches to the root after detaching the root. That seems really
ugly and unexpected to me. I had no idea JDOM was even doing that
until this thread popped up. I think it violates the principle of
least surprise. There is simply no way to predict that JDOM is going
to create a new root element behind my back with an unknown name and
assign it to the Document. It seems a lot less surprising to me that
JDOM tosses an exception when I try to detach the root element.
Furthermore, the proposed semantics of detach seem quite clear: if
the parent is an element, then remove it from the parent. If the
parent is a document throw an exception. The logical distinction
between the two cases and the reason they behave differently is clear.
--
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
| The XML Bible (IDG Books, 1999) |
| http://metalab.unc.edu/xml/books/bible/ |
| http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/ |
+----------------------------------+---------------------------------+
| Read Cafe au Lait for Java News: http://metalab.unc.edu/javafaq/ |
| Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/ |
+----------------------------------+---------------------------------+
More information about the jdom-interest
mailing list