[jdom-interest] detach() [eg]

Patrick Dowler patrick.dowler at nrc.ca
Wed Apr 18 09:27:00 PDT 2001


On 18 April 2001 08:01, Steven D. Keens wrote:
> In a hack situation I would rather have to catch an
> exception than test if the root element is a <placeholder>
> element.  Also, what happens if the <placeholder> is a
> legal root element?  There is no way of knowing if it was
> placed there because of the detach().  At least with
> an exception you always know your document is not well
> formed or not.

The standard way of handling this in java appears to be

class Element {
   public static final Element MISSING_ROOT_ELEMENT 
                        = new Element("DOCUMENT_HAS_NO_ROOT_YOU_FOO");

  ...
}

and then use Element.MISSING_ROOT_ELEMENT as the placeholder.
I'm not sure what happens when someone tries to call a mutator on
the above static instance... one would probably want a subclass that
threw (RuntimeException? UnsupportedOperationException?) on all 
mutator access... any use of a placeholder has this problem: the code might
blindly use it :-(

I'm not saying it is particularly elegant or anything, but you can make it
easily distinguishable from all code- or xml-supplied elements. 

It does seem that throwing an exception on root.detach() makes it hard
for code to do some things: nuke current content, try to call GC, then
create new content (alternative is to get rid of the Document as well,
but it might contain  stuff one wants to keep...).

just some thoughts from a user,

-- 
Patrick Dowler
Canadian Astronomy Data Centre
National Research Council
Victoria, BC



More information about the jdom-interest mailing list