[jdom-interest] setContent NOT setting parent correctly.

Dave Bartmess dingodave at edingo.net
Thu Oct 16 09:35:55 PDT 2003


Actually, it's not. The FAQ covers how to copy/move a single element,
but not the entire contents of an element. I don't want to clone the
element, I want to copy the contents of element A to element B,
replacing the existing contents in element B.

The problem is, I have over 70 classes which all need to be able to do
this. setContent(Collection) is specifically supposed to 1) remove the
existing contents of the element, detach the contents of the passed
element's ContentList, and add the resulting set of detached children to
the current element.

That's a LOT of coding to do for each class, and since the 70 classes I
ahve are all based off different (non-XML based) classes, I need to do
that for each one individually. And in some cases, I need to do that for
the children of those classes also, and that causes it to explode in
code base...

Maybe I'm going about this all wrong, but with over 300,000 lines of
code already written, I'm not about to re-design it... This should be a
simple one-line addition of code... set the contents of one element to
equal the contents of another. 

On Thu, 2003-10-16 at 14:06, Jason Hunter wrote:
> 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
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
-- 
David A. Bartmess
Software Configuration Manager / Sr. Software Developer
eDingo Enterprises
http://edingo.net




More information about the jdom-interest mailing list