[jdom-interest] setContent NOT setting parent correctly.

Dave Bartmess dingodave at edingo.net
Thu Oct 16 08:13:38 PDT 2003


I'm using JDOM b10 (latest from CVS). 

>From looking at the javadocs, Element.setContent(Collection) SHOULD set
the parent of each of the passed objects in the Collection to the
calling instance.
>From the javadocs:
In addition, all objects in the supplied List will have their parentage
set to this element, but the List itself will not be "live" and further
removals and additions will have no effect on this elements content.

But, from trying the code below, this is definitely NOT the case.
Instead, it throws an IllegalAddException because in the calls to (in
sequence) Element.setContent, ContentList.clearAndSet,
ContentList.addAll, ContentList.add(Object), and finally
ContentList.add(Element), nothing resets the passed List members'
parent, and thus throws the exception.

Therefore, setContent cannot work, unless you first go into the passed
Collection, and one by one, set the parent to null for each object.

Try the simple test class below, and you'll see what I mean:

import org.jdom.*;

public class Chance_1 extends Element {
   public Chance_1() {
      super("Chance");
      setDefaults();
   }
   public Chance_1(Chance_1 newchance) {
      super("Chance");
      setDefaults2();
      this.setContent(newchance.getContent());
   }
   
   public void setDefaults() {
      for (int i = 0; i < 10; i++) {
         Element child = new Element("Test" + i);
         child.setText("This is child " + i);
         this.addContent(child);
      }
   }
   
   public void setDefaults2() {
      for (int i = 100; i < 110; i++) {
         Element child = new Element("Test" + i);
         child.setText("This is child " + i);
         this.addContent(child);
      }
   }
   
   public static void main(String args[]) {
      Chance_1 test = new Chance_1();
      Chance_1 test2 = new Chance_1(test);
   }
}

-- 
David A. Bartmess
Software Configuration Manager / Sr. Software Developer
eDingo Enterprises
http://edingo.net




More information about the jdom-interest mailing list