[jdom-interest] getText() and getAttributeValue()

Bart Read bart at wdi.co.uk
Wed Feb 13 02:16:55 PST 2002

> > getAttributeValue() should return empty string if the attribute does not
> > exist because:
> > 1) It makes it more consistent with getText()
> > 2) One won't have to check for null on the return value.
> >
> > Right now, my code is littered with:
> > String name = Parse.assure(elem.getAttributeValue("name"));
> > String id = Parse.assure(elem.getAttributeValue("id"));
> > // etc.
> >
> > ...where Parse.assure(String str) returns empty string if str is null;
> > str unchanged if it is non-null. It sure would be nice to get rid of all
> > those checks.
> What you'd really want is getAttributeValue(String name, String
> fallback).  It's even more powerful since "" doesn't have to be the
> fallback.  We had that before but it was removed since people didn't
> like its ability to "mask errors".  I personally would rather have it.

Actually I really like this idea.  Sure, one side effect of this is that is
could "mask" errors, but really no more so than for example the
getProperty(String key, String defaultValue) method of the
java.util.Properties class.  One situation where it WOULD be very useful is
if you have an attribute that should have a default value -- you could
assign it without having to either (i) turn on validation, or (ii) do
something like:

String value = element.getAttributeValue("myAttribute");
value = (value != null ? value : "default");

which is a maybe a wee bit cumbersome.  Certainly, I'd be in favour of
re-introducing this method.  However, what I definitely DON'T want to do is
spark off a huge debate over an issue that's already been discussed in
considerable depth, and which many people are happy about, so if the
consensus is "no" then I'll happily go with that.

Bart Read

---------- Automatically inserted by WDI Mailserver ----------------
This message has been scanned for virus, trojans, illegal relays and
spam. If you have any comments, problems or questions please do not
hesitate to contact:
Abbotsbury Software Ltd
Tel: +44 1305 871543 Fax: +44 1305 871688
Email: postmaster at wdi.co.uk
Web: http://www.wdi.co.uk/virus.phtml

More information about the jdom-interest mailing list