Hi All, Thanks to all your suggestions, I got myHandler to work for java.util.Calendar. But, If have an empty date <BirthDate></BirthDate> it gives a nullpointer exception. Any idea on what I should do in convertUponGet and convertUponSet or anywhere else to get rid of this problem(I tried to return null in the event of an empty value, but it doesn't seem to be the cure)?
Thanks, Rajkumar -----Original Message----- From: Keith Visco [mailto:[EMAIL PROTECTED]] Sent: Thursday, November 14, 2002 5:28 PM To: [EMAIL PROTECTED] Subject: Re: [castor-dev] FieldHandler question "Alagesan, Rajkumar" wrote: > > Hi, > That was helpful. Now convertUponGet in myHandler is called only > once. But it's called with the current instance(date,time) of Calendar > instead of what's found in the xml file. convertUponGet should only be called during Marshalling and validation, so the value will always be the instance from the object model and not from the XML. The value from the XML file will be passed into convertUponSet during unmarshalling. Unmarshalling is the act of creating the object model from an XML instance, marshalling is the opposite, the act of creating the XML instance from the object model. convertUponGet is used during marshalling and validation, or when comparing values for reuseable objects for unmarshalling. convertUponSet is only used during unmarshalling. --Keith Also, if I don't give > any value for the BirthDate field, I get the following error:- > > java.lang.IllegalArgumentException: Type conversion error: could not set > value of myHandler@48cc8c w > ith value of type java.lang.String > at > org.exolab.castor.mapping.loader.FieldHandlerImpl.setValue(Unknown Source) > at org.exolab.castor.xml.UnmarshalHandler.endElement(Unknown Source) > at > org.apache.xerces.parsers.SAXParser.endElement(SAXParser.java:1392) > at > org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator > .java:1480) > at > org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XM > LDocumentScan > ner.java:1149) > at > org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner. > java:381) > at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1081) > at org.exolab.castor.xml.Unmarshaller.unmarshal(Unknown Source) > at org.exolab.castor.xml.Unmarshaller.unmarshal(Unknown Source) > at Test.main(Test.java:29) > >>>---- End Exception ----<<< > {file: [not available]; line: 4; column: 28} > java.lang.NullPointerException > > Thanks for your time, > Rajkumar > > -----Original Message----- > From: Keith Visco [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 14, 2002 4:05 PM > To: [EMAIL PROTECTED] > Subject: Re: [castor-dev] FieldHandler question > > "Alagesan, Rajkumar" wrote: > > > > Hi, > > I changed the mapping.xml and it seems to work a little. but a problem. > > convertUponGet() method in myHandler is called three times. > > The extra calls might be done during validation. If you want to verify > this, you can disable validation [marshaller#setValidation(false)]. Also > if you are unmarshalling and you have setReuseObjects = true, simple > values are compared to see if the value needs to be set, so an > additional call might be done then as well. > > > The first time, when it is called, it prints the correct date > > as in the XML. the next two times, it prints the current date and > > time. > > > > Any Idea why it calls this method three times > > See my explaination above. > > > and why it calls with the current date and time in the second and third > call? > > This I don't know, I'd have to see an example and investigate it > further. My > example for GeneralizedFieldHandler is working fine for me. > > --Keith > > > > > Thanks, > > Raj > > public java.lang.Object convertUponGet(java.lang.Object value){ > > Calendar c = (Calendar)value; > > > > Date d = c.getTime(); > > System.out.println("Date " +d); > > SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd"); > > String s = formatter.format(d); > > > > return s; > > > > } > > > > -----Original Message----- > > From: Keith Visco [mailto:[EMAIL PROTECTED]] > > Sent: Thursday, November 14, 2002 12:09 PM > > To: [EMAIL PROTECTED] > > Subject: Re: [castor-dev] FieldHandler question > > > > Hi Rajkumar, > > > > You need to specify the type that your handler is expecting to recieve: > > > > <field name="BirthDate" type="string" handler="myHander"/> > > > > Your special FieldHandler will convert the string into the Date I > > assume. > > > > --Keith > > > > "Alagesan, Rajkumar" wrote: > > > > > > Going by the message it appears that the castor tries to > > > construct the calendar object(and since it's an abstract class, > > > it can't create one). But, I wonder why would castor even > > > try instantiating when I have the field handler written for > > > the field. Is it that my field handler never gets called before > > > castor tries to create an instance of that type? > > > > > > Rajkumar > > > > > > -----Original Message----- > > > From: Alagesan, Rajkumar > > > Sent: Wednesday, November 13, 2002 3:12 PM > > > To: [EMAIL PROTECTED] > > > Subject: [castor-dev] FieldHandler question > > > > > > Hi, > > > > > > I try to use java.util.Collection for an xml field 'Birthdate' and wrote > > my > > > Field > > > Handler. > > > > > > I have written a mapping file that looks like > > > > > > <class name="org.ets.enterprise.profile.valueobject.PersonValueObject" > > > > <field name="LanguageTypeCode" type="string"/> > > > <field name="BirthDate" type="java.util.Calendar" > handler="myHandler"/> > > > </class> > > > > > > When I try to unmarshal the Xml, I get the following error > > > > > > java.lang.IllegalStateException: Java schema change: Class > > > java.util.Calendar can no longer be const > > > ructed: java.util.Calendar{file: [not available]; line: 3; column: 16} > > > java.lang.NullPointerException > > > > > > my xml data file looks like this:- > > > > > > <?xml version="1.0" encoding="UTF-8"?> > > > <person-value-object> > > > <BirthDate>2002-10-3 11:10:50</BirthDate> > > > <languagetypecode>1</languagetypecode> > > > </personvalueobject> > > > > > > myHandler.java code > > > > > > import org.exolab.castor.mapping.GeneralizedFieldHandler; > > > import java.text.SimpleDateFormat; > > > import java.util.Date; > > > import java.util.Calendar; > > > public class myHandler extends GeneralizedFieldHandler{ > > > public java.lang.Class getFieldType(){ > > > return java.util.Calendar.class; > > > } > > > public java.lang.Object convertUponSet(java.lang.Object value){ > > > Calendar c = null; > > > try{ > > > SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-yy > > > hh:mm:ss"); > > > Date d = formatter.parse((String)value); > > > c= Calendar.getInstance(); > > > c.setTime(d); > > > } > > > catch(Exception e){ > > > System.out.println("setvalue"+e); > > > } > > > return c; > > > > > > } > > > public java.lang.Object convertUponGet(java.lang.Object value){ > > > Calendar c = (Calendar)value; > > > Date d = c.getTime(); > > > SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-yy > > > hh:mm:ss"); > > > String s = formatter.format(d); > > > return s; > > > > > > } > > > } > > > > > > Let me know where I am going wrong? > > > > > > Thanks, > > > Raj > > > > > > > ************************************************************************** > > > This e-mail and any files transmitted with it may contain privileged or > > > confidential information. It is solely for use by the individual for > whom > > > it is intended, even if addressed incorrectly. If you received this > e-mail > > > in error, please notify the sender; do not disclose, copy, distribute, > or > > > take any action in reliance on the contents of this information; and > > delete > > > it from your system. Any other use of this e-mail is prohibited. Thank > you > > > for your compliance. > > > > > > ----------------------------------------------------------- > > > If you wish to unsubscribe from this mailing, send mail to > > > [EMAIL PROTECTED] with a subject of: > > > unsubscribe castor-dev > > > > > > > ************************************************************************** > > > This e-mail and any files transmitted with it may contain privileged or > > > confidential information. It is solely for use by the individual for > whom > > > it is intended, even if addressed incorrectly. If you received this > e-mail > > > in error, please notify the sender; do not disclose, copy, distribute, > or > > > take any action in reliance on the contents of this information; and > > delete > > > it from your system. Any other use of this e-mail is prohibited. Thank > you > > > for your compliance. > > > > > > ----------------------------------------------------------- > > > 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 > > > > ************************************************************************** > > This e-mail and any files transmitted with it may contain privileged or > > confidential information. It is solely for use by the individual for whom > > it is intended, even if addressed incorrectly. If you received this e-mail > > in error, please notify the sender; do not disclose, copy, distribute, or > > take any action in reliance on the contents of this information; and > delete > > it from your system. Any other use of this e-mail is prohibited. Thank you > > for your compliance. > > > > ----------------------------------------------------------- > > 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 > > ************************************************************************** > This e-mail and any files transmitted with it may contain privileged or > confidential information. It is solely for use by the individual for whom > it is intended, even if addressed incorrectly. If you received this e-mail > in error, please notify the sender; do not disclose, copy, distribute, or > take any action in reliance on the contents of this information; and delete > it from your system. Any other use of this e-mail is prohibited. Thank you > for your compliance. > > ----------------------------------------------------------- > 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 ************************************************************************** This e-mail and any files transmitted with it may contain privileged or confidential information. It is solely for use by the individual for whom it is intended, even if addressed incorrectly. If you received this e-mail in error, please notify the sender; do not disclose, copy, distribute, or take any action in reliance on the contents of this information; and delete it from your system. Any other use of this e-mail is prohibited. Thank you for your compliance. ----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
