cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonas Ekstedt <ekst...@ibg.uu.se>
Subject Re: [RT] Attribute Rendering and CForms Convertors
Date Wed, 17 Nov 2004 05:12:15 GMT
On Tue, 2004-11-16 at 08:57 +0100, Bruno Dumon wrote:
> On Sat, 2004-11-13 at 05:50 +0100, Jonas Ekstedt wrote:
> > On Wed, 2004-11-10 at 12:15 +0100, Daniel Fagerstrom wrote:
> <snip/>
> > 
> > How about an interface like this:
> > 
> > public interface Convertor {
> > 
> >     public String getId();
> >     public void setId(String id);
> >     public Class getTypeClass();
> >     public String convertToString(Object value, Locale locale) 
> > 	throws Exception;
> >     public Object convertFromString(String value, Locale locale) 
> > 	throws Exception;
> > 
> > }
> > 
> > Each convertor has an id (useful for error reporting when conversion
> > fails) and a typeClass (ie the class type it converts to/from). Those
> > convertors that need extra configuration would implement Configurable.
> 
> So if convertFromString fails, you'd throw an exception? A user who
> enters something invalid is far from an exceptional condition, that's
> why currently the ConversionResult is returned which includes this
> information (exceptions are rather heavy to create).

It would be up to the component that uses the convertor to decide what
to do in case of an exception. For example we could have:

public class MyIntegerBean {
  Convertor convertor;
  String unparsedInteger;
  Integer integer;

  public MyIntegerBean() {
    ConvertorManager convertorManager = ...
    convertor = convertorManager.getConvertor("java.lang.Integer");
  }

  public void setInteger(Integer integer) {
    this.integer = integer;
  } 

  public void setInteger(String intStr, Locale locale) {
    try {
      integer = convertor.convertFromString(intStr, locale);
      unparsedInteger = null;
    } catch (Exception e) {
      unparsedInteger = intStr;
    }
  } 

  public Integer getInteger() {
    if (unparsedInteger != null)
      throw new RuntimeException("Integer is in an unparsed state");
    else
      return integer;
  }

  public String getInteger(Locale locale) {
    if (unparsedInteger != null) 
      return unparsedInteger;
    else
      return convertor.convertToString(integer, locale);
  }
}

Whether the error condition should be reported via an exception or a
conversion result I really don't know. I'm a bit of a novice  when it
comes to exception handling and optimization (as you might have noticed
if you look through any of my code ;) ).

Cheers Jonas


Mime
View raw message