hi guys
I have written a custom field handler to convert a java.util.Date
attribute to the format ('yyyyMMdd') I require.
The custom handler works fine with the marshalling framework. However
during unmarshalling it throws a IllegalStateException. It is evident
from the stack tarce that my custom field handler is not being invoked
while unmarshalling the field. I am not using the static methods defined
on the UnMarshaller.
Questions
(1) Can you please shed light on what I am doing wrong.
CUSTOM HANDLER
import org.exolab.castor.mapping.AbstractFieldHandler;
public class MyNewDateHandler extends AbstractFieldHandler {
private DateFormat formatter = null;
public MyNewDateHandler() {
super();
formatter = new SimpleDateFormat("yyyyMMdd");
}
public Object getValue(Object object) throws
IllegalStateException {
SimpleVO vo = null;
String formattedDate = null;
vo = (SimpleVO) object;
formattedDate = formatter.format(vo.getOrderDate());
return formattedDate;
}
.....
.....
.....
public void setValue(Object arg0, Object arg1)
throws IllegalStateException, IllegalArgumentException {
Date d = null;
(SimpleVO)arg0).setDate(formatter.parse((String)arg1))
}
}
public class SimpleVO {
private Date orderDate = null;
private int quantity = 0;
private float amount = 0.0f;
public Date getOrderDate() {
return this.orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void setAmount(float amount) {
this.amount = amount;
}
public float getAmount() {
return this.amount;
}
public SimpleVO() {
Random r = new Random();
this.orderDate = Calendar.getInstance().getTime();
this.quantity = r.nextInt();
this.amount = r.nextFloat() * quantity;
}
}
This is the stack trace which I encounter while unmarshalling the xml
org.xml.sax.SAXException: unable to add 'order-date' to <Order> due to
the following exception:
>>>--- Begin Exception ---<<<
java.lang.IllegalStateException: Invalid dateTime format: 20031110
at
org.exolab.castor.xml.handlers.DateFieldHandler.setValue(DateFieldHandle
r.java:202)
at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:
852)
at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:
918)
at
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDis
patcher.dispatch(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkno
wn 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)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
Source)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:583)
at JDOTester.testUnMarshalling(JDOTester.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:392)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:276)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:167)
>>>---- End Exception ----<<<
at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:
885)
at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:
918)
at
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDis
patcher.dispatch(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkno
wn 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)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
Source)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:583)
at JDOTester.testUnMarshalling(JDOTester.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:392)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:276)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:167)
MAPPING
<mapping>
<description>mapping for simple vo</description>
<class name="SimpleVO">
<map-to xml="Order"/>
<field name="quantity"
type="int">
<bind-xml name="quantity" node="element"/>
</field>
<field name="amount"
type="float">
<bind-xml name="net-amount" node="element"/>
</field>
<field name="orderDate" type="date" handler="MyNewDateHandler">
<bind-xml name="order-date" node="element"/>
</field>
</class>
</mapping>
CODE SNIPPET
Mapping mapping = new Mapping();
mapping.loadMapping("d:/work/test/m1");
Unmarshaller unmarshaller = new Unmarshaller(SimpleVO.class);
unmarshaller.setLogWriter(new PrintWriter(new
FileWriter("d:/work/test/U.log")));
unmarshaller.setMapping(mapping);
InputSource is = new InputSource(new
FileReader("d:/work/test/simplevo.xml"));
SimpleVO vo = (SimpleVO)unmarshaller.unmarshal(is);
Thanks for your time
anand
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev