[jdom-interest] How to "crash" JDOM, part 1

trebor.a.rude at lmco.com trebor.a.rude at lmco.com
Thu Aug 31 10:35:43 PDT 2000

	After thinking about this a little more, I suspect we may be making
things harder than they need to be. Given the existing constraint that you
can't add an Element that already has a parent, I can't see any way that
it's possible for an Element to have a pointer to any of its ancestors in
its content list. The only degenerate case I can see is
"element.addContent(element)" on a newly created Element, which can be
solved with a simple check for self-reference in addition to the check for a
null parent. Did I miss a case?

	BTW, that getRootElement() function might be useful, regardless of
whether we need it for solving this particular problem.

Trebor A. Rude
trebor.a.rude at lmco.com

> -----Original Message-----
> From:	David W. Smiley [SMTP:dsmiley at mitre.org]
> Sent:	Thursday, August 31, 2000 11:00 AM
> To:	trebor.a.rude at lmco.com; jdom-interest at jdom.org
> Cc:	Jason Hunter
> Subject:	Re: [jdom-interest] How to "crash" JDOM, part 1
> (corrected subject line)
> I just now realized this is overall a more complicated issue.  I've
> decided to present the following _pseuodocode_ instead:
> private:
> /** If null, is a "root element", possibly a "document root element". */
> Element parentElement= null;
> /** Means that this element and all childs are bound to a document. To
> be true, parentElement must be null. */
> boolean isDocRootElement= false;
> //not same as docRootElement
> package-private:
> Element getRootElement() {
>   return (parentElement == null ? this : parent.getRootElement());
> }
> public:
> addElement(Element b) {
>   if (b.parentElement == null && b != getRootElement())
> 	...add element...
>   else throw 
> }
> removeElement(Element b) {
>   if (b not a child of this)
> 	throw
>   b.parentElement= null;
> }
> Element copyElement() {
>   b= super.clone();
>   ...set other fields, leaving parentElement as null and
> isDocRootElement as false...
> }
> Questions?  Comments?  The only real time-hit here is getRootElement(),
> but that will work really fast, especially considering documents don't
> get that deep.  Each ancestor in-between this and getRootElement doesn't
> need to be checked because they all have an parent whereas 'b' doesn't.
> -- David Smiley

More information about the jdom-interest mailing list