[jdom-interest] detach() [eg]

Joseph Bowbeer jozart at csi.com
Wed Apr 18 12:14:16 PDT 2001

Patrick Dowler writes:

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

One point of clarification:

Any time we add a singleton like MISSING_ROOT_ELEMENT, intending to use ==
comparison, we also need to tweak the serialization so that we don't
accidentally create multiple copies.

To implement this in Element, we'd need to use readResolve.

NOTE: readResolve does not exist in JDK1.1.

For example, the following implementation resolves based on the name string:

  protected Object readResolve() throws ObjectStreamException {
      return getName().equals.MISSING_ROOT_ELEMENT.getName() ?
          MISSING_ROOT_ELEMENT : this;

In the specific case of MISSING_ROOT_ELEMENT, we might be able to do the
fixup in the Document serialization code.  Not sure..

----- original message -----
From: Patrick Dowler patrick.dowler at nrc.ca
Date: Wed, 18 Apr 2001 09:27:00 -0700

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