Hi,

Could someone explain why when I run the test class the value of the other
date field is always null.

value of startEventDate is OK.
value of endEventDate is always NULL.


// MyOrder bean class

package sample;

public class MyOrder {

        private ClientData clientData;

        public ClientData getClientData() {
                return clientData;
        }

        public void setClientData(ClientData clientData) {
                this.clientData = clientData;
        }

}

---------

// ClientData  bean class

package sample;

public class ClientData {
        
        private java.util.Date startEventDate;
    private java.util.Date endEventDate;

        public java.util.Date getStartEventDate() {
                return startEventDate;
        }
        public void setStartEventDate(java.util.Date startEventDate) {
                this.startEventDate = startEventDate;
        }
        public java.util.Date getEndEventDate() {
                return endEventDate;
        }
        public void setEndEventDate(java.util.Date endEventDate) {
                this.endEventDate = endEventDate;
        }
        
}

----------------

//From the castor user guide..

package sample;

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

import org.exolab.castor.mapping.ConfigurableFieldHandler;
import org.exolab.castor.mapping.GeneralizedFieldHandler;
import org.exolab.castor.mapping.ValidityException;

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

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

    public void setConfiguration(final Properties config) throws
ValidityException {
        String pattern = config.getProperty("date-format");
        if (pattern == null) {
                throw new ValidityException("Required parameter \"date-format\" 
is
missing for FieldHandlerImpl.");
        }
        try {
                formatter = new SimpleDateFormat(pattern);
        } catch (IllegalArgumentException e) {
                throw new ValidityException("Pattern \""+pattern+"\" is not a 
valid
date format.");
        }
    }
    /**
     * 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;
    }

}


---------------------------------

<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version
1.0//EN" "http://castor.exolab.org/mapping.dtd";>

<mapping>
        <description>Description of the mapping</description>
        
        <field-handler name="myHandler" class="sample.MyDateHandler">
        
    </field-handler>

        <class name="sample.MyOrder">
                <map-to xml="myOrder"/>
                
                <field name="clientData" type="sample.ClientData"/>
        </class>
        
        <class name="sample.ClientData">                
                <field name="startEventDate" type="string" handler="myHandler">
              <bind-xml name="startEventDate" node="element"/>
            </field>

                <field name="endEventDate" type="string" handler="myHandler">
              <bind-xml name="endEventDate" node="element"/>
            </field>
        </class>
</mapping>

---------------
sample data

<?xml version="1.0"?>
<myOrder>
        <clientData>
                <startEventDate>2010-08-08 16:54:41</startEventDate>
                <endEventDate>2010-08-08 16:54:42</endEventDate>
        </clientData>
</myOrder>

-----------------

Test class

package sample;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;

public class Test2 {

        public Test2() {
                processFile();
        }
        
        public static void main(String[] args) throws MarshalException,
                ValidationException, IOException, MappingException {
                new Test2();
        }

        public void processFile() {
                try {
                        // -- Load a mapping file
                // Load the mapping file
                        Mapping _mapping = new 
Mapping(getClass().getClassLoader());
                String MAPPING_FILE = "mapping.xml";
                        
_mapping.loadMapping(getClass().getResource(MAPPING_FILE));
        
                        Unmarshaller un = new Unmarshaller(MyOrder.class);
                        un.setMapping(_mapping);
        
                        // -- Read in the Addressbook using the mapping
                        Reader reader = new BufferedReader(new 
FileReader("test.xml"));
                        MyOrder root = (MyOrder) un.unmarshal(reader);
        
                        // -- Display the Event Type
                        
System.out.println(root.getClientData().getStartEventDate());
                        
System.out.println(root.getClientData().getEndEventDate());
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}



Can anyone help me on this..

Thanks

-- 
View this message in context: 
http://www.nabble.com/Nul-value-returned-from-field-handler-tp23342019p23342019.html
Sent from the Castor - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to