[jdom-interest] More changes
Patrick Dowler
Patrick.Dowler at nrc.ca
Wed Jul 19 17:16:00 PDT 2000
On Wed, 19 Jul 2000, you wrote:
> > > On a somewhat related note, I'm really starting to like the idea of
> > > getChildContent(String childName) and getChildContent(String childName,
> > > String fallback). These methods would greatly simplify my coding when
> > > reading config files.
snip
> Not any better imho. Now let's try my proposal:
>
> Element ip = servlet.getChild("init-param");
> String name = value = desc = null;
> if (ip != null) {
> name = ip.getChildContent("param-name"); // may return null
> value = ip.getChildContent("param-value");
> desc = ip.getChildContent("description", "No description");
> }
> if (name != null && value != null) {
> // Handle good input here
> }
> else {
> // Handle missing elements
> }
The same thing can be accomplished with a single method in your
code or a utility class without burdening Element:
public String ensureContent( Element e, String default )
{
if ( e == null )
return default;
return e.getContent();
}
Then the above example becomes:
Element ip = servlet.getChild("init-param");
String name = value = desc = null;
if (ip != null) {
name = ensureContent( ip.getContent("param-name"), null );
value = ensureContent( ip.getContent("param-value"), null );
desc = ensureContent( ip.getContent("description") , "No description");
}
if (name != null && value != null) {
// Handle good input here
}
else {
// Handle missing elements
}
Having the code in the ensureContent (bad name) method as part of the
above code is what makes the current situation nasty, but it can be put
elsewhere: add it to Element, in the form of getChildContent() or add it
to a utility class. The latter has the advantage that the same thing could be
done with getXXXContent() methods that might return a default value for
people who don't care about syntax errors.
IMO these utility methods can be easily done in a utility class and keep the
Element API lean.
--
Patrick Dowler
Canadian Astronomy Data Centre
More information about the jdom-interest
mailing list