Hi,

Here is a reply from Keith few days ago that might interest you:

Thanks,
Arnaud


> Arvind Gudipati wrote:
> 
> Im sorry if someone has already asked this question earlier!
> 
> I was trying to map the XML of the form below (xml not complete)
> .....
> <Item id="001">
>  <name> abc-001 </name>
> </Item>
> 
> <Item id="002">
>  <name> abc-002 </name>
> </Item>
> 
> to a java collection object java.util.Hashtable with
> "item id" as key and Item object as value.
> 
> I read through the documentation for Castor mapping of XML to java
> objects. It said and i quote:
>  "For hashtable and map, Castor will add an object with put(object,
> object): the object is both the key and the value. This will be
> improved in the future."
> 
> Is there any work around for this problem? Is it already fixed? If not
> any ideas when it will be fixed?

Hi Arvind, the behavior is still the same. There are a few workarounds,
However.

1. Abstract your collection by creating an add method for it:

    public void addItem(Item item) {
        items.put(item.getId(), item);
    }


2. If you can't change your object model then, if you use the CVS
version of Castor,
   you can write you own FieldHandler as a work around:

Assuming your class which holds the items is called Foo:

import org.exolab.castor.mapping.FieldHandler;
import org.exolab.castor.mapping.ValidityException;

/** 
 * The FieldHandler for the "item" field of the Foo class
**/
public class ItemFieldHandler 
    implements org.exolab.castor.mapping.FieldHandler 
{
   
   /**
    * Creates a new ItemFieldHandler
   **/
   public ItemFieldHandler() {
    super();
   } 
   
    /**
     * Returns the value of the field from the object.
     *
     * @param object The object
     * @return The value of the field
     * @throws IllegalStateException The Java object has changed and
     *  is no longer supported by this handler, or the handler is not
     *  compatiable with the Java object
     */
    public Object getValue( Object object ) 
        throws IllegalStateException 
    {

       return ((Foo)object).getItems();
    }
    

    /**
     * Sets the value of the field on the object.
     *
     * @param object The object
     * @param value The new value
     * @throws IllegalStateException The Java object has changed and
     *  is no longer supported by this handler, or the handler is not
     *  compatiable with the Java object
     * @thorws IllegalArgumentException The value passed is not of
     *  a supported type
     */
    public void setValue( Object object, Object value )
        throws IllegalStateException, IllegalArgumentException
    {
        Hashtable items = ((Foo)object).getItems();
        items.put( ((Item)value).getId(), value);
    }


    /**
     * Sets the value of the field to a default value.
     * <p>
     * Reference fields are set to null, primitive fields are set to
     * their default value, collection fields are emptied of all
     * elements.
     *
     * @param object The object
     * @throws IllegalStateException The Java object has changed and
     *  is no longer supported by this handler, or the handler is not
     *  compatiable with the Java object
     */
    public void resetValue( Object object )
        throws IllegalStateException, IllegalArgumentException
    {
        ((Foo)object).getItems().clear();
    }


    /**
     * @deprecated No longer supported
     */
    public void checkValidity( Object object )
        throws ValidityException, IllegalStateException 
    {
        //-- add validation check if desired
    }


    /**
     * 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
    {
        return new Hashtable();
    }
    
}

> 
> Any help is sincerely appreciated!

Hopefully one of those work arounds will work for you.

Thanks,

--Keith

-> -----Original Message-----
-> From: Isabelle Sieuv [mailto:[EMAIL PROTECTED]]
-> Sent: Tuesday, December 11, 2001 10:30 AM
-> To: [EMAIL PROTECTED]
-> Subject: [castor-dev] XML Data Binding
-> 
-> 
-> Hi All,
-> 
->    Could you tell me if it is possible to do a data binding from an
-> hashtable
-> instead of java bean ?
-> 
-> Any help will be greatly appreciated.
-> Best regards,
-> 
-> Isabelle S.
-> Email: [EMAIL PROTECTED]
-> 
-> ----------------------------------------------------------- 
-> If you wish to unsubscribe from this mailing, send mail to
-> [EMAIL PROTECTED] with a subject of:
->      unsubscribe castor-dev

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to