xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining
- From: Stefan Matthias Aust <sma AT 3plus4.de>
- To: xom-interest AT lists.ibiblio.org
- Subject: Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining
- Date: Tue, 17 Aug 2004 09:27:24 +0200
Just to give you different point of view...
Wolfgang Hoschek schrieb:
RFE: Element.appendChild(String text) should return "this" for chaining.
Knowing that some people feel strongly against chaining I'm not promoting using chaining everywhere and inappropriately, but there is one place where it would be very practical, as it arises very frequently
Actually, I'm all for chaining (as this is something I really miss from Smalltalk :) Originally, I wanted to use this to create a DOM:
return new Template()
.element("root").a("version", "1.0")
.when("getItems")
.element("items")
.loop("getItems", "each")
.element("item", new Var("each"))
.end()
.end()
.instantiate(bean);
but then agreed to use a more conservative approach:
Element e = a(element("root"), "version", "1.0");
if (!getItems().isEmpty()) {
Element e1 = element("items");
for (Iterator i = getItems().iterator(); i.hasNext();) {
append(e1, element("item", (String) i.next());
}
}
return document(append(e, e1));
using some static factory methods to make element construction easier (actually, I've also a factory method for the for-loop):
Element element(String name) {
return new Element(name);
}
Element element(String name, String value) {
return append(element(name), value);
}
Element a(Element e, String name, String value) {
e.addAttribute(new Attribute(name, value));
return e;
}
Element append(Element parent, Element child) {
parent.appendChild(child);
return parent;
}
Element append(Element parent, String text) {
parent.appendChild(text);
return parent;
}
I'd love to use var-args for element construction
Element element(String name, String... namesAndValues) {
...
}
but I'm not yet on 1.5.
> [...]
Do you immediately see what the code is about? Probably not.
If Element.appendChild(String text) would return this one could write this more understandably and consisely as follows.
IMHO the "chained" code is more readable.
However appending text nodes isn't such a big problem. Dealing with attributes can be quite annoying as you need to put the Element you want to add an attribute into a variable because addAttribute also returns void. A "withAttribute" method that returns the receiver might be handy.
bye
--
Stefan Matthias Aust // "Zweifel sind der Ansporn des Denkens..." -U
-
[XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining,
Wolfgang Hoschek, 08/16/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining,
Elliotte Rusty Harold, 08/16/2004
- Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining, Wolfgang Hoschek, 08/16/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining,
Stefan Matthias Aust, 08/17/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining,
Elliotte Rusty Harold, 08/17/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) shouldreturn "this" for chaining,
passani, 08/17/2004
- Re: [XOM-interest] RFE: Element.appendChild(String text) shouldreturn "this" for chaining, Stefan Matthias Aust, 08/17/2004
- Re: [XOM-interest] RFE: Element.appendChild(String text) shouldreturn "this" for chaining, John Cowan, 08/17/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) shouldreturn "this" for chaining,
passani, 08/17/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining,
Elliotte Rusty Harold, 08/17/2004
-
Re: [XOM-interest] RFE: Element.appendChild(String text) should return "this" for chaining,
Elliotte Rusty Harold, 08/16/2004
Archive powered by MHonArc 2.6.24.