[jdom-interest] setContent NOT setting parent correctly.
Jason Hunter
jhunter at servlets.com
Thu Oct 16 11:06:38 PDT 2003
This kind of thing is covered in the FAQ, btw.
-jh-
Dave Bartmess wrote:
> Oops, even that remedy won't work. I have to go through manually for
> each call to detach the contents... arghhh
>
> On Thu, 2003-10-16 at 11:50, Dave Bartmess wrote:
>
>>Actually, I was hoping that setContent would be fixed <grin> It's a
>>major part of my application, and it doesn't make much sense to even
>>have it in the API if it doesn't detach the nodes itself. Otherwise, you
>>can just do a removeContent() and then addAll(Collection).
>>
>>On Thu, 2003-10-16 at 13:40, Bradley S. Huffman wrote:
>>
>>>So we need to put a note in the javadocs that all nodes in the supplied
>>>collection must be detached?
>>>
>>>Brad
>>>
>>>"Dave Bartmess" writes:
>>>
>>>
>>>>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
>>>>
>>>>_______________________________________________
>>>>To control your jdom-interest membership:
>>>>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost
>>>>.com
>>>
>>>_______________________________________________
>>>To control your jdom-interest membership:
>>>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
More information about the jdom-interest
mailing list