Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

RE: [jdom-interest] new element methods?

William Krick

2003-07-15

Replies:

Phill addressed this problem in his implementation a few emails back.

Essentially, his getXXX methods also take a default value that it returns in
the case where it fails...


getInt(String name, int default) {
int result;
try{
  result = Integer.parseInt(getChildTextTrim(name));
}
catch(Exception ex){
  result = default;
}
return result;
}


...
Krick






> -----Original Message-----
> From: jdom-interest-admin@(protected)
> [mailto:jdom-interest-admin@(protected)
> Sent: Tuesday, July 15, 2003 1:50 PM
> To: jdom-interest@(protected)
> Subject: Re: [jdom-interest] new element methods?
>
>
> The problem is that what to do when the parsing fails or the element is
> missing is largely application dependent. In your example
> implementations, you assume default values such as false, 0, and ""
> which may work great for your apps, but I may want to return NaN or an
> exception instead. It is a simple matter to create a class with static
> methods to implement your type safe methods as in:
>
> public abstract class MyApp
> {
>  public static final float getFloat( Element element, String name){
>   try{
>     return Float.parseFloat(element.getChildTextTrim(name));
>   }
>   catch(Exception ex){
>     return 0f;
>   }
>  }
> }
>
> ...
>
> float myFloat = MyApp.getFloat( e, name );
>
> If all of your apps require the same behavior, this is a write once and
> use forever class. However, if you need different logic for different
> apps, you are free to define as many of these parsing classes as
> required without getting JDOM involved. I suppose you could extend
> Element instead, but that seems like more work than it is worth for such
> a simple task.
>
> Bryan.
>
> --
> Bryan Thale
> Networks & Infrastructure Research, Motorola Labs
> mailto:bryan.thale@(protected)
>
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yourad
dr@(protected)

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.