[jdom-interest] detach() [eg]

Amy Lewis amyzing at talsever.com
Mon Apr 23 19:02:35 PDT 2001

On Mon, Apr 23, 2001 at 10:40:19AM -0400, Elliotte Rusty Harold wrote:
>At 10:48 PM -0400 4/22/01, Scott Means wrote:
>>It seems to me that one of Jason's major objections is the need to catch a
>>random new exception anytime he wants to use the .detach() method, e.g.:
>>>  >} catch (IllegalXXXException e) {

Note that the try/catch is unnecessary in any event if the exception
thrown is one of the suggested IllegalThisOrThatExceptions (they're all

>OK. Here's another proposal that comes from still another direction, 
>and avoids all exceptions. Suppose instead of letting a child detach 
>itself from its parent we let the parent element detach the child. In 
>other words the signature of the detach() method would now look 
>something like this:
>public void detach(Element toBeDetached)
>public void detach(Comment toBeDetached)
>public void detach(String toBeDetached)

Isn't this one a problem?

>public void detach(ProcessingInstruction toBeDetached)
>public void detach(Entity toBeDetached)
>We would simply not give Document a detach() method so the root 
>element could not be detached and no exceptions would ever be thrown 
>by detach()?  In fact when this discussion popped up this is how I 
>thought the detach() method actually worked, and I was a little 
>surprised when the JavaDoc told me otherwise.

I'd be +1 on this, except for the existence of bare Strings in JDOM. 
Since all text is stored as bare java.lang.String, separate child nodes
may end up indistinguishable.  Stupid example:

        <elementContentElement thisAttr="foo" thatAttr="bar" />-**-

The two strings are, to some degree, distinguishable, if you haven't
called trim(); (because of the indentation), but the distinction is
hard to rely upon, and the example should be clear enough anyway.

This particular example is both trivial and stupid; a better example
might be XHTML that includes some random text at two places in the same
mixed content parent (but a more realistic example would be harder to

Using a parent-based detach, in that case, means that the strings
(which are now being interned, no?) are effectively identical, which
means, in most implementations of List, that the first one always gets
detached, even if you actually meant to detach the second.

I still think Elliotte's idea here is preferable (I like the semantics
better anyway: detach(thatThing) instead of me.detach()), and suggest
that this might be one of the arguments for a more complex text storage
element than String.

Could we dispense with .detach() altogether?  Can't you call

Amelia A. Lewis          alicorn at mindspring.com          amyzing at talsever.com
The flesh is strong.  The spirit stronger.  So shed your skin, baby.
Let it through.  Come on over.
                                                -- Amy Ray

More information about the jdom-interest mailing list