[jdom-interest] attributes

Jason Hunter jhunter at servlets.com
Wed Jan 30 14:18:53 PST 2002


allvard Tratteberg wrote:
> 
> Jason,
> 
> > elt.getAttribute("foo").setValue("bar") will not drop the existing
> attribute reference.
> >
> > elt.setAttribute(new Attribute("foo", "bar")) of course will do a
> replacement.
> >
> > elt.setAttribute("foo", "bar") is a convenience method for the above
> > syntax, which is why it does a replacement.  One could argue it
> > shouldn't, and one can argue it should.
> 
> I would've thought the latter was equivalent to
> Attribute attr = elt.getAttribute("foo");
> if (attr != null)
>    attr.setValue("bar);
> else
>    elt.setAttribute(new Attribute("foo", "bar"));
> 
> What is the motivation for the "always replace" logic? I don't get it. If
> the reasons are good, perhaps we need an ensureAttribute method?

Reason one:

Why should setAttribute(Attribute) be different than
setAttribute(String, String)?  I personally would think
Element.setAttribute(String, String) would be a wrapper around
Element.setAttribute(Attribute).

Reason two:

Maybe someone else has a reference to the original Attribute and
changing that object's state indirectly (by a call to its parent) is not
desirable if it can be avoided.

I don't think we need another method.  You can always just write the
code you gave if you really care about this.

-jh-



More information about the jdom-interest mailing list