Swati,

Which version of Castor are you using?

--Keith

Swati Singhal wrote:
Hi,

I have an XML file which we will be unmarshalling
using Castor.
If I write a CustomHandler, it works just fine.

However, we have the dates all over the XML, so I
decided to write a GeneralizedFieldHandler.
Now, my mapping.xml has an entry like:

<class name="com.dataaccess.beans.SoftBean">
 <field name="swTime" type="string"
handler="com.businesslogic.DateHandler">
  <bind-xml name="TIME" node="element"/>
 </field>
</class>

My DateHandler is exactly what is there on your site :

import
org.exolab.castor.mapping.GeneralizedFieldHandler;
import org.exolab.castor.mapping.FieldDescriptor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * The FieldHandler for the Date class
 *
 */
public class MyDateHandler
    extends GeneralizedFieldHandler
{

    private static final String FORMAT = "MM/dd/yyyy
hh:mm:ss";

    /**
     * Creates a new MyDateHandler instance
     */
    public MyDateHandler() {
        super();
    }

    /**
     * This method is used to convert the value when
the
     * getValue method is called. The getValue method
will
     * obtain the actual field value from given
'parent' object.
     * This convert method is then invoked with the
field's
     * value. The value returned from this method will
be
     * the actual value returned by getValue method.
     *
     * @param value the object value to convert after
     *  performing a get operation
     * @return the converted value.
     */
    public Object convertUponGet(Object value) {
        if (value == null) return null;
        SimpleDateFormat formatter = new
SimpleDateFormat(FORMAT);
        Date date = (Date)value;
        return formatter.format(date);
    }


    /**
     * This method is used to convert the value when
the
     * setValue method is called. The setValue method
will
     * call this method to obtain the converted value.
     * The converted value will then be used as the
value to
     * set for the field.
     *
     * @param value the object value to convert before
     *  performing a set operation
     * @return the converted value.
     */
    public Object convertUponSet(Object value) {
        SimpleDateFormat formatter = new
SimpleDateFormat(FORMAT);
        Date date = null;
        try {
            date = formatter.parse((String)value);
        }
        catch(ParseException px) {
            throw new
IllegalArgumentException(px.getMessage());
        }
        return date;
    }

    /**
     * Returns the class type for the field that this
     * GeneralizedFieldHandler converts to and from.
This
     * should be the type that is used in the
     * object model.
     *
     * @return the class type of of the field
     */
    public Class getFieldType() {
        return Date.class;
    }

    /**
     * Creates a new instance of the object described
by
     * this field.
     *
     * @param parent The object for which the field is
created
     * @return A new instance of the field's value
     * @throws IllegalStateException This field is a
simple
     *  type and cannot be instantiated
     */
    public Object newInstance( Object parent )
        throws IllegalStateException
    {
        //-- Since it's marked as a string...just
return null,
        //-- it's not needed.
        return null;
    }
}

However, when I run, i get an error:

Exception:unable to add 'TIME' to <SOFTWARE> due to
the following exception:
--- Begin Exception ---<<<

java.lang.IllegalArgumentException: Type conversion
error: could not set value of
[EMAIL PROTECTED] with value of
type java.lang.String
        at
org.exolab.castor.mapping.loader.FieldHandlerImpl.setValue(FieldHandlerImpl.java:500)
        at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1053)
        at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1121)
        at
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
        at
org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
        at
org.apache.xerces.parsers.DTDConfiguration.parse(Unknown
Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown
Source)

Any help would be appreciated.

Thanks

-Swati



__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------




-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to