Apologies! I intended this to go to the list, it went only to Jakob.
         - Thomas.

At 18:45 16/06/2001 +0200, Jakob Jenkov wrote:
>I can fully support having adds and also removes return a reference to the
>element added or removed!! You can always forget about the returned
>reference if you don't want to use it. James ??

Actually I'd prefer the return value to be the object _to which_ an item 
has been added or removed: i.e.

=== AbstractElement.java ===
package org.dom4j.tree;
public class AbstractElement {
     Element add (Widget w) {
         // ... add widget to self
         return this;
     }
}


This allows me to do things like this:

Element complexType = new DefaultElement ("complexType") . add (new 
DefaultAttribute ("mixed", "true"));

If the return type were the added object, the RHS would  return a 
DefaultAttribute, which is not what I want. The RHS currently evaluates to 
void, which is _definitely_ not what I want :)

With the "return self" syntax, you can build quite complex but easily 
understood trees without needing to explicitly declare every node (i.e. 
without lots of "Element tempElement = ..." statements). For example:

Element complexType = new DefaultElement ("complexType") . add (new 
DefaultAttribute ("mixed", "true"))
     .add (new DefaultElement ("sequence")
           .add ((new DefaultElement ("element") . add (new 
DefaultAttribute ("ref", "xyz")))));
This matches the visual appearance (conceptual tree) of
<complexType mixed="true">
     <sequence>
         <element ref="xyz" />
     </sequence>
</complexType>

The alternative reads something like:
Element complexType = new DefaultElement ("complexType");
Attribute complexTypeAttrib1 = new DefaultAttribute ("mixed", "true");
Element sequence = new DefaultElement ("sequence");
Element elementRef = new DefaultElement ("element");
elementRef.add (new DefaultAttribute ("ref", "xyz"));
sequence.add (elementRef);
complexType.add (sequence);


Having been doing this for the last couple of hours, I'd definitely prefer 
the 3-line version to the 7 line version!

And as Jakob says, you can still do
add (new DefaultAttribute (...));
and disregard the return type.

What do others think? Hideous hack, or worth changing?

Regards,
Thomas.


_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to