[jdom-interest] detach() [eg]
Steven D. Keens
skeens at planetfred.com
Wed Apr 18 08:01:20 PDT 2001
You know I don't agree with you here. Let's say through
a long series of calls (let's throw some side effects in
there) someone calls detach() on the root element and
eventually tries to output the document. Now, I agree
with you this is a bug and the REAL source of the bug
should be fixed. However, it's a bug that may not be
easy to fix so instead, because of time constraints, a
hack is applied just before outputting the document.
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.
I believe this scenario is actually quick likely to
happen when JDOM catches on and replaces DOM :-).
But rather than just scream and shout I'll offer another
solution to using the <placeholder> and fixing the fact
that we can't have a document without a root.. Have detach() throw
a RuntimeException. Of course that means either adding
a new exception to JDOM or using an existing one. In
the example below I use a new NoRootElementException
derived from RuntimeException
/**
* <p>
* This detaches the element from its parent, or does nothing if the
* element has no parent.
* </p>
*
* @return <code>Element</code> - this <code>Element</code> modified.
*/
public Element detach() {
Element p = getParent();
if (p != null) {
p.removeContent(this);
}
else {
Document d = getDocument();
if (d != null) {
throw new NoRootElementException( );
}
}
return this;
}
>You may not agree that it's worth it, but I don't agree that in JDOM's case
>that the api is in defiance of the programmer's expectations. Honestly, if
>I did something stupid like expecting my document to ouput after I emptied
>the content (naturally this would NEVER happen), I would find it far more
>useful viewing the broken document had a <placeholder /> element than the
>outputter throwing a NullPointerException that I would have to
>troubleshoot.
>>
>> In other words, if we're going to enforce well-formedness,
>> then we have
>> to enforce it (and in the process, take power and flexibility
>> away from
>> the programmer; it always happens that if you dull a knife to keep it
>> from cutting someone's hands, it's harder to cut the bread). We have
>> to enforce it *everywhere*, and not just here, in a place where it's
>> easy.
>
--
Steven Keens mailto:skeens at planetfred.com
PlanetFred Inc. http://www.planetfred.com
44 Byward Market, Suite 240, Ottawa, ON, K1N 7A2, Canada
More information about the jdom-interest
mailing list